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