28package com.jogamp.opengl.test.junit.jogl.demos.gl4;
30import java.nio.FloatBuffer;
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;
41import com.jogamp.opengl.util.glsl.ShaderCode;
42import com.jogamp.opengl.util.glsl.ShaderProgram;
54 private static final double ANIMATION_RATE = 950.0;
57 private final int[] vertexArray =
new int[1];
58 private FloatBuffer vertexOffset;
59 private FloatBuffer backgroundColor;
65 program = createProgram(auto);
66 if(
null == program ) {
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);
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);
91 if(
null == program ) {
95 final double value = System.currentTimeMillis() / ANIMATION_RATE;
98 vertexOffset.put(0, (
float)(Math.sin(value) * 0.5f));
99 vertexOffset.put(1, (
float)(Math.cos(value) * 0.6f));
106 if(
null == program ) {
121 final String vertexSource =
122 "#version 440 core \n" +
124 "layout (location = 0) in vec4 offset; \n" +
126 "void main(void) \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" +
134 final String tessCtrlSource =
135 "#version 440 core \n" +
136 "layout (vertices = 3) out; \n" +
138 "void main(void) \n" +
140 " if (gl_InvocationID == 0) \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" +
147 " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n" +
149 final String tessEvalSource =
150 "#version 440 core \n" +
152 "layout (triangles, equal_spacing, cw) in; \n" +
154 "void main(void) \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" +
160 final String fragmentSource =
161 "#version 440 core \n" +
163 "out vec4 color; \n" +
165 "void main(void) \n" +
167 " color = vec4(1.0, 1.0, 1.0, 1.0); \n" +
171 if(
null == vertexShader ) {
175 if(
null == tessCtrlShader ) {
180 if(
null == tessEvalShader ) {
182 tessCtrlShader.destroy(gl);
186 if(
null == fragmentShader ) {
188 tessCtrlShader.destroy(gl);
189 tessEvalShader.destroy(gl);
193 final ShaderProgram program =
new ShaderProgram();
196 program.add(vertexShader);
197 program.add(tessCtrlShader);
198 program.add(tessEvalShader);
199 program.add(fragmentShader);
201 program.link(gl, System.err);
202 if( !program.validateProgram(gl, System.out) ) {
203 System.err.println(
"[error] Program linking failed.");
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);
216 final boolean compiled = shader.compile(gl, System.err);
218 System.err.println(
"[error] Shader compilation failed.");
JOGL Tessellation ShaderCode GL4 test case.
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.
void destroy(final GL2ES2 gl)
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...
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...
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...
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...
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,...
static final int GL_TESS_EVALUATION_SHADER
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_EXT_tessellation_shader,...
static final int GL_TESS_CONTROL_SHADER
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_EXT_tessellation_shader,...
static final int GL_PATCH_VERTICES
GL_ARB_tessellation_shader, GL_ES_VERSION_3_2, GL_VERSION_4_0, GL_OES_tessellation_shader,...
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 ...