use of org.terasology.math.AABB in project Terasology by MovingBlocks.
the class SkeletonRenderer method renderOpaque.
@Override
public void renderOpaque() {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quat4f worldRot = new Quat4f();
Vector3f worldPos = new Vector3f();
Quat4f inverseWorldRot = new Quat4f();
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;
}
AABB 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);
inverseWorldRot.inverse(worldRot);
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();
aabb = aabb.transform(worldRot, worldPos, worldScale);
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();
worldPositionCameraSpace.sub(worldPos, cameraPosition);
worldPos.y -= skeletalMesh.heightOffset;
Matrix4f matrixCameraSpace = new Matrix4f(worldRot, worldPositionCameraSpace, worldScale);
Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
skeletalMesh.material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
skeletalMesh.material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
skeletalMesh.material.setFloat("sunlight", worldRenderer.getMainLightIntensityAt(worldPos), true);
skeletalMesh.material.setFloat("blockLight", worldRenderer.getBlockLightIntensityAt(worldPos), true);
List<Vector3f> bonePositions = Lists.newArrayListWithCapacity(skeletalMesh.mesh.getVertexCount());
List<Quat4f> boneRotations = Lists.newArrayListWithCapacity(skeletalMesh.mesh.getVertexCount());
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) {
Vector3f pos = boneLocation.getWorldPosition();
pos.sub(worldPos);
inverseWorldRot.rotate(pos, pos);
bonePositions.add(pos);
Quat4f rot = new Quat4f(inverseWorldRot);
rot.mul(boneLocation.getWorldRotation());
boneRotations.add(rot);
} else {
logger.warn("Unable to resolve bone \"{}\"", bone.getName());
bonePositions.add(new Vector3f());
boneRotations.add(new Quat4f());
}
}
((OpenGLSkeletalMesh) skeletalMesh.mesh).setScaleTranslate(skeletalMesh.scale, skeletalMesh.translate);
((OpenGLSkeletalMesh) skeletalMesh.mesh).render(bonePositions, boneRotations);
}
}
use of org.terasology.math.AABB in project Terasology by MovingBlocks.
the class MD5AnimationLoader method createAnimation.
private MeshAnimationData createAnimation(MD5 md5) {
List<String> boneNames = Lists.newArrayListWithCapacity(md5.numJoints);
TIntList boneParents = new TIntArrayList(md5.numJoints);
for (int i = 0; i < md5.numJoints; ++i) {
boneNames.add(md5.joints[i].name);
boneParents.add(md5.joints[i].parent);
}
float timePerFrame = 1.0f / md5.frameRate;
List<MeshAnimationFrame> frames = Lists.newArrayList();
for (int frameIndex = 0; frameIndex < md5.numFrames; ++frameIndex) {
MD5Frame frame = md5.frames[frameIndex];
List<Vector3f> positions = Lists.newArrayListWithExpectedSize(md5.numJoints);
List<Vector3f> rawRotations = Lists.newArrayListWithExpectedSize(md5.numJoints);
for (int i = 0; i < md5.numJoints; ++i) {
positions.add(new Vector3f(md5.baseFramePosition[i]));
rawRotations.add(new Vector3f(md5.baseFrameOrientation[i]));
}
for (int jointIndex = 0; jointIndex < md5.numJoints; ++jointIndex) {
int compIndex = 0;
if ((md5.joints[jointIndex].flags & POSITION_X_FLAG) != 0) {
positions.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & POSITION_Y_FLAG) != 0) {
positions.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & POSITION_Z_FLAG) != 0) {
positions.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_X_FLAG) != 0) {
rawRotations.get(jointIndex).x = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_Y_FLAG) != 0) {
rawRotations.get(jointIndex).y = frame.components[md5.joints[jointIndex].startIndex + compIndex];
compIndex++;
}
if ((md5.joints[jointIndex].flags & ORIENTATION_Z_FLAG) != 0) {
rawRotations.get(jointIndex).z = frame.components[md5.joints[jointIndex].startIndex + compIndex];
}
}
List<Quat4f> rotations = rawRotations.stream().map(rot -> MD5ParserCommon.completeQuat4f(rot.x, rot.y, rot.z)).collect(Collectors.toCollection(ArrayList::new));
// Rotate just the root bone to correct for coordinate system differences
rotations.set(0, MD5ParserCommon.correctQuat4f(rotations.get(0)));
positions.set(0, MD5ParserCommon.correctOffset(positions.get(0)));
frames.add(new MeshAnimationFrame(positions, rotations));
}
AABB aabb = AABB.createEncompassing(Arrays.asList(md5.bounds));
return new MeshAnimationData(boneNames, boneParents, frames, timePerFrame, aabb);
}
use of org.terasology.math.AABB in project Terasology by MovingBlocks.
the class LwjglCanvasRenderer method drawMesh.
@Override
public void drawMesh(Mesh mesh, Material material, Rect2i drawRegion, Rect2i cropRegion, Quat4f rotation, Vector3f offset, float scale, float alpha) {
if (!material.isRenderable()) {
return;
}
AABB meshAABB = mesh.getAABB();
Vector3f meshExtents = meshAABB.getExtents();
float fitScale = 0.35f * Math.min(drawRegion.width(), drawRegion.height()) / Math.max(meshExtents.x, Math.max(meshExtents.y, meshExtents.z));
Vector3f centerOffset = meshAABB.getCenter();
centerOffset.scale(-1.0f);
Matrix4f centerTransform = new Matrix4f(BaseQuat4f.IDENTITY, centerOffset, 1.0f);
Matrix4f userTransform = new Matrix4f(rotation, offset, -fitScale * scale);
Matrix4f translateTransform = new Matrix4f(BaseQuat4f.IDENTITY, new Vector3f(drawRegion.minX() + drawRegion.width() / 2, drawRegion.minY() + drawRegion.height() / 2, 0), 1);
userTransform.mul(centerTransform);
translateTransform.mul(userTransform);
Matrix4f finalMat = new Matrix4f(modelView);
finalMat.mul(translateTransform);
MatrixUtils.matrixToFloatBuffer(finalMat, matrixBuffer);
material.setFloat4(CROPPING_BOUNDARIES_PARAM, cropRegion.minX(), cropRegion.maxX(), cropRegion.minY(), cropRegion.maxY());
material.setMatrix4("posMatrix", translateTransform);
glEnable(GL11.GL_DEPTH_TEST);
glClear(GL11.GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL11.GL_MODELVIEW);
glPushMatrix();
glLoadMatrix(matrixBuffer);
matrixBuffer.rewind();
boolean matrixStackSupported = material.supportsFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
if (matrixStackSupported) {
material.activateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
}
material.setFloat("alpha", alpha);
material.bindTextures();
mesh.render();
if (matrixStackSupported) {
material.deactivateFeature(ShaderProgramFeature.FEATURE_USE_MATRIX_STACK);
}
glPopMatrix();
glDisable(GL11.GL_DEPTH_TEST);
}
Aggregations