use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.
the class FloatingTextRenderer method render.
private void render(Iterable<EntityRef> floatingTextEntities) {
Vector3f cameraPosition = camera.getPosition();
for (EntityRef entity : floatingTextEntities) {
LocationComponent location = entity.getComponent(LocationComponent.class);
if (location == null) {
continue;
}
Vector3f worldPos = location.getWorldPosition();
if (!worldProvider.isBlockRelevant(worldPos)) {
continue;
}
FloatingTextComponent floatingText = entity.getComponent(FloatingTextComponent.class);
String[] linesOfText = floatingText.text.split("\n");
Color baseColor = floatingText.textColor;
Color shadowColor = floatingText.textShadowColor;
boolean underline = false;
int textWidth = 0;
for (String singleLine : linesOfText) {
if (font.getWidth(singleLine) > textWidth) {
textWidth = font.getWidth(singleLine);
}
}
FontMeshBuilder meshBuilder = new FontMeshBuilder(underlineMaterial);
Map<Material, Mesh> meshMap = entityMeshCache.get(entity);
if (meshMap == null) {
meshMap = meshBuilder.createTextMesh(font, Arrays.asList(linesOfText), textWidth, HorizontalAlign.CENTER, baseColor, shadowColor, underline);
entityMeshCache.put(entity, meshMap);
}
if (floatingText.isOverlay) {
glDisable(GL_DEPTH_TEST);
}
glPushMatrix();
float scale = METER_PER_PIXEL * floatingText.scale;
glTranslated(worldPos.x - cameraPosition.x, worldPos.y - cameraPosition.y, worldPos.z - cameraPosition.z);
OpenGLUtils.applyBillboardOrientation();
glScaled(scale, -scale, scale);
glTranslated(-textWidth / 2.0, 0.0, 0.0);
for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
Mesh mesh = meshMapEntry.getValue();
Material material = meshMapEntry.getKey();
material.enable();
material.bindTextures();
material.setFloat4("croppingBoundaries", Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE);
material.setFloat2("offset", 0.0f, 0.0f);
material.setFloat("alpha", 1.0f);
mesh.render();
}
glPopMatrix();
// Revert to default state
if (floatingText.isOverlay) {
glEnable(GL_DEPTH_TEST);
}
}
}
use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.
the class FloatingTextRenderer method diposeMeshMap.
private void diposeMeshMap(Map<Material, Mesh> meshMap) {
for (Map.Entry<Material, Mesh> meshMapEntry : meshMap.entrySet()) {
Mesh mesh = meshMapEntry.getValue();
mesh.dispose();
// Note: Material belongs to font and must not be disposed
}
}
use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntitiesByMaterial.
private void renderEntitiesByMaterial(SetMultimap<Material, EntityRef> meshByMaterial) {
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Quat4f worldRot = new Quat4f();
Vector3f worldPos = new Vector3f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (Material material : meshByMaterial.keySet()) {
if (material.isRenderable()) {
OpenGLMesh lastMesh = null;
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 || !isRelevant(entity, location.getWorldPosition())) {
continue;
}
if (meshComp.mesh.isDisposed()) {
logger.error("Attempted to render disposed mesh");
continue;
}
location.getWorldRotation(worldRot);
location.getWorldPosition(worldPos);
float worldScale = location.getWorldScale();
Transform toWorldSpace = new Transform(worldPos, worldRot, worldScale);
Vector3f offsetFromCamera = new Vector3f();
offsetFromCamera.sub(worldPos, cameraPosition);
Matrix4f matrixCameraSpace = new Matrix4f(worldRot, offsetFromCamera, worldScale);
AABB aabb = meshComp.mesh.getAABB().transform(toWorldSpace);
if (worldRenderer.getActiveCamera().hasInSight(aabb)) {
if (meshComp.mesh != lastMesh) {
if (lastMesh != null) {
lastMesh.postRender();
}
lastMesh = (OpenGLMesh) meshComp.mesh;
lastMesh.preRender();
}
Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix(), true);
material.setMatrix4("worldViewMatrix", 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);
lastMesh.doRender();
}
}
if (lastMesh != null) {
lastMesh.postRender();
}
}
}
}
use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.
the class MeshRenderer method renderEntities.
private void renderEntities(Iterable<EntityRef> entityRefs) {
SetMultimap<Material, EntityRef> entitiesToRender = HashMultimap.create();
for (EntityRef entity : entityRefs) {
MeshComponent meshComponent = entity.getComponent(MeshComponent.class);
if (meshComponent != null && meshComponent.material != null) {
entitiesToRender.put(meshComponent.material, entity);
}
}
renderEntitiesByMaterial(entitiesToRender);
}
use of org.terasology.rendering.assets.material.Material in project Terasology by MovingBlocks.
the class SkeletonRenderer method renderOverlay.
@Override
public void renderOverlay() {
if (config.getRendering().getDebug().isRenderSkeletons()) {
glDisable(GL_DEPTH_TEST);
Vector3f cameraPosition = worldRenderer.getActiveCamera().getPosition();
Material material = Assets.getMaterial("engine:white").get();
material.setFloat("sunlight", 1.0f, true);
material.setFloat("blockLight", 1.0f, true);
material.setMatrix4("projectionMatrix", worldRenderer.getActiveCamera().getProjectionMatrix());
glLineWidth(2);
Vector3f worldPos = new Vector3f();
FloatBuffer tempMatrixBuffer44 = BufferUtils.createFloatBuffer(16);
FloatBuffer tempMatrixBuffer33 = BufferUtils.createFloatBuffer(12);
for (EntityRef entity : entityManager.getEntitiesWith(SkeletalMeshComponent.class, LocationComponent.class)) {
LocationComponent location = entity.getComponent(LocationComponent.class);
location.getWorldPosition(worldPos);
Vector3f worldPositionCameraSpace = new Vector3f();
worldPositionCameraSpace.sub(worldPos, cameraPosition);
float worldScale = location.getWorldScale();
Matrix4f matrixCameraSpace = new Matrix4f(new Quat4f(0, 0, 0, 1), worldPositionCameraSpace, worldScale);
Matrix4f modelViewMatrix = MatrixUtils.calcModelViewMatrix(worldRenderer.getActiveCamera().getViewMatrix(), matrixCameraSpace);
MatrixUtils.matrixToFloatBuffer(modelViewMatrix, tempMatrixBuffer44);
material.setMatrix4("worldViewMatrix", tempMatrixBuffer44, true);
MatrixUtils.matrixToFloatBuffer(MatrixUtils.calcNormalMatrix(modelViewMatrix), tempMatrixBuffer33);
material.setMatrix3("normalMatrix", tempMatrixBuffer33, true);
SkeletalMeshComponent skeletalMesh = entity.getComponent(SkeletalMeshComponent.class);
renderBone(skeletalMesh.rootBone, worldPos);
}
glEnable(GL_DEPTH_TEST);
}
}
Aggregations