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: 4101
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:
 
 
 
Flag effect (waving texture) - NeHe Tutorial JOGL Port E-mail
User Rating: / 10
PoorBest 

This example shows how to bend fold and manipulate your textures.

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

import demos.common.GLDisplay;

/**
 @author Pepijn Van Eeckhoudt
 */
public class Lesson11 {
    public static void main(String[] args) {
        final GLDisplay neheGLDisplay = GLDisplay.createGLDisplay("Lesson 11: Flag effect");
        Renderer renderer = new Renderer();
        neheGLDisplay.addGLEventListener(renderer);
        neheGLDisplay.start();
    }
}


package demos.nehe.lesson11;

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 {
    
    // The Array For The Points On The Grid Of Our "Wave"
    float[][][] points = new float[45][45][3];    
    int wiggle_count = 0;   // Counter Used To Control How Fast Flag Waves
    float xrot;             // X Rotation ( NEW )
    float yrot;             // Y Rotation ( NEW )
    float zrot;             // Z Rotation ( NEW )
    float hold;
    int[] textures = new int[1]//Storage for one texture ( NEW )

    private GLU glu = new GLU();

    private void loadGLTextures(GLAutoDrawable gldrawablethrows IOException {
        TextureReader.Texture texture = null;
        texture = TextureReader.readTexture("demos/data/images/tim.png");

        GL gl = gldrawable.getGL();

        //Create Nearest Filtered Texture
        gl.glGenTextures(1, textures, 0);
        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,
                0,
                3,
                texture.getWidth(),
                texture.getHeight(),
                0,
                GL.GL_RGB,
                GL.GL_UNSIGNED_BYTE,
                texture.getPixels());
    }

    public void init(GLAutoDrawable glDrawable) {
        try {
            loadGLTextures(glDrawable);
        catch (IOException e) {
            System.out.println("Failed to load Textures,Bailing!");
            throw new RuntimeException(e);
        }

        GL gl = glDrawable.getGL();
        gl.glEnable(GL.GL_TEXTURE_2D);       // Enable Texture Mapping ( NEW )
        gl.glShadeModel(GL.GL_SMOOTH);             // Enables Smooth Color Shading
        gl.glClearColor(0.0f0.0f0.0f0.0f);   // This Will Clear The Background Color To Black
        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);  

        for (int x = 0; x < 45; x++) {
            for (int y = 0; y < 45; y++) {
                points[x][y][0((x / 5.0f4.5f);
                points[x][y][1((y / 5.0f4.5f);
                points[x][y][2(float
                        (Math.sin((((x / 5.0f40.0f360.0f3.141592654 2.0f));
            }
        }
    }

    public void display(GLAutoDrawable glDrawable) {
        GL gl = glDrawable.getGL();
        int x, y;
        float float_x, float_y, float_xb, float_yb;

        // 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, -12.0f);

        gl.glRotatef(xrot, 1.0f0.0f0.0f);
        gl.glRotatef(yrot, 0.0f1.0f0.0f);
        gl.glRotatef(zrot, 0.0f0.0f1.0f);

        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);

        gl.glBegin(GL.GL_QUADS);
        for (x = 0; x < 44; x++) {
            for (y = 0; y < 44; y++) {
                float_x = (float) (x44.0f;
                float_y = (float) (y44.0f;
                float_xb = (float) (x + 144.0f;
                float_yb = (float) (y + 144.0f;

                gl.glTexCoord2f(float_x, float_y);
                gl.glVertex3f(points[x][y][0], points[x][y][1], points[x][y][2]);

                gl.glTexCoord2f(float_x, float_yb);
                gl.glVertex3f(points[x][y + 1][0], points[x][y + 1][1], points[x][y + 1][2]);

                gl.glTexCoord2f(float_xb, float_yb);
                gl.glVertex3f(points[x + 1][y + 1][0], points[x + 1][y + 1][1]
                        points[x + 1][y + 1][2]);

                gl.glTexCoord2f(float_xb, float_y);
                gl.glVertex3f(points[x + 1][y][0], points[x + 1][y][1], points[x + 1][y][2]);
            }
        }
        gl.glEnd();

        if (wiggle_count == 2) {
            for (y = 0; y < 45; y++) {
                hold = points[0][y][2];
                for (x = 0; x < 44; x++) {
                    points[x][y][2= points[x + 1][y][2];
                }
                points[44][y][2= hold;
            }
            wiggle_count = 0;
        }

        wiggle_count++;

        xrot += 0.3f;
        yrot += 0.2f;
        zrot += 0.4f;
    }

    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.