OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / jmetest / renderer / state / TestGLSLShaderAttributes.java
1 /*
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.
22  */
23
24 package jmetest.renderer.state;
25
26 import java.nio.FloatBuffer;
27
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;
36
37 /**
38  * Tests GLSL shader attributes functionality
39  * 
40  * @author Rikard Herlitz (MrCoder)
41  */
42 public class TestGLSLShaderAttributes extends SimpleGame {
43     private ColorQuad quad0, quad1, quad2, quad3;
44
45     public static void main(String[] args) {
46         TestGLSLShaderAttributes app = new TestGLSLShaderAttributes();
47         app.setConfigShowMode(ConfigShowMode.AlwaysShow);
48         app.start();
49     }
50
51     protected void simpleInitGame() {
52         display.setTitle("Test GLSL attributes");
53
54         cam.setLocation(new Vector3f(0, 0, 2));
55         cam.update();
56         input = new NodeHandler(rootNode, 10, 2);
57
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;
62
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;
67
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;
72
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;
77
78         rootNode.updateRenderState();
79     }
80
81     protected void simpleUpdate() {
82         quad0.update(0.5f);
83         quad1.update(1.0f);
84         quad2.update(3.0f);
85         quad3.update(5.0f);
86     }
87
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;
94
95         public ColorQuad(ColorRGBA color, float size) {
96             super("glslQuad", 1f, 1f);
97
98             // Check is GLSL is supported on current hardware.
99             if (!GLSLShaderObjectsState.isSupported()) {
100                 quit();
101             }
102
103             GLSLShaderObjectsState so = display.getRenderer()
104                     .createGLSLShaderObjectsState();
105
106             setDefaultColor(color);
107
108             so.load(TestGLSLShaderAttributes.class
109                             .getClassLoader()
110                             .getResource(
111                                     "jmetest/data/images/attributeshader.vert"),
112                     TestGLSLShaderAttributes.class
113                             .getClassLoader()
114                             .getResource(
115                                     "jmetest/data/images/attributeshader.frag"));
116
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);
123             }
124             so.setAttributePointer("vertexColors", 4, true, 0, vertexColors);
125
126             vertexOffset = BufferUtils.createFloatBuffer(4);
127             so.setAttributePointer("vertexOffset", 1, true, 0, vertexOffset);
128
129             so.setUniform("size", size);
130
131             so.setEnabled(true);
132
133             setRenderState(so);
134         }
135
136         public void update(float speed) {
137             vertexOffset.rewind();
138             vertexOffset.put(
139                             FastMath.sin(timer.getTimeInSeconds() * speed) * 0.5f + 0.5f)
140                         .put(
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);
147         }
148
149     }
150 }