001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 *
032 */
033package org.openimaj.vis.general;
034
035import javax.media.nativewindow.util.PointImmutable;
036import javax.media.opengl.GLAutoDrawable;
037import javax.media.opengl.GLCapabilities;
038import javax.media.opengl.GLProfile;
039
040import com.jogamp.newt.Display;
041import com.jogamp.newt.NewtFactory;
042import com.jogamp.newt.Screen;
043import com.jogamp.newt.event.WindowAdapter;
044import com.jogamp.newt.event.WindowEvent;
045import com.jogamp.newt.opengl.GLWindow;
046
047/**
048 *      A wrapper around a NEWT window that allows you to get
049 *      a canvas on which to draw.
050 *
051 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
052 *      @created 4 Jul 2013
053 *      @version $Author$, $Revision$, $Date$
054 */
055public class JOGLWindow
056{
057        /** The width of the OpenGL viewport */
058        private int width;
059
060        /** The height of the OpenGL viewport */
061        private int height;
062
063        /** The position of the window (if windowed) */
064        private PointImmutable wpos;
065
066        /** Whether the window is undecorated (if windowed) */
067        private boolean undecorated = false;
068
069        /** Whether the 3D view should always be on top */
070        private boolean alwaysOnTop = false;
071
072        /** Whether to run full screen (rather than windowed) */
073        private boolean fullscreen = false;
074
075        /** Whether the mouse is visible in the window */
076        private boolean mouseVisible = true;
077
078        /** Whether the mouse is confined to the window */
079        private boolean mouseConfined = false;
080
081        /** The window that is created */
082        private GLWindow glWindow;
083
084        /** Which screen to create the viewport on */
085        private final int screenIdx = 0;
086
087        /**
088         *      Create a JOGL Window with the given size and the default
089         *      properties.
090         *
091         *      @param width The width in pixels
092         *      @param height The height in pixels
093         */
094        public JOGLWindow( final int width, final int height )
095        {
096                this.showWindow( width, height );
097        }
098
099        /**
100         *      Constructor that does not initialise the GL window. This allows you
101         *      to alter the window properties prior to calling showWindow().
102         */
103        public JOGLWindow()
104        {
105        }
106
107        /**
108         *      Force an initialisation of the window. Only call this if you used
109         *      the no arguments constructor.
110         *
111         *      @param width The width
112         *      @param height The height
113         */
114        public void showWindow( final int width, final int height )
115        {
116                this.width = width;
117                this.height = height;
118                this.initGL();
119        }
120
121        /**
122         * Initialise the JOGL Window
123         */
124        private void initGL()
125        {
126                // Get the OpenGL profile and its capabilities.
127                // Tries to get OpenGL 3
128                final GLProfile glp = GLProfile.get( GLProfile.GL2 );
129                final GLCapabilities caps = new GLCapabilities( glp );
130
131                // Get the display
132                final Display dpy = NewtFactory.createDisplay( null );
133
134                // Get the screen on the display (defaults to the first screen)
135                final Screen screen = NewtFactory.createScreen( dpy, this.screenIdx );
136
137                // Create a window
138                this.glWindow = GLWindow.create( screen, caps );
139
140                // Set the size and position of the window
141                this.glWindow.setSize( this.width, this.height );
142                if( null != this.wpos ) this.glWindow.setPosition( this.wpos.getX(), this.wpos.getY() );
143
144                // Set the properties of the window
145                this.glWindow.setUndecorated( this.undecorated );
146                this.glWindow.setAlwaysOnTop( this.alwaysOnTop );
147                this.glWindow.setFullscreen( this.fullscreen );
148                this.glWindow.setPointerVisible( this.mouseVisible );
149                this.glWindow.confinePointer( this.mouseConfined );
150
151                // Add a listener to kill the app once the window is closed
152                this.glWindow.addWindowListener( new WindowAdapter()
153                {
154                        @Override
155                        public void windowDestroyNotify(final WindowEvent e)
156                        {
157                                System.exit(1);
158                        };
159                } );
160
161                // Show the window
162                this.glWindow.setVisible( true );
163        }
164
165        /**
166         *      Closes the window and cleans up.
167         */
168        public void close()
169        {
170                this.glWindow.destroy();
171        }
172
173        /**
174         *      Get the drawable surface for 3D operations.
175         *      @return the drawable surface
176         */
177        public GLAutoDrawable getDrawableSurface()
178        {
179                return this.glWindow;
180        }
181
182        /**
183         *      @return the undecorated
184         */
185        public boolean isUndecorated()
186        {
187                return this.undecorated;
188        }
189
190        /**
191         *      @param undecorated the undecorated to set
192         */
193        public void setUndecorated( final boolean undecorated )
194        {
195                this.undecorated = undecorated;
196        }
197
198        /**
199         *      @return the alwaysOnTop
200         */
201        public boolean isAlwaysOnTop()
202        {
203                return this.alwaysOnTop;
204        }
205
206        /**
207         *      @param alwaysOnTop the alwaysOnTop to set
208         */
209        public void setAlwaysOnTop( final boolean alwaysOnTop )
210        {
211                this.alwaysOnTop = alwaysOnTop;
212        }
213
214        /**
215         *      @return the fullscreen
216         */
217        public boolean isFullscreen()
218        {
219                return this.fullscreen;
220        }
221
222        /**
223         *      @param fullscreen the fullscreen to set
224         */
225        public void setFullscreen( final boolean fullscreen )
226        {
227                this.fullscreen = fullscreen;
228        }
229
230        /**
231         *      @return the mouseVisible
232         */
233        public boolean isMouseVisible()
234        {
235                return this.mouseVisible;
236        }
237
238        /**
239         *      @param mouseVisible the mouseVisible to set
240         */
241        public void setMouseVisible( final boolean mouseVisible )
242        {
243                this.mouseVisible = mouseVisible;
244        }
245
246        /**
247         *      @return the mouseConfined
248         */
249        public boolean isMouseConfined()
250        {
251                return this.mouseConfined;
252        }
253
254        /**
255         *      @param mouseConfined the mouseConfined to set
256         */
257        public void setMouseConfined( final boolean mouseConfined )
258        {
259                this.mouseConfined = mouseConfined;
260        }
261
262        /**
263         * Simple test that opens a window, waits 2 seconds, then closes it.
264         * @param args Command-line args (not used)
265         * @throws InterruptedException
266         */
267        public static void main( final String[] args ) throws InterruptedException
268        {
269                // Simple test that opens a window, waits 2 seconds, then closes it.
270                final JOGLWindow jw = new JOGLWindow( 400, 400 );
271                Thread.sleep( 2000 );
272                jw.close();
273        }
274}