JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
GeomShader01TextureGL3.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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.gl3;
29
30import java.io.IOException;
31import java.io.InputStream;
32import java.net.URLConnection;
33import java.nio.FloatBuffer;
34
35import com.jogamp.opengl.GL;
36import com.jogamp.opengl.GL2ES2;
37import com.jogamp.opengl.GL3;
38import com.jogamp.opengl.GL3ES3;
39import com.jogamp.opengl.GLAutoDrawable;
40import com.jogamp.opengl.GLEventListener;
41import com.jogamp.opengl.GLException;
42import com.jogamp.opengl.GLUniformData;
43import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
44
45import com.jogamp.common.util.IOUtil;
46import com.jogamp.opengl.util.GLArrayDataServer;
47import com.jogamp.opengl.util.PMVMatrix;
48import com.jogamp.opengl.util.glsl.ShaderCode;
49import com.jogamp.opengl.util.glsl.ShaderProgram;
50import com.jogamp.opengl.util.glsl.ShaderState;
51import com.jogamp.opengl.util.glsl.ShaderUtil;
52import com.jogamp.opengl.util.texture.Texture;
53import com.jogamp.opengl.util.texture.TextureCoords;
54import com.jogamp.opengl.util.texture.TextureData;
55import com.jogamp.opengl.util.texture.TextureIO;
56
57/**
58 * JOGL Geometry ShaderCode test case using OpenGL 3.2 core profile features only.
59 * <p>
60 * Demonstrates <code>pass through</code> and <code>XYZ flipping</code>
61 * geometry shader.
62 * </p>
63 * <p>
64 * If the <code>XYZ flipping</code> geometry shader functions properly,
65 * the texture will be flipped horizontally and vertically.
66 * </p>
67 *
68 * @author Chuck Ritola December 2012
69 * @author Sven Gothel (GL3 core, pass-though, core geometry shader)
70 */
71public class GeomShader01TextureGL3 implements GLEventListener {
72 private final int geomShader;
73 private Texture texture;
74 private ShaderState st;
75 private PMVMatrix pmvMatrix;
76 private GLUniformData pmvMatrixUniform;
77 private GLArrayDataServer interleavedVBO;
78
79 static final String shaderBasename = "texture01_xxx";
80 static final String[] geomShaderBaseNames = new String[] { "passthrough01_xxx", "flipXYZ01_xxx" };
81
82 public GeomShader01TextureGL3(final int geomShader) {
83 this.geomShader = geomShader;
84 }
85
86 @Override
87 public void init(final GLAutoDrawable drawable) {
88 {
89 final GL gl = drawable.getGL();
90 System.err.println("Init - START - useGeomShader "+geomShader+" -> "+geomShaderBaseNames[geomShader]);
91 System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
92 System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
93 System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
94 System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler-func: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none"));
95 System.err.println("GL Profile: "+gl.getGLProfile());
96 System.err.println("GL Renderer Quirks:" + gl.getContext().getRendererQuirks().toString());
97 System.err.println("GL:" + gl + ", " + gl.getContext().getGLVersion());
98 if( !gl.isGL3() ) {
99 throw new RuntimeException("GL object not a GL3 core compatible profile: "+gl);
100 }
102 throw new RuntimeException("GL object not >= 3.2, i.e. no geometry shader support.: "+gl);
103 }
104 }
105 final GL3 gl = drawable.getGL().getGL3();
106
107 final ShaderProgram sp;
108 {
109 final ShaderCode vs, gs, fs;
110 vs = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(),
111 "shader", "shader/bin", shaderBasename, true);
112 gs = ShaderCode.create(gl, GL3ES3.GL_GEOMETRY_SHADER, this.getClass(),
113 "shader", "shader/bin", geomShaderBaseNames[geomShader], true);
114 fs = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(),
115 "shader", "shader/bin", shaderBasename, true);
116 vs.defaultShaderCustomization(gl, true, true);
117 gs.defaultShaderCustomization(gl, true, true);
118 fs.defaultShaderCustomization(gl, true, true);
119
120 sp = new ShaderProgram();
121 sp.add(gl, vs, System.err);
122 sp.add(gl, gs, System.err);
123 sp.add(gl, fs, System.err);
124 if(!sp.link(gl, System.err)) {
125 throw new GLException("Couldn't link program: "+sp);
126 }
127 }
128
129 st=new ShaderState();
130 st.attachShaderProgram(gl, sp, true);
131
132 // setup mgl_PMVMatrix
133 pmvMatrix = new PMVMatrix();
135 pmvMatrix.glLoadIdentity();
137 pmvMatrix.glLoadIdentity();
138 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv()); // P, Mv
139 st.ownUniform(pmvMatrixUniform);
140 st.uniform(gl, pmvMatrixUniform);
141
142 st.ownUniform(pmvMatrixUniform);
143 if(!st.uniform(gl, pmvMatrixUniform)) {
144 throw new GLException("Error setting PMVMatrix in shader: "+st);
145 }
146 if(!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", 0))) {
147 throw new GLException("Error setting mgl_ActiveTexture in shader: "+st);
148 }
149
150 try {
151 texture = createTestTexture(gl);
152 } catch (final IOException e) {
153 throw new RuntimeException(e);
154 }
155 if(null == texture) {
156 throw new RuntimeException("Could not load test texture");
157 }
158
159 // Tri order:
160 // TL, BL, BR
161 // TL, TR, BR
162 {
163 int i=0;
164 final TextureCoords tc = texture.getImageTexCoords();
165 s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.top();
166 s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.bottom();
167 s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.bottom();
168 s_triTexCoords[i++] = tc.left(); s_triTexCoords[i++] = tc.top();
169 s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.top();
170 s_triTexCoords[i++] = tc.right(); s_triTexCoords[i++] = tc.bottom();
171 }
172
173 interleavedVBO = GLArrayDataServer.createGLSLInterleaved(2+4+2, GL.GL_FLOAT, false, 3*6, GL.GL_STATIC_DRAW);
174 {
175 interleavedVBO.addGLSLSubArray("mgl_Vertex", 2, GL.GL_ARRAY_BUFFER);
176 interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
177 interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);
178
179 final FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
180
181 for(int i=0; i<6; i++) {
182 ib.put(s_triVertices, i*2, 2);
183 ib.put(s_triColors, i*4, 4);
184 ib.put(s_triTexCoords, i*2, 2);
185 }
186 }
187 interleavedVBO.seal(gl, true);
188 interleavedVBO.enableBuffer(gl, false);
189 st.ownAttribute(interleavedVBO, true);
190
191 gl.glClearColor(0f, 0f, 0f, 0f);
193 st.useProgram(gl, false);
194 }
195
196 private Texture createTestTexture(final GL3 gl) throws IOException {
197 final URLConnection urlConn = IOUtil.getResource("../../util/texture/test-ntscN_3-01-160x90.png", this.getClass().getClassLoader(), this.getClass());
198 if(null == urlConn) { return null; }
199 final InputStream istream = urlConn.getInputStream();
200 if(null == istream) { return null; }
201 final TextureData texData = TextureIO.newTextureData(gl.getGLProfile(), istream, false /* mipmap */, TextureIO.PNG);
202 final Texture res = TextureIO.newTexture(gl, texData);
203 texData.destroy();
204 return res;
205 }
206
207 @Override
208 public void dispose(final GLAutoDrawable drawable) {
209 final GL3 gl = drawable.getGL().getGL3();
210 if(null!=texture) {
211 texture.disable(gl);
212 texture.destroy(gl);
213 }
214
215 if(null != st) {
216 pmvMatrixUniform = null;
217 pmvMatrix=null;
218 st.destroy(gl);
219 st=null;
220 }
221 }
222
223 @Override
224 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
225 final GL3 gl = drawable.getGL().getGL3();
226
227 gl.setSwapInterval(1);
228
229 // Clear background to white
230 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.4f);
231
232 if(null != st) {
234 pmvMatrix.glLoadIdentity();
235 pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
236
238 pmvMatrix.glLoadIdentity();
239
240 st.useProgram(gl, true);
241 st.uniform(gl, pmvMatrixUniform);
242 st.useProgram(gl, false);
243 }
244 }
245
246 @Override
247 public void display(final GLAutoDrawable drawable) {
248 final GL3 gl = drawable.getGL().getGL3();
249
251
252 if(null != st) {
253 //Draw the image as a pseudo-quad using two triangles
254 st.useProgram(gl, true);
255 interleavedVBO.enableBuffer(gl, true);
257 texture.enable(gl);
258 texture.bind(gl);
259
260 gl.glDrawArrays(GL.GL_TRIANGLES, 0, 6);
261
262 texture.disable(gl);
263 interleavedVBO.enableBuffer(gl, false);
264 st.useProgram(gl, false);
265 }
266 }//end display()
267
268 private static final float[] s_triVertices = {
269 -1f, 1f, // TL
270 -1f, -1f, // BL
271 1f, -1f, // BR
272 -1f, 1f, // TL
273 1f, 1f, // TR
274 1f, -1f // BR
275 };
276 private static final float[] s_triColors = {
277 1f, 1f, 1f, 1f,
278 1f, 1f, 1f, 1f,
279 1f, 1f, 1f, 1f,
280 1f, 1f, 1f, 1f,
281 1f, 1f, 1f, 1f,
282 1f, 1f, 1f, 1f
283 };
284 private static final float[] s_triTexCoords = {
285 0f, 1f, // TL
286 0f, 0f, // BL
287 1f, 0f, // BR
288 0f, 1f, // TL
289 1f, 1f, // TR
290 1f, 0f // BR
291 };
292
293}//end Test
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
final GLRendererQuirks getRendererQuirks()
Returns the instance of GLRendererQuirks, allowing one to determine workarounds.
Definition: GLContext.java:290
final String getGLVersion()
Returns a valid OpenGL version string, ie
Definition: GLContext.java:769
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
final StringBuilder toString(StringBuilder sb)
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
JOGL Geometry ShaderCode test case using OpenGL 3.2 core profile features only.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void reshape(final GLAutoDrawable drawable, 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 dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void seal(final GL gl, final boolean seal)
Convenience method calling seal(boolean) and enableBuffer(GL, boolean).
void enableBuffer(final GL gl, final boolean enable)
Enables the buffer if enable is true, and transfers the data if required.
static GLArrayDataServer createGLSLInterleaved(final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO for GLSL interleaved array data starting with a new created Buffer object with initialEl...
GLArrayDataWrapper addGLSLSubArray(final String name, final int comps, final int vboTarget)
Configure a segment of this GLSL interleaved array (see createGLSLInterleaved(int,...
Buffer getBuffer()
The Buffer holding the data, may be null if a GPU buffer without client bound data.
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void glOrthof(final float left, final float right, final float bottom, final float top, final float zNear, final float zFar)
Multiply the current matrix with the orthogonal matrix.
Definition: PMVMatrix.java:469
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
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,...
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.
ShaderState allows to sharing data between shader programs, while updating the attribute and uniform ...
void ownAttribute(final GLArrayData attribute, final boolean own)
Binds or unbinds the GLArrayData lifecycle to this ShaderState.
synchronized void useProgram(final GL2ES2 gl, final boolean on)
Turns the shader program on or off.
synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
synchronized void destroy(final GL2ES2 gl)
Calls release(gl, true, true, true).
boolean uniform(final GL2ES2 gl, final GLUniformData data)
Set the uniform data, if it's location is valid, i.e.
void ownUniform(final GLUniformData uniform)
Bind the GLUniform lifecycle to this ShaderState.
static boolean isGeometryShaderSupported(final GL _gl)
Returns true if GeometryShader is supported, i.e.
Specifies texture coordinates for a rectangular area of a texture.
Represents an OpenGL texture object.
Definition: Texture.java:173
TextureCoords getImageTexCoords()
Returns the set of texture coordinates corresponding to the entire image.
Definition: Texture.java:480
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
Definition: Texture.java:357
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
Definition: Texture.java:330
void destroy(final GL gl)
Destroys and nulls the underlying native texture used by this Texture instance if owned,...
Definition: Texture.java:388
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
static final int GL_SHADING_LANGUAGE_VERSION
GL_ES_VERSION_2_0, GL_VERSION_2_0, GL_ARB_shading_language_100 Alias for: GL_SHADING_LANGUAGE_VERSION...
Definition: GL2ES2.java:234
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
static final int GL_GEOMETRY_SHADER
GL_ES_VERSION_3_2, GL_VERSION_3_2, GL_OES_geometry_shader, GL_EXT_geometry_shader4,...
Definition: GL3ES3.java:402
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
boolean hasGLSL()
Indicates whether this GL object supports GLSL.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
GL3 getGL3()
Casts this object to the GL3 interface.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
boolean isGL3()
Indicates whether this GL object conforms to the OpenGL ≥ 3.1 core profile.
GLContext getContext()
Returns the GLContext associated which this GL object.
boolean isFunctionAvailable(String glFunctionName)
Returns true if the specified OpenGL core- or extension-function can be used successfully through thi...
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_STATIC_DRAW
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_STATI...
Definition: GL.java:673
static final int GL_TRIANGLES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLES" with expre...
Definition: GL.java:145
static final int GL_VERSION
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VERSION" with express...
Definition: GL.java:190
static final int GL_FLOAT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FLOAT" with expressio...
Definition: GL.java:786
static final int GL_COLOR_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_COLOR_BUFFER_BIT" wit...
Definition: GL.java:390
void glClearColor(float red, float green, float blue, float alpha)
Entry point to C language function: void {@native glClearColor}(GLfloat red, GLfloat green,...
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
void glActiveTexture(int texture)
Entry point to C language function: void {@native glActiveTexture}(GLenum texture) Part of GL_ES_V...
static final int GL_RENDERER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_RENDERER" with expres...
Definition: GL.java:662
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_VENDOR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_VENDOR" with expressi...
Definition: GL.java:607
static final int GL_TEXTURE0
GL_ES_VERSION_2_0, GL_VERSION_1_3, GL_VERSION_ES_1_0, GL_ARB_multitexture Alias for: GL_TEXTURE0_ARB ...
Definition: GL.java:71
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738
static final int GL_ARRAY_BUFFER
GL_VERSION_1_5, GL_ES_VERSION_2_0, GL_VERSION_ES_1_0, GL_ARB_vertex_buffer_object Alias for: GL_ARRAY...
Definition: GL.java:633
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
static final int GL_MODELVIEW
Matrix mode modelview.