OSDN Git Service

Fix gles10 blending
authorXoppa <contact@xoppa.nl>
Sun, 29 Sep 2013 18:25:58 +0000 (20:25 +0200)
committerXoppa <contact@xoppa.nl>
Sun, 29 Sep 2013 18:25:58 +0000 (20:25 +0200)
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/GLES10Shader.java
tests/gdx-tests/src/com/badlogic/gdx/tests/g3d/MaterialTest.java

index 4f23757..8c62989 100644 (file)
@@ -131,8 +131,11 @@ public class GLES10Shader implements Shader{
                return getValues(out, color.r, color.g, color.b, color.a);
        }
        
+       private Color tmpC = new Color();
        @Override
        public void render (final Renderable renderable) {
+               tmpC.set(1,1,1,1);
+               boolean hasColor = false;
                if (currentMaterial != renderable.material) {
                        currentMaterial = renderable.material;
                        if (!currentMaterial.has(BlendingAttribute.Type))
@@ -145,15 +148,16 @@ public class GLES10Shader implements Shader{
                                Gdx.gl10.glDisable(GL10.GL_TEXTURE_2D);
                        int cullFace = defaultCullFace;
                        for (final Attribute attribute : currentMaterial) {
-                               if (attribute.type == BlendingAttribute.Type)
+                               if (attribute.type == BlendingAttribute.Type) {
                                        context.setBlending(true, ((BlendingAttribute)attribute).sourceFunction, ((BlendingAttribute)attribute).destFunction);
+                                       hasColor = true;
+                                       tmpC.a = ((BlendingAttribute)attribute).opacity;
+                               }
                                else if (attribute.type == ColorAttribute.Diffuse) {
-                                       Gdx.gl10.glColor4f(((ColorAttribute)attribute).color.r, ((ColorAttribute)attribute).color.g, ((ColorAttribute)attribute).color.b, ((ColorAttribute)attribute).color.a);
-                                       if (renderable.environment != null) {
-                                               Gdx.gl10.glEnable(GL10.GL_COLOR_MATERIAL);
-                                               Gdx.gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, getValues(lightVal, ((ColorAttribute)attribute).color), 0);
-                                               Gdx.gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, getValues(lightVal, ((ColorAttribute)attribute).color), 0);
-                                       }
+                                       float a = tmpC.a;
+                                       tmpC.set(((ColorAttribute)attribute).color);
+                                       tmpC.a = a;
+                                       hasColor = true;
                                } else if (attribute.type == TextureAttribute.Diffuse) {
                                        TextureDescriptor textureDesc = ((TextureAttribute)attribute).textureDescription;
                                        if (currentTexture0 != textureDesc.texture)
@@ -167,6 +171,14 @@ public class GLES10Shader implements Shader{
                        }
                        context.setCullFace(cullFace);
                }
+               if (hasColor) {
+                       Gdx.gl10.glColor4f(tmpC.r, tmpC.g, tmpC.b, tmpC.a);
+                       if (renderable.environment != null) {
+                               Gdx.gl10.glEnable(GL10.GL_COLOR_MATERIAL);
+                               Gdx.gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, getValues(lightVal, tmpC), 0);
+                               Gdx.gl10.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, getValues(lightVal, tmpC), 0);
+                       }
+               }
                if (currentTransform != renderable.worldTransform) { // FIXME mul localtransform
                        if (currentTransform != null)
                                Gdx.gl10.glPopMatrix();
index 8ff8c96..b24a4e3 100644 (file)
@@ -31,6 +31,7 @@ import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
 import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
+import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
 import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.math.Vector3;
@@ -40,8 +41,9 @@ public class MaterialTest extends GdxTest {
        
        float angleY = 0;
        
-       Model model;
+       Model model, backModel;
        ModelInstance modelInstance;
+       ModelInstance background;
        ModelBatch modelBatch;
        
        TextureAttribute textureAttribute;
@@ -58,23 +60,33 @@ public class MaterialTest extends GdxTest {
        public void create () {
                texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true);
                
+               // Create material attributes. Each material can contain x-number of attributes.
+               textureAttribute = new TextureAttribute(TextureAttribute.Diffuse, texture);
+               colorAttribute = new ColorAttribute(ColorAttribute.Diffuse, Color.ORANGE);
+               blendingAttribute = new BlendingAttribute(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
+               
+               
                ModelBuilder builder = new ModelBuilder();
                model = builder.createBox(1, 1, 1, new Material(), Usage.Position | Usage.Normal | Usage.TextureCoordinates);
                model.manageDisposable(texture);
                modelInstance = new ModelInstance(model);
+               modelInstance.transform.rotate(Vector3.X, 45);
                
-               // Create material attributes. Each material can contain x-number of attributes.
-               textureAttribute = new TextureAttribute(TextureAttribute.Diffuse, texture);
-               colorAttribute = new ColorAttribute(ColorAttribute.Diffuse, Color.ORANGE);
-               blendingAttribute = new BlendingAttribute(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
-
                material = modelInstance.materials.get(0);
                
+               builder.begin();
+               MeshPartBuilder mpb = builder.part("back", GL10.GL_TRIANGLES, 
+                       Usage.Position | Usage.TextureCoordinates, new Material(textureAttribute));
+               mpb.rect(-2, -2, -2, 2, -2, -2, 2, 2, -2, -2, 2, -2, 0, 0, 1);
+               backModel = builder.end();
+               background = new ModelInstance(backModel);
+               
                modelBatch = new ModelBatch();
                
                camera = new PerspectiveCamera(45, 4, 4);
-               camera.position.set(3, 3, 3);
-               camera.direction.set(-1, -1, -1);
+               camera.position.set(0, 0, 3);
+               camera.direction.set(0, 0, -1);
+               camera.update();
                
                Gdx.input.setInputProcessor(this);
        }
@@ -87,11 +99,10 @@ public class MaterialTest extends GdxTest {
                
                Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
-               
-               camera.update();
 
                modelInstance.transform.rotate(Vector3.Y, 30 * Gdx.graphics.getDeltaTime());
                modelBatch.begin(camera);
+               modelBatch.render(background);
                modelBatch.render(modelInstance);
                modelBatch.end();
        }
@@ -114,11 +125,12 @@ public class MaterialTest extends GdxTest {
        @Override
        public void dispose () {
                model.dispose();
+               backModel.dispose();
                modelBatch.dispose();
        }
 
        @Override
        public boolean needsGL20 () {
-               return true;
+               return false;
        }
 }
\ No newline at end of file