JOGL v2.6.0-rc-20250712
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.test.junit.jogl.demos.es2;
29
30import java.nio.FloatBuffer;
31
32import com.jogamp.common.nio.Buffers;
33import com.jogamp.math.FloatUtil;
34import com.jogamp.opengl.test.junit.jogl.demos.PointsDemo;
35import com.jogamp.opengl.util.GLArrayDataServer;
36import com.jogamp.opengl.util.PMVMatrix;
37import com.jogamp.opengl.util.glsl.ShaderCode;
38import com.jogamp.opengl.util.glsl.ShaderProgram;
39import com.jogamp.opengl.util.glsl.ShaderState;
40
41import com.jogamp.opengl.GL;
42import com.jogamp.opengl.GL2ES1;
43import com.jogamp.opengl.GL2ES2;
44import com.jogamp.opengl.GL2GL3;
45import com.jogamp.opengl.GLAutoDrawable;
46import com.jogamp.opengl.GLUniformData;
47import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
48
49public class PointsDemoES2 extends PointsDemo {
50 ShaderState st;
51 PMVMatrix pmvMatrix;
52 GLUniformData pmvMatrixUniform;
53 GLArrayDataServer vertices ;
54 GLArrayDataServer pointSizes ;
55 private int swapInterval = 0;
56 final int edge = 8; // 8*8
57 /** vec4[2]: { (sz, smooth, attnMinSz, attnMaxSz), (attnCoeff(3), attnFadeTs) } */
58 private static final String mgl_PointParams = "mgl_PointParams";
59
60 /** ( pointSize, pointSmooth, attn. pointMinSize, attn. pointMaxSize ) , ( attenuation coefficients 1f 0f 0f, attenuation fade theshold 1f ) */
61 private final FloatBuffer pointParams = Buffers.newDirectFloatBuffer(new float[] { 1.0f, 0.0f, 0.0f, 4096.0f, 1.0f, 0.0f, 0.0f, 1.0f });
62
63 public PointsDemoES2(final int swapInterval) {
64 this.swapInterval = swapInterval;
65 }
66
67 public PointsDemoES2() {
68 this.swapInterval = 1;
69 }
70
71 @Override
72 public void setSmoothPoints(final boolean v) {
73 pointParams.put(1, v ? 1.0f : 0.0f);
74 }
75
76 @Override
77 public void setPointParams(final float minSize, final float maxSize, final float distAttenConst, final float distAttenLinear, final float distAttenQuadratic, final float fadeThreshold) {
78 pointParams.put(2, minSize);
79 pointParams.put(3, maxSize);
80 pointParams.put(4+0, distAttenConst);
81 pointParams.put(4+1, distAttenLinear);
82 pointParams.put(4+2, distAttenQuadratic);
83 pointParams.put(4+3, fadeThreshold);
84 }
85
86 @Override
87 public void init(final GLAutoDrawable glad) {
88 final GL2ES2 gl = glad.getGL().getGL2ES2();
89
90 System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
91 System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
92 System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
93 System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler-func: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none"));
94 System.err.println("GL Profile: "+gl.getGLProfile());
95
96 st = new ShaderState();
97 st.setVerbose(true);
98 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader",
99 "shader/bin", "PointsShader", true);
100 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader",
101 "shader/bin", "PointsShader", true);
102 vp0.defaultShaderCustomization(gl, true, true);
103 fp0.defaultShaderCustomization(gl, true, true);
104 final ShaderProgram sp0 = new ShaderProgram();
105 sp0.add(gl, vp0, System.err);
106 sp0.add(gl, fp0, System.err);
107 st.attachShaderProgram(gl, sp0, true);
108
109 // setup mgl_PMVMatrix
110 pmvMatrix = new PMVMatrix();
112 pmvMatrix.glLoadIdentity();
114 pmvMatrix.glLoadIdentity();
115 pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv()); // P, Mv
116 st.ownUniform(pmvMatrixUniform);
117 st.uniform(gl, pmvMatrixUniform);
118
119 st.uniform(gl, new GLUniformData(mgl_PointParams, 4, pointParams));
120
121 final GLUniformData colorStaticUniform = new GLUniformData("mgl_ColorStatic", 4, Buffers.newDirectFloatBuffer(new float[] { 1.0f, 1.0f, 1.0f, 1.0f }) );
122 st.uniform(gl, colorStaticUniform);
123 st.ownUniform(colorStaticUniform);
124
125 // Allocate Vertex Array
126 vertices = GLArrayDataServer.createGLSL("mgl_Vertex", 3, GL.GL_FLOAT, false, edge*edge, GL.GL_STATIC_DRAW);
127 pointSizes = GLArrayDataServer.createGLSL("mgl_PointSize", 1, GL.GL_FLOAT, false, edge*edge, GL.GL_STATIC_DRAW);
128 for(int i=0; i<edge; i++) {
129 for(int j=0; j<edge; j++) {
130 final float x = -3+j*0.7f;
131 final float y = -3+i*0.7f;
132 final float p = (i*edge+j)*0.5f;
133 // System.err.println("["+j+"/"+i+"]: "+x+"/"+y+": "+p);
134 vertices.putf(x); vertices.putf(y); vertices.putf( 0);
135 pointSizes.putf(p);
136 }
137 }
138 vertices.seal(gl, true);
139 st.ownAttribute(vertices, true);
140 vertices.enableBuffer(gl, false);
141 pointSizes.seal(gl, true);
142 st.ownAttribute(pointSizes, true);
143 pointSizes.enableBuffer(gl, false);
144
145 // OpenGL Render Settings
147 st.useProgram(gl, false);
148 }
149
150 @Override
151 public void display(final GLAutoDrawable glad) {
152 final GL2ES2 gl = glad.getGL().getGL2ES2();
153 gl.glClearColor(0f, 0f, 0f, 0f);
155 st.useProgram(gl, true);
157 pmvMatrix.glLoadIdentity();
158 pmvMatrix.glTranslatef(0, 0, -10);
159 st.uniform(gl, pmvMatrixUniform);
160
161 final GLUniformData ud = st.getUniform(mgl_PointParams);
162 if(null!=ud) {
163 // same data object
164 st.uniform(gl, ud);
165 }
166
167 vertices.enableBuffer(gl, true);
168 pointSizes.enableBuffer(gl, true);
169
170 if(gl.isGL2GL3()) {
172 }
173 if(gl.isGL2ES1()) {
174 gl.glEnable(GL2ES1.GL_POINT_SPRITE); // otherwise no gl_PointCoord
175 }
176 gl.glEnable ( GL.GL_BLEND );
177 gl.glBlendFunc ( GL.GL_SRC_ALPHA, GL.GL_ONE );
178
179 gl.glDrawArrays(GL.GL_POINTS, 0, edge*edge);
180
181 if(gl.isGL2GL3()) {
183 }
184
185 pointSizes.enableBuffer(gl, false);
186 vertices.enableBuffer(gl, false);
187 st.useProgram(gl, false);
188 }
189
190 @Override
191 public void reshape(final GLAutoDrawable glad, final int x, final int y, final int width, final int height) {
192 // Thread.dumpStack();
193 final GL2ES2 gl = glad.getGL().getGL2ES2();
194
195 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
196
197 st.useProgram(gl, true);
198 // Set location in front of camera
200 pmvMatrix.glLoadIdentity();
201 pmvMatrix.gluPerspective(FloatUtil.QUARTER_PI, ( (float) width / (float) height ) / 1.0f, 1.0F, 100.0F);
202 //pmvMatrix.glOrthof(-4.0f, 4.0f, -4.0f, 4.0f, 1.0f, 100.0f);
203 st.uniform(gl, pmvMatrixUniform);
204 st.useProgram(gl, false);
205 }
206
207 @Override
208 public void dispose(final GLAutoDrawable glad) {
209 final GL2ES2 gl = glad.getGL().getGL2ES2();
210 st.destroy(gl);
211 st = null;
212 pmvMatrix = null;
213 }
214}
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.
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
void dispose(final GLAutoDrawable glad)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(final GLAutoDrawable glad)
Called by the drawable to initiate OpenGL rendering by the client.
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 setPointParams(final float minSize, final float maxSize, final float distAttenConst, final float distAttenLinear, final float distAttenQuadratic, final float fadeThreshold)
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
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.
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.