java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow Moving bitmaps in 3D space - NeHe Tutorial JOGL Port
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4096
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Moving bitmaps in 3D space - NeHe Tutorial JOGL Port E-mail
User Rating: / 5
PoorBest 

This tutorials shows mainly how to move the objects around the screen, how to draw a bitmap to the screen and how to create a simple animation.

This is the Java port of the one of the NeHe OpenGL tutorials.

You can get complete IntelliJ IDEA project structure (all source, resources, build script, …) by downloading the source distribution from here.

The original post of the programmer who ported the examples can be found here.


Image

package demos.nehe.lesson09;

/*--.          .-"-.
/   o_O        / O o \
\_  (__\       \_ v _/
//   \\        //   \\
((     ))      ((     ))
¤¤¤¤¤¤¤¤¤¤¤¤¤¤--""---""--¤¤¤¤--""---""--¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
¤                 |||            |||                             ¤
¤                  |              |                              ¤
¤                                                                ¤
¤ Programmer:Abdul Bezrati                                       ¤
¤ Program   :Nehe's 9th lesson port to JOGL                     ¤
¤ Comments  :None                                                ¤
¤    _______                                                     ¤
¤  /` _____ `\;,     This e-mail address is being protected from spam bots, you need JavaScript enabled to view it                          ¤
¤ (__(^===^)__)';,                                 ___           ¤
¤   /  :::  \   ,;                               /^   ^\         ¤
¤  |   :::   | ,;'                              ( Ö   Ö )        ¤
¤¤¤'._______.'`¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ --°oOo--(_)--oOo°--¤¤*/

import demos.common.GLDisplay;

/**
 @author Abdul Bezrati
 */
public class Lesson09 {
    public static void main(String[] args) {
        GLDisplay neheGLDisplay = GLDisplay.createGLDisplay(
                "Lesson 09: Moving bitmaps in 3D space");
        
        Renderer renderer = new Renderer();
        InputHandler inputHandler = new InputHandler(renderer, neheGLDisplay);
        neheGLDisplay.addGLEventListener(renderer);
        neheGLDisplay.addKeyListener(inputHandler);
        neheGLDisplay.start();
    }
}


package demos.nehe.lesson09;

import demos.common.GLDisplay;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

class InputHandler extends KeyAdapter {
    private Renderer renderer;

    public InputHandler(Renderer renderer, GLDisplay glDisplay) {
        this.renderer = renderer;
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_T, 0)"Toggle twinkle");
        
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)"Decrease tilt");
        
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)"Increase tilt");
        
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP, 0)"Zoom out");
        
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_DOWN, 0)"Zoom in");
    }

    public void keyPressed(KeyEvent e) {
        processKeyEvent(e, true);
    }

    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_T:
                renderer.toggleTwinkle();
                break;
            default:
                processKeyEvent(e, false);
        }
    }

    private void processKeyEvent(KeyEvent e, boolean pressed) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                renderer.decreaseTilt(pressed);
                break;
            case KeyEvent.VK_DOWN:
                renderer.increaseTilt(pressed);
                break;
            case KeyEvent.VK_PAGE_UP:
                renderer.zoomIn(pressed);
                break;
            case KeyEvent.VK_PAGE_DOWN:
                renderer.zoomOut(pressed);
                break;
        }
    }
}


package demos.nehe.lesson09;

import demos.common.TextureReader;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

import java.io.IOException;

class Renderer implements GLEventListener {
    private static final int NUMBER_OF_STARS = 50;

    // User Defined Variables
    private boolean twinkle;  // Twinkling Stars

    private Star stars[] new Star[NUMBER_OF_STARS]// Need To Keep Track Of 'num' Stars

    private float zoom = -15.0f;             // Distance Away From Stars
    private boolean zoomIn;
    private boolean zoomOut;

    private float tilt = 90.0f;              // Tilt The View
    private boolean increaseTilt;
    private boolean decreaseTilt;

    private float spin;                      // Spin Stars

    private int textures[] new int[1];     // Storage For One textures

    private GLU glu = new GLU();

    public void zoomIn(boolean zoom) {
        zoomIn = zoom;
    }

    public void zoomOut(boolean zoom) {
        zoomOut = zoom;
    }

    public void increaseTilt(boolean increase) {
        increaseTilt = increase;
    }

    public void decreaseTilt(boolean decrease) {
        decreaseTilt = decrease;
    }

    public void toggleTwinkle() {
        twinkle = !twinkle;
    }

    private void LoadGLTextures(GL gl) {     // Load Bitmaps And Convert To Textures
        TextureReader.Texture texture = null;// Create Storage Space For The Texture
        
        try {
            texture = TextureReader.readTexture("demos/data/images/Star.bmp");
        catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        
        gl.glGenTextures(1, textures, 0);  // Create One Texture

        // Create Linear Filtered Texture
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        gl.glTexImage2D(GL.GL_TEXTURE_2D, 03, texture.getWidth()
                texture.getHeight()0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, texture.getPixels());
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();
        gl.glEnable(GL.GL_TEXTURE_2D);               // Enable Texture Mapping
        gl.glShadeModel(GL.GL_SMOOTH);               // Enable Smooth Shading
        gl.glClearColor(0.0f0.0f0.0f0.5f);     // Black Background
        gl.glClearDepth(1.0f);                       // Depth Buffer Setup
        
        // Really Nice Perspective Calculations
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
        
        // Set The Blending Function For Translucency
        gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);                  
        gl.glEnable(GL.GL_BLEND);

        for (int loop = 0; loop < stars.length; loop++) {
            stars[loopnew Star();
            stars[loop].angle = 0.0f;
            stars[loop].dist = ((floatloop / stars.length5.0f;
            stars[loop].r = (int) (Math.random() 1000256;
            stars[loop].g = (int) (Math.random() 1000256;
            stars[loop].b = (int) (Math.random() 1000256;
        }

        LoadGLTextures(gl);
    }

    private void update() {
        if (decreaseTilt)
            tilt -= 0.5f;
        if (increaseTilt)
            tilt += 0.5f;
        if (zoomOut)
            zoom -= 0.2f;
        if (zoomIn)
            zoom += 0.2f;
    }

    public void display(GLAutoDrawable drawable) {
        update();
        GL gl = drawable.getGL();

        // Clear Color Buffer, Depth Buffer
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);   // Select Our Texture

        for (int loop = 0; loop < stars.length; loop++) {  // Loop Through All The Stars
            gl.glLoadIdentity();                // Reset The View Before We Draw Each Star
            gl.glTranslatef(0.0f0.0f, zoom);  // Zoom Into The Screen (Using The Value In 'zoom')
            gl.glRotatef(tilt, 1.0f0.0f0.0f);  // Tilt The View (Using The Value In 'tilt')
            gl.glRotatef(stars[loop].angle, 0.0f1.0f0.0f)// Rotate To The Current Stars Angle
            gl.glTranslatef(stars[loop].dist, 0.0f0.0f);  // Move Forward On The X Plane
            gl.glRotatef(-stars[loop].angle, 0.0f1.0f0.0f)// Cancel The Current Stars Angle
            gl.glRotatef(-tilt, 1.0f0.0f0.0f);  // Cancel The Screen Tilt

            if (twinkle) {
                gl.glColor4ub((bytestars[(stars.length - loop1].r, 
                        (bytestars[(stars.length - loop1].g, 
                        (bytestars[(stars.length - loop1].b, (byte255);
                
                gl.glBegin(GL.GL_QUADS);
                gl.glTexCoord2f(0.0f0.0f);
                gl.glVertex3f(-1.0f, -1.0f0.0f);
                gl.glTexCoord2f(1.0f0.0f);
                gl.glVertex3f(1.0f, -1.0f0.0f);
                gl.glTexCoord2f(1.0f1.0f);
                gl.glVertex3f(1.0f1.0f0.0f);
                gl.glTexCoord2f(0.0f1.0f);
                gl.glVertex3f(-1.0f1.0f0.0f);
                gl.glEnd();
            }

            gl.glRotatef(spin, 0.0f0.0f1.0f);
            gl.glColor4ub((bytestars[loop].r, (bytestars[loop].g, 
                    (bytestars[loop].b, (byte255);
            
            gl.glBegin(GL.GL_QUADS);
            gl.glTexCoord2f(0.0f0.0f);
            gl.glVertex3f(-1.0f, -1.0f0.0f);
            gl.glTexCoord2f(1.0f0.0f);
            gl.glVertex3f(1.0f, -1.0f0.0f);
            gl.glTexCoord2f(1.0f1.0f);
            gl.glVertex3f(1.0f1.0f0.0f);
            gl.glTexCoord2f(0.0f1.0f);
            gl.glVertex3f(-1.0f1.0f0.0f);
            gl.glEnd();

            spin += 0.01f;
            stars[loop].angle += (floatloop / stars.length;
            stars[loop].dist -= 0.01f;

            if (stars[loop].dist < 0.0f) {
                stars[loop].dist += 5.0f;
                stars[loop].r = (int) (Math.random() 1000256;
                stars[loop].g = (int) (Math.random() 1000256;
                stars[loop].b = (int) (Math.random() 1000256;
            }
        }
    }

    public void reshape(GLAutoDrawable drawable,
                        int xstart,
                        int ystart,
                        int width,
                        int height) {
        GL gl = drawable.getGL();

        height = (height == 0: height;

        gl.glViewport(00, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();

        glu.gluPerspective(45(floatwidth / height, 11000);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

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

    private class Star {  // Create A Structure For Star
        int r, g, b;          // Stars Color
        float dist,           // Stars Distance From Center
        angle;          // Stars Current Angle
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.