JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
LineSquareXDemoES2.java
Go to the documentation of this file.
1/**
2 * Copyright (C) 2015 JogAmp Community. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22package com.jogamp.opengl.test.junit.jogl.demos.es2;
23
24import com.jogamp.opengl.GL;
25import com.jogamp.opengl.GL2ES2;
26import com.jogamp.opengl.GLAutoDrawable;
27import com.jogamp.opengl.GLEventListener;
28import com.jogamp.opengl.GLUniformData;
29import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
30
31import com.jogamp.opengl.util.ImmModeSink;
32import com.jogamp.opengl.util.PMVMatrix;
33import com.jogamp.opengl.util.glsl.ShaderCode;
34import com.jogamp.opengl.util.glsl.ShaderProgram;
35import com.jogamp.opengl.util.glsl.ShaderState;
36
37public class LineSquareXDemoES2 implements GLEventListener {
38
39 private boolean multisample, clearBuffers;
40 private final ShaderState st;
41 private final PMVMatrix pmvMatrix;
42 private ShaderProgram sp0;
43 private GLUniformData pmvMatrixUniform;
44 private ImmModeSink immModeSink;
45
46 public LineSquareXDemoES2(final boolean multisample) {
47 this.multisample = multisample;
48 this.clearBuffers = true;
49 st = new ShaderState();
50 st.setVerbose(true);
51 pmvMatrix = new PMVMatrix();
52 }
53
54 public void setClearBuffers(final boolean v) { clearBuffers = v; }
55
56 @Override
57 public void init(final GLAutoDrawable glad) {
58 final GL2ES2 gl = glad.getGL().getGL2ES2();
59
60 System.err.println();
61 System.err.println("req. msaa: "+multisample);
62 System.err.println("Requested: " + glad.getNativeSurface().getGraphicsConfiguration().getRequestedCapabilities());
63 multisample = multisample && glad.getChosenGLCapabilities().getNumSamples() > 0 ;
64 System.err.println("Chosen : " + glad.getChosenGLCapabilities());
65 System.err.println("has msaa: "+multisample);
66 System.err.println();
67
69 "shader/bin", "mgl_default_xxx", true);
71 "shader/bin", "mgl_default_xxx", true);
72 vp0.defaultShaderCustomization(gl, true, true);
73 fp0.defaultShaderCustomization(gl, true, true);
74
75 sp0 = new ShaderProgram();
76 sp0.add(gl, vp0, System.err);
77 sp0.add(gl, fp0, System.err);
78 st.attachShaderProgram(gl, sp0, true);
79
80 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv());
81 st.ownUniform(pmvMatrixUniform);
82 st.uniform(gl, pmvMatrixUniform);
83
84 final float c = 0f;
85 final float eX = 0.5f;
86 final float eH = 0.98f;
87 final float e2 = 1f;
88
89 // Using predef array names, see
90 // GLPointerFuncUtil.getPredefinedArrayIndexName(glArrayIndex);
91 immModeSink = ImmModeSink.createGLSL(20*2,
92 3, GL.GL_FLOAT, // vertex
93 4, GL.GL_FLOAT, // color
94 0, GL.GL_FLOAT, // normal
95 0, GL.GL_FLOAT, // texCoords
96 GL.GL_STATIC_DRAW, st);
97 immModeSink.glBegin(GL.GL_LINES);
98
99 // Rectangle
100 immModeSink.glVertex3f(-eX, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
101 immModeSink.glVertex3f(-eX, eH, 0f); immModeSink.glColor4f( c, c, c, c );
102 immModeSink.glVertex3f(-eX, eH, 0f); immModeSink.glColor4f( c, c, c, c );
103 immModeSink.glVertex3f( eX, eH, 0f); immModeSink.glColor4f( c, c, c, c );
104 immModeSink.glVertex3f( eX, eH, 0f); immModeSink.glColor4f( c, c, c, c );
105 immModeSink.glVertex3f( eX, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
106 immModeSink.glVertex3f( eX, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
107 immModeSink.glVertex3f(-eX, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
108
109 // Square
110 immModeSink.glVertex3f(-eH, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
111 immModeSink.glVertex3f(-eH, eH, 0f); immModeSink.glColor4f( c, c, c, c );
112 immModeSink.glVertex3f(-eH, eH, 0f); immModeSink.glColor4f( c, c, c, c );
113 immModeSink.glVertex3f( eH, eH, 0f); immModeSink.glColor4f( c, c, c, c );
114 immModeSink.glVertex3f( eH, eH, 0f); immModeSink.glColor4f( c, c, c, c );
115 immModeSink.glVertex3f( eH, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
116 immModeSink.glVertex3f( eH, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
117 immModeSink.glVertex3f(-eH, -eH, 0f); immModeSink.glColor4f( c, c, c, c );
118
119 // X
120 immModeSink.glVertex3f(-e2, -e2, 0f); immModeSink.glColor4f( c, c, c, c );
121 immModeSink.glVertex3f( e2, e2, 0f); immModeSink.glColor4f( c, c, c, c );
122 immModeSink.glVertex3f(-e2, e2, 0f); immModeSink.glColor4f( c, c, c, c );
123 immModeSink.glVertex3f( e2, -e2, 0f); immModeSink.glColor4f( c, c, c, c );
124
125 immModeSink.glEnd(gl, false);
126
127 st.useProgram(gl, false);
128 }
129
130 @Override
131 public void dispose(final GLAutoDrawable glad) {
132 final GL2ES2 gl = glad.getGL().getGL2ES2();
133 immModeSink.destroy(gl);
134 immModeSink = null;
135 st.destroy(gl);
136 }
137
138 @Override
139 public void display(final GLAutoDrawable glad) {
140 final GL2ES2 gl = glad.getGL().getGL2ES2();
141 if (multisample) {
143 }
144 if( clearBuffers ) {
145 final float c = 0.9f;
146 gl.glClearColor(c, c, c, 0);
147 // gl.glEnable(GL.GL_DEPTH_TEST);
148 // gl.glDepthFunc(GL.GL_LESS);
150 }
151
152 st.useProgram(gl, true);
153
154 immModeSink.draw(gl, true);
155
156 st.useProgram(gl, false);
157 }
158
159 // Unused routines
160 @Override
161 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
162 System.err.println("reshape ..");
163 final GL2ES2 gl = glad.getGL().getGL2ES2();
165 pmvMatrix.glLoadIdentity();
166 final float left, right, bottom, top;
167 if( height > width ) {
168 final float a = (float)height / (float)width;
169 left = -1.0f;
170 right = 1.0f;
171 bottom = -a;
172 top = a;
173 } else {
174 final float a = (float)width / (float)height;
175 left = -a;
176 right = a;
177 bottom = -1.0f;
178 top = 1.0f;
179 }
180 // pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
181 // pmvMatrix.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 10.0f);
182 pmvMatrix.glOrthof(left, right, top, bottom, 0.0f, 10.0f);
184 pmvMatrix.glLoadIdentity();
185
186 st.useProgram(gl, true);
187 st.uniform(gl, pmvMatrixUniform);
188 st.useProgram(gl, false);
189 }
190
191 public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) {
192 }
193}
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 display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
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 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 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.
void draw(final GL gl, final boolean disableBufferAfterDraw)
final void glColor4f(final float x, final float y, final float z, final float a)
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,...
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.
CapabilitiesImmutable getRequestedCapabilities()
Return the capabilities used to choose this graphics configuration.
AbstractGraphicsConfiguration getGraphicsConfiguration()
Returns the graphics configuration corresponding to this window.
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.
int getNumSamples()
Returns the number of sample buffers to be allocated if sample buffers are enabled,...
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
static final int GL_MULTISAMPLE
Common in ES1, GL2 and GL3.
Definition: GL.java:1249
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,...
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
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_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.