JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
LandscapeES2.java
Go to the documentation of this file.
1/**
2 * Copyright 2013 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 com.jogamp.opengl.util.GLArrayDataServer;
31import com.jogamp.opengl.util.glsl.ShaderCode;
32import com.jogamp.opengl.util.glsl.ShaderProgram;
33import com.jogamp.opengl.util.glsl.ShaderState;
34import java.nio.FloatBuffer;
35
36import com.jogamp.opengl.GL;
37import com.jogamp.opengl.GL2ES2;
38import com.jogamp.opengl.GLAutoDrawable;
39import com.jogamp.opengl.GLEventListener;
40import com.jogamp.opengl.GLUniformData;
41
42/**
43 * LandscapeES2
44 */
45public class LandscapeES2 implements GLEventListener {
46 private int swapInterval = 0;
47 private boolean verbose = true;
48
49 static public final int TARGET_FPS = 120;
50 private long millisOffset;
51 private int frameCount;
52 private float frameRate;
53 private ShaderCode vertShader;
54 private ShaderCode fragShader;
55 private ShaderProgram shaderProg;
56 private ShaderState shaderState;
57 private float[] resolution;
58 private GLUniformData resolutionUni;
59 private GLUniformData timeUni;
60 private GLArrayDataServer vertices;
61
62 private int fcount = 0, lastm = 0;
63 private final int fint = 1;
64
65 public LandscapeES2(final int swapInterval) {
66 this.swapInterval = swapInterval;
67 }
68
69 public LandscapeES2() {
70 this.swapInterval = 1;
71 }
72
73 public void setVerbose(final boolean v) { verbose = v; }
74
75 @Override
76 public void init(final GLAutoDrawable drawable) {
77 System.err.println(Thread.currentThread()+" LandscapeES2.init ...");
78 final GL2ES2 gl = drawable.getGL().getGL2ES2();
79
80 if(verbose) {
81 System.err.println("LandscapeES2 init on "+Thread.currentThread());
82 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
83 System.err.println("INIT GL IS: " + gl.getClass().getName());
84 System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
85 System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
86 System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
87 System.err.println("GL GLSL: "+gl.hasGLSL()+", has-compiler-func: "+gl.isFunctionAvailable("glCompileShader")+", version "+(gl.hasGLSL() ? gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION) : "none")+", "+gl.getContext().getGLSLVersionNumber());
88 System.err.println("GL FBO: basic "+ gl.hasBasicFBOSupport()+", full "+gl.hasFullFBOSupport());
89 System.err.println("GL Profile: "+gl.getGLProfile());
90 System.err.println("GL Renderer Quirks:" + gl.getContext().getRendererQuirks().toString());
91 System.err.println("GL:" + gl + ", " + gl.getContext().getGLVersion());
92 }
93
94 vertShader = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", "landscape", true);
95 fragShader = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "landscape", true);
96 vertShader.defaultShaderCustomization(gl, true, true);
97 fragShader.defaultShaderCustomization(gl, true, true);
98 shaderProg = new ShaderProgram();
99 shaderProg.add(gl, vertShader, System.err);
100 shaderProg.add(gl, fragShader, System.err);
101
102 shaderState = new ShaderState();
103 shaderState.attachShaderProgram(gl, shaderProg, true);
104
105 resolution = new float[] { drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), 0};
106 resolutionUni = new GLUniformData("iResolution", 3, FloatBuffer.wrap(resolution));
107 shaderState.ownUniform(resolutionUni);
108 shaderState.uniform(gl, resolutionUni);
109
110 timeUni = new GLUniformData("iGlobalTime", 0.0f);
111 shaderState.ownUniform(timeUni);
112
113 vertices = GLArrayDataServer.createGLSL("inVertex", 2, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
114 vertices.putf(-1.0f); vertices.putf(-1.0f);
115 vertices.putf(+1.0f); vertices.putf(-1.0f);
116 vertices.putf(-1.0f); vertices.putf(+1.0f);
117 vertices.putf(+1.0f); vertices.putf(+1.0f);
118 vertices.seal(gl, true);
119 shaderState.ownAttribute(vertices, true);
120 shaderState.useProgram(gl, false);
121
122 millisOffset = System.currentTimeMillis();
123
124 System.err.println(Thread.currentThread()+" LandscapeES2.init FIN");
125 }
126
127 @Override
128 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
129 System.err.println(Thread.currentThread()+" LandscapeES2.reshape "+x+"/"+y+" "+width+"x"+height+", swapInterval "+swapInterval+", drawable 0x"+Long.toHexString(drawable.getHandle()));
130
131 final GL2ES2 gl = drawable.getGL().getGL2ES2();
132
133 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
134
135 shaderState.useProgram(gl, true);
136
137 resolution[0] = drawable.getSurfaceWidth();
138 resolution[1] = drawable.getSurfaceHeight();
139 shaderState.uniform(gl, resolutionUni);
140
141 shaderState.useProgram(gl, false);
142 }
143
144 @Override
145 public void dispose(final GLAutoDrawable drawable) {
146 System.err.println(Thread.currentThread()+" LandscapeES2.dispose ... ");
147 final GL2ES2 gl = drawable.getGL().getGL2ES2();
148 shaderState.useProgram(gl, false);
149 shaderState.destroy(gl);
150 shaderState = null;
151
152 System.err.println(Thread.currentThread()+" LandscapeES2.dispose FIN");
153 }
154
155 @Override
156 public void display(final GLAutoDrawable drawable) {
157 final GL2ES2 gl = drawable.getGL().getGL2ES2();
158 // Shader fills complete framebuffer regardless of DEPTH, no Clear required.
159 // gl.glClearColor(0.5f, 0.1f, 0.1f, 1);
160 // gl.glClear(GL.GL_COLOR_BUFFER_BIT);
161
162 shaderState.useProgram(gl, true);
163
164 timeUni.setData((System.currentTimeMillis() - millisOffset) / 1000.0f);
165 shaderState.uniform(gl, timeUni);
166 vertices.enableBuffer(gl, true);
168 vertices.enableBuffer(gl, false);
169
170 shaderState.useProgram(gl, false);
171
172 // Compute current framerate and printout.
173 frameCount++;
174 fcount += 1;
175 final int m = (int) (System.currentTimeMillis() - millisOffset);
176 if (m - lastm > 1000 * fint) {
177 frameRate = (float)(fcount) / fint;
178 fcount = 0;
179 lastm = m;
180 }
181 if (frameCount % TARGET_FPS == 0) {
182 System.out.println("FrameCount: " + frameCount + " - " + "FrameRate: " + frameRate);
183 }
184 }
185
186 boolean confinedFixedCenter = false;
187
188 public void setConfinedFixedCenter(final boolean v) {
189 confinedFixedCenter = v;
190 }
191}
final GLRendererQuirks getRendererQuirks()
Returns the instance of GLRendererQuirks, allowing one to determine workarounds.
Definition: GLContext.java:290
final String getGLVersion()
Returns a valid OpenGL version string, ie
Definition: GLContext.java:769
final StringBuilder toString(StringBuilder sb)
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
GLUniformData setData(final int data)
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.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
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 ...
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_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
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
boolean hasGLSL()
Indicates whether this GL object supports GLSL.
boolean hasBasicFBOSupport()
Returns true if basic FBO support is available, otherwise false.
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.
GLContext getContext()
Returns the GLContext associated which this GL object.
boolean isFunctionAvailable(String glFunctionName)
Returns true if the specified OpenGL core- or extension-function can be used successfully through thi...
boolean hasFullFBOSupport()
Returns true if full FBO support is available, otherwise false.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
long getHandle()
Returns the GL drawable handle, guaranteed to be valid after realization and while it's surface is be...
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.
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
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
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_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