JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TextureDraw01ES2Listener.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 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 */
28
29package com.jogamp.opengl.test.junit.jogl.demos.es2;
30
31import java.nio.FloatBuffer;
32
33import com.jogamp.opengl.test.junit.jogl.demos.TextureDraw01Accessor;
34import com.jogamp.opengl.util.GLArrayDataServer;
35import com.jogamp.opengl.util.PMVMatrix;
36import com.jogamp.opengl.util.glsl.ShaderCode;
37import com.jogamp.opengl.util.glsl.ShaderProgram;
38import com.jogamp.opengl.util.glsl.ShaderState;
39import com.jogamp.opengl.util.texture.Texture;
40import com.jogamp.opengl.util.texture.TextureData;
41import com.jogamp.opengl.util.texture.TextureIO;
42
43import com.jogamp.opengl.GL;
44import com.jogamp.opengl.GL2ES2;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLEventListener;
47import com.jogamp.opengl.GLException;
48import com.jogamp.opengl.GLUniformData;
49import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
50
52 TextureData textureData;
53 Texture texture;
54 int textureUnit;
55 boolean keepTextureBound;
56
57 ShaderState st;
58 PMVMatrix pmvMatrix;
59 GLUniformData pmvMatrixUniform;
60 GLArrayDataServer interleavedVBO;
61 float[] clearColor = new float[] { 1.0f, 1.0f, 1.0f, 1.0f };
62
63 /**
64 *
65 * @param td
66 * @param textureUnit of range [0..]
67 */
68 public TextureDraw01ES2Listener(final TextureData td, final int textureUnit) {
69 this.textureData = td;
70 this.textureUnit = textureUnit;
71 this.keepTextureBound = false;
72 }
73
74 public void setClearColor(final float[] clearColor) {
75 this.clearColor = clearColor;
76 }
77
78 @Override
79 public void setKeepTextureBound(final boolean v) {
80 this.keepTextureBound = v;
81 }
82 @Override
83 public Texture getTexture( ) {
84 return this.texture;
85 }
86
87 /**
88 public void setTextureData(GL gl, TextureData textureData ) {
89 if(null!=texture) {
90 texture.disable(gl);
91 texture.destroy(gl);
92 }
93 if(null!=this.textureData) {
94 this.textureData.destroy();
95 }
96 this.textureData = textureData;
97 this.texture = TextureIO.newTexture(this.textureData);
98
99 // fix VBO !
100 } */
101
102 static final String shaderBasename = "texture01_xxx";
103
104 private void initShader(final GL2ES2 gl, final boolean use_program) {
105 // Create & Compile the shader objects
106 final ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(),
107 "shader", "shader/bin", shaderBasename, true);
108 final ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(),
109 "shader", "shader/bin", shaderBasename, true);
110 rsVp.defaultShaderCustomization(gl, true, true);
111 rsFp.defaultShaderCustomization(gl, true, true);
112
113 // Create & Link the shader program
114 final ShaderProgram sp = new ShaderProgram();
115 sp.add(rsVp);
116 sp.add(rsFp);
117 if(!sp.link(gl, System.err)) {
118 throw new GLException("Couldn't link program: "+sp);
119 }
120
121 // Let's manage all our states using ShaderState.
122 st = new ShaderState();
123 st.attachShaderProgram(gl, sp, use_program);
124 }
125
126 @Override
127 public void init(final GLAutoDrawable glad) {
128 if( null != textureData ) {
129 texture = TextureIO.newTexture(glad.getGL(), textureData);
130 }
131 final GL2ES2 gl = glad.getGL().getGL2ES2();
132
133 initShader(gl, true);
134
135 // setup mgl_PMVMatrix
136 pmvMatrix = new PMVMatrix();
138 pmvMatrix.glLoadIdentity();
140 pmvMatrix.glLoadIdentity();
141 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv()); // P, Mv
142
143 st.ownUniform(pmvMatrixUniform);
144 if(!st.uniform(gl, pmvMatrixUniform)) {
145 throw new GLException("Error setting PMVMatrix in shader: "+st);
146 }
147 if(!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", textureUnit))) {
148 throw new GLException("Error setting mgl_ActiveTexture in shader: "+st);
149 }
150
151 if( null != texture ) {
152 // fetch the flipped texture coordinates
153 texture.getImageTexCoords().getST_LB_RB_LT_RT(s_quadTexCoords, 0, 1f, 1f);
154 }
155
156 interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3+4+2, GL.GL_FLOAT, false, 3*4, GL.GL_STATIC_DRAW);
157 {
158 interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
159 interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
160 //interleavedVBO.addGLSLSubArray("mgl_Normal", 3, GL.GL_ARRAY_BUFFER);
161 interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);
162
163 final FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
164
165 for(int i=0; i<4; i++) {
166 ib.put(s_quadVertices, i*3, 3);
167 ib.put(s_quadColors, i*4, 4);
168 //ib.put(s_cubeNormals, i*3, 3);
169 ib.put(s_quadTexCoords, i*2, 2);
170 }
171 }
172 interleavedVBO.seal(gl, true);
173 interleavedVBO.enableBuffer(gl, false);
174 st.ownAttribute(interleavedVBO, true);
175
176 // OpenGL Render Settings
177 gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
179
180 if( keepTextureBound && null != texture ) {
181 gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit);
182 texture.enable(gl);
183 texture.bind(gl);
184 }
185 st.useProgram(gl, false);
186 }
187
188 @Override
189 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
190 final GL2ES2 gl = drawable.getGL().getGL2ES2();
191
192 // Clear background to white
193 gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
194 if(null != st) {
196 pmvMatrix.glLoadIdentity();
197 pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
198
200 pmvMatrix.glLoadIdentity();
201
202 st.useProgram(gl, true);
203 st.uniform(gl, pmvMatrixUniform);
204 st.useProgram(gl, false);
205 }
206 }
207
208 @Override
209 public void dispose(final GLAutoDrawable drawable) {
210 final GL2ES2 gl = drawable.getGL().getGL2ES2();
211 if(null!=texture) {
212 texture.disable(gl);
213 texture.destroy(gl);
214 }
215 if(null!=textureData) {
216 textureData.destroy();
217 }
218
219 pmvMatrixUniform = null;
220 pmvMatrix=null;
221 st.destroy(gl);
222 st=null;
223 }
224
225 @Override
226 public void display(final GLAutoDrawable drawable) {
227 final GL2ES2 gl = drawable.getGL().getGL2ES2();
228
230
231 st.useProgram(gl, true);
232 interleavedVBO.enableBuffer(gl, true);
233 if( !keepTextureBound && null != texture ) {
234 gl.glActiveTexture(GL.GL_TEXTURE0 + textureUnit);
235 texture.enable(gl);
236 texture.bind(gl);
237 }
238
240
241 if( !keepTextureBound && null != texture ) {
242 texture.disable(gl);
243 }
244 interleavedVBO.enableBuffer(gl, false);
245 st.useProgram(gl, false);
246 }
247
248 private static final float[] s_quadVertices = {
249 -1f, -1f, 0f, // LB
250 1f, -1f, 0f, // RB
251 -1f, 1f, 0f, // LT
252 1f, 1f, 0f // RT
253 };
254 private static final float[] s_quadColors = {
255 1f, 1f, 1f, 1f,
256 1f, 1f, 1f, 1f,
257 1f, 1f, 1f, 1f,
258 1f, 1f, 1f, 1f };
259 private static final float[] s_quadTexCoords = {
260 0f, 0f, // LB
261 1f, 0f, // RB
262 0f, 1f, // LT
263 1f, 1f // RT
264 };
265}
266
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
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 display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
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.
float[] getST_LB_RB_LT_RT(final float[] d, final int d_off, final float ss, final float ts)
Transfers {s * ss, t * ts} from this object into the given float[8+d_off] in the following order:
Represents the data for an OpenGL texture.
static Texture newTexture(final TextureData data)
Creates an OpenGL texture object from the specified TextureData using the current OpenGL context.
Definition: TextureIO.java:459
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_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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 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_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_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,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
void glActiveTexture(int texture)
Entry point to C language function: void {@native glActiveTexture}(GLenum texture) Part of GL_ES_V...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
static final int GL_TRIANGLE_STRIP
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TRIANGLE_STRIP" with ...
Definition: GL.java:760
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.