OSDN Git Service

added diffuse texture sub shader
authorbadlogic <badlogicgames@gmail.com>
Fri, 31 May 2013 20:52:56 +0000 (22:52 +0200)
committerbadlogic <badlogicgames@gmail.com>
Fri, 31 May 2013 20:52:56 +0000 (22:52 +0200)
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/CompositeShader.java
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/subshaders/DiffuseTextureShader.java [new file with mode: 0644]

index e0cfccc..2fa2b80 100644 (file)
@@ -2,8 +2,10 @@ package com.badlogic.gdx.graphics.g3d.shaders;
 
 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Camera;
+import com.badlogic.gdx.graphics.GL20;
 import com.badlogic.gdx.graphics.g3d.Renderable;
 import com.badlogic.gdx.graphics.g3d.Shader;
+import com.badlogic.gdx.graphics.g3d.shaders.subshaders.DiffuseTextureShader;
 import com.badlogic.gdx.graphics.g3d.shaders.subshaders.SubShader;
 import com.badlogic.gdx.graphics.g3d.shaders.subshaders.TransformShader;
 import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
@@ -31,6 +33,7 @@ public class CompositeShader implements Shader {
        
        public CompositeShader(Renderable renderable) {
                subShaders.add(new TransformShader());
+               subShaders.add(new DiffuseTextureShader());
                init(renderable);
        }
        
@@ -148,6 +151,7 @@ public class CompositeShader implements Shader {
                program.begin();
                this.camera = camera;
                this.context = context;
+               context.setDepthTest(true, GL20.GL_LEQUAL);
        }
 
        @Override
diff --git a/gdx/src/com/badlogic/gdx/graphics/g3d/shaders/subshaders/DiffuseTextureShader.java b/gdx/src/com/badlogic/gdx/graphics/g3d/shaders/subshaders/DiffuseTextureShader.java
new file mode 100644 (file)
index 0000000..1ba6964
--- /dev/null
@@ -0,0 +1,42 @@
+package com.badlogic.gdx.graphics.g3d.shaders.subshaders;
+
+import com.badlogic.gdx.graphics.Camera;
+import com.badlogic.gdx.graphics.g3d.Renderable;
+import com.badlogic.gdx.graphics.g3d.materials.Material.Attribute;
+import com.badlogic.gdx.graphics.g3d.materials.TextureAttribute;
+import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
+import com.badlogic.gdx.graphics.glutils.ShaderProgram;
+
+public class DiffuseTextureShader extends BaseSubShader {
+       private boolean enabled;
+       
+       @Override
+       public void init (Renderable renderable) {
+               if(renderable.material.has(TextureAttribute.Diffuse)) {
+                       vertexVars.addAll(new String[] {
+                               "attribute vec2 a_texCoord0;",
+                               "varying vec2 v_texCoords0;"
+                       });
+                       vertexCode.addAll(new String[] {
+                               "v_texCoords0 = a_texCoord0;"
+                       });
+                       fragmentVars.addAll(new String[] {
+                               "uniform sampler2D u_diffuseTexture;",
+                               "varying MED vec2 v_texCoords0;"
+                       });
+                       fragmentCode.addAll(new String[] {
+                               "color = texture2D(u_diffuseTexture, v_texCoords0);"
+                       });
+                       enabled = true;
+               }
+       }
+
+       @Override
+       public void apply (ShaderProgram program, RenderContext context, Camera camera, Renderable renderable) {
+               if(enabled) {
+                       TextureAttribute attribute = (TextureAttribute)renderable.material.get(TextureAttribute.Diffuse);
+                       int unit = context.textureBinder.bind(attribute.textureDescription);
+                       program.setUniformi("u_diffuseTexture", unit);
+               }
+       }
+}