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: 4086
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 texture map a checkerboard image onto two rectangles E-mail
User Rating: / 14
PoorBest 

This program texture maps a checkerboard image onto two rectangles. This program clamps the texture, if the texture coordinates fall outside 0.0 and 1.0. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook10;

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 texture maps a checkerboard image onto two rectangles. This
 * program clamps the texture, if the texture coordinates fall outside 0.0 and
 * 1.0.
 
 @author Kiet Le
 */
public class checker
  extends JFrame
    implements GLEventListener, KeyListener
{
  private GLU glu; 
  private GLCapabilities caps;
  private GLCanvas canvas; 
  /* Create checkerboard texture */
  private static final int checkImageWidth = 64;
  private static final int checkImageHeight = 64;
  private static final int color = 3;
  private byte checkImage[][][] new byte[checkImageWidth][checkImageHeight][color];
  private ByteBuffer checkImageBuf = //
  BufferUtil.newByteBuffer(checkImageHeight * checkImageWidth * color);


  public checker()
  {
    super("checker");

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

    getContentPane().add(canvas);
  }

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

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

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

    gl.glClearColor(0.0f0.0f0.0f0.0f);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LESS);

    makeCheckImage();

    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, color, checkImageWidth, checkImageHeight, 0,
        GL.GL_RGB, GL.GL_UNSIGNED_BYTE, checkImageBuf);// checkImage[0][0][0]);
    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
    gl.glTexParameterf(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.glEnable(GL.GL_TEXTURE_2D);
    gl.glShadeModel(GL.GL_FLAT);
  }

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

    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
    gl.glBegin(GL.GL_QUADS);
    gl.glTexCoord2f(0.0f0.0f);
    gl.glVertex3f(-2.0f, -1.0f0.0f);
    gl.glTexCoord2f(0.0f1.0f);
    gl.glVertex3f(-2.0f1.0f0.0f);
    gl.glTexCoord2f(1.0f1.0f);
    gl.glVertex3f(0.0f1.0f0.0f);
    gl.glTexCoord2f(1.0f0.0f);
    gl.glVertex3f(0.0f, -1.0f0.0f);

    gl.glTexCoord2f(0.0f0.0f);
    gl.glVertex3f(1.0f, -1.0f0.0f);
    gl.glTexCoord2f(0.0f1.0f);
    gl.glVertex3f(1.0f1.0f0.0f);
    gl.glTexCoord2f(1.0f1.0f);
    gl.glVertex3f(2.41421f1.0f, -1.41421f);
    gl.glTexCoord2f(1.0f0.0f);
    gl.glVertex3f(2.41421f, -1.0f, -1.41421f);
    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.01.0 (floatw / (floath, 1.030.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslatef(0.0f0.0f, -3.6f);
  }

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

  /*
   * 3D array won't be used. I left it here for you to see.
   */
  private void makeCheckImage()
  {
    byte c = (byte0xFF;

    for (int i = 0; i < checkImageWidth; i++)
    {
      for (int j = 0; j < checkImageHeight; j++)
      {
        // c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
        c = (byte)( ( ((byte)((i & 0x8)==0?0x00:0xff)//
            ^(byte)((j & 0x8)==0?0x00:0xff))));

        checkImage[i][j][0(bytec;
        checkImage[i][j][1(bytec;
        checkImage[i][j][2(bytec;
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
        checkImageBuf.put((bytec);
      }
    }
    checkImageBuf.rewind();
  }//

  public void keyTyped(KeyEvent key)
  {
    // TODO Auto-generated method stub
  }

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

      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key)
  {
    // TODO Auto-generated method stub
  }

}

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.