JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Hello.java
Go to the documentation of this file.
1/**
2 * Copyright 2019 Gothel Software e.K. 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 Gothel Software e.K. ``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 Gothel Software e.K. 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 Gothel Software e.K.
27 */
28package com.jogamp.opengl.demos.ios;
29
30import com.jogamp.common.util.ReflectionUtil;
31import com.jogamp.common.util.VersionUtil;
32import com.jogamp.nativewindow.ScalableSurface;
33import com.jogamp.newt.Display;
34import com.jogamp.newt.NewtFactory;
35import com.jogamp.newt.Screen;
36import com.jogamp.newt.opengl.GLWindow;
37import com.jogamp.common.GlueGenVersion;
38import com.jogamp.opengl.JoglVersion;
39import com.jogamp.opengl.demos.es2.RedSquareES2;
40import com.jogamp.opengl.util.Animator;
41
42import jogamp.nativewindow.ios.IOSUtil;
43
44import com.jogamp.opengl.GLAutoDrawable;
45import com.jogamp.opengl.GLCapabilities;
46import com.jogamp.opengl.GLEventListener;
47import com.jogamp.opengl.GLProfile;
48
49public class Hello {
50
51 private static int parseInt(final String s, final int def) {
52 try {
53 return Integer.parseInt(s);
54 } catch (final NumberFormatException nfe) {}
55 return def;
56 }
57 private static float parseFloat(final String s, final float def) {
58 try {
59 return Float.parseFloat(s);
60 } catch (final NumberFormatException nfe) {}
61 return def;
62 }
63
64 public static void main(final String[] args) {
65 final float[] reqSurfacePixelScale = new float[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
66
67 int secondsDuration = 10; // 10s
68 int width = 832, height = 480; // ipad pro 11: 2388x1668 px (scale: 2)
69 int fboDepthBits = -1; // CAEAGLLayer fails with depth 16 + 24 in Simulation; -1 means don't change
70 boolean translucent = false;
71 boolean exitJVM = false;
72 String demoName = "com.jogamp.opengl.demos.es2.GearsES2";
73 for(int i=0; i<args.length; i++) {
74 if(args[i].equals("-exit")) {
75 exitJVM = true;
76 } else if(args[i].equals("-demo") && i+1<args.length) {
77 demoName = args[++i];
78 } else if(args[i].equals("-width") && i+1<args.length) {
79 width = parseInt(args[++i], width);
80 } else if(args[i].equals("-height") && i+1<args.length) {
81 height = parseInt(args[++i], height);
82 } else if(args[i].equals("-fboDepthBits") && i+1<args.length) {
83 fboDepthBits = parseInt(args[++i], fboDepthBits);
84 } else if(args[i].equals("-pixelScale") && i+1<args.length) {
85 reqSurfacePixelScale[0] = parseFloat(args[++i], reqSurfacePixelScale[0]);
86 reqSurfacePixelScale[1] = reqSurfacePixelScale[0];
87 } else if(args[i].equals("-seconds") && i+1<args.length) {
88 secondsDuration = parseInt(args[++i], secondsDuration);
89 } else if(args[i].equals("-translucent")) {
90 translucent = true;
91 } else {
92 System.err.println("ignoring arg["+i+"]: "+args[i]);
93 }
94 }
95 System.out.println("Hello JogAmp World: exitJVM "+exitJVM+", size "+width+"x"+height+", fboDepthBits "+fboDepthBits+", demo "+demoName);
96 System.out.println("os.name: <"+System.getProperty("os.name")+">");
97 System.out.println("os.version: <"+System.getProperty("os.version")+">");
98 System.out.println("os.arch: <"+System.getProperty("os.arch")+">");
99 System.out.println("java.vendor: <"+System.getProperty("java.vendor")+">");
100 System.out.println("java.vendor.url: <"+System.getProperty("java.vendor.url")+">");
101 System.out.println("java.version: <"+System.getProperty("java.version")+">");
102 System.out.println("java.vm.name: <"+System.getProperty("java.vm.name")+">");
103 System.out.println("java.runtime.name: <"+System.getProperty("java.runtime.name")+">");
104 System.out.println("");
105 System.out.println(VersionUtil.getPlatformInfo());
106 System.out.println("");
107 System.out.println("Version Info:");
108 System.out.println(GlueGenVersion.getInstance());
109 System.out.println("");
110 System.out.println("Full Manifest:");
111 System.out.println(GlueGenVersion.getInstance().getFullManifestInfo(null));
112
113 System.out.println("");
114 System.err.println("mark-01");
115 System.err.println("");
116 System.err.println(JoglVersion.getInstance());
117 System.err.println("");
118 System.err.println("mark-02");
119 System.err.println("");
121 System.err.println("");
122 System.err.println("mark-03");
123 System.out.println("");
124 System.out.println(JoglVersion.getDefaultOpenGLInfo(GLProfile.getDefaultDevice(), null, true));
125 System.out.println("");
126 System.err.println("mark-04");
127 System.err.println("");
128
129 GLWindow glWindow = null;
130 try {
131 // 1) Config ..
132 final GLProfile glp = GLProfile.getGL2ES2();
133 final GLCapabilities reqCaps = new GLCapabilities(glp);
134 if( 0 <= fboDepthBits) {
135 reqCaps.setDepthBits(fboDepthBits);
136 }
137 reqCaps.setBackgroundOpaque(!translucent);
138 System.out.println("Requested GL Caps: "+reqCaps);
139
140 // 2) Create newt native window
141 final Display dpy = NewtFactory.createDisplay(null);
142 final Screen screen = NewtFactory.createScreen(dpy, 0);
143 glWindow = GLWindow.create(screen, reqCaps);
144 glWindow.setSurfaceScale(reqSurfacePixelScale);
145 final float[] valReqSurfacePixelScale = glWindow.getRequestedSurfaceScale(new float[2]);
146 glWindow.setSize(width, height);
147 glWindow.setPosition(0, 0);
148 final GLEventListener tracker = new GLEventListener() {
149 void printInfo(final String prefix, final GLAutoDrawable d, final String postfix) {
150 System.out.print(prefix+": drawable "+d.getSurfaceWidth()+"x"+d.getSurfaceHeight());
151 if(null != postfix) {
152 System.out.println(" - "+postfix);
153 } else {
154 System.out.println();
155 }
156 }
157 @Override
158 public void init(final GLAutoDrawable drawable) {
159 printInfo("GLEvent::Init", drawable, null);
160 }
161
162 @Override
163 public void dispose(final GLAutoDrawable drawable) {
164 printInfo("GLEvent::Dispose", drawable, null);
165 }
166
167 @Override
168 public void display(final GLAutoDrawable drawable) {
169 }
170
171 @Override
172 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
173 printInfo("GLEvent::Reshape", drawable, "reshape["+x+"/"+y+" "+width+"x"+height+"]");
174 }
175 };
176 glWindow.addGLEventListener(tracker);
177 GLEventListener demo = null;
178 {
179 try {
180 demo = (GLEventListener) ReflectionUtil.createInstance(demoName, Hello.class.getClassLoader());
181 } catch( final Exception e ) {
182 System.err.println(e.getMessage()+" using: <"+demoName+">");
183 }
184 if( null == demo ) {
185 demo = new RedSquareES2();
186 }
187 }
188 System.out.println("Choosen demo "+demo.getClass().getName());
189 glWindow.addGLEventListener(demo);
190 glWindow.setVisible(true); // force native context creation
191
192 // Check caps of GLDrawable after realization
193 System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
194 System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
195 System.err.println("window pos/siz: "+glWindow.getX()+"/"+glWindow.getY()+"[wu] "+glWindow.getWidth()+"x"+glWindow.getHeight()+"[wu] "+glWindow.getSurfaceWidth()+"x"+glWindow.getSurfaceHeight()+"[px], "+glWindow.getInsets());
196
197 final float[] hasSurfacePixelScale1 = glWindow.getCurrentSurfaceScale(new float[2]);
198 System.err.println("HiDPI PixelScale: "+reqSurfacePixelScale[0]+"x"+reqSurfacePixelScale[1]+" (req) -> "+
199 valReqSurfacePixelScale[0]+"x"+valReqSurfacePixelScale[1]+" (val) -> "+
200 hasSurfacePixelScale1[0]+"x"+hasSurfacePixelScale1[1]+" (has)");
201 {
202 final long uiWindow = glWindow.getWindowHandle();
203 final long uiView = IOSUtil.GetUIView(uiWindow, true);
204 final long caeaglLayer = IOSUtil.GetCAEAGLLayer(uiView);
205 System.out.println("EAGL: UIWindow 0x"+Long.toHexString(uiWindow));
206 System.out.println("EAGL: UIView 0x"+Long.toHexString(uiView));
207 System.out.println("EAGL: EAGLLayer 0x"+Long.toHexString(caeaglLayer));
208 System.out.println("isUIWindow "+IOSUtil.isUIWindow(uiWindow)+", isUIView "+IOSUtil.isUIView(uiView)+
209 ", isCAEAGLLayer "+IOSUtil.isCAEAGLLayer(caeaglLayer));
210 }
211
212 final Animator animator = new Animator(0 /* w/o AWT */);
213 // animator.setExclusiveContext(exclusiveContext);
214 animator.setUpdateFPSFrames(60, System.err);
215 animator.add(glWindow);
216 animator.start();
217
218 for(int i=0; i<secondsDuration; i++) {
219 try {
220 Thread.sleep(1000);
221 } catch (final InterruptedException e) {
222 e.printStackTrace();
223 }
224 }
225 animator.stop();
226
227 } finally {
228 System.err.println("");
229 System.err.println("mark-05");
230 System.err.println("");
231
232 if( null != glWindow ) {
233 glWindow.destroy();
234 }
235 }
236
237 System.err.println("");
238 System.err.println("mark-06");
239 System.err.println("");
240
241 if( exitJVM ) {
242 System.exit(0);
243 }
244 }
245}
void setBackgroundOpaque(final boolean opaque)
Sets whether the surface shall be opaque or translucent.
static Display createDisplay(final String name)
Create a Display entity.
static Screen createScreen(final Display display, final int index)
Create a Screen entity.
A screen may span multiple MonitorDevices representing their combined virtual size.
Definition: Screen.java:58
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final void setPosition(final int x, final int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:525
final float[] getRequestedSurfaceScale(final float[] result)
Returns the requested pixel scale of the associated NativeSurface.
Definition: GLWindow.java:500
final int getX()
Returns the current x position of this window, relative to it's parent.
Definition: GLWindow.java:436
final int getY()
Returns the current y position of the top-left corner of the client area relative to it's parent in w...
Definition: GLWindow.java:441
final float[] getCurrentSurfaceScale(final float[] result)
Returns the current pixel scale of the associated NativeSurface.
Definition: GLWindow.java:505
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
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 int getHeight()
Returns the height of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:451
final CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
Definition: GLWindow.java:266
final InsetsImmutable getInsets()
Returns the insets defined as the width and height of the window decoration on the left,...
Definition: GLWindow.java:431
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 long getWindowHandle()
Returns the window handle for this NativeWindow.
Definition: GLWindow.java:1040
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
final int getWidth()
Returns the width of the client area excluding insets (window decorations) in window units.
Definition: GLWindow.java:446
final Window getDelegatedWindow()
If the implementation uses delegation, return the delegated Window instance, otherwise return this in...
Definition: GLWindow.java:277
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.
void setDepthBits(final int depthBits)
Sets the number of bits requested for the depth buffer.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static AbstractGraphicsDevice getDefaultDevice()
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
Definition: GLProfile.java:913
static void initSingleton()
Static initialization of JOGL.
Definition: GLProfile.java:204
static JoglVersion getInstance()
static StringBuilder getDefaultOpenGLInfo(AbstractGraphicsDevice device, StringBuilder sb, final boolean withCapabilitiesInfo)
static void main(final String[] args)
Definition: Hello.java:64
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
Adding mutable surface pixel scale property to implementing class, usually to a NativeSurface impleme...
static final float AUTOMAX_PIXELSCALE
Setting surface-pixel-scale of {@value}, results in maximum platform dependent pixel-scale,...
CapabilitiesImmutable getChosenCapabilities()
Gets an immutable set of chosen capabilities.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
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.