JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TessellationShader01bGL4.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;
40import com.jogamp.opengl.GLException;
41
42import com.jogamp.opengl.util.glsl.ShaderCode;
43import com.jogamp.opengl.util.glsl.ShaderProgram;
44
45/**
46 * JOGL Tessellation ShaderCode GL4 test case.
47 * <p>
48 * Demonstrates tessellation-control and -evaluation shaders.
49 * </p>
50 *
51 * @author Raymond L. Rivera, 2014
52 * @author Sven Gothel
53 */
55 private static final double ANIMATION_RATE = 950.0;
56
57 private ShaderProgram program;
58 private final int[] vertexArray = new int[1];
59 private FloatBuffer vertexOffset;
60 private FloatBuffer backgroundColor;
61
62
63 @Override
64 public void init(final GLAutoDrawable auto) {
65 final GL4 gl = auto.getGL().getGL4();
66 program = createProgram(auto);
67 if( null == program ) {
68 return;
69 }
70
71 final double theta = System.currentTimeMillis() / ANIMATION_RATE;
72 vertexOffset = FloatBuffer.allocate(4);
73 vertexOffset.put(0, (float)(Math.sin(theta) * 0.5f));
74 vertexOffset.put(1, (float)(Math.cos(theta) * 0.6f));
75 vertexOffset.put(2, 0.0f);
76 vertexOffset.put(3, 0.0f);
77
78 backgroundColor = FloatBuffer.allocate(4);
79 backgroundColor.put(0, 0.25f);
80 backgroundColor.put(1, 0.25f);
81 backgroundColor.put(2, 0.25f);
82 backgroundColor.put(3, 1.0f);
83
84 gl.glGenVertexArrays(vertexArray.length, vertexArray, 0);
85 gl.glBindVertexArray(vertexArray[0]);
88 }
89
90 @Override
91 public void display(final GLAutoDrawable auto) {
92 if( null == program ) {
93 return;
94 }
95 final GL4 gl = auto.getGL().getGL4();
96 final double value = System.currentTimeMillis() / ANIMATION_RATE;
97 gl.glClearBufferfv(GL2ES3.GL_COLOR, 0, backgroundColor);
98 gl.glUseProgram(program.program());
99 vertexOffset.put(0, (float)(Math.sin(value) * 0.5f));
100 vertexOffset.put(1, (float)(Math.cos(value) * 0.6f));
101 gl.glVertexAttrib4fv(0, vertexOffset);
103 }
104
105 @Override
106 public void dispose(final GLAutoDrawable auto) {
107 if( null == program ) {
108 return;
109 }
110 final GL4 gl = auto.getGL().getGL4();
111 gl.glDeleteVertexArrays(vertexArray.length, vertexArray, 0);
112 program.destroy(gl);
113 }
114
115 @Override
116 public void reshape(final GLAutoDrawable auto, final int x, final int y, final int width, final int height) {
117 // final GL4 gl = auto.getGL().getGL4();
118 }
119
120 static final String shaderBasename = "tess_example01";
121
122 private ShaderProgram createProgram(final GLAutoDrawable auto) {
123 final GL4 gl = auto.getGL().getGL4();
124
125 final ShaderProgram sp;
126 {
127 final ShaderCode vs, tcs, tes, fs;
128 vs = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(),
129 "shader", "shader/bin", shaderBasename, true);
130 tcs = ShaderCode.create(gl, GL3ES3.GL_TESS_CONTROL_SHADER, this.getClass(),
131 "shader", "shader/bin", shaderBasename, true);
132 tes = ShaderCode.create(gl, GL3ES3.GL_TESS_EVALUATION_SHADER, this.getClass(),
133 "shader", "shader/bin", shaderBasename, true);
134 fs = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(),
135 "shader", "shader/bin", shaderBasename, true);
136 vs.defaultShaderCustomization(gl, true, true);
137 tcs.defaultShaderCustomization(gl, true, true);
138 tes.defaultShaderCustomization(gl, true, true);
139 fs.defaultShaderCustomization(gl, true, true);
140
141 sp = new ShaderProgram();
142 sp.add(gl, vs, System.err);
143 sp.add(gl, tcs, System.err);
144 sp.add(gl, tes, System.err);
145 sp.add(gl, fs, System.err);
146 }
147 if( !sp.link(gl, System.err) ) {
148 System.err.println("[error] Couldn't link program: "+sp);
149 sp.destroy(gl);
150 return null;
151 } else {
152 return sp;
153 }
154 }
155}
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.
void display(final GLAutoDrawable auto)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable auto)
Called by the drawable immediately after the OpenGL context is initialized.
void dispose(final GLAutoDrawable auto)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
Convenient shader code class to use and instantiate vertex or fragment programs.
Definition: ShaderCode.java:75
final int defaultShaderCustomization(final GL2ES2 gl, final boolean preludeVersion, final boolean addDefaultPrecision)
Default customization of this shader source code.
static ShaderCode create(final GL2ES2 gl, final int type, final int count, final Class<?> context, final String[] sourceFiles, final boolean mutableStringBuilder)
Creates a complete ShaderCode object while reading all shader source of sourceFiles,...
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.
synchronized boolean link(final GL2ES2 gl, final PrintStream verboseOut)
Links the shader code to the program.
synchronized void add(final ShaderCode shaderCode)
Adds a new shader to this 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