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: 4100
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:
 
 
 
Demonstration of using glBindTexture by creating and managing two textures E-mail
User Rating: / 7
PoorBest 

This program demonstrates using glBindTexture() by creating and managing two textures. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook11;

import java.awt.event.*;
import java.nio.ByteBuffer;

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

/**
 * This program demonstrates using glBindTexture() by creating and managing two
 * textures.
 
 @author Kiet Le (Java port)
 */
public class texbind
  extends JFrame
    implements GLEventListener, KeyListener
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private GLU glu;
  private GLUT glut;
  //
  private static final int rgba = 4;
  private static final int checkImageWidth = 64;
  private static final int checkImageHeight = 64;
  // private byte checkImage[][][];
  // private byte otherImage[][][];
  private ByteBuffer checkImageBuf = //
  BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
  private ByteBuffer otherImageBuf = //
  BufferUtil.newByteBuffer(checkImageWidth * checkImageHeight * rgba);
  private int[] texName = new int[2];

  public texbind()
  {
    super("texbind");

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

    getContentPane().add(canvas);
  }

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

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

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    glu = new GLU();

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

    makeCheckImages();

    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);

    gl.glGenTextures(2, texName, 0);
    gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
        GL.GL_NEAREST);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
        GL.GL_NEAREST);
    gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth,
        checkImageHeight, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, checkImageBuf);

    gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
        GL.GL_NEAREST);
    gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
        GL.GL_NEAREST);
    gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
    gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, checkImageWidth,
        checkImageHeight, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, otherImageBuf);
    gl.glEnable(GL.GL_TEXTURE_2D);
  }

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

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glBindTexture(GL.GL_TEXTURE_2D, texName[0]);
    gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2d(0.00.0);
    gl.glVertex3d(-2.0, -1.00.0);
    gl.glTexCoord2d(0.01.0);
    gl.glVertex3d(-2.01.00.0);
    gl.glTexCoord2d(1.01.0);
    gl.glVertex3d(0.01.00.0);
    gl.glTexCoord2d(1.00.0);
    gl.glVertex3d(0.0, -1.00.0);
    gl.glEnd();
    gl.glBindTexture(GL.GL_TEXTURE_2D, texName[1]);
    gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2d(0.00.0);
    gl.glVertex3d(1.0, -1.00.0);
    gl.glTexCoord2d(0.01.0);
    gl.glVertex3d(1.01.00.0);
    gl.glTexCoord2d(1.01.0);
    gl.glVertex3d(2.414211.0, -1.41421);
    gl.glTexCoord2d(1.00.0);
    gl.glVertex3d(2.41421, -1.0, -1.41421);
    gl.glEnd();
    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(60.0(floatw / (floath, 1.030.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslated(0.00.0, -3.6);
  }

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

  private void makeCheckImages()
  {
    byte c = 0x00;

    for (int i = 0; i < checkImageWidth; i++)
    {
      for (int j = 0; j < checkImageHeight; j++)
      {

        c = (byte) ((((byte) ((i & 0x8== 0x00 0xff)//
        (byte) ((j & 0x8== 0x00 0xff))));
        // checkImage[i][j][0] = (byte) c;
        // checkImage[i][j][1] = (byte) c;
        // checkImage[i][j][2] = (byte) c;
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
        checkImageBuf.put((byte0xff);

        c = (byte) ((((byte) ((i & 0x10== 0x00 0xff)//
        (byte) ((j & 0x10== 0x00 0xff))));

        otherImageBuf.put((bytec);
        otherImageBuf.put((byte0);
        otherImageBuf.put((byte0);
        otherImageBuf.put((byte0xff);
      }
    }
    checkImageBuf.rewind();
    otherImageBuf.rewind();
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    switch (key.getKeyCode()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
      default:
        break;
    }
  }

  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.