JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TessellationShader01aGLSL440CoreHardcoded.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.opengl.test.junit.jogl.demos.gl4;
29
30import java.nio.FloatBuffer;
31
32import com.jogamp.opengl.GL;
33import com.jogamp.opengl.GL2ES2;
34import com.jogamp.opengl.GL2ES3;
35import com.jogamp.opengl.GL2GL3;
36import com.jogamp.opengl.GL3ES3;
37import com.jogamp.opengl.GL4;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLEventListener;
40
41import com.jogamp.opengl.util.glsl.ShaderCode;
42import com.jogamp.opengl.util.glsl.ShaderProgram;
43
44/**
45 * JOGL Tessellation ShaderCode GL4 test case.
46 * <p>
47 * Demonstrates tessellation-control and -evaluation shaders.
48 * </p>
49 *
50 * @author Raymond L. Rivera, 2014
51 * @author Sven Gothel
52 */
54 private static final double ANIMATION_RATE = 950.0;
55
56 private ShaderProgram program;
57 private final int[] vertexArray = new int[1];
58 private FloatBuffer vertexOffset;
59 private FloatBuffer backgroundColor;
60
61
62 @Override
63 public void init(final GLAutoDrawable auto) {
64 final GL4 gl = auto.getGL().getGL4();
65 program = createProgram(auto);
66 if( null == program ) {
67 return;
68 }
69
70 final double theta = System.currentTimeMillis() / ANIMATION_RATE;
71 vertexOffset = FloatBuffer.allocate(4);
72 vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f));
73 vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f));
74 vertexOffset.put(2, 0.0f);
75 vertexOffset.put(3, 0.0f);
76
77 backgroundColor = FloatBuffer.allocate(4);
78 backgroundColor.put(0, 0.25f);
79 backgroundColor.put(1, 0.25f);
80 backgroundColor.put(2, 0.25f);
81 backgroundColor.put(3, 1.0f);
82
83 gl.glGenVertexArrays(vertexArray.length, vertexArray, 0);
84 gl.glBindVertexArray(vertexArray[0]);
87 }
88
89 @Override
90 public void display(final GLAutoDrawable auto) {
91 if( null == program ) {
92 return;
93 }
94 final GL4 gl = auto.getGL().getGL4();
95 final double value = System.currentTimeMillis() / ANIMATION_RATE;
96 gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor);
97 gl.glUseProgram(program.program());
98 vertexOffset.put(0, (float)(Math.sin(value) * 0.5f));
99 vertexOffset.put(1, (float)(Math.cos(value) * 0.6f));
100 gl.glVertexAttrib4fv(0, vertexOffset);
102 }
103
104 @Override
105 public void dispose(final GLAutoDrawable auto) {
106 if( null == program ) {
107 return;
108 }
109 final GL4 gl = auto.getGL().getGL4();
110 gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0);
111 program.destroy(gl);
112 }
113
114 @Override
115 public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) {
116 // final GL4 gl = auto.getGL().getGL4();
117 }
118
119 private ShaderProgram createProgram(final GLAutoDrawable auto) {
120 final GL4 gl = auto.getGL().getGL4();
121 final String vertexSource =
122 "#version 440 core \n" +
123 " \n" +
124 "layout (location = 0) in vec4 offset; \n" +
125 " \n" +
126 "void main(void) \n" +
127 "{ \n" +
128 " const vec4 vertices[3] = vec4[3] ( \n" +
129 " vec4( 0.25, 0.25, 0.5, 1.0), \n" +
130 " vec4(-0.25, -0.25, 0.5, 1.0), \n" +
131 " vec4( 0.25, -0.25, 0.5, 1.0)); \n" +
132 " gl_Position = vertices[gl_VertexID] + offset; \n" +
133 "} \n";
134 final String tessCtrlSource =
135 "#version 440 core \n" +
136 "layout (vertices = 3) out; \n" +
137 " \n" +
138 "void main(void) \n" +
139 "{ \n" +
140 " if (gl_InvocationID == 0) \n" +
141 " { \n" +
142 " gl_TessLevelInner[0] = 5.0; \n" +
143 " gl_TessLevelOuter[0] = 5.0; \n" +
144 " gl_TessLevelOuter[1] = 5.0; \n" +
145 " gl_TessLevelOuter[2] = 5.0; \n" +
146 " } \n" +
147 " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n" +
148 "} \n";
149 final String tessEvalSource =
150 "#version 440 core \n" +
151 " \n" +
152 "layout (triangles, equal_spacing, cw) in; \n" +
153 " \n" +
154 "void main(void) \n" +
155 "{ \n" +
156 " gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position) + \n" +
157 " (gl_TessCoord.y * gl_in[1].gl_Position) + \n" +
158 " (gl_TessCoord.z * gl_in[2].gl_Position); \n" +
159 "} \n";
160 final String fragmentSource =
161 "#version 440 core \n" +
162 " \n" +
163 "out vec4 color; \n" +
164 " \n" +
165 "void main(void) \n" +
166 "{ \n" +
167 " color = vec4(1.0, 1.0, 1.0, 1.0); \n" +
168 "} \n";
169
170 final ShaderCode vertexShader = createShader(gl, GL2ES2.GL_VERTEX_SHADER, vertexSource);
171 if( null == vertexShader ) {
172 return null;
173 }
174 final ShaderCode tessCtrlShader = createShader(gl, GL3ES3.GL_TESS_CONTROL_SHADER, tessCtrlSource);
175 if( null == tessCtrlShader ) {
176 vertexShader.destroy(gl);
177 return null;
178 }
179 final ShaderCode tessEvalShader = createShader(gl, GL3ES3.GL_TESS_EVALUATION_SHADER, tessEvalSource);
180 if( null == tessEvalShader ) {
181 vertexShader.destroy(gl);
182 tessCtrlShader.destroy(gl);
183 return null;
184 }
185 final ShaderCode fragmentShader = createShader(gl, GL2ES2.GL_FRAGMENT_SHADER, fragmentSource);
186 if( null == fragmentShader ) {
187 vertexShader.destroy(gl);
188 tessCtrlShader.destroy(gl);
189 tessEvalShader.destroy(gl);
190 return null;
191 }
192
193 final ShaderProgram program = new ShaderProgram();
194
195 program.init(gl);
196 program.add(vertexShader);
197 program.add(tessCtrlShader);
198 program.add(tessEvalShader);
199 program.add(fragmentShader);
200
201 program.link(gl, System.err);
202 if( !program.validateProgram(gl, System.out) ) {
203 System.err.println("[error] Program linking failed.");
204 program.destroy(gl);
205 return null;
206 } else {
207 return program;
208 }
209 }
210
211 private ShaderCode createShader(final GL4 gl, final int shaderType, final String source) {
212 final String[][] sources = new String[1][1];
213 sources[0] = new String[]{ source };
214 final ShaderCode shader = new ShaderCode(shaderType, sources.length, sources);
215
216 final boolean compiled = shader.compile(gl, System.err);
217 if (!compiled) {
218 System.err.println("[error] Shader compilation failed.");
219 shader.destroy(gl);
220 return null;
221 } else {
222 return shader;
223 }
224 }
225
226}
void display(final GLAutoDrawable auto)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable auto)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable auto)
Called by the drawable immediately after the OpenGL context is initialized.
void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
Convenient shader code class to use and instantiate vertex or fragment programs.
Definition: ShaderCode.java:75
int program()
Returns the shader program name, which is non zero if valid.
synchronized void destroy(final GL2ES2 gl)
Detaches all shader codes and deletes the program.
static final int GL_VERTEX_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_EXT_vertex_shader, GL_ARB_vertex_shader Alias for: GL_VERTEX_SH...
Definition: GL2ES2.java:39
void glVertexAttrib4fv(int index, FloatBuffer v)
Entry point to C language function: void {@native glVertexAttrib4fv}(GLuint index,...
void glUseProgram(int program)
Entry point to C language function: void {@native glUseProgram}(GLuint program) Part of GL_ES_VERS...
static final int GL_FRAGMENT_SHADER
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ATI_fragment_shader, GL_ARB_fragment_shader Alias for: GL_FRAGM...
Definition: GL2ES2.java:541
void glGenVertexArrays(int n, IntBuffer arrays)
Entry point to C language function: void {@native glGenVertexArrays}(GLsizei n, GLuint * arrays) P...
void glBindVertexArray(int array)
Entry point to C language function: void {@native glBindVertexArray}(GLuint array) Part of GL_ARB_...
void glClearBufferfv(int buffer, int drawbuffer, FloatBuffer value)
Entry point to C language function: void {@native glClearBufferfv}(GLenum buffer,...
static final int GL_COLOR
GL_ES_VERSION_3_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_EXT_discard_framebuffer Alias for: GL_COLOR_EXT...
Definition: GL2ES3.java:426
void glDeleteVertexArrays(int n, IntBuffer arrays)
Entry point to C language function: void {@native glDeleteVertexArrays}(GLsizei n,...
void glPolygonMode(int face, int mode)
Entry point to C language function: void {@native glPolygonMode}(GLenum face, GLenum mode) Part of...
static final int GL_LINE
GL_VERSION_1_1, GL_VERSION_1_0, GL_NV_polygon_mode Alias for: GL_LINE_NV Define "GL_LINE" with expre...
Definition: GL2GL3.java:394
void glPatchParameteri(int pname, int value)
Entry point to C language function: void {@native glPatchParameteri}(GLenum pname,...
static final int GL_PATCHES
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_OES_tessellation_shader,...
Definition: GL3ES3.java:494
static final int GL_TESS_EVALUATION_SHADER
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_EXT_tessellation_shader,...
Definition: GL3ES3.java:575
static final int GL_TESS_CONTROL_SHADER
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_EXT_tessellation_shader,...
Definition: GL3ES3.java:316
static final int GL_PATCH_VERTICES
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_OES_tessellation_shader,...
Definition: GL3ES3.java:560
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL4 getGL4()
Casts this object to the GL4 interface.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
void glDrawArrays(int mode, int first, int count)
Entry point to C language function: void {@native glDrawArrays}(GLenum mode, GLint first,...
static final int GL_FRONT_AND_BACK
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FRONT_AND_BACK" with ...
Definition: GL.java:619