List of usage examples for com.badlogic.gdx.graphics.g3d.materials TextureAttribute texturePortionEquals
public boolean texturePortionEquals(TextureAttribute other)
From source file:com.badlogic.gdx.graphics.g3d.test.PrototypeRendererGL20.java
License:Apache License
private void flush() { // opaque is sorted front to back // transparent is sorted back to front drawableManager.drawables.sort(opaqueSorter); for (int i = drawableManager.drawables.size; --i >= 0;) { final Drawable drawable = drawableManager.drawables.get(i); final Vector3 center = drawable.sortCenter; lightManager.calculateLights(center.x, center.y, center.z); final Matrix4 modelMatrix = drawable.transform; normalMatrix.set(modelMatrix);/*ww w. j a va2 s.c om*/ if (drawable.isAnimated) ((AnimatedModel) (drawable.model)).setAnimation(drawable.animation, drawable.animationTime, drawable.isLooping); final SubMesh subMeshes[] = drawable.model.getSubMeshes(); boolean matrixChanged = true; for (int j = 0; j < subMeshes.length; j++) { final SubMesh subMesh = subMeshes[j]; final Material material = drawable.materials.get(j); // bind new shader if material can't use old one final boolean shaderChanged = bindShader(material); if (shaderChanged || matrixChanged) { currentShader.setUniformMatrix("u_normalMatrix", normalMatrix, false); currentShader.setUniformMatrix("u_modelMatrix", modelMatrix, false); matrixChanged = false; } for (int k = 0, len = material.getNumberOfAttributes(); k < len; k++) { final MaterialAttribute atrib = material.getAttribute(k); // special case for textures. really important to batch these if (atrib instanceof TextureAttribute) { final TextureAttribute texAtrib = (TextureAttribute) atrib; if (!texAtrib.texturePortionEquals(lastTexture[texAtrib.unit])) { lastTexture[texAtrib.unit] = texAtrib; texAtrib.bind(currentShader); } else { // need to be done, shader textureAtribute name could be changed. currentShader.setUniformi(texAtrib.name, texAtrib.unit); } } else if (atrib instanceof GpuSkinningAttribute) { GpuSkinningAttribute gpuAttrib = (GpuSkinningAttribute) atrib; gpuAttrib.setModelMatrix(modelMatrix); gpuAttrib.bind(currentShader); } else { atrib.bind(currentShader); } } // finally render current submesh subMesh.getMesh().render(currentShader, subMesh.primitiveType); } } // if transparent queue is not empty enable blending(this force gpu to // flush and there is some time to sort) if (drawableManager.drawablesBlended.size > 0) renderBlended(); // cleaning if (currentShader != null) { currentShader.end(); currentShader = null; } for (int i = 0, len = TextureAttribute.MAX_TEXTURE_UNITS; i < len; i++) lastTexture[i] = null; // clear all queus drawing = false; drawableManager.clear(); }
From source file:com.badlogic.gdx.graphics.g3d.test.PrototypeRendererGL20.java
License:Apache License
private void renderBlended() { Gdx.gl.glEnable(GL10.GL_BLEND);/* w ww . ja v a2s . c om*/ final Array<Drawable> transparentDrawables = drawableManager.drawablesBlended; transparentDrawables.sort(); // find N nearest lights per model // draw all models from opaque queue int lastSrcBlend = -1; int lastDstBlend = -1; for (int i = 0, size = transparentDrawables.size; i < size; i++) { final Drawable drawable = transparentDrawables.get(i); final Vector3 center = drawable.sortCenter; lightManager.calculateLights(center.x, center.y, center.z); final Matrix4 modelMatrix = drawable.transform; normalMatrix.set(modelMatrix); if (drawable.isAnimated) ((AnimatedModel) (drawable.model)).setAnimation(drawable.animation, drawable.animationTime, drawable.isLooping); final SubMesh subMeshes[] = drawable.model.getSubMeshes(); boolean matrixChanged = true; for (int j = 0; j < subMeshes.length; j++) { final SubMesh subMesh = subMeshes[j]; final Material material = drawable.materials.get(j); // bind new shader if material can't use old one final boolean shaderChanged = bindShader(material); if (shaderChanged || matrixChanged) { currentShader.setUniformMatrix("u_normalMatrix", normalMatrix, false); currentShader.setUniformMatrix("u_modelMatrix", modelMatrix, false); matrixChanged = false; } for (int k = 0, len = material.getNumberOfAttributes(); k < len; k++) { final MaterialAttribute atrib = material.getAttribute(k); // yet another instanceof. TODO is there any better way to do this? maybe stuffing this to material if (atrib instanceof BlendingAttribute) { final BlendingAttribute blending = (BlendingAttribute) atrib; if (blending.blendSrcFunc != lastSrcBlend || blending.blendDstFunc != lastDstBlend) { atrib.bind(currentShader); lastSrcBlend = blending.blendSrcFunc; lastDstBlend = blending.blendDstFunc; } } else if (atrib instanceof TextureAttribute) { // special case for textures. really important to batch these final TextureAttribute texAtrib = (TextureAttribute) atrib; if (!texAtrib.texturePortionEquals(lastTexture[texAtrib.unit])) { lastTexture[texAtrib.unit] = texAtrib; texAtrib.bind(currentShader); } else { // need to be done, shader textureAtribute name could be changed. currentShader.setUniformi(texAtrib.name, texAtrib.unit); } } else if (atrib instanceof GpuSkinningAttribute) { final GpuSkinningAttribute gpuAtrib = (GpuSkinningAttribute) atrib; gpuAtrib.setModelMatrix(modelMatrix); gpuAtrib.bind(currentShader); } else { atrib.bind(currentShader); } } // finally render current submesh subMesh.getMesh().render(currentShader, subMesh.primitiveType); } } Gdx.gl.glDisable(GL10.GL_BLEND); }