OSDN Git Service

ran gdx-tools HeaderFixer tool
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / graphics / g3d / shaders / DepthShader.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/
16
17 package com.badlogic.gdx.graphics.g3d.shaders;
18
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.graphics.Camera;
21 import com.badlogic.gdx.graphics.GL10;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.VertexAttribute;
24 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
25 import com.badlogic.gdx.graphics.g3d.Renderable;
26 import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
27 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
28 import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader.Config;
29 import com.badlogic.gdx.graphics.g3d.utils.RenderContext;
30 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
31
32 public class DepthShader extends DefaultShader {
33         public static class Config extends DefaultShader.Config {
34                 public boolean depthBufferOnly = false;
35                 
36                 public Config () {
37                         super();
38                 }
39                 public Config (String vertexShader, String fragmentShader) {
40                         super(vertexShader, fragmentShader);
41                 }
42         }
43         
44         private static String defaultVertexShader = null;
45         public final static String getDefaultVertexShader() {
46                 if (defaultVertexShader == null)
47                         defaultVertexShader = Gdx.files.classpath("com/badlogic/gdx/graphics/g3d/shaders/depth.vertex.glsl").readString();
48                 return defaultVertexShader;
49         }
50         
51         private static String defaultFragmentShader = null;
52         public final static String getDefaultFragmentShader() {
53                 if (defaultFragmentShader == null)
54                         defaultFragmentShader = Gdx.files.classpath("com/badlogic/gdx/graphics/g3d/shaders/depth.fragment.glsl").readString();
55                 return defaultFragmentShader;
56         }
57         
58         public static String createPrefix(final Renderable renderable, final Config config) {
59                 String prefix = "";
60                 final long mask = renderable.material.getMask();
61                 final long attributes = renderable.mesh.getVertexAttributes().getMask();
62                 if ((attributes & Usage.BoneWeight) == Usage.BoneWeight) {
63                         final int n = renderable.mesh.getVertexAttributes().size();
64                         for (int i = 0; i < n; i++) {
65                                 final VertexAttribute attr = renderable.mesh.getVertexAttributes().get(i);
66                                 if (attr.usage == Usage.BoneWeight)
67                                         prefix += "#define boneWeight"+attr.unit+"Flag\n";
68                         }
69                 }
70                 // FIXME Add transparent texture support
71 //              if ((mask & BlendingAttribute.Type) == BlendingAttribute.Type)
72 //                      prefix += "#define "+BlendingAttribute.Alias+"Flag\n";
73 //              if ((mask & TextureAttribute.Diffuse) == TextureAttribute.Diffuse)
74 //                      prefix += "#define "+TextureAttribute.DiffuseAlias+"Flag\n";
75                 if (renderable.bones != null && config.numBones > 0)
76                         prefix += "#define numBones "+config.numBones+"\n";
77                 if (!config.depthBufferOnly)
78                         prefix += "#define PackedDepthFlag\n";
79                 return prefix;
80         }
81         
82         public final int numBones;
83         public final int weights;
84         
85         public DepthShader(final Renderable renderable) {
86                 this(renderable, new Config());
87         }
88         
89         public DepthShader(final Renderable renderable, final Config config) {
90                 this(renderable, config, createPrefix(renderable, config));
91         }
92
93         public DepthShader(final Renderable renderable, final Config config, final String prefix) {
94                 this(renderable, config, prefix, 
95                                 config.vertexShader != null ? config.vertexShader : getDefaultVertexShader(), 
96                                 config.fragmentShader != null ? config.fragmentShader : getDefaultFragmentShader());
97         }
98         
99         public DepthShader(final Renderable renderable, final Config config, final String prefix, final String vertexShader, final String fragmentShader) {
100                 this(renderable, config, new ShaderProgram(prefix + vertexShader, prefix + fragmentShader));
101         }
102         
103         public DepthShader(final Renderable renderable, final Config config, final ShaderProgram shaderProgram) {
104                 super(renderable, config, shaderProgram);
105                 this.numBones = renderable.bones == null ? 0 : config.numBones;
106                 int w = 0;
107                 final int n = renderable.mesh.getVertexAttributes().size();
108                 for (int i = 0; i < n; i++) {
109                         final VertexAttribute attr = renderable.mesh.getVertexAttributes().get(i);
110                         if (attr.usage == Usage.BoneWeight)
111                                 w |= (1 << attr.unit);
112                 }
113                 weights = w;
114         }
115         
116         private int originalCullFace;   
117         @Override
118         public void begin (Camera camera, RenderContext context) {
119                 originalCullFace = DefaultShader.defaultCullFace;
120                 DefaultShader.defaultCullFace = GL10.GL_FRONT; //0; //GL10.GL_BACK; //GL10.GL_FRONT;
121                 super.begin(camera, context);
122                 //Gdx.gl20.glEnable(GL20.GL_POLYGON_OFFSET_FILL);
123                 //Gdx.gl20.glPolygonOffset(2.f, 100.f);
124         }
125         
126         @Override
127         public void end () {
128                 super.end();
129                 DefaultShader.defaultCullFace = originalCullFace;
130                 Gdx.gl20.glDisable(GL20.GL_POLYGON_OFFSET_FILL);
131         }
132         
133         @Override
134         public boolean canRender (Renderable renderable) {
135                 final boolean skinned = ((renderable.mesh.getVertexAttributes().getMask() & Usage.BoneWeight) == Usage.BoneWeight);
136                 if (skinned != (numBones > 0))
137                         return false;
138                 if (!skinned)
139                         return true;
140                 int w = 0;
141                 final int n = renderable.mesh.getVertexAttributes().size();
142                 for (int i = 0; i < n; i++) {
143                         final VertexAttribute attr = renderable.mesh.getVertexAttributes().get(i);
144                         if (attr.usage == Usage.BoneWeight)
145                                 w |= (1 << attr.unit);
146                 }
147                 return w == weights;
148         }
149 }