java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
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: 4094
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:
 
 
 
Masking - NeHe Tutorial JOGL Port E-mail
User Rating: / 2
PoorBest 

This example shows how to implement masking with JOGL.

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.lesson20;

import demos.common.GLDisplay;

/**
 @author Pepijn Van Eeckhoudt
 */
public class Lesson20 {
    public static void main(String[] args) {
        GLDisplay neheGLDisplay = GLDisplay.createGLDisplay("Lesson 20: Masking");
        Renderer renderer = new Renderer();
        InputHandler inputHandler = new InputHandler(renderer, neheGLDisplay);
        neheGLDisplay.addGLEventListener(renderer);
        neheGLDisplay.addKeyListener(inputHandler);
        neheGLDisplay.start();
    }
}


package demos.nehe.lesson20;

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_M, 0)"Toggle masking");
        
        glDisplay.registerKeyStrokeForHelp(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)"Switch scene");
    }

    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_M:
                renderer.toggleMasking();
                break;
            case KeyEvent.VK_SPACE:
                renderer.switchScene();
                break;
        }
    }
}


package demos.nehe.lesson20;

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 {
    boolean maskingEnabled = true;  // Masking On/Off
    boolean scene;      // Which Scene To Draw

    int[] textures = new int[5];  // Storage For Our Five Textures
    int loop;        // Generic Loop Variable
    float roll;        // Rolling Texture

    private GLU glu = new GLU();

    public Renderer() {
    }

    public void toggleMasking() {
        maskingEnabled = !maskingEnabled;
    }

    public void switchScene() {
        scene = !scene;
    }

    public void loadGLTextures(GL glthrows IOException {
        String tileNames [] =
                {"demos/data/images/logo.png",
                 "demos/data/images/mask1.png",
                 "demos/data/images/image1.png",
                 "demos/data/images/mask2.png",
                 "demos/data/images/image2.png"
                };

        gl.glGenTextures(5, textures, 0);

        for (int i = 0; i < 5; i++) {
            TextureReader.Texture texture = TextureReader.readTexture(tileNames[i]);
            //Create Nearest Filtered Texture
            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[i]);

            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,
                    0,
                    3,
                    texture.getWidth(),
                    texture.getHeight(),
                    0,
                    GL.GL_RGB,
                    GL.GL_UNSIGNED_BYTE,
                    texture.getPixels());


        }
    }

    public void init(GLAutoDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        try {
            loadGLTextures(gl);
        catch (IOException e) {
            throw new RuntimeException(e);
        }

        gl.glShadeModel(GL.GL_SMOOTH)// Enables Smooth Color Shading
        
        // This Will Clear The Background Color To Black
        gl.glClearColor(0.0f0.0f0.0f0.0f);  
        gl.glClearDepth(1.0);    // Enables Clearing Of The Depth Buffer
        gl.glEnable(GL.GL_DEPTH_TEST);  // Enables Depth Testing
        gl.glDepthFunc(GL.GL_LEQUAL);   // The Type Of Depth Test To Do
        
        // Really Nice Perspective Calculations
        gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);  
        gl.glEnable(GL.GL_TEXTURE_2D);
    }

    public void display(GLAutoDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        
        // Clear The Screen And The Depth Buffer
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);       
        gl.glLoadIdentity();       // Reset The View
        gl.glTranslatef(0.0f0.0f, -2.0f);  // Move Into The Screen 5 Units

        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0])// Select Our Logo Texture
        gl.glBegin(GL.GL_QUADS);  // Start Drawing A Textured Quad
        gl.glTexCoord2f(0.0f, -roll + 0.0f);
        gl.glVertex3f(-1.1f, -1.1f0.0f);  // Bottom Left
        gl.glTexCoord2f(3.0f, -roll + 0.0f);
        gl.glVertex3f(1.1f, -1.1f0.0f);  // Bottom Right
        gl.glTexCoord2f(3.0f, -roll + 3.0f);
        gl.glVertex3f(1.1f1.1f0.0f);  // Top Right
        gl.glTexCoord2f(0.0f, -roll + 3.0f);
        gl.glVertex3f(-1.1f1.1f0.0f);  // Top Left
        gl.glEnd();  // Done Drawing The Quad

        gl.glEnable(GL.GL_BLEND);  // Enable Blending
        gl.glDisable(GL.GL_DEPTH_TEST);  // Disable Depth Testing

        if (maskingEnabled)  // Is Masking Enabled?
        {
            // Blend Screen Color With Zero (Black)
            gl.glBlendFunc(GL.GL_DST_COLOR, GL.GL_ZERO)
        }

        if (scene)  // Are We Drawing The Second Scene?
        {
            gl.glTranslatef(0.0f0.0f, -1.0f);  // Translate Into The Screen One Unit
            gl.glRotatef(roll * 3600.0f0.0f1.0f);  // Rotate On The Z Axis 360 Degrees.
            if (maskingEnabled)  // Is Masking On?
            {
                // Select The Second Mask Texture
                gl.glBindTexture(GL.GL_TEXTURE_2D, textures[3]);  
                gl.glBegin(GL.GL_QUADS);  // Start Drawing A Textured Quad
                gl.glTexCoord2f(0.0f0.0f);
                gl.glVertex3f(-1.1f, -1.1f0.0f);  // Bottom Left
                gl.glTexCoord2f(1.0f0.0f);
                gl.glVertex3f(1.1f, -1.1f0.0f);  // Bottom Right
                gl.glTexCoord2f(1.0f1.0f);
                gl.glVertex3f(1.1f1.1f0.0f);  // Top Right
                gl.glTexCoord2f(0.0f1.0f);
                gl.glVertex3f(-1.1f1.1f0.0f);  // Top Left
                gl.glEnd();        // Done Drawing The Quad
            }

            gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);  // Copy Image 2 Color To The Screen
            
            // Select The Second Image Texture
            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[4]);    
            gl.glBegin(GL.GL_QUADS);  // Start Drawing A Textured Quad
            gl.glTexCoord2f(0.0f0.0f);
            gl.glVertex3f(-1.1f, -1.1f0.0f);  // Bottom Left
            gl.glTexCoord2f(1.0f0.0f);
            gl.glVertex3f(1.1f, -1.1f0.0f);  // Bottom Right
            gl.glTexCoord2f(1.0f1.0f);
            gl.glVertex3f(1.1f1.1f0.0f);  // Top Right
            gl.glTexCoord2f(0.0f1.0f);
            gl.glVertex3f(-1.1f1.1f0.0f);  // Top Left
            gl.glEnd();  // Done Drawing The Quad
        else  // Otherwise
        {
            if (maskingEnabled)  // Is Masking On?
            {
                // Select The First Mask Texture
                gl.glBindTexture(GL.GL_TEXTURE_2D, textures[1]);  
                
                // Start Drawing A Textured Quad
                gl.glBegin(GL.GL_QUADS);              
                gl.glTexCoord2f(roll + 0.0f0.0f);
                gl.glVertex3f(-1.1f, -1.1f0.0f);  // Bottom Left
                gl.glTexCoord2f(roll + 4.0f0.0f);
                gl.glVertex3f(1.1f, -1.1f0.0f);  // Bottom Right
                gl.glTexCoord2f(roll + 4.0f4.0f);
                gl.glVertex3f(1.1f1.1f0.0f);  // Top Right
                gl.glTexCoord2f(roll + 0.0f4.0f);
                gl.glVertex3f(-1.1f1.1f0.0f);  // Top Left
                gl.glEnd();  // Done Drawing The Quad
            }

            gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE)// Copy Image 1 Color To The Screen
            
            // Select The First Image Texture
            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[2]);
            gl.glBegin(GL.GL_QUADS);    // Start Drawing A Textured Quad
            gl.glTexCoord2f(roll + 0.0f0.0f);
            gl.glVertex3f(-1.1f, -1.1f0.0f);  // Bottom Left
            gl.glTexCoord2f(roll + 4.0f0.0f);
            gl.glVertex3f(1.1f, -1.1f0.0f);  // Bottom Right
            gl.glTexCoord2f(roll + 4.0f4.0f);
            gl.glVertex3f(1.1f1.1f0.0f);  // Top Right
            gl.glTexCoord2f(roll + 0.0f4.0f);
            gl.glVertex3f(-1.1f1.1f0.0f);  // Top Left
            gl.glEnd();    // Done Drawing The Quad
        }

        gl.glEnable(GL.GL_DEPTH_TEST);  // Enable Depth Testing
        gl.glDisable(GL.GL_BLEND);  // Disable Blending

        roll += 0.002f;    // Increase Our Texture Roll Variable
        if (roll > 1.0f)  // Is Roll Greater Than One
        {
            roll -= 1.0f;  // Subtract 1 From Roll
        }
    }

    public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) {
        if (h == 0h = 1;
        GL gl = glDrawable.getGL();

        // Reset The Current Viewport And Perspective Transformation
        gl.glViewport(00, w, h);                       
        gl.glMatrixMode(GL.GL_PROJECTION);  // Select The Projection Matrix
        gl.glLoadIdentity();                // Reset The Projection Matrix
        
        // Calculate The Aspect Ratio Of The Window
        glu.gluPerspective(45.0f(floatw / (floath, 0.1f100.0f)
        
        gl.glMatrixMode(GL.GL_MODELVIEW);  // Select The Modelview Matrix
        gl.glLoadIdentity();               // Reset The ModalView Matrix
    }

    public void displayChanged(GLAutoDrawable glDrawable, boolean b, boolean b1) {
    }
}

 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.