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 use glColorTable|EXT function in JOGL E-mail
User Rating: / 1
PoorBest 

Invert a passed block of pixels. This program illustrates the use of the glColorTable|EXT() function. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").


Image

-> Copyright and Permission Notice

package glredbook12x;

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

import java.io.*;
import java.nio.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

import com.sun.opengl.util.*;

/**
 * Invert a passed block of pixels. This program illustrates the use of the
 * glColorTable|EXT() function.
 
 @author Kiet Le (java port)
 */
public class colortable
  extends JFrame
    implements GLEventListener//
    , KeyListener //
// , MouseListener //
// , MouseMotionListener //
// , MouseWheelListener //
{
  private GLCapabilities caps;
  private GLCanvas canvas;

  //
  private ByteBuffer pixels;
  // private int width; not reference as params...
  // private int height;...as are all Java primitives
  private Dimension dim = new Dimension(00);

  public colortable()
  {
    super("colortable");

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

    add(canvas);
  }

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

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

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

    pixels = readImage("Data/leeds.bin", dim);
    System.out.println(pixels.toString());

    // byte colorTable[][] = new byte[256][3];
    ByteBuffer colorTableBuf = BufferUtil.newByteBuffer(256 3);

    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    gl.glClearColor(0.0f0.0f0.0f0.0f);

    /* Set up an inverse color table */

    for (int i = 0; i < 256; ++i)
    {
      // colorTable[i][0] = (byte)(255 - i);
      // colorTable[i][1] = (byte)(255 - i);
      // colorTable[i][2] = (byte)(255 - i);
      colorTableBuf.put((byte) (255 - i));
      colorTableBuf.put((byte) (255 - i));
      colorTableBuf.put((byte) (255 - i));
    }
    colorTableBuf.rewind();

    if (gl.isExtensionAvailable("GL_ARB_imaging"))
    {
      if (gl.isFunctionAvailable("glColorTable"))
      {
      gl.glColorTable(GL.GL_COLOR_TABLE, GL.GL_RGB, 256//
          GL.GL_RGB, GL.GL_UNSIGNED_BYTE, colorTableBuf);
      }
      else {
        gl.glColorTableEXT(GL.GL_COLOR_TABLE, GL.GL_RGB, 256//
          GL.GL_RGB, GL.GL_UNSIGNED_BYTE, colorTableBuf);
      }
      gl.glEnable(GL.GL_COLOR_TABLE);
    }
    else
    {
      String msg = "GL_ARB_imaging (optional) subset is is not available.";
      setTitle(getTitle() " GL_ARB_imaging not available :(");
      System.err.println(msg);
      add(new JLabel(msg), BorderLayout.NORTH);
      SwingUtilities.updateComponentTreeUI(this);    }

  }

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

    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    gl.glRasterPos2i(11);
    gl.glDrawPixels(dim.width, dim.height, //
        GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);

    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();
    gl.glOrtho(0, w, 0, h, -1.01.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
  }

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

  /**
   * Reads an image from an archived file and return it as ByteBuffer object.
   
   @author Mike Butler, Kiet Le
   */
  private ByteBuffer readImage(String filename, Dimension dim)
  {
    if (dim == nulldim = new Dimension(00);
    ByteBuffer bytes = null;
    try
    {
      DataInputStream dis = new DataInputStream(getClass().getClassLoader()
          .getResourceAsStream(filename));
      dim.width = dis.readInt();
      dim.height = dis.readInt();
      System.out.println("Creating buffer, width: " + dim.width + " height: "
                         + dim.height);
      // byte[] buf = new byte[3 * dim.height * dim.width];
      bytes = BufferUtil.newByteBuffer(* dim.width * dim.height);
      for (int i = 0; i < bytes.capacity(); i++)
      {
        bytes.put(dis.readByte());
      }
      dis.close();

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    bytes.rewind();
    return bytes;
  }

  public void keyTyped(KeyEvent key)
  {
  }

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

      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;

      default:
        System.out.println("nothing pressed.");
        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.