OSDN Git Service

Add vertex attribute defines to DefaultShader
authorXoppa <contact@xoppa.nl>
Mon, 15 Apr 2013 15:01:20 +0000 (17:01 +0200)
committerXoppa <contact@xoppa.nl>
Mon, 15 Apr 2013 15:01:20 +0000 (17:01 +0200)
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/DefaultShader.java
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/default.fragment.glsl
gdx/src/com/badlogic/gdx/graphics/g3d/shaders/default.vertex.glsl
gdx/src/com/badlogic/gdx/graphics/g3d/utils/DefaultShaderProvider.java

index 0e1a7d7..5a0b943 100644 (file)
@@ -5,6 +5,8 @@ import com.badlogic.gdx.graphics.Camera;
 import com.badlogic.gdx.graphics.Color;
 import com.badlogic.gdx.graphics.GL10;
 import com.badlogic.gdx.graphics.Mesh;
+import com.badlogic.gdx.graphics.VertexAttributes;
+import com.badlogic.gdx.graphics.VertexAttributes.Usage;
 import com.badlogic.gdx.graphics.g3d.Light;
 import com.badlogic.gdx.graphics.g3d.Renderable;
 import com.badlogic.gdx.graphics.g3d.Shader;
@@ -78,20 +80,21 @@ public class DefaultShader implements Shader {
        
        protected RenderContext context;
        protected long mask;
+       protected long attributes;
        
-       public DefaultShader(final Material material, int maxLightsCount) {
-               this(getDefaultVertexShader(), getDefaultFragmentShader(), material, maxLightsCount);
+       public DefaultShader(final Material material, final VertexAttributes attributes, int maxLightsCount) {
+               this(getDefaultVertexShader(), getDefaultFragmentShader(), material, attributes, maxLightsCount);
        }
        
-       public DefaultShader(final long mask, int maxLightsCount) {
-               this(getDefaultVertexShader(), getDefaultFragmentShader(), mask, maxLightsCount);
+       public DefaultShader(final long mask, final long attributes, int maxLightsCount) {
+               this(getDefaultVertexShader(), getDefaultFragmentShader(), mask, attributes, maxLightsCount);
        }
 
-       public DefaultShader(final String vertexShader, final String fragmentShader, final Material material, int maxLightsCount) {
-               this(vertexShader, fragmentShader, material.getMask(), maxLightsCount);
+       public DefaultShader(final String vertexShader, final String fragmentShader, final Material material, final VertexAttributes attributes, int maxLightsCount) {
+               this(vertexShader, fragmentShader, material.getMask(), getAttributesMask(attributes), maxLightsCount);
        }
        
-       public DefaultShader(final String vertexShader, final String fragmentShader, final long mask, int maxLightsCount) {
+       public DefaultShader(final String vertexShader, final String fragmentShader, final long mask, final long attributes, int maxLightsCount) {
                if (!Gdx.graphics.isGL20Available())
                        throw new GdxRuntimeException("This shader requires OpenGL ES 2.0");
                
@@ -102,12 +105,18 @@ public class DefaultShader implements Shader {
                
                String prefix = "";
                this.mask = mask;
-               
+               this.attributes = attributes;
+                               
                if (!ignoreUnimplemented && (implementedFlags & mask) != mask)
                        throw new GdxRuntimeException("Some attributes not implemented yet ("+mask+")");
                
-               if (maxLightsCount > 0)
-                       prefix += "#define lightsCount "+maxLightsCount+"\n";
+               if ((attributes & Usage.Color) == Usage.Color)
+                       prefix += "#define colorFlag\n";
+               if ((attributes & Usage.Normal) == Usage.Normal) {
+                       prefix += "#define normalFlag\n";
+                       if (maxLightsCount > 0)
+                               prefix += "#define lightsCount "+maxLightsCount+"\n";
+               }
                if (can(BlendingAttribute.Type))
                        prefix += "#define "+BlendingAttribute.Alias+"Flag\n";
                if (can(TextureAttribute.Diffuse))
@@ -152,9 +161,19 @@ public class DefaultShader implements Shader {
                // FIXME Cache vertex attribute locations...
        }
        
+       private static long getAttributesMask(final VertexAttributes attributes) {
+               long result = 0;
+               final int n = attributes.size();
+               for (int i = 0; i < n; i++)
+                       result |= (long)attributes.get(i).usage;
+               return result;
+       }
+       
        @Override
        public boolean canRender(final Renderable renderable) {
-               return mask == renderable.material.getMask() && (renderable.lights == null) == (currentLights == null);
+               return mask == renderable.material.getMask() && 
+                       attributes == getAttributesMask(renderable.mesh.getVertexAttributes()) && 
+                       (renderable.lights == null) == (currentLights == null);
        }
        
        private final boolean can(final long flag) {
index 2624ce8..bbaa53a 100644 (file)
@@ -8,7 +8,12 @@ precision mediump float;
 #define LOWP
 #endif
 
+#ifdef normalFlag
 varying vec3 v_normal;
+#endif
+#ifdef colorFlag
+varying vec4 v_color;
+#endif
 
 #if defined(diffuseTextureFlag) || defined(specularTextureFlag)
 varying MED vec2 v_texCoords0;
@@ -47,6 +52,10 @@ void main() {
        #else
                vec4 diffuse = vec4(1.0);
        #endif
+               
+       #ifdef colorFlag
+       diffuse *= v_color;
+       #endif
 
        #ifdef NUM_LIGHTS
        #if defined(specularTextureFlag) && defined(specularColorFlag)
index b3952b0..5c6638e 100644 (file)
@@ -3,11 +3,15 @@
 #endif
 
 attribute vec3 a_position;
-
+#ifdef colorFlag
+attribute vec4 a_color;
+varying vec4 v_color;
+#endif
+#ifdef normalFlag
 attribute vec3 a_normal;
-
 uniform mat3 u_normalMatrix;
 varying vec3 v_normal;
+#endif
 
 #ifdef textureFlag
 attribute vec2 a_texCoord0;
@@ -57,15 +61,22 @@ void main() {
        #ifdef textureFlag
                v_texCoords0 = a_texCoord0;
        #endif
-               
+       
+       #ifdef colorFlag
+       v_color = a_color;
+       #endif
+       
        vec4 pos = u_modelTrans * vec4(a_position, 1.0);
        gl_Position = u_projTrans * pos;
+       
+       #ifdef normalFlag
+       v_normal = u_normalMatrix * a_normal;
+       #endif
 
        #ifdef NUM_LIGHTS
        v_lightDiffuse = ambient.rgb;
        v_lightSpecular = vec3(0.0);
        #if (NUM_LIGHTS > 0)
-       v_normal = u_normalMatrix * a_normal;
        v_viewVec = normalize(u_cameraPosition - pos.xyz);
                
        for (int i = 0; i < NUM_LIGHTS; i++) {
index ab2477b..e25b904 100644 (file)
@@ -15,7 +15,7 @@ public class DefaultShaderProvider extends BaseShaderProvider {
        protected Shader createShader(final Renderable renderable) {
                Gdx.app.log("DefaultShaderProvider", "Creating new shader");
                if (Gdx.graphics.isGL20Available())
-                       return new DefaultShader(renderable.material, renderable.lights == null ? -1 : maxLightsCount);
+                       return new DefaultShader(renderable.material, renderable.mesh.getVertexAttributes(), renderable.lights == null ? -1 : maxLightsCount);
                return new GLES10Shader(maxLightsCount);
        }
 }