2 * Copyright (c) 2003-2009 jMonkeyEngine All rights reserved. Redistribution and
3 * use in source and binary forms, with or without modification, are permitted
4 * provided that the following conditions are met: * Redistributions of source
5 * code must retain the above copyright notice, this list of conditions and the
6 * following disclaimer. * Redistributions in binary form must reproduce the
7 * above copyright notice, this list of conditions and the following disclaimer
8 * in the documentation and/or other materials provided with the distribution. *
9 * Neither the name of 'jMonkeyEngine' nor the names of its contributors may be
10 * used to endorse or promote products derived from this software without
11 * specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
12 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
14 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
15 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
17 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
18 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
21 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 package jmetest.renderer.state;
26 import java.nio.FloatBuffer;
28 import com.jme.app.SimpleGame;
29 import com.jme.input.NodeHandler;
30 import com.jme.math.FastMath;
31 import com.jme.math.Vector3f;
32 import com.jme.renderer.ColorRGBA;
33 import com.jme.scene.shape.Quad;
34 import com.jme.scene.state.GLSLShaderObjectsState;
35 import com.jme.util.geom.BufferUtils;
38 * Tests GLSL shader attributes functionality
40 * @author Rikard Herlitz (MrCoder)
42 public class TestGLSLShaderAttributes extends SimpleGame {
43 private ColorQuad quad0, quad1, quad2, quad3;
45 public static void main(String[] args) {
46 TestGLSLShaderAttributes app = new TestGLSLShaderAttributes();
47 app.setConfigShowMode(ConfigShowMode.AlwaysShow);
51 protected void simpleInitGame() {
52 display.setTitle("Test GLSL attributes");
54 cam.setLocation(new Vector3f(0, 0, 2));
56 input = new NodeHandler(rootNode, 10, 2);
58 quad0 = new ColorQuad(ColorRGBA.red.clone(), 1.0f);
59 rootNode.attachChild(quad0);
60 quad0.getLocalTranslation().x -= 0.5f;
61 quad0.getLocalTranslation().y -= 0.5f;
63 quad1 = new ColorQuad(ColorRGBA.green.clone(), 0.7f);
64 rootNode.attachChild(quad1);
65 quad1.getLocalTranslation().x += 0.5f;
66 quad1.getLocalTranslation().y -= 0.5f;
68 quad2 = new ColorQuad(ColorRGBA.blue.clone(), 0.5f);
69 rootNode.attachChild(quad2);
70 quad2.getLocalTranslation().x -= 0.5f;
71 quad2.getLocalTranslation().y += 0.5f;
73 quad3 = new ColorQuad(ColorRGBA.orange.clone(), 1.1f);
74 rootNode.attachChild(quad3);
75 quad3.getLocalTranslation().x += 0.5f;
76 quad3.getLocalTranslation().y += 0.5f;
78 rootNode.updateRenderState();
81 protected void simpleUpdate() {
88 private class ColorQuad extends Quad {
89 private static final long serialVersionUID = 1L;
90 /** Shader attribute buffer for vertex colors */
91 private FloatBuffer vertexColors;
92 /** Shader attribute buffer for amount of offset to normal */
93 private FloatBuffer vertexOffset;
95 public ColorQuad(ColorRGBA color, float size) {
96 super("glslQuad", 1f, 1f);
98 // Check is GLSL is supported on current hardware.
99 if (!GLSLShaderObjectsState.isSupported()) {
103 GLSLShaderObjectsState so = display.getRenderer()
104 .createGLSLShaderObjectsState();
106 setDefaultColor(color);
108 so.load(TestGLSLShaderAttributes.class
111 "jmetest/data/images/attributeshader.vert"),
112 TestGLSLShaderAttributes.class
115 "jmetest/data/images/attributeshader.frag"));
117 vertexColors = BufferUtils.createFloatBuffer(16);
118 for (int i = 0; i < 4; i++) {
119 vertexColors.put(color.r);
120 vertexColors.put(color.g);
121 vertexColors.put(color.b);
122 vertexColors.put(color.a);
124 so.setAttributePointer("vertexColors", 4, true, 0, vertexColors);
126 vertexOffset = BufferUtils.createFloatBuffer(4);
127 so.setAttributePointer("vertexOffset", 1, true, 0, vertexOffset);
129 so.setUniform("size", size);
136 public void update(float speed) {
137 vertexOffset.rewind();
139 FastMath.sin(timer.getTimeInSeconds() * speed) * 0.5f + 0.5f)
141 FastMath.sin(timer.getTimeInSeconds() * speed
142 + 1.0f) * 0.5f + 0.5f).put(
143 FastMath.sin(timer.getTimeInSeconds() * speed
144 + 2.0f) * 0.5f + 0.5f).put(
145 FastMath.sin(timer.getTimeInSeconds() * speed
146 + 3.0f) * 0.5f + 0.5f);