JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
PointsDemoES2.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 */
28package com.jogamp.opengl.demos.es2;
29
30import java.nio.FloatBuffer;
31
32import com.jogamp.common.nio.Buffers;
33import com.jogamp.common.util.VersionUtil;
34import com.jogamp.math.FloatUtil;
35import com.jogamp.newt.event.WindowAdapter;
36import com.jogamp.newt.event.WindowEvent;
37import com.jogamp.newt.opengl.GLWindow;
38import com.jogamp.opengl.util.Animator;
39import com.jogamp.opengl.util.GLArrayDataServer;
40import com.jogamp.opengl.util.PMVMatrix;
41import com.jogamp.opengl.util.caps.NonFSAAGLCapsChooser;
42import com.jogamp.opengl.util.glsl.ShaderCode;
43import com.jogamp.opengl.util.glsl.ShaderProgram;
44import com.jogamp.opengl.util.glsl.ShaderState;
45
46import com.jogamp.opengl.GL;
47import com.jogamp.opengl.GL2ES1;
48import com.jogamp.opengl.GL2ES2;
49import com.jogamp.opengl.GL2GL3;
50import com.jogamp.opengl.GLAutoDrawable;
51import com.jogamp.opengl.GLCapabilities;
52import com.jogamp.opengl.GLProfile;
53import com.jogamp.opengl.GLUniformData;
54import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
55import com.jogamp.opengl.demos.PointsDemo;
56import com.jogamp.opengl.demos.util.CommandlineOptions;
57
58public class PointsDemoES2 extends PointsDemo {
59 ShaderState st;
60 PMVMatrix pmvMatrix;
61 GLUniformData pmvMatrixUniform;
62 GLArrayDataServer vertices ;
63 GLArrayDataServer pointSizes ;
64 private int swapInterval = 0;
65 final int edge = 8; // 8*8
66 /** vec4[2]: { (sz, smooth, attnMinSz, attnMaxSz), (attnCoeff(3), attnFadeTs) } */
67 private static final String mgl_PointParams = "mgl_PointParams";
68
69 /** ( pointSize, pointSmooth, attn. pointMinSize, attn. pointMaxSize ) , ( attenuation coefficients 1f 0f 0f, attenuation fade theshold 1f ) */
70 private final FloatBuffer pointParams = Buffers.newDirectFloatBuffer(new float[] { 1.0f, 0.0f, 0.0f, 4096.0f, 1.0f, 0.0f, 0.0f, 1.0f });
71
72 public PointsDemoES2(final int swapInterval) {
73 this.swapInterval = swapInterval;
74 }
75
76 public PointsDemoES2() {
77 this.swapInterval = 1;
78 }
79
80 @Override
81 public void setSmoothPoints(final boolean v) {
82 pointParams.put(1, v ? 1.0f : 0.0f);
83 }
84
85 @Override
86 public void setPointParams(final float minSize, final float maxSize, final float distAttenConst, final float distAttenLinear, final float distAttenQuadratic, final float fadeThreshold) {
87 pointParams.put(2, minSize);
88 pointParams.put(3, maxSize);
89 pointParams.put(4+0, distAttenConst);
90 pointParams.put(4+1, distAttenLinear);
91 pointParams.put(4+2, distAttenQuadratic);
92 pointParams.put(4+3, fadeThreshold);
93 }
94
95 @Override
96 public void init(final GLAutoDrawable glad) {
97 final GL2ES2 gl = glad.getGL().getGL2ES2();
98
99 System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
100 System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
101 System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
102 System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler-func: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none"));
103 System.err.println("GL Profile: "+gl.getGLProfile());
104
105 st = new ShaderState();
106 st.setVerbose(true);
107 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader",
108 "shader/bin", "PointsShader", true);
109 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader",
110 "shader/bin", "PointsShader", true);
111 vp0.defaultShaderCustomization(gl, true, true);
112 fp0.defaultShaderCustomization(gl, true, true);
113 final ShaderProgram sp0 = new ShaderProgram();
114 sp0.add(gl, vp0, System.err);
115 sp0.add(gl, fp0, System.err);
116 st.attachShaderProgram(gl, sp0, true);
117
118 // setup mgl_PMVMatrix
119 pmvMatrix = new PMVMatrix();
121 pmvMatrix.glLoadIdentity();
123 pmvMatrix.glLoadIdentity();
124 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv()); // P, Mv
125 st.ownUniform(pmvMatrixUniform);
126 st.uniform(gl, pmvMatrixUniform);
127
128 st.uniform(gl, new GLUniformData(mgl_PointParams, 4, pointParams));
129
130 final GLUniformData colorStaticUniform = new GLUniformData("mgl_ColorStatic", 4, Buffers.newDirectFloatBuffer(new float[] { 1.0f, 1.0f, 1.0f, 1.0f }) );
131 st.uniform(gl, colorStaticUniform);
132 st.ownUniform(colorStaticUniform);
133
134 // Allocate Vertex Array
135 vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT, false, edge*edge, GL.GL_STATIC_DRAW);
136 pointSizes = GLArrayDataServer.createGLSL("mgl_PointSize", 1, GL.GL_FLOAT, false, edge*edge, GL.GL_STATIC_DRAW);
137 for(int i=0; i<edge; i++) {
138 for(int j=0; j<edge; j++) {
139 final float x = -3+j*0.7f;
140 final float y = -3+i*0.7f;
141 final float p = (i*edge+j)*0.5f;
142 // System.err.println("["+j+"/"+i+"]: "+x+"/"+y+": "+p);
143 vertices.putf(x); vertices.putf(y); vertices.putf( 0);
144 pointSizes.putf(p);
145 }
146 }
147 vertices.seal(gl, true);
148 st.ownAttribute(vertices, true);
149 vertices.enableBuffer(gl, false);
150 pointSizes.seal(gl, true);
151 st.ownAttribute(pointSizes, true);
152 pointSizes.enableBuffer(gl, false);
153
154 // OpenGL Render Settings
156 st.useProgram(gl, false);
157 }
158
159 @Override
160 public void display(final GLAutoDrawable glad) {
161 final GL2ES2 gl = glad.getGL().getGL2ES2();
162 gl.glClearColor(0f, 0f, 0f, 0f);
164 st.useProgram(gl, true);
166 pmvMatrix.glLoadIdentity();
167 pmvMatrix.glTranslatef(0, 0, -10);
168 st.uniform(gl, pmvMatrixUniform);
169
170 final GLUniformData ud = st.getUniform(mgl_PointParams);
171 if(null!=ud) {
172 // same data object
173 st.uniform(gl, ud);
174 }
175
176 vertices.enableBuffer(gl, true);
177 pointSizes.enableBuffer(gl, true);
178
179 if(gl.isGL2GL3()) {
181 }
182 if(gl.isGL2ES1()) {
183 gl.glEnable(GL2ES1.GL_POINT_SPRITE); // otherwise no gl_PointCoord
184 }
185 gl.glEnable ( GL.GL_BLEND );
186 gl.glBlendFunc ( GL.GL_SRC_ALPHA, GL.GL_ONE );
187
188 gl.glDrawArrays(GL.GL_POINTS, 0, edge*edge);
189
190 if(gl.isGL2GL3()) {
192 }
193
194 pointSizes.enableBuffer(gl, false);
195 vertices.enableBuffer(gl, false);
196 st.useProgram(gl, false);
197 }
198
199 @Override
200 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
201 // Thread.dumpStack();
202 final GL2ES2 gl = glad.getGL().getGL2ES2();
203
204 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
205
206 st.useProgram(gl, true);
207 // Set location in front of camera
209 pmvMatrix.glLoadIdentity();
210 pmvMatrix.gluPerspective(FloatUtil.QUARTER_PI, ( (float) width / (float) height ) / 1.0f, 1.0F, 100.0F);
211 //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
212 st.uniform(gl, pmvMatrixUniform);
213 st.useProgram(gl, false);
214 }
215
216 @Override
217 public void dispose(final GLAutoDrawable glad) {
218 final GL2ES2 gl = glad.getGL().getGL2ES2();
219 st.destroy(gl);
220 st = null;
221 pmvMatrix = null;
222 }
223
224 public static void main(final String[] args) {
225 final CommandlineOptions options = new CommandlineOptions(1280, 720, 0);
226
227 System.err.println(options);
228 System.err.println(VersionUtil.getPlatformInfo());
229 // System.err.println(JoglVersion.getAllAvailableCapabilitiesInfo(dpy.getGraphicsDevice(), null).toString());
230
231 final GLCapabilities reqCaps = options.getGLCaps();
232 System.out.println("Requested: " + reqCaps);
233
234 final GLWindow window = GLWindow.create(reqCaps);
235 if( 0 == options.sceneMSAASamples ) {
237 }
238 window.setSize(options.surface_width, options.surface_height);
239 window.setTitle(PointsDemoES2.class.getSimpleName());
240
241 window.addGLEventListener(new PointsDemoES2(1));
242
243 final Animator animator = new Animator(0 /* w/o AWT */);
244 animator.setUpdateFPSFrames(5*60, null);
245 animator.add(window);
246
247 window.addWindowListener(new WindowAdapter() {
248 @Override
249 public void windowDestroyed(final WindowEvent e) {
250 animator.stop();
251 }
252 });
253
254 window.setVisible(true);
255 animator.start();
256 }
257}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static final float QUARTER_PI
The value PI/4, i.e.
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final void setTitle(final String title)
Definition: GLWindow.java:297
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
CapabilitiesChooser setCapabilitiesChooser(final CapabilitiesChooser chooser)
Set the CapabilitiesChooser to help determine the native visual type.
Definition: GLWindow.java:261
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
static void main(final String[] args)
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setPointParams(final float minSize, final float maxSize, final float distAttenConst, final float distAttenLinear, final float distAttenQuadratic, final float fadeThreshold)
void init(final GLAutoDrawable glad)
Called by the drawable immediately after the OpenGL context is initialized.
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 display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
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 createGLSL(final String name, final int compsPerElement, final int dataType, final boolean normalized, final int initialElementCount, final int vboUsage)
Create a VBO, using a custom GLSL array attribute name and starting with a new created Buffer object ...
PMVMatrix implements a subset of the fixed function pipeline GLMatrixFunc using PMVMatrix4f.
Definition: PMVMatrix.java:62
final void glTranslatef(final float x, final float y, final float z)
Translate the current matrix.
Definition: PMVMatrix.java:379
final void glMatrixMode(final int matrixName)
Sets the current matrix mode.
Definition: PMVMatrix.java:218
final void gluPerspective(final float fovy_rad, final float aspect, final float zNear, final float zFar)
Multiply the current matrix with the perspective/frustum matrix.
Definition: PMVMatrix.java:499
final void glLoadIdentity()
Load the current matrix with the identity matrix.
Definition: PMVMatrix.java:325
Custom GLCapabilitiesChooser, filtering out all full screen anti-aliasing (FSAA, multisample) capabil...
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.
GLUniformData getUniform(final String name)
Get the uniform data, previously set.
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_POINT_SPRITE
GL_VERSION_2_0, GL_ARB_point_sprite, GL_NV_point_sprite, GL_OES_point_sprite Alias for: GL_POINT_SPRI...
Definition: GL2ES1.java:260
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_VERTEX_PROGRAM_POINT_SIZE
GL_VERSION_2_0, GL_ARB_vertex_program, GL_NV_vertex_program Alias for: GL_VERTEX_PROGRAM_POINT_SIZE_A...
Definition: GL2GL3.java:460
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
boolean isGL2GL3()
Indicates whether this GL object conforms to a GL2GL3 compatible profile.
boolean isGL2ES1()
Indicates whether this GL object conforms to a GL2ES1 compatible profile.
boolean hasGLSL()
Indicates whether this GL object supports GLSL.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
boolean isFunctionAvailable(String glFunctionName)
Returns true if the specified OpenGL core- or extension-function can be used successfully through thi...
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_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
void glDisable(int cap)
Entry point to C language function: void {@native glDisable}(GLenum cap) Part of GL_ES_VERSION_2_0...
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,...
static final int GL_SRC_ALPHA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_SRC_ALPHA" with expre...
Definition: GL.java:379
static final int GL_POINTS
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_POINTS" with expressi...
Definition: GL.java:675
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
String glGetString(int name)
Entry point to C language function: const GLubyte * {@native glGetString}(GLenum name) Part of GL_...
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
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_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
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.