Support Forums
First JOGL Application - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22)
+---- Thread: First JOGL Application (/showthread.php?tid=4396)



First JOGL Application - Project Evolution - 01-17-2010

Last night I decided to download JOGL, so this app is more of an experimentation of how it works. Not too hard when you get help from a resource, but nonetheless very cool to work with. Next im planning on adding shapes through an ArrayList and individually giving them actions via a KeyHandler.

Src

Code:
package org.yourorghere;

import com.sun.opengl.util.Animator;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;

public class JOGLApp extends JFrame implements KeyListener, GLEventListener {

    // Causes our JOGL app to be animated- that is, it keeps calling display()
    static Animator anim = null;
        static final JOGLApp app = new JOGLApp();
        boolean rotateOn = false;
        float shapeSpeed, rotate = 0f;

    public static void main(String[] args) {
        // Creates the window in another thread
        SwingUtilities.invokeLater (
                new Runnable() {
                    public void run() {
                        // Removes the normal title bar. Most games do this.
                        app.setUndecorated(true);
                        app.setVisible(true);
                                                // Fullscreen mode
                                                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
                                                app.setSize(screen.width, screen.height);
                    }
                }
        );

        // Starts the Animator in a new thread.
        SwingUtilities.invokeLater (
                new Runnable() {
                    public void run() {
                        anim.start();
                    }
                }
        );
    }

    public JOGLApp() {
        // Set the JFrame title
        super("JOGL Application");

        // Kill the process when the JFrame is closed
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GLCapabilities glCaps = new GLCapabilities();
        GLCanvas glCanvas = new GLCanvas(glCaps);
        glCanvas.addGLEventListener(this);

        // Add the GLCanvas just like we would any Component
        getContentPane().add(glCanvas, BorderLayout.CENTER);
                addKeyListener(this);
        anim = new Animator(glCanvas);
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        char keyChar = e.getKeyChar();
        if (keyChar == KeyEvent.VK_ENTER)
            destruct();
        if (keyChar == KeyEvent.VK_SPACE)
            if (rotateOn == false)
                rotateOn = true;
            else
                rotateOn = false;
        if (keyChar == KeyEvent.VK_BACK_SPACE)
            shapeSpeed += .1;
    }

    public void keyReleased(KeyEvent e) {
    }

    // Only put stuff here that should happen once, at the beginning of the program
    public void init(GLAutoDrawable gld) {
        GL gl = gld.getGL();
        GLU glu = new GLU();

        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        gl.glViewport(0,0,500,300);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, 800.0f / 600.0f, 1.0f, 500.0f);
        gl.glMatrixMode(GL.GL_MODELVIEW);
    }

    GL createShape1(GL gl) {
           gl.glBegin(GL.GL_TRIANGLES);
           gl.glColor3f(1.0f, 0.0f, 0.0f);
           gl.glVertex3f( 0.0f, 1.5f,0.0f);
           gl.glVertex3f(-1.0f,-1.0f,0.0f);
           gl.glVertex3f( 1.0f,-1.0f,0.0f);
           return gl;
    }

    GL createShape2(GL gl) {
           gl.glBegin(GL.GL_TRIANGLES);
           // Right Side Facing Front
           gl.glColor3f(0.0f, 1.0f, 1.0f);
           gl.glVertex3f(0.0f, 1.0f, 0.0f);
           gl.glColor3f(0.0f, 0.0f, 1.0f);
           gl.glVertex3f(1.0f, -1.0f, 1.0f);
           gl.glColor3f(0.0f, 0.0f, 0.0f);
           gl.glVertex3f(0.0f, -1.0f, -1.0f);
           // Left Side Facing Front
           gl.glColor3f(0.0f, 1.0f, 1.0f);
           gl.glVertex3f(0.0f, 1.0f, 0.0f);
           gl.glColor3f(0.0f, 0.0f, 1.0f);
           gl.glVertex3f(0.0f, -1.0f, -1.0f);
           gl.glColor3f(0.0f, 0.0f, 0.0f);
           gl.glVertex3f(-1.0f, -1.0f, 1.0f);
           // Bottom
           gl.glColor3f(0.0f, 1.0f, 1.0f);
           gl.glVertex3f(-1.0f, -1.0f, 1.0f);
           gl.glColor3f(0.0f, 0.0f, 1.0f);
           gl.glVertex3f(1.0f, -1.0f, 1.0f);
           gl.glColor3f(0.0f, 0.0f, 0.0f);
           gl.glVertex3f(0.0f, -1.0f, -1.0f);
           return gl;
    }

    // This function will get called repeatedly by the Animator. Think of it as your game loop.
    public void display(GLAutoDrawable gld) {
           GL gl = gld.getGL();
           gl.glLoadIdentity();
           gl.glTranslatef(0.0f,0.0f,-5f);
           gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
           if (!rotateOn) {
               gl.glRotatef(rotate, 1.0f, 0.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 0.0f, 1.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               rotate = rotate + 0;
           } else if (rotateOn && shapeSpeed == 0) {
               gl.glRotatef(rotate, 1.0f, 0.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 0.0f, 1.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               rotate = rotate + shapeSpeed + .1f;
           } else if (rotateOn && shapeSpeed > 0) {
               gl.glRotatef(rotate, 1.0f, 0.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               gl.glRotatef(rotate, 0.0f, 0.0f, 1.0f);
               gl.glRotatef(rotate, 0.0f, 1.0f, 0.0f);
               rotate = rotate + shapeSpeed;
           }

           createShape1(gl);
           createShape2(gl);

           gl.glEnd();
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

    static void destruct() {
           anim.stop();
    app.dispose();
    System.exit(0);
    }
        
}

How it works is you start it up, and it will go into fullscreen mode. Two shapes will appear. Use the spacebar to begin/stop rotating of the two shapes. The backspace will slightly increase the speed of the rotation everytime. Enter closes the app.

[Image: rtartwery.png]

You MUST have JOGL to run this!
Have fun. Smile


RE: First JOGL Application - Ⱳąŗɗ - 03-13-2010

Neat, hope you turn it into something epic!


RE: First JOGL Application - Project Evolution - 03-13-2010

As do I, soon enough.