Comedy Hometown - Watch Comedy Hometown's Latest Video!

Welcome to Havenworld's Gaming, Computing and Programming Forums!

Unlock extra board access and remove this notice by registering for free below:
Choose username: Email:
Choose password: Antibot:
Type the letters shown in the picture to the left:
Verify password:

Pages: [1]   Go Down
  Print  
Author Topic: First JOGL Application  (Read 283 times)
Project Evolution
Elitist Jerk of SMF
Ex-Staff
Havenworld Inhabitant
****

Reputation: 13
Offline Offline

Posts: 617


WWW
« on: January 17, 2010, 09:42:05 pm »


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: [Select]
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.



You MUST have JOGL to run this!
Have fun. :)
Tom
>
Administrator
Havenworld Junkie
*

Reputation: 54
Offline Offline

Posts: 1502

i > you


« Reply #1 on: January 18, 2010, 08:40:45 am »

I'm curious, how big is the JOGL lib compared to others? I mean like is it small or big, based on other libs? I know it's around 2MB.
Project Evolution
Elitist Jerk of SMF
Ex-Staff
Havenworld Inhabitant
****

Reputation: 13
Offline Offline

Posts: 617


WWW
« Reply #2 on: January 18, 2010, 10:08:42 pm »

Java 3D - approximately 2.95 MB.
LWJGL - approximately 3.5 MB.
jMonkeyEngine - much larger.

So I guess you can see here that JOGL is smaller. But really whats the big deal, they are all about a megabyte difference?
Pages: [1]   Go Up
  Print  
 
 

Powered by SMF 1.1.6 | SMF © 2006-2008, Simple Machines LLC | Havenworld © 2006 - 2009 Havenworld Plc | The Peoples World
Page created in 0.131 seconds.