import java.awt.*; import java.awt.event.*; import javax.swing.*; import net.java.games.jogl.*; import net.java.games.jogl.util.*; public class Order { private static final int DEFAULT_WIDTH = 640; private static final int DEFAULT_HEIGHT = 480; public static GLDisplay createGLDisplay(String title) { GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); boolean fullscreen = false; if (device.isFullScreenSupported()) { int selectedOption = JOptionPane.showOptionDialog(null, "How would you like to run this lesson?", title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] {"Fullscreen", "Windowed"}, "Windowed"); fullscreen = selectedOption == 0; } return new GLDisplay(title, DEFAULT_WIDTH, DEFAULT_HEIGHT, fullscreen); } public static void main(String[] args) { GLDisplay neheGLDisplay = createGLDisplay("Lesson 05: 3D Shapes"); neheGLDisplay.addGLEventListener(new Renderer()); neheGLDisplay.start(); } } /** Port of the NeHe OpenGL Tutorial (Lesson 5) * to Java using the Jogl interface to OpenGL. Jogl can be obtained * at http://jogl.dev.java.net/ * * @author Kevin Duling (jattier@hotmail.com) */ class Renderer implements GLEventListener { private float rquad = 0.0f; private float rtri = 0.0f; /** Called by the drawable to initiate OpenGL rendering by the client. * After all GLEventListeners have been notified of a display event, the * drawable will swap its buffers if necessary. * @param gLDrawable The GLDrawable object. */ public void display(GLDrawable gLDrawable) { final GL gl = gLDrawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(-1.5f, 0.0f, -6.0f); gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f); gl.glBegin(GL.GL_TRIANGLES); // Drawing Using Triangles gl.glColor3f(1.0f,0.0f,0.0f); // Red gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front) gl.glColor3f(0.0f,1.0f,0.0f); // Green gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front) gl.glColor3f(0.0f,0.0f,1.0f); // Blue gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front) gl.glColor3f(1.0f,0.0f,0.0f); // Red gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right) gl.glColor3f(0.0f,0.0f,1.0f); // Blue gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right) gl.glColor3f(0.0f,1.0f,0.0f); // Green gl.glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right) gl.glColor3f(1.0f,0.0f,0.0f); // Red gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back) gl.glColor3f(0.0f,1.0f,0.0f); // Green gl.glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back) gl.glColor3f(0.0f,0.0f,1.0f); // Blue gl.glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back) gl.glColor3f(1.0f,0.0f,0.0f); // Red gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left) gl.glColor3f(0.0f,0.0f,1.0f); // Blue gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left) gl.glColor3f(0.0f,1.0f,0.0f); // Green gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left) gl.glEnd(); // Finished Drawing The Triangle gl.glLoadIdentity(); gl.glTranslatef(1.5f, 0.0f, -6.0f); gl.glRotatef(rquad, 1.0f, 1.0f, 1.0f); gl.glBegin(GL.GL_QUADS); // Draw A Quad gl.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top) gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top) gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) gl.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom) gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom) gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom) gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom) gl.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front) gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front) gl.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Back) gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Back) gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Back) gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Back) gl.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) gl.glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left) gl.glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left) gl.glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left) gl.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet gl.glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right) gl.glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) gl.glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right) gl.glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right) gl.glEnd(); // Done Drawing The Quad gl.glFlush(); rtri += 0.2f; rquad += 0.15f; } /** Called when the display mode has been changed. !! CURRENTLY UNIMPLEMENTED IN JOGL !! * @param gLDrawable The GLDrawable object. * @param modeChanged Indicates if the video mode has changed. * @param deviceChanged Indicates if the video device has changed. */ public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) { } /** Called by the drawable immediately after the OpenGL context is * initialized for the first time. Can be used to perform one-time OpenGL * initialization such as setup of lights and display lists. * @param gLDrawable The GLDrawable object. */ public void init(GLDrawable gLDrawable) { GL gl = gLDrawable.getGL(); gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background gl.glClearDepth(1.0f); // Depth Buffer Setup gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really Nice Perspective Calculations System.out.println(); System.out.println("OPENGL DRIVER INFORMATION"); System.out.println(" Version: " + gl.glGetString(GL.GL_VERSION)); System.out.println("Renderer: " + gl.glGetString(GL.GL_RENDERER)); System.out.println(" Vendor: " + gl.glGetString(GL.GL_VENDOR)); System.out.println(); System.out.println("DISPLAY INFORMATION"); Dimension dim = gLDrawable.getSize(); System.out.println("Resolution: " + (int)dim.getWidth() + "x" + (int)dim.getHeight()); GLCapabilities glCaps = new GLCapabilities(); System.out.println(glCaps.toString()); } /** Called by the drawable during the first repaint after the component has * been resized. The client can update the viewport and view volume of the * window appropriately, for example by a call to * GL.glViewport(int, int, int, int); note that for convenience the component * has already called GL.glViewport(int, int, int, int)(x, y, width, height) * when this method is called, so the client may not have to do anything in * this method. * @param gLDrawable The GLDrawable object. * @param x The X Coordinate of the viewport rectangle. * @param y The Y coordinate of the viewport rectanble. * @param width The new width of the window. * @param height The new height of the window. */ public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) { final GL gl = gLDrawable.getGL(); final GLU glu = gLDrawable.getGLU(); if (height <= 0) // avoid a divide by zero error! height = 1; final float h = (float)width / (float)height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); } } class GLDisplay { private static final int DEFAULT_WIDTH = 640; private static final int DEFAULT_HEIGHT = 480; private static final int DONT_CARE = -1; private JFrame frame; private GLCanvas glCanvas; private Animator animator; private boolean fullscreen; private int width; private int height; private GraphicsDevice usedDevice; DisplayMode oldDisplayMode; public GLDisplay(String title, boolean fullscreen) { this(title, DEFAULT_WIDTH, DEFAULT_HEIGHT, fullscreen); } public GLDisplay(String title, int width, int height, boolean fullscreen) { GLCapabilities glCaps = new GLCapabilities(); boolean hwAccel = glCaps.getHardwareAccelerated(); glCanvas = GLDrawableFactory.getFactory().createGLCanvas(glCaps); glCanvas.setSize(width, height); glCanvas.setIgnoreRepaint(true); frame = new JFrame(title); frame.getContentPane().setLayout(new BorderLayout()); // // Adding the canvas before showing in a fullscreen app // prior to 1.1b04 (July 16) causes a white screen of death frame.getContentPane().add(glCanvas, BorderLayout.CENTER); addKeyListener(new MyShutdownKeyAdapter()); this.fullscreen = fullscreen; this.width = width; this.height = height; animator = new Animator(glCanvas); } public void start() { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setSize(width, height); frame.setLocation((screenSize.width - frame.getWidth()) / 2, (screenSize.height - frame.getHeight()) / 2); frame.addWindowListener(new MyShutdownWindowAdapter()); if (fullscreen) { frame.setUndecorated(true); usedDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); this.oldDisplayMode = usedDevice.getDisplayMode(); usedDevice.setFullScreenWindow(frame); usedDevice.setDisplayMode(findDisplayMode(usedDevice.getDisplayModes(), width, height, usedDevice.getDisplayMode().getBitDepth(), usedDevice.getDisplayMode().getRefreshRate())); } else { frame.setVisible(true); } // Moved the add call to this location to save the virtual world // frame.getContentPane().add(glCanvas, BorderLayout.CENTER); glCanvas.requestFocus(); animator.start(); } public void stop() { animator.stop(); if (fullscreen) { usedDevice.setDisplayMode(oldDisplayMode); usedDevice.setFullScreenWindow(null); usedDevice = null; } frame.dispose(); System.exit(0); } private DisplayMode findDisplayMode(DisplayMode[] displayModes, int requestedWidth, int requestedHeight, int requestedDepth, int requestedRefreshRate) { // Try to find an exact match DisplayMode displayMode = findDisplayModeInternal(displayModes, requestedWidth, requestedHeight, requestedDepth, requestedRefreshRate); // Try again, ignoring the requested bit depth if (displayMode == null) displayMode = findDisplayModeInternal(displayModes, requestedWidth, requestedHeight, DONT_CARE, DONT_CARE); // Try again, and again ignoring the requested bit depth and height if (displayMode == null) displayMode = findDisplayModeInternal(displayModes, requestedWidth, DONT_CARE, DONT_CARE, DONT_CARE); // If all else fails try to get any display mode if (displayMode == null) displayMode = findDisplayModeInternal(displayModes, DONT_CARE, DONT_CARE, DONT_CARE, DONT_CARE); return displayMode; } private DisplayMode findDisplayModeInternal(DisplayMode[] displayModes, int requestedWidth, int requestedHeight, int requestedDepth, int requestedRefreshRate) { DisplayMode displayModeToUse = null; for (int i = 0; i < displayModes.length; i++) { DisplayMode displayMode = displayModes[i]; if ((requestedWidth == DONT_CARE || displayMode.getWidth() == requestedWidth) && (requestedHeight == DONT_CARE || displayMode.getHeight() == requestedHeight) && (requestedHeight == DONT_CARE || displayMode.getRefreshRate() == requestedRefreshRate) && (requestedDepth == DONT_CARE || displayMode.getBitDepth() == requestedDepth)) displayModeToUse = displayMode; } return displayModeToUse; } public void addGLEventListener(GLEventListener glEventListener) { glCanvas.addGLEventListener(glEventListener); } public void removeGLEventListener(GLEventListener glEventListener) { glCanvas.removeGLEventListener(glEventListener); } public void addKeyListener(KeyListener l) { glCanvas.addKeyListener(l); } public void addMouseListener(MouseListener l) { glCanvas.addMouseListener(l); } public void addMouseMotionListener(MouseMotionListener l) { glCanvas.addMouseMotionListener(l); } public void removeKeyListener(KeyListener l) { glCanvas.removeKeyListener(l); } public void removeMouseListener(MouseListener l) { glCanvas.removeMouseListener(l); } public void removeMouseMotionListener(MouseMotionListener l) { glCanvas.removeMouseMotionListener(l); } private class MyShutdownKeyAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { stop(); } } } private class MyShutdownWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent e) { stop(); } } }