JOGL v2.6.0-rc-20250712
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.demos.es2;
29
30import com.jogamp.opengl.util.Animator;
31import com.jogamp.opengl.util.GLArrayDataServer;
32import com.jogamp.opengl.util.caps.NonFSAAGLCapsChooser;
33import com.jogamp.opengl.util.glsl.ShaderCode;
34import com.jogamp.opengl.util.glsl.ShaderProgram;
35import com.jogamp.opengl.util.glsl.ShaderState;
36import java.nio.FloatBuffer;
37
38import com.jogamp.common.util.VersionUtil;
39import com.jogamp.newt.event.WindowAdapter;
40import com.jogamp.newt.event.WindowEvent;
41import com.jogamp.newt.opengl.GLWindow;
42import com.jogamp.opengl.GL;
43import com.jogamp.opengl.GL2ES2;
44import com.jogamp.opengl.GLAutoDrawable;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.GLEventListener;
47import com.jogamp.opengl.GLUniformData;
48import com.jogamp.opengl.demos.util.CommandlineOptions;
49
50/**
51 * LandscapeES2
52 */
53public class LandscapeES2 implements GLEventListener {
54 private int swapInterval = 0;
55 private boolean verbose = true;
56
57 static public final int TARGET_FPS = 120;
58 private long millisOffset;
59 private int frameCount;
60 private float frameRate;
61 private ShaderCode vertShader;
62 private ShaderCode fragShader;
63 private ShaderProgram shaderProg;
64 private ShaderState shaderState;
65 private float[] resolution;
66 private GLUniformData resolutionUni;
67 private GLUniformData timeUni;
68 private GLArrayDataServer vertices;
69
70 private int fcount = 0, lastm = 0;
71 private final int fint = 1;
72
73 public LandscapeES2(final int swapInterval) {
74 this.swapInterval = swapInterval;
75 }
76
77 public LandscapeES2() {
78 this.swapInterval = 1;
79 }
80
81 public void setVerbose(final boolean v) { verbose = v; }
82
83 @Override
84 public void init(final GLAutoDrawable drawable) {
85 System.err.println(Thread.currentThread()+" LandscapeES2.init ...");
86 final GL2ES2 gl = drawable.getGL().getGL2ES2();
87
88 if(verbose) {
89 System.err.println("LandscapeES2 init on "+Thread.currentThread());
90 System.err.println("Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
91 System.err.println("INIT GL IS: " + gl.getClass().getName());
92 System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
93 System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
94 System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
95 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());
96 System.err.println("GL FBO: basic "+ gl.hasBasicFBOSupport()+", full "+gl.hasFullFBOSupport());
97 System.err.println("GL Profile: "+gl.getGLProfile());
98 System.err.println("GL Renderer Quirks:" + gl.getContext().getRendererQuirks().toString());
99 System.err.println("GL:" + gl + ", " + gl.getContext().getGLVersion());
100 }
101
102 vertShader = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shader", "shader/bin", "landscape", true);
103 fragShader = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shader", "shader/bin", "landscape", true);
104 vertShader.defaultShaderCustomization(gl, true, true);
105 fragShader.defaultShaderCustomization(gl, true, true);
106 shaderProg = new ShaderProgram();
107 shaderProg.add(gl, vertShader, System.err);
108 shaderProg.add(gl, fragShader, System.err);
109
110 shaderState = new ShaderState();
111 shaderState.attachShaderProgram(gl, shaderProg, true);
112
113 resolution = new float[] { drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), 0};
114 resolutionUni = new GLUniformData("iResolution", 3, FloatBuffer.wrap(resolution));
115 shaderState.ownUniform(resolutionUni);
116 shaderState.uniform(gl, resolutionUni);
117
118 timeUni = new GLUniformData("iGlobalTime", 0.0f);
119 shaderState.ownUniform(timeUni);
120
121 vertices = GLArrayDataServer.createGLSL("inVertex", 2, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
122 vertices.putf(-1.0f); vertices.putf(-1.0f);
123 vertices.putf(+1.0f); vertices.putf(-1.0f);
124 vertices.putf(-1.0f); vertices.putf(+1.0f);
125 vertices.putf(+1.0f); vertices.putf(+1.0f);
126 vertices.seal(gl, true);
127 shaderState.ownAttribute(vertices, true);
128 shaderState.useProgram(gl, false);
129
130 millisOffset = System.currentTimeMillis();
131
132 System.err.println(Thread.currentThread()+" LandscapeES2.init FIN");
133 }
134
135 @Override
136 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
137 System.err.println(Thread.currentThread()+" LandscapeES2.reshape "+x+"/"+y+" "+width+"x"+height+", swapInterval "+swapInterval+", drawable 0x"+Long.toHexString(drawable.getHandle()));
138
139 final GL2ES2 gl = drawable.getGL().getGL2ES2();
140
141 gl.setSwapInterval(swapInterval); // in case switching the drawable (impl. may bound attribute there)
142
143 shaderState.useProgram(gl, true);
144
145 resolution[0] = drawable.getSurfaceWidth();
146 resolution[1] = drawable.getSurfaceHeight();
147 shaderState.uniform(gl, resolutionUni);
148
149 shaderState.useProgram(gl, false);
150 }
151
152 @Override
153 public void dispose(final GLAutoDrawable drawable) {
154 System.err.println(Thread.currentThread()+" LandscapeES2.dispose ... ");
155 final GL2ES2 gl = drawable.getGL().getGL2ES2();
156 shaderState.useProgram(gl, false);
157 shaderState.destroy(gl);
158 shaderState = null;
159
160 System.err.println(Thread.currentThread()+" LandscapeES2.dispose FIN");
161 }
162
163 @Override
164 public void display(final GLAutoDrawable drawable) {
165 final GL2ES2 gl = drawable.getGL().getGL2ES2();
166 // Shader fills complete framebuffer regardless of DEPTH, no Clear required.
167 // gl.glClearColor(0.5f, 0.1f, 0.1f, 1);
168 // gl.glClear(GL.GL_COLOR_BUFFER_BIT);
169
170 shaderState.useProgram(gl, true);
171
172 timeUni.setData((System.currentTimeMillis() - millisOffset) / 1000.0f);
173 shaderState.uniform(gl, timeUni);
174 vertices.enableBuffer(gl, true);
176 vertices.enableBuffer(gl, false);
177
178 shaderState.useProgram(gl, false);
179
180 // Compute current framerate and printout.
181 frameCount++;
182 fcount += 1;
183 final int m = (int) (System.currentTimeMillis() - millisOffset);
184 if (m - lastm > 1000 * fint) {
185 frameRate = (float)(fcount) / fint;
186 fcount = 0;
187 lastm = m;
188 }
189 if (frameCount % TARGET_FPS == 0) {
190 System.out.println("FrameCount: " + frameCount + " - " + "FrameRate: " + frameRate);
191 }
192 }
193
194 boolean confinedFixedCenter = false;
195
196 public void setConfinedFixedCenter(final boolean v) {
197 confinedFixedCenter = v;
198 }
199
200 public static void main(final String[] args) {
201 final CommandlineOptions options = new CommandlineOptions(1280, 720, 0);
202
203 System.err.println(options);
204 System.err.println(VersionUtil.getPlatformInfo());
205 // System.err.println(JoglVersion.getAllAvailableCapabilitiesInfo(dpy.getGraphicsDevice(), null).toString());
206
207 final GLCapabilities reqCaps = options.getGLCaps();
208 System.out.println("Requested: " + reqCaps);
209
210 final GLWindow window = GLWindow.create(reqCaps);
211 if( 0 == options.sceneMSAASamples ) {
213 }
214 window.setSize(options.surface_width, options.surface_height);
215 window.setTitle(LandscapeES2.class.getSimpleName());
216
217 window.addGLEventListener(new LandscapeES2(1));
218
219 final Animator animator = new Animator(0 /* w/o AWT */);
220 animator.setUpdateFPSFrames(5*60, null);
221 animator.add(window);
222
223 window.addWindowListener(new WindowAdapter() {
224 @Override
225 public void windowDestroyed(final WindowEvent e) {
226 animator.stop();
227 }
228 });
229
230 window.setVisible(true);
231 animator.start();
232 }
233}
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.
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 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 dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setConfinedFixedCenter(final boolean v)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
static void main(final String[] args)
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 ...
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.
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.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
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