JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
Hello1.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.AbstractGraphicsScreen;
33import com.jogamp.nativewindow.MutableGraphicsConfiguration;
34import com.jogamp.nativewindow.NativeWindowFactory;
35import com.jogamp.nativewindow.UpstreamWindowHookMutableSizePos;
36
37import com.jogamp.common.GlueGenVersion;
38import com.jogamp.opengl.JoglVersion;
39import com.jogamp.opengl.demos.es2.RedSquareES2;
40import com.jogamp.opengl.util.Animator;
41import com.jogamp.opengl.util.AnimatorBase;
42
43import jogamp.nativewindow.WrappedWindow;
44import jogamp.nativewindow.ios.IOSUtil;
45import jogamp.opengl.GLDrawableFactoryImpl;
46
47import com.jogamp.opengl.GLAutoDrawable;
48import com.jogamp.opengl.GLAutoDrawableDelegate;
49import com.jogamp.opengl.GLCapabilities;
50import com.jogamp.opengl.GLCapabilitiesImmutable;
51import com.jogamp.opengl.GLDrawable;
52import com.jogamp.opengl.GLDrawableFactory;
53import com.jogamp.opengl.GLEventListener;
54import com.jogamp.opengl.GLProfile;
55
56public class Hello1 {
57
58 private static int parseInt(final String s, final int def) {
59 try {
60 return Integer.parseInt(s);
61 } catch (final NumberFormatException nfe) {}
62 return def;
63 }
64
65 public static void main(final String[] args) {
66 int width = 832, height = 480; // ipad pro 11: 2388x1668 px (scale: 2)
67 int fboDepthBits = -1; // CAEAGLLayer fails with depth 16 + 24 in Simulation; -1 means don't change
68 boolean exitJVM = false;
69 String demoName = "com.jogamp.opengl.demos.es2.GearsES2";
70 for(int i=0; i<args.length; i++) {
71 if(args[i].equals("-exit")) {
72 exitJVM = true;
73 } else if(args[i].equals("-demo") && i+1<args.length) {
74 demoName = args[++i];
75 } else if(args[i].equals("-width") && i+1<args.length) {
76 width = parseInt(args[++i], width);
77 } else if(args[i].equals("-height") && i+1<args.length) {
78 height = parseInt(args[++i], height);
79 } else if(args[i].equals("-fboDepthBits") && i+1<args.length) {
80 fboDepthBits = parseInt(args[++i], fboDepthBits);
81 } else {
82 System.err.println("ignoring arg["+i+"]: "+args[i]);
83 }
84 }
85 System.out.println("Hello JogAmp World: exitJVM "+exitJVM+", size "+width+"x"+height+", fboDepthBits "+fboDepthBits+", demo "+demoName);
86 System.out.println("os.name: <"+System.getProperty("os.name")+">");
87 System.out.println("os.version: <"+System.getProperty("os.version")+">");
88 System.out.println("os.arch: <"+System.getProperty("os.arch")+">");
89 System.out.println("java.vendor: <"+System.getProperty("java.vendor")+">");
90 System.out.println("java.vendor.url: <"+System.getProperty("java.vendor.url")+">");
91 System.out.println("java.version: <"+System.getProperty("java.version")+">");
92 System.out.println("java.vm.name: <"+System.getProperty("java.vm.name")+">");
93 System.out.println("java.runtime.name: <"+System.getProperty("java.runtime.name")+">");
94 System.out.println("");
95 System.out.println(VersionUtil.getPlatformInfo());
96 System.out.println("");
97 System.out.println("Version Info:");
98 System.out.println(GlueGenVersion.getInstance());
99 System.out.println("");
100 System.out.println("Full Manifest:");
101 System.out.println(GlueGenVersion.getInstance().getFullManifestInfo(null));
102
103 System.out.println("");
104 System.err.println("mark-01");
105 System.err.println("");
106 System.err.println(JoglVersion.getInstance());
107 System.err.println("");
108 System.err.println("mark-02");
109 System.err.println("");
111 System.err.println("");
112 System.err.println("mark-03");
113 System.out.println("");
114 System.out.println(JoglVersion.getDefaultOpenGLInfo(GLProfile.getDefaultDevice(), null, true));
115 System.out.println("");
116 System.err.println("mark-04");
117 System.err.println("");
118
119 GLAutoDrawableDelegate glad = null;
120 final long uiWindow = IOSUtil.CreateUIWindow(0, 0, width, height, true);
121 try {
122 // 1) Config ..
123 final GLProfile glp = GLProfile.getGL2ES2();
124 final GLCapabilities reqCaps = new GLCapabilities(glp);
125 if( 0 <= fboDepthBits) {
126 reqCaps.setDepthBits(fboDepthBits);
127 }
128 System.out.println("Requested GL Caps: "+reqCaps);
129 final GLDrawableFactoryImpl factory = (GLDrawableFactoryImpl) GLDrawableFactory.getFactory(glp);
130
131 // 2) Create native window and wrap it around out NativeWindow structure
132 final long uiView = IOSUtil.GetUIView(uiWindow, true);
133 final long caeaglLayer = IOSUtil.GetCAEAGLLayer(uiView);
134 System.out.println("EAGL: UIWindow 0x"+Long.toHexString(uiWindow));
135 System.out.println("EAGL: UIView 0x"+Long.toHexString(uiView));
136 System.out.println("EAGL: EAGLLayer 0x"+Long.toHexString(caeaglLayer));
137 System.out.println("isUIWindow "+IOSUtil.isUIWindow(uiWindow)+", isUIView "+IOSUtil.isUIView(uiView)+
138 ", isCAEAGLLayer "+IOSUtil.isCAEAGLLayer(caeaglLayer));
140 final UpstreamWindowHookMutableSizePos hook = new UpstreamWindowHookMutableSizePos(0, 0, width, height, width, height);
141 final MutableGraphicsConfiguration config = new MutableGraphicsConfiguration(aScreen, reqCaps, reqCaps);
142 final WrappedWindow nativeWindow = new WrappedWindow(config, uiView, hook, true, uiWindow);
143
144 // 3) Create a GLDrawable ..
145 final GLDrawable drawable = factory.createGLDrawable(nativeWindow);
146 drawable.setRealized(true);
147 // final GLOffscreenAutoDrawable glad = factory.createOffscreenAutoDrawable(aScreen.getDevice(), reqCaps, null, width, height);
148 glad = new GLAutoDrawableDelegate(drawable, null, nativeWindow, false, null) {
149 @Override
150 protected void destroyImplInLock() {
151 super.destroyImplInLock(); // destroys drawable/context
152 nativeWindow.destroy(); // destroys the actual window, incl. the device
153 IOSUtil.DestroyUIWindow(uiWindow);
154 }
155 };
156 final GLEventListener tracker = new GLEventListener() {
157 void printInfo(final String prefix, final GLAutoDrawable d, final String postfix) {
158 System.out.print(prefix+": drawable "+d.getSurfaceWidth()+"x"+d.getSurfaceHeight());
159 if(null != postfix) {
160 System.out.println(" - "+postfix);
161 } else {
162 System.out.println();
163 }
164 }
165 @Override
166 public void init(final GLAutoDrawable drawable) {
167 printInfo("GLEvent::Init", drawable, null);
168 }
169
170 @Override
171 public void dispose(final GLAutoDrawable drawable) {
172 printInfo("GLEvent::Dispose", drawable, null);
173 }
174
175 @Override
176 public void display(final GLAutoDrawable drawable) {
177 }
178
179 @Override
180 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
181 printInfo("GLEvent::Reshape", drawable, "reshape["+x+"/"+y+" "+width+"x"+height+"]");
182 }
183 };
184 glad.addGLEventListener(tracker);
185 glad.display(); // force native context creation
186
187 // Check caps of GLDrawable after realization
188 final GLCapabilitiesImmutable chosenCaps = glad.getChosenGLCapabilities();
189 System.out.println("Choosen GL Caps: "+chosenCaps);
190
191 GLEventListener demo = null;
192 {
193 try {
194 demo = (GLEventListener) ReflectionUtil.createInstance(demoName, Hello1.class.getClassLoader());
195 } catch( final Exception e ) {
196 System.err.println(e.getMessage()+" using: <"+demoName+">");
197 }
198 if( null == demo ) {
199 demo = new RedSquareES2();
200 }
201 }
202 System.out.println("Choosen demo "+demo.getClass().getName());
203 glad.addGLEventListener(demo);
204
205 final Animator animator = new Animator(0 /* w/o AWT */);
206 // animator.setExclusiveContext(exclusiveContext);
207 animator.setUpdateFPSFrames(60, System.err);
208 animator.add(glad);
209 animator.start();
210
211 for(int i=0; i<10; i++) { // 10s
212 try {
213 Thread.sleep(1000);
214 } catch (final InterruptedException e) {
215 e.printStackTrace();
216 }
217 }
218 animator.stop();
219
220 } finally {
221 if( null != glad ) {
222 glad.destroy();
223 }
224 }
225
226 System.err.println("");
227 System.err.println("mark-05");
228 System.err.println("");
229
230 if( exitJVM ) {
231 System.exit(0);
232 }
233 }
234}
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static AbstractGraphicsDevice createDevice(final String displayConnection, final boolean own)
Creates a native device type, following getNativeWindowType(true).
static AbstractGraphicsScreen createScreen(final AbstractGraphicsDevice device, int screen)
Fully functional GLAutoDrawable implementation utilizing already created GLDrawable and GLContext ins...
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext....
Specifies a set of OpenGL capabilities.
void setDepthBits(final int depthBits)
Sets the number of bits requested for the depth buffer.
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
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: Hello1.java:65
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
A interface describing a graphics screen in a toolkit-independent manner.
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.
Specifies an immutable set of OpenGL capabilities.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
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.
void setRealized(boolean realized)
Indicates to GLDrawable implementations whether the underlying surface has been created and can be dr...
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.