use of org.joml.Matrix3f in project Terasology by MovingBlocks.
the class SkeletonRenderer method renderOpaque.
@Override
public void renderOpaque() {
Vector3fc cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quaternionf worldRot = new Quaternionf();
Vector3f worldPos = new Vector3f();
Quaternionf inverseWorldRot = new Quaternionf();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
if (skeletalMesh.mesh == null || skeletalMesh.material == null || skeletalMesh.boneEntities == null || !skeletalMesh.material.isRenderable()) {
continue;
}
AABBf aabb;
MeshAnimation animation = skeletalMesh.animation;
if (animation != null) {
aabb = animation.getAabb();
} else {
aabb = skeletalMesh.mesh.getStaticAabb();
}
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldRotation(worldRot);
worldRot.invert(inverseWorldRot);
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();
aabb = aabb.transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
// Scale bounding box for skeletalMesh.
Vector3f scale = skeletalMesh.scale;
Vector3f aabbCenter = aabb.center(new Vector3f());
Vector3f scaledExtents = aabb.extent(new Vector3f()).mul(scale.x(), scale.y(), scale.z());
aabb = new AABBf(aabbCenter, aabbCenter).expand(scaledExtents);
if (!worldRenderer.getActiveCamera().hasInSight(aabb)) {
continue;
}
skeletalMesh.material.enable();
skeletalMesh.material.setFloat("sunlight", 1.0f, true);
skeletalMesh.material.setFloat("blockLight", 1.0f, true);
skeletalMesh.material.setFloat3("colorOffset", skeletalMesh.color.rf(), skeletalMesh.color.gf(), skeletalMesh.color.bf(), true);
skeletalMesh.material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
skeletalMesh.material.bindTextures();
Vector3f worldPositionCameraSpace = new Vector3f();
worldPos.sub(cameraPosition, worldPositionCameraSpace);
worldPositionCameraSpace.y += skeletalMesh.heightOffset;
Matrix4f matrixCameraSpace = new Matrix4f().translationRotateScale(worldPositionCameraSpace, worldRot, worldScale);
Matrix4f modelViewMatrix = worldRenderer.getActiveCamera().getViewMatrix().mul(matrixCameraSpace, new Matrix4f());
modelViewMatrix.get(tempMatrixBuffer44);
skeletalMesh.material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
modelViewMatrix.normal(new Matrix3f()).get(tempMatrixBuffer33);
skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
Matrix4f[] boneTransforms = new Matrix4f[skeletalMesh.mesh.getBones().size()];
for (Bone bone : skeletalMesh.mesh.getBones()) {
EntityRef boneEntity = skeletalMesh.boneEntities.get(bone.getName());
if (boneEntity == null) {
boneEntity = EntityRef.NULL;
}
LocationComponent boneLocation = boneEntity.getComponent(LocationComponent.class);
if (boneLocation != null) {
Matrix4f boneTransform = new Matrix4f();
boneLocation.getRelativeTransform(boneTransform, entity);
boneTransform.mul(bone.getInverseBindMatrix());
boneTransforms[bone.getIndex()] = boneTransform.transpose();
} else {
logger.warn("Unable to resolve bone \"{}\"", bone.getName());
boneTransforms[bone.getIndex()] = new Matrix4f();
}
}
((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
((OpenGLSkeletalMesh) skeletalMesh.mesh).render(Arrays.asList(boneTransforms));
}
}
use of org.joml.Matrix3f in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntitiesByMaterial.
private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quaternionf worldRot = new Quaternionf();
Vector3f worldPos = new Vector3f();
Matrix3f normalMatrix = new Matrix3f();
Matrix4f matrixCameraSpace = new Matrix4f();
Matrix4f modelViewMatrix = new Matrix4f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (Material material : meshByMaterial.keySet()) {
if (material.isRenderable()) {
material.enable();
material.setFloat("sunlight", 1.0f, true);
material.setFloat("blockLight", 1.0f, true);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.bindTextures();
Set<EntityRef> entities = meshByMaterial.get(material);
lastRendered = entities.size();
for (EntityRef entity : entities) {
MeshComponent meshComp = entity.getComponent(MeshComponent.class);
LocationComponent location = entity.getComponent(LocationComponent.class);
if (isHidden(entity, meshComp) || location == null || meshComp.mesh == null) {
continue;
}
Vector3f worldPosition = location.getWorldPosition(new Vector3f());
if (!worldPosition.isFinite() && !isRelevant(entity, worldPosition)) {
continue;
}
if (meshComp.mesh.isDisposed()) {
logger.error("Attempted to render disposed mesh");
continue;
}
worldRot.set(location.getWorldRotation(new Quaternionf()));
worldPos.set(location.getWorldPosition(new Vector3f()));
float worldScale = location.getWorldScale();
Vector3f offsetFromCamera = worldPos.sub(cameraPosition, new Vector3f());
matrixCameraSpace.translationRotateScale(offsetFromCamera, worldRot, worldScale);
AABBf aabb = meshComp.mesh.getAABB().transform(new Matrix4f().translationRotateScale(worldPos, worldRot, worldScale), new AABBf());
if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
modelViewMatrix.set(worldRenderer.getActiveCamera().getViewMatrix()).mul(matrixCameraSpace);
modelViewMatrix.get(tempMatrixBuffer44);
modelViewMatrix.normal(normalMatrix).get(tempMatrixBuffer33);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.setMatrix4("modelViewMatrix", tempMatrixBuffer44, true);
material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
material.setFloat3("colorOffset", meshComp.color.rf(), meshComp.color.gf(), meshComp.color.bf(), true);
material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
material.setFloat("blockLight", Math.max(worldRenderer.getBlockLightIntensityAt(worldPos), meshComp.selfLuminance), true);
meshComp.mesh.render();
}
}
}
}
}
use of org.joml.Matrix3f in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawMesh.
@Override
public void drawMesh(Mesh mesh, Material material, Rectanglei drawRegion, Rectanglei cropRegion, Quaternionfc rotation, Vector3fc offset, float scale, float alpha) {
if (!material.isRenderable()) {
return;
}
AABBfc meshAABB = mesh.getAABB();
Vector3f meshExtents = meshAABB.extent(new Vector3f());
float fitScale = 0.35f * Math.min(drawRegion.getSizeX(), drawRegion.getSizeY()) / Math.max(meshExtents.x, Math.max(meshExtents.y, meshExtents.z));
Vector3f centerOffset = meshAABB.center(new Vector3f());
centerOffset.mul(-1.0f);
Matrix4f centerTransform = new Matrix4f().translationRotateScale(centerOffset, new Quaternionf(), 1);
Matrix4f userTransform = new Matrix4f().translationRotateScale(offset, rotation, -fitScale * scale);
Matrix4f translateTransform = new Matrix4f().translationRotateScale(new Vector3f((drawRegion.minX + drawRegion.getSizeX() / 2) * uiScale, (drawRegion.minY + drawRegion.getSizeY() / 2) * uiScale, 0), new Quaternionf(), 1);
userTransform.mul(centerTransform);
translateTransform.mul(userTransform);
Matrix4f finalMat = new Matrix4f().setTranslation(0, 0, -1024f);
finalMat.mul(translateTransform);
material.setFloat4(CROPPING_BOUNDARIES_PARAM, cropRegion.minX * uiScale, cropRegion.maxX * uiScale, cropRegion.minY * uiScale, cropRegion.maxY * uiScale);
glEnable(GL11.GL_DEPTH_TEST);
glClear(GL11.GL_DEPTH_BUFFER_BIT);
modelMatrixStack.pushMatrix();
modelMatrixStack.set(finalMat);
modelMatrixStack.scale(this.uiScale, this.uiScale, this.uiScale);
material.setMatrix4("posMatrix", translateTransform);
material.setMatrix4("projectionMatrix", projMatrix);
material.setMatrix4("modelViewMatrix", modelMatrixStack);
material.setMatrix3("normalMatrix", modelMatrixStack.normal(new Matrix3f()));
material.setFloat("alpha", alpha);
material.bindTextures();
mesh.render();
modelMatrixStack.popMatrix();
glDisable(GL11.GL_DEPTH_TEST);
}
Aggregations