1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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 ******************************************************************************/
17 package com.badlogic.gdx.graphics.g3d.shaders;
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;
32 public class DepthShader extends DefaultShader {
33 public static class Config extends DefaultShader.Config {
34 public boolean depthBufferOnly = false;
39 public Config (String vertexShader, String fragmentShader) {
40 super(vertexShader, fragmentShader);
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;
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;
58 public static String createPrefix(final Renderable renderable, final Config config) {
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";
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";
82 public final int numBones;
83 public final int weights;
85 public DepthShader(final Renderable renderable) {
86 this(renderable, new Config());
89 public DepthShader(final Renderable renderable, final Config config) {
90 this(renderable, config, createPrefix(renderable, config));
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());
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));
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;
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);
116 private int originalCullFace;
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);
129 DefaultShader.defaultCullFace = originalCullFace;
130 Gdx.gl20.glDisable(GL20.GL_POLYGON_OFFSET_FILL);
134 public boolean canRender (Renderable renderable) {
135 final boolean skinned = ((renderable.mesh.getVertexAttributes().getMask() & Usage.BoneWeight) == Usage.BoneWeight);
136 if (skinned != (numBones > 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);