JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
DemoGL2ES2ImmModeSink.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.util;
30
31import com.jogamp.opengl.GL;
32import com.jogamp.opengl.GL2ES2;
33import com.jogamp.opengl.GLAutoDrawable;
34import com.jogamp.opengl.GLEventListener;
35import com.jogamp.opengl.GLException;
36import com.jogamp.opengl.GLUniformData;
37import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
38
39import com.jogamp.opengl.util.ImmModeSink;
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 DemoGL2ES2ImmModeSink implements GLEventListener {
46
47 private final ShaderState st;
48 private final PMVMatrix pmvMatrix;
49 private final int glBufferUsage;
50 private ShaderProgram sp;
51 private GLUniformData pmvMatrixUniform;
52 private ImmModeSink ims;
53
54 public DemoGL2ES2ImmModeSink(final boolean useVBO, final boolean useShaderState) {
55 if(useShaderState) {
56 st = new ShaderState();
57 st.setVerbose(true);
58 } else {
59 st = null;
60 }
61 glBufferUsage = useVBO ? GL.GL_STATIC_DRAW : 0;
62 pmvMatrix = new PMVMatrix();
63 }
64
65 @Override
66 public void init(final GLAutoDrawable glad) {
67 final GL2ES2 gl = glad.getGL().getGL2ES2();
68
69 System.err.println("GL_VENDOR "+gl.glGetString(GL.GL_VENDOR));
70 System.err.println("GL_RENDERER "+gl.glGetString(GL.GL_RENDERER));
71 System.err.println("GL_VERSION "+gl.glGetString(GL.GL_VERSION));
72
74 "../demos/es2/shader", "../demos/es2/shader/bin", "mgl_default_xxx", true);
76 "../demos/es2/shader", "../demos/es2/shader/bin", "mgl_default_xxx", true);
77 vp0.defaultShaderCustomization(gl, true, true);
78 fp0.defaultShaderCustomization(gl, true, true);
79
80 sp = new ShaderProgram();
81 sp.add(gl, vp0, System.err);
82 sp.add(gl, fp0, System.err);
83 if( null != st ) {
84 st.attachShaderProgram(gl, sp, true);
85 } else {
86 if(!sp.link(gl, System.err)) {
87 throw new GLException("Could not link program: "+sp);
88 }
89 sp.useProgram(gl, true);
90 }
91
92 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv().getSyncFloats());
93 if(null != st) {
94 st.ownUniform(pmvMatrixUniform);
95 st.uniform(gl, pmvMatrixUniform);
96 } else {
97 if( pmvMatrixUniform.setLocation(gl, sp.program()) < 0 ) {
98 throw new GLException("Could not find location for uniform: "+pmvMatrixUniform+", "+sp);
99 }
100 gl.glUniform(pmvMatrixUniform);
101 }
102
103 // Using predef array names, see
104 // GLPointerFuncUtil.getPredefinedArrayIndexName(glArrayIndex);
105 if( null != st ) {
106 ims = ImmModeSink.createGLSL(40,
107 3, GL.GL_FLOAT, // vertex
108 4, GL.GL_FLOAT, // color
109 0, GL.GL_FLOAT, // normal
110 0, GL.GL_FLOAT, // texCoords
111 glBufferUsage, st);
112 } else {
113 ims = ImmModeSink.createGLSL(40,
114 3, GL.GL_FLOAT, // vertex
115 4, GL.GL_FLOAT, // color
116 0, GL.GL_FLOAT, // normal
117 0, GL.GL_FLOAT, // texCoords
118 glBufferUsage, sp.program());
119 }
120 final int numSteps = 20;
121 final double increment = Math.PI / numSteps;
122 final double radius = 1;
123 ims.glBegin(GL.GL_LINES);
124 for (int i = numSteps - 1; i >= 0; i--) {
125 ims.glVertex3f((float) (radius * Math.cos(i * increment)),
126 (float) (radius * Math.sin(i * increment)),
127 0f);
128 ims.glColor4f( 1f, 1f, 1f, 1f );
129 ims.glVertex3f((float) (-1.0 * radius * Math.cos(i * increment)),
130 (float) (-1.0 * radius * Math.sin(i * increment)),
131 0f);
132 ims.glColor4f( 1f, 1f, 1f, 1f );
133 }
134 ims.glEnd(gl, false);
135
136 if(null != st) {
137 st.useProgram(gl, false);
138 } else {
139 gl.glUseProgram(0);
140 }
141 }
142
143 @Override
144 public void dispose(final GLAutoDrawable glad) {
145 final GL2ES2 gl = glad.getGL().getGL2ES2();
146 ims.destroy(gl);
147 ims = null;
148 if(null != st) {
149 st.destroy(gl);
150 }
151 }
152
153 @Override
154 public void display(final GLAutoDrawable drawable) {
155 final GL2ES2 gl = drawable.getGL().getGL2ES2();
156
158
159 // draw a triangle filling the window
161 ims.glColor3f( 1, 0, 0 );
162 ims.glVertex2f( 0, 0 );
163 ims.glColor3f( 0, 1, 0 );
164 ims.glVertex2f( drawable.getSurfaceWidth(), 0 );
165 ims.glColor3f( 0, 0, 1 );
166 ims.glVertex2f( drawable.getSurfaceWidth() / 2f, drawable.getSurfaceHeight() );
167 ims.glEnd(gl, true);
168 }
169
170 // Unused routines
171 @Override
172 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
173 System.err.println("reshape ..");
174 final GL2ES2 gl = glad.getGL().getGL2ES2();
176 pmvMatrix.glLoadIdentity();
177
178 // coordinate system origin at lower left with width and height same as the window
179 pmvMatrix.glOrthof( 0.0f, width, 0.0f, height, -1, 1 );
180
182 pmvMatrix.glLoadIdentity();
183
184 if(null != st) {
185 st.useProgram(gl, true);
186 st.uniform(gl, pmvMatrixUniform);
187 st.useProgram(gl, false);
188 } else {
189 gl.glUseProgram(sp.program());
190 gl.glUniform(pmvMatrixUniform);
191 gl.glUseProgram(0);
192 }
193 }
194
195 public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {
196 }
197}
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.
int setLocation(final int location)
Sets the given location of the shader uniform.
DemoGL2ES2ImmModeSink(final boolean useVBO, final boolean useShaderState)
void reshape(final GLAutoDrawable glad, 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 glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged)
final void glVertex2f(final float x, final float y)
final void glVertex3f(final float x, final float y, final float z)
static ImmModeSink createGLSL(final int initialElementCount, final int vComps, final int vDataType, final int cComps, final int cDataType, final int nComps, final int nDataType, final int tComps, final int tDataType, final int glBufferUsage, final ShaderState st)
Uses a GL2ES2 GLSL shader immediate mode sink, utilizing the given ShaderState.
final void glColor4f(final float x, final float y, final float z, final float a)
final void glColor3f(final float x, final float y, final float z)
final void glEnd(final GL gl)
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,...
int program()
Returns the shader program name, which is non zero if valid.
synchronized void useProgram(final GL2ES2 gl, boolean on)
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 ...
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.
FloatBuffer getSyncFloats()
Return the FloatBuffer after synchronizing it w/ the underlying getMatrices().
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 glUniform(GLUniformData data)
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
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.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
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
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
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_LINES
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINES" with expressio...
Definition: GL.java:430
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.