JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestRulerNEWT01.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 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.glsl;
29
30import com.jogamp.common.nio.Buffers;
31import com.jogamp.newt.Window;
32import com.jogamp.newt.event.KeyAdapter;
33import com.jogamp.newt.event.KeyEvent;
34import com.jogamp.newt.opengl.GLWindow;
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;
40import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
41import com.jogamp.opengl.test.junit.util.MiscUtils;
42import com.jogamp.opengl.test.junit.util.UITestCase;
43
44import java.io.IOException;
45import java.lang.reflect.InvocationTargetException;
46import java.nio.FloatBuffer;
47
48import com.jogamp.nativewindow.ScalableSurface;
49import com.jogamp.opengl.GL;
50import com.jogamp.opengl.GL2ES2;
51import com.jogamp.opengl.GLAutoDrawable;
52import com.jogamp.opengl.GLCapabilities;
53import com.jogamp.opengl.GLEventListener;
54import com.jogamp.opengl.GLProfile;
55import com.jogamp.opengl.GLUniformData;
56import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
57
58import org.junit.Assert;
59import org.junit.Test;
60import org.junit.FixMethodOrder;
61import org.junit.runners.MethodSorters;
62
63@FixMethodOrder(MethodSorters.NAME_ASCENDING)
64public class TestRulerNEWT01 extends UITestCase {
65 static long durationPerTest = 500; // ms
66 static float[] reqSurfacePixelScale = new float[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
67 static boolean manualTest = false;
68
69 private void setTitle(final Window win) {
70 final float[] sDPI = win.getPixelsPerMM(new float[2]);
71 sDPI[0] *= 25.4f;
72 sDPI[1] *= 25.4f;
73 win.setTitle("GLWindow: win: "+win.getBounds()+", pix: "+win.getSurfaceWidth()+"x"+win.getSurfaceHeight()+", sDPI "+sDPI[0]+" x "+sDPI[1]);
74 }
75
76 private void runTestGL() throws InterruptedException {
78 Assert.assertNotNull(glWindow);
79 glWindow.setSurfaceScale(reqSurfacePixelScale);
80 final float[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new float[2]);
81 glWindow.setSize(640, 480);
82
83 glWindow.addGLEventListener(new GLEventListener() {
84 final ShaderState st = new ShaderState();
85 final PMVMatrix pmvMatrix = new PMVMatrix();
86 final GLUniformData pmvMatrixUniform = new GLUniformData("gcu_PMVMatrix", 4, 4, pmvMatrix.getSyncPMv());
87 final GLArrayDataServer vertices0 = GLArrayDataServer.createGLSL("gca_Vertices", 3, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
88 final GLUniformData rulerPixFreq = new GLUniformData("gcu_RulerPixFreq", 2, Buffers.newDirectFloatBuffer(2));
89
90 @Override
91 public void init(final GLAutoDrawable drawable) {
92 final GL2ES2 gl = drawable.getGL().getGL2ES2();
93
94 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
95
96 final ShaderCode vp0 = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, RedSquareES2.class, "shader",
97 "shader/bin", "default", true);
98 final ShaderCode fp0 = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, RedSquareES2.class, "shader",
99 "shader/bin", "ruler", true);
100 vp0.defaultShaderCustomization(gl, true, true);
101 fp0.defaultShaderCustomization(gl, true, true);
102
103 final ShaderProgram sp0 = new ShaderProgram();
104 sp0.add(gl, vp0, System.err);
105 sp0.add(gl, fp0, System.err);
106 Assert.assertTrue(0 != sp0.program());
107 Assert.assertTrue(!sp0.inUse());
108 Assert.assertTrue(!sp0.linked());
109 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
110
111 st.attachShaderProgram(gl, sp0, true);
112
113 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
114 st.ownUniform(pmvMatrixUniform);
115 st.uniform(gl, pmvMatrixUniform);
116 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
117
118 final GLUniformData rulerColor= new GLUniformData("gcu_RulerColor", 3, Buffers.newDirectFloatBuffer(3));
119 final FloatBuffer rulerColorV = (FloatBuffer) rulerColor.getBuffer();
120 rulerColorV.put(0, 0.5f);
121 rulerColorV.put(1, 0.5f);
122 rulerColorV.put(2, 0.5f);
123 st.ownUniform(rulerColor);
124 st.uniform(gl, rulerColor);
125 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
126
127 st.ownUniform(rulerPixFreq);
128
129 vertices0.putf(0); vertices0.putf(1); vertices0.putf(0);
130 vertices0.putf(1); vertices0.putf(1); vertices0.putf(0);
131 vertices0.putf(0); vertices0.putf(0); vertices0.putf(0);
132 vertices0.putf(1); vertices0.putf(0); vertices0.putf(0);
133 vertices0.seal(gl, true);
134 st.ownAttribute(vertices0, true);
135
136 // misc GL setup
137 gl.glClearColor(1, 1, 1, 1);
139 Assert.assertEquals(GL.GL_NO_ERROR, gl.glGetError());
140 }
141
142 @Override
143 public void dispose(final GLAutoDrawable drawable) {
144 }
145
146 @Override
147 public void display(final GLAutoDrawable drawable) {
148 final GL2ES2 gl = drawable.getGL().getGL2ES2();
149 vertices0.enableBuffer(gl, true);
152 vertices0.enableBuffer(gl, false);
153 }
154
155 @Override
156 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
157 final GL2ES2 gl = drawable.getGL().getGL2ES2();
159 pmvMatrix.glLoadIdentity();
160 pmvMatrix.glOrthof(0f, 1f, 0f, 1f, -10f, 10f);
161 // pmvMatrix.gluPerspective(45.0F, (float) drawable.getWidth() / (float) drawable.getHeight(), 1.0F, 100.0F);
163 pmvMatrix.glLoadIdentity();
164 // pmvMatrix.glTranslatef(0, 0, -6);
165 // pmvMatrix.glRotatef(45f, 1f, 0f, 0f);
166 st.uniform(gl, pmvMatrixUniform);
167
168 final float[] ppmmStore = glWindow.getPixelsPerMM(new float[2]);
169 final FloatBuffer rulerPixFreqV = (FloatBuffer) rulerPixFreq.getBuffer();
170 rulerPixFreqV.put(0, ppmmStore[0] * 10.0f);
171 rulerPixFreqV.put(1, ppmmStore[1] * 10.0f);
172 st.uniform(gl, rulerPixFreq);
173 System.err.println("Screen pixel/cm "+rulerPixFreqV.get(0)+", "+rulerPixFreqV.get(1));
174 }
175
176 });
178 glWindow.addGLEventListener(snap);
179 glWindow.addKeyListener(new KeyAdapter() {
180 @Override
181 public void keyPressed(final KeyEvent e) {
182 if( e.isAutoRepeat() ) {
183 return;
184 }
185 if(e.getKeyChar()=='x') {
186 final float[] hadSurfacePixelScale = glWindow.getCurrentSurfaceScale(new float[2]);
187 final float[] reqSurfacePixelScale;
188 if( hadSurfacePixelScale[0] == ScalableSurface.IDENTITY_PIXELSCALE ) {
189 reqSurfacePixelScale = new float[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
190 } else {
191 reqSurfacePixelScale = new float[] { ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE };
192 }
193 System.err.println("[set PixelScale pre]: had "+hadSurfacePixelScale[0]+"x"+hadSurfacePixelScale[1]+" -> req "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]);
194 snap.setMakeSnapshot();
195 glWindow.setSurfaceScale(reqSurfacePixelScale);
196 final float[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new float[2]);
197 final float[] hasSurfacePixelScale = glWindow.getCurrentSurfaceScale(new float[2]);
198 final float[] nativeSurfacePixelScale = glWindow.getMaximumSurfaceScale(new float[2]);
199 System.err.println("[set PixelScale post]: "+hadSurfacePixelScale[0]+"x"+hadSurfacePixelScale[1]+" (had) -> "+
200 reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
201 valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
202 hasSurfacePixelScale[0]+"x"+hasSurfacePixelScale[1]+" (has), "+
203 nativeSurfacePixelScale[0]+"x"+nativeSurfacePixelScale[1]+" (native)");
204 setTitle(glWindow);
205 }
206 }
207 });
208
209 glWindow.setVisible(true);
210
211 final float[] hasSurfacePixelScale1 = glWindow.getCurrentSurfaceScale(new float[2]);
212 System.err.println("HiDPI PixelScale: "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
213 valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
214 hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
215 setTitle(glWindow);
216
217 snap.setMakeSnapshot();
218 glWindow.display();
219
220 Thread.sleep(durationPerTest);
221
222 glWindow.destroy();
223
224 }
225
226 @Test
227 public void test01_PSA() throws InterruptedException {
228 runTestGL();
229 }
230
231 @Test
232 public void test99_PS1() throws InterruptedException, InvocationTargetException {
233 if(manualTest) return;
234 reqSurfacePixelScale[0] = ScalableSurface.IDENTITY_PIXELSCALE;
235 reqSurfacePixelScale[1] = ScalableSurface.IDENTITY_PIXELSCALE;
236 runTestGL();
237 }
238
239 public static void main(final String args[]) throws IOException {
240 System.err.println("main - start");
241 for(int i=0; i<args.length; i++) {
242 if(args[i].equals("-time")) {
243 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest);
244 } else if(args[i].equals("-pixelScale")) {
245 i++;
246 final float pS = MiscUtils.atof(args[i], reqSurfacePixelScale[0]);
247 reqSurfacePixelScale[0] = pS;
248 reqSurfacePixelScale[1] = pS;
249 } else if(args[i].equals("-manual")) {
250 manualTest = true;
251 }
252 }
253 final String tstname = TestRulerNEWT01.class.getName();
254 org.junit.runner.JUnitCore.main(tstname);
255 System.err.println("main - end");
256 }
257}
258
final SyncMatrices4f getSyncPMv()
Returns SyncMatrices4f of 2 matrices within one FloatBuffer: P and Mv.
final boolean isAutoRepeat()
getModifiers() contains AUTOREPEAT_MASK.
final char getKeyChar()
Returns the UTF-16 character reflecting the key symbol incl.
Definition: KeyEvent.java:161
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final float[] getMaximumSurfaceScale(final float[] result)
Returns the maximum pixel scale of the associated NativeSurface.
Definition: GLWindow.java:515
final float[] getPixelsPerMM(final float[] ppmmStore)
Returns the pixels per millimeter of this window's NativeSurface according to the main monitor's curr...
Definition: GLWindow.java:520
final float[] getRequestedSurfaceScale(final float[] result)
Returns the requested pixel scale of the associated NativeSurface.
Definition: GLWindow.java:500
final void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
final float[] getCurrentSurfaceScale(final float[] result)
Returns the current pixel scale of the associated NativeSurface.
Definition: GLWindow.java:505
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 boolean setSurfaceScale(final float[] pixelScale)
Request a pixel scale in x- and y-direction for the associated NativeSurface, where size_in_pixel_uni...
Definition: GLWindow.java:495
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
Buffer getBuffer()
Returns the data buffer.
static float atof(final String str, final float def)
Definition: MiscUtils.java:75
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57
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 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,...
int program()
Returns the shader program name, which is non zero if valid.
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 boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)
Attach or switch a shader program.
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.
int getSurfaceWidth()
Returns the width of the client area excluding insets (window decorations) in pixel units.
int getSurfaceHeight()
Returns the height of the client area excluding insets (window decorations) in pixel units.
Rectangle getBounds()
Returns a newly created Rectangle containing window origin, getX() & getY(), and size,...
Adding mutable surface pixel scale property to implementing class, usually to a NativeSurface impleme...
static final float IDENTITY_PIXELSCALE
Setting surface-pixel-scale of {@value}, results in same pixel- and window-units.
static final float AUTOMAX_PIXELSCALE
Setting surface-pixel-scale of {@value}, results in maximum platform dependent pixel-scale,...
Specifying NEWT's Window functionality:
Definition: Window.java:115
void setTitle(String title)
float[] getPixelsPerMM(final float[] ppmmStore)
Returns the pixels per millimeter of this window's NativeSurface according to the main monitor's curr...
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.
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
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_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_NO_ERROR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NO_ERROR" with expres...
Definition: GL.java:481
int glGetError()
Entry point to C language function: GLenum {@native glGetError}() 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,...
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
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
void glClear(int mask)
Entry point to C language function: void {@native glClear}(GLbitfield mask) Part of GL_ES_VERSION_...
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_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.