JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Mix2TexturesES2.java
Go to the documentation of this file.
1/**
2 * Copyright 2011 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.es2;
29
30import java.nio.FloatBuffer;
31
32import com.jogamp.opengl.GL;
33import com.jogamp.opengl.GL2ES2;
34import com.jogamp.opengl.GLAutoDrawable;
35import com.jogamp.opengl.GLEventListener;
36import com.jogamp.opengl.GLUniformData;
37import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
38
39import com.jogamp.opengl.util.GLArrayDataServer;
40import com.jogamp.opengl.util.PMVMatrix;
41import com.jogamp.opengl.util.glsl.ShaderCode;
42import com.jogamp.opengl.util.glsl.ShaderProgram;
43import com.jogamp.opengl.util.glsl.ShaderState;
44
45public class Mix2TexturesES2 implements GLEventListener {
46 private final int swapInterval;
47
48 private final ShaderState st;
49 private final PMVMatrix pmvMatrix;
50 private final GLUniformData texUnit0, texUnit1;
51
52 private final Object syncTexIDs = new Object();
53 private int texID0, texID1;
54 private ShaderProgram sp0;
55 private GLUniformData pmvMatrixUniform;
56 private GLArrayDataServer interleavedVBO;
57
58 public Mix2TexturesES2(final int swapInterval, final int texUnit0, final int texUnit1) {
59 this.swapInterval = swapInterval;
60
61 st = new ShaderState();
62 // st.setVerbose(true);
63 pmvMatrix = new PMVMatrix();
64
65 if(0 == texUnit1) {
66 this.texUnit0 = new GLUniformData("mgl_ActiveTexture", texUnit0);
67 this.texUnit1 = null;
68 } else {
69 this.texUnit0 = new GLUniformData("mgl_Texture0", texUnit0);
70 this.texUnit1 = new GLUniformData("mgl_Texture1", texUnit1);
71 }
72 this.texID0 = 0;
73 this.texID1 = 0;
74 }
75
76 public void setTexID0(final int texID) {
77 synchronized( syncTexIDs ) {
78 this.texID0 = texID;
79 }
80 }
81 public void setTexID1(final int texID) {
82 synchronized( syncTexIDs ) {
83 this.texID1 = texID;
84 }
85 }
86
87 @Override
88 public void init(final GLAutoDrawable drawable) {
89 final GL2ES2 gl = drawable.getGL().getGL2ES2();
90
91 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, Mix2TexturesES2.class, "shader",
92 "shader/bin", "texture01_xxx", true);
94 "shader/bin", null == texUnit1 ? "texture01_xxx" : "texture02_xxx", true);
95 vp0.defaultShaderCustomization(gl, true, true);
96 fp0.defaultShaderCustomization(gl, true, true);
97
98 sp0 = new ShaderProgram();
99 sp0.add(gl, vp0, System.err);
100 sp0.add(gl, fp0, System.err);
101 st.attachShaderProgram(gl, sp0, true);
102
103 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv());
104 st.ownUniform(pmvMatrixUniform);
105 st.uniform(gl, pmvMatrixUniform);
106
107 interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3+4+2, GL.GL_FLOAT, false, 3*4, GL.GL_STATIC_DRAW);
108 {
109 interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
110 interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
111 //interleavedVBO.addGLSLSubArray("mgl_Normal", 3, GL.GL_ARRAY_BUFFER);
112 interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);
113
114 final FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
115
116 for(int i=0; i<4; i++) {
117 ib.put(s_quadVertices, i*3, 3);
118 ib.put(s_quadColors, i*4, 4);
119 //ib.put(s_cubeNormals, i*3, 3);
120 ib.put(s_quadTexCoords, i*2, 2);
121 }
122 }
123 interleavedVBO.seal(gl, true);
124 interleavedVBO.enableBuffer(gl, false);
125 st.ownAttribute(interleavedVBO, true);
126
127 st.ownUniform(texUnit0);
128 st.uniform(gl, texUnit0);
129 if(null != texUnit1) {
130 st.ownUniform(texUnit1);
131 st.uniform(gl, texUnit1);
132 }
133
134 st.useProgram(gl, false);
135 }
136
137 @Override
138 public void dispose(final GLAutoDrawable drawable) {
139 final GL2ES2 gl = drawable.getGL().getGL2ES2();
140 st.destroy(gl);
141
142 sp0 = null;
143 pmvMatrixUniform = null;
144 interleavedVBO = null;
145 }
146
147 @Override
148 public void display(final GLAutoDrawable drawable) {
149 final GL2ES2 gl = drawable.getGL().getGL2ES2();
150
151 st.useProgram(gl, true);
152 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
154
155 interleavedVBO.enableBuffer(gl, true);
156
157 synchronized( syncTexIDs ) {
158 if(0<texID0) {
159 gl.glActiveTexture(GL.GL_TEXTURE0 + texUnit0.intValue());
160 gl.glBindTexture(GL.GL_TEXTURE_2D, texID0);
161 }
162
163 if(0<texID1 && null != texUnit1) {
164 gl.glActiveTexture(GL.GL_TEXTURE0 + texUnit1.intValue());
165 gl.glBindTexture(GL.GL_TEXTURE_2D, texID1);
166 }
167
168 if( !gl.isGLcore() ) {
170 }
171
173 }
174
175 interleavedVBO.enableBuffer(gl, false);
176
177 st.useProgram(gl, false);
178 }
179
180 @Override
181 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
182 final GL2ES2 gl = drawable.getGL().getGL2ES2();
183
184 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
185
187 pmvMatrix.glLoadIdentity();
188 pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
189
191 pmvMatrix.glLoadIdentity();
192
193 st.useProgram(gl, true);
194 st.uniform(gl, pmvMatrixUniform);
195 st.useProgram(gl, false);
196
197 }
198
199 private static final float[] s_quadVertices = {
200 -1f, -1f, 0f, // LB
201 1f, -1f, 0f, // RB
202 -1f, 1f, 0f, // LT
203 1f, 1f, 0f // RT
204 };
205 private static final float[] s_quadColors = {
206 1f, 1f, 1f, 1f,
207 1f, 1f, 1f, 1f,
208 1f, 1f, 1f, 1f,
209 1f, 1f, 1f, 1f };
210 private static final float[] s_quadTexCoords = {
211 0f, 0f, // LB
212 1f, 0f, // RB
213 0f, 1f, // LT
214 1f, 1f // RT
215 };
216}
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
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 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.
Mix2TexturesES2(final int swapInterval, final int texUnit0, final int texUnit1)
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 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 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.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
boolean isGLcore()
Indicates whether this GL object uses a GL core profile.
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_TEXTURE_2D
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_2D" with expr...
Definition: GL.java:491
void glBindTexture(int target, int texture)
Entry point to C language function: void {@native glBindTexture}(GLenum target, GLuint texture) Pa...
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,...
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.