use of org.terasology.joml.geom.AABBf 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.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.
the class SkeletalMeshDataBuilder method build.
public SkeletalMeshData build() {
int rootBones = 0;
for (Bone bone : bones) {
if (bone.getParent() == null) {
rootBones++;
}
}
Vector3f minOfAABB = new Vector3f(vertices.get(0));
Vector3f maxOfAABB = new Vector3f(vertices.get(0));
for (Vector3f vert : vertices) {
minOfAABB.min(vert);
maxOfAABB.max(vert);
}
if (rootBones == 0) {
throw new IllegalStateException("Cannot create a skeleton with no root bones");
} else if (rootBones > 1) {
throw new IllegalStateException("Cannot create a skeleton with multiple root bones");
}
AABBf staticAabb = new AABBf(minOfAABB, maxOfAABB);
return new SkeletalMeshData(bones, vertices, normals, weights, uvs, indices, staticAabb);
}
use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.
the class AABBTypeHandlerTest method testSerializeAABB1.
@Test
public void testSerializeAABB1() {
AABB3Test aabb3 = new AABB3Test();
aabb3.a1 = new AABBf(10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
aabb3.a2 = new AABBi(0, 0, 0, 10, 10, 10);
JsonElement tree = gson.toJsonTree(aabb3);
JsonObject obj = tree.getAsJsonObject();
assertTrue(obj.has("a1"));
assertAABBf(obj.get("a1"), 10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
assertTrue(obj.has("a2"));
assertAABBi(obj.get("a2"), 0, 0, 0, 10, 10, 10);
}
use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.
the class AABBTypeHandlerTest method testSerializeAABB1Missing.
@Test
public void testSerializeAABB1Missing() {
AABB3Test aabb3 = new AABB3Test();
aabb3.a1 = new AABBf(10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
JsonElement tree = gson.toJsonTree(aabb3);
JsonObject obj = tree.getAsJsonObject();
assertTrue(obj.has("a1"));
assertAABBf(obj.get("a1"), 10.0f, 5.0f, 0, 5.3f, 2.0f, 2.2f);
assertFalse(obj.has("a2"));
}
use of org.terasology.joml.geom.AABBf in project Terasology by MovingBlocks.
the class AABBfTypeHandler method deserialize.
@Override
public Optional<AABBf> deserialize(PersistedData data) {
if (!data.isNull() && data.isValueMap()) {
PersistedDataMap map = data.getAsValueMap();
PersistedDataArray minDataArr = map.get(MIN_FIELD).getAsArray();
PersistedDataArray maxDataArr = map.get(MAX_FIELD).getAsArray();
TFloatList minArr = minDataArr.getAsFloatArray();
TFloatList maxArr = maxDataArr.getAsFloatArray();
return Optional.of(new AABBf(minArr.get(0), minArr.get(1), minArr.get(2), maxArr.get(0), maxArr.get(1), maxArr.get(2)));
}
return Optional.empty();
}
Aggregations