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: 3947
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:
 
 
 
How to use cube map textures E-mail
User Rating: / 5
PoorBest 

This program demonstrates cube map textures. Six different colored checker board textures are created and applied to a lit sphere. Pressing the 'f' and 'b' keys translate the viewer forward and backward. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook1314;

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

import java.nio.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;

/**
 * This program demonstrates cube map textures. Six different colored checker
 * board textures are created and applied to a lit sphere. Pressing the 'f' and
 * 'b' keys translate the viewer forward and backward.
 
 @author Kiet Le (java port)
 */
public class cubemap
  extends JFrame
    implements GLEventListener//
    , KeyListener //
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private GLU glu;
  private GLUT glut;
  private static int imageSize = 4;
  // private static byte image1[][][] = new byte[imageSize][imageSize][4];
  // private static byte image2[][][] = new byte[imageSize][imageSize][4];
  // private static byte image3[][][] = new byte[imageSize][imageSize][4];
  // private static byte image4[][][] = new byte[imageSize][imageSize][4];
  // private static byte image5[][][] = new byte[imageSize][imageSize][4];
  // private static byte image6[][][] = new byte[imageSize][imageSize][4];
  private static ByteBuffer imageBuf1 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
  private static ByteBuffer imageBuf2 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
  private static ByteBuffer imageBuf3 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
  private static ByteBuffer imageBuf4 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
  private static ByteBuffer imageBuf5 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);
  private static ByteBuffer imageBuf6 //
  = BufferUtil.newByteBuffer(imageSize * imageSize * 4);

  private static double ztrans = 0.0;


  public cubemap()
  {
    super("cubemap");

    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);

    add(canvas);
  }

  public void run()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400400);
    setLocationRelativeTo(null);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  public static void main(String[] args)
  {
    new cubemap().run();
  }

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();
    glut = new GLUT();
 
    float diffuse[] =
    1.0f1.0f1.0f1.0f };

    gl.glClearColor(0.0f0.0f0.0f0.0f);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glShadeModel(GL.GL_SMOOTH);

    makeImages();
    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_S,
        GL.GL_REPEAT);
    gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_T,
        GL.GL_REPEAT);
    gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_WRAP_R,
        GL.GL_REPEAT);
    gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MAG_FILTER,
        GL.GL_NEAREST);
    gl.glTexParameteri(GL.GL_TEXTURE_CUBE_MAP, GL.GL_TEXTURE_MIN_FILTER,
        GL.GL_NEAREST);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf1);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf4);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf2);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf5);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf3);
    gl.glTexImage2D(GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL.GL_RGBA,
        imageSize, imageSize, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, imageBuf6);
    gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_NORMAL_MAP);
    gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_NORMAL_MAP);
    gl.glTexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, GL.GL_NORMAL_MAP);
    gl.glEnable(GL.GL_TEXTURE_GEN_S);
    gl.glEnable(GL.GL_TEXTURE_GEN_T);
    gl.glEnable(GL.GL_TEXTURE_GEN_R);

    gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_MODULATE);

    gl.glEnable(GL.GL_TEXTURE_CUBE_MAP);
    gl.glEnable(GL.GL_LIGHTING);
    gl.glEnable(GL.GL_LIGHT0);
    gl.glEnable(GL.GL_AUTO_NORMAL);
    gl.glEnable(GL.GL_NORMALIZE);
    gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, diffuse, 0);

  }

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

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

    gl.glPushMatrix();
    gl.glTranslatef(0.0f0.0f(floatztrans);
    glut.glutSolidSphere(5.02010);
    gl.glPopMatrix();

    gl.glFlush();

  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
 
    gl.glViewport(00, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(40.0(floatw / (floath, 1.0300.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f0.0f, -20.0f);
  }

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

  private void makeImages()
  {
    int c = 0;
    for (int i = 0; i < imageSize; i++)
    {
      for (int j = 0; j < imageSize; j++)
      {
        // c = ((((i&0x1)==0)^((j&0x1))==0))*255;
        c = (byte) ((((byte) ((i & 0x1== 0x00 0xff)//
        (byte) ((j & 0x1== 0x00 0xff))));
        imageBuf1.put((bytec);
        imageBuf1.put((bytec);
        imageBuf1.put((bytec);
        imageBuf1.put((byte255);

        imageBuf2.put((bytec);
        imageBuf2.put((bytec);
        imageBuf2.put((byte0);
        imageBuf2.put((byte255);

        imageBuf3.put((bytec);
        imageBuf3.put((byte0);
        imageBuf3.put((bytec);
        imageBuf3.put((byte255);

        imageBuf4.put((byte0);
        imageBuf4.put((bytec);
        imageBuf4.put((bytec);
        imageBuf4.put((byte255);

        imageBuf5.put((byte255);
        imageBuf5.put((bytec);
        imageBuf5.put((bytec);
        imageBuf5.put((byte255);

        imageBuf6.put((bytec);
        imageBuf6.put((bytec);
        imageBuf6.put((byte255);
        imageBuf6.put((byte255);
      }
    }
    imageBuf1.rewind();
    imageBuf2.rewind();
    imageBuf3.rewind();
    imageBuf4.rewind();
    imageBuf5.rewind();
    imageBuf6.rewind();
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyChar()) {

      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;
      case 'f':
        ztrans = ztrans - 0.2;
        break;
      case 'b':
        ztrans = ztrans + 0.2;
        break;
      default:
        break;
    }
    canvas.display();
  }

  public void keyReleased(KeyEvent key)
  {
  }

}

Source: Kiet Le's The Red Book Examples using JOGL


 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.