java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow JOGL arrow Vertex Arrays in JOGL
 
 
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: 4093
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:
 
 
 
Vertex Arrays in JOGL E-mail
User Rating: / 15
PoorBest 

Vertex arrays can greatly improve rendering speed. The primary advantage is decreasing the number of subroutine calls required to transfer non-display listed geometry data to the GL. A secondary advantage is improving the efficiency of the transfer; especially to allow direct memory access (DMA) hardware to be used to effect the transfer.

This program below demonstrates usage of vertex arrays in JOGL. 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.*;
import java.awt.event.*;

import javax.swing.*;
import java.io.*;
import java.nio.*;

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

import sun.security.provider.certpath.Vertex;

import com.sun.opengl.util.*;

/**
 * This program demonstrates vertex arrays.
 
 @author Kiet Le (Java conversion)
 */

public class varray
  extends JFrame
    implements GLEventListener, KeyListener, MouseListener
{
  // private GL gl;
  private GLU glu;
  private GLUT glut;
  private GLCapabilities caps;
  private GLCanvas canvas; 
  private static int POINTER = 1;
  private static int INTERLEAVED = 2;

  private static int DRAWARRAY = 1;
  private static int ARRAYELEMENT = 2;
  private static int DRAWELEMENTS = 3;

  private static int setupMethod = POINTER;
  private static int derefMethod = DRAWARRAY;
  private static IntBuffer verticesBuf;
  private static FloatBuffer colorsBuf;
  private static FloatBuffer intertwinedBuf;
  Screenshot capture;

  //
  public varray()
  {
    super("varray");
    //
    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);
    canvas.addMouseListener(this);
    //
    getContentPane().add(canvas);
  }

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

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

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();  
    glu = new GLU();
    glut = new GLUT();
    //
    gl.glClearColor(0.0f0.0f0.0f0.0f);
    gl.glShadeModel(GL.GL_SMOOTH);
    setupPointers(gl);
  }

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

    if (derefMethod == DRAWARRAY)
    {
      gl.glDrawArrays(GL.GL_TRIANGLES, 06);
    }
    else if (derefMethod == ARRAYELEMENT)
    {
      gl.glBegin(GL.GL_TRIANGLES);
      gl.glArrayElement(2);
      gl.glArrayElement(3);
      gl.glArrayElement(5);
      gl.glEnd();
    }
    else if (derefMethod == DRAWELEMENTS)
    {
      int indices[] new int[]
      013};
      IntBuffer indicesBuf = BufferUtil.newIntBuffer(indices.length);
      for (int i = 0; i < indices.length; i++)
        indicesBuf.put(indices[i]);
      indicesBuf.rewind();
      gl.glDrawElements(GL.GL_POLYGON, 4, GL.GL_UNSIGNED_INT, indicesBuf);
    }
    gl.glFlush();

    // gl calls from C example's mouse routine are moved here
    if (setupMethod == INTERLEAVEDsetupInterleave(gl);
    else setupPointers(gl);
  }

  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.gluOrtho2D(0.0(doublew, 0.0(doubleh);
    gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
  }

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

  private void setupPointers(GL gl)
  {
    int vertices[] new int[]
    25251003251752517532525025325325 };
    float colors[] new float[]
    1.0f0.2f0.2f0.2f0.2f1.0f0.8f1.0f0.2f0.75f0.75f,
      0.75f0.35f0.35f0.35f0.5f0.5f0.5f };
    IntBuffer tmpVerticesBuf = BufferUtil.newIntBuffer(vertices.length);
    FloatBuffer tmpColorsBuf = BufferUtil.newFloatBuffer(colors.length);
    for (int i = 0; i < vertices.length; i++)
      tmpVerticesBuf.put(vertices[i]);
    for (int j = 0; j < colors.length; j++)
      tmpColorsBuf.put(colors[j]);
    tmpVerticesBuf.rewind();
    tmpColorsBuf.rewind();
    //
    gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL.GL_COLOR_ARRAY);
    //
    gl.glVertexPointer(2, GL.GL_INT, 0, tmpVerticesBuf);
    gl.glColorPointer(3, GL.GL_FLOAT, 0, tmpColorsBuf);
    this.verticesBuf = tmpVerticesBuf;
    this.colorsBuf = tmpColorsBuf;

  }

  private void setupInterleave(GL gl)
  {
    float intertwined[] new float[]
    1.0f0.2f1.0f100.0f100.0f0.0f1.0f0.2f0.2f0.0f200.0f,
      0.0f1.0f1.0f0.2f100.0f300.0f0.0f0.2f1.0f0.2f200.0f,
      300.0f0.0f0.2f1.0f1.0f300.0f200.0f0.0f0.2f0.2f1.0f,
      200.0f100.0f0.0f };
    FloatBuffer tmpIntertwinedBuf = BufferUtil
        .newFloatBuffer(intertwined.length);
    tmpIntertwinedBuf.rewind();
    //
    gl.glInterleavedArrays(GL.GL_C3F_V3F, 0, tmpIntertwinedBuf);
    this.intertwinedBuf = tmpIntertwinedBuf;
  }

  public void keyTyped(KeyEvent key)
  
  }

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

      default:
        break;
    }
  }

  public void keyReleased(KeyEvent key)
  
  }

  public void mouseClicked(MouseEvent mouse)
  {  
  }

  public void mousePressed(MouseEvent mouse)
  {
    if (mouse.getButton() == MouseEvent.BUTTON1)
    {
      if (setupMethod == POINTER)
      {
        setupMethod = INTERLEAVED;
        // setupInterleave(gl);don't call
      }
      else if (setupMethod == INTERLEAVED)
      {
        setupMethod = POINTER;
        // setupPointers(gl);
      }
      // validate();
    }
    if (mouse.getButton() == MouseEvent.BUTTON2
        || mouse.getButton() == MouseEvent.BUTTON3)
    {
      if (derefMethod == DRAWARRAYderefMethod = ARRAYELEMENT;
      else if (derefMethod == ARRAYELEMENTderefMethod = DRAWELEMENTS;
      else if (derefMethod == DRAWELEMENTSderefMethod = DRAWARRAY;
      // validate();
    }
    canvas.display();
  }

  public void mouseReleased(MouseEvent mouse)
  {
  }

  public void mouseEntered(MouseEvent mouse)
  {
  }

  public void mouseExited(MouseEvent mouse)
  {
  }

}

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.