Vertex Array Problem - You know you love them

First, I would just like to say that after I found out how to get JOGL workin in Eclipse (Who knew .jar files could be .zip’s too?!! lol) I’ve been having a blast making nothing useful at all. I’ve found these forums very helpful, you guys are definately a daily stop for me.

Anyway, here’s what i’ve got cooking at the moment: It plays the chaos game in 3D (or it did until I broke it).
http://math.bu.edu/DYSYS/chaos-game/node1.html#SECTION00010000000000000000

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import javax.imageio.*;

import net.java.games.jogl.*;
import net.java.games.jogl.util.*;

/*
 * I have used code from Neon Helium Productions!, Lesson 06.
 * Converted to Java by Kevin J. Duling.
 */


public class chaosRenderer implements GLEventListener, KeyListener
{
    private float xRot = 0.0f;
    private float yRot = 0.0f;
    private float zRot = 0.0f;
    private float zOffset = 0.0f;
    private float xOffset = 0.0f;
	
    public chaosObject[] chaosObjArray = new chaosObject[1];
	
    private GL              gl;
    private GLDrawable      glDrawable;
    public Animator animator = null;
    private FloatBuffer chaosBuffer;


   public void init(GLDrawable drawable)
   {
       this.gl = drawable.getGL();
       this.glDrawable = drawable;
       drawable.setGL( new DebugGL(drawable.getGL() ));

       gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
       gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
       glDrawable.addKeyListener(this);
       gl.glEnableClientState(GL.GL_VERTEX_ARRAY);

       System.out.println("Init GL is " + gl.getClass().getName());
   }

   public void display(GLDrawable drawable)
   {
       gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
       gl.glLoadIdentity();
 
       gl.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
       gl.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
       gl.glRotatef(zRot, 0.0f, 0.0f, 1.0f);

       gl.glBegin( GL.GL_POINTS );
       	gl.glColor3f(1.0f, 1.0f, 1.0f);
       	gl.glDrawArrays(GL.GL_POINTS, 0, this.chaosObjArray[0].points.length);
       gl.glEnd();
       gl.glFlush();
   }

   public void reshape(GLDrawable drawable, int x, int y, int width, int height)
   {
   }

   public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged)
   {
   }
   
   /** Invoked when a key has been pressed.
    * See the class description for {@link KeyEvent} for a definition of
    * a key pressed event.
    * @param e The KeyEvent.
    */
   public void keyPressed(KeyEvent e)
   {
     if (e.getKeyCode() == KeyEvent.VK_NUMPAD7) this.xRot -= 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_NUMPAD1) this.xRot += 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_NUMPAD8) this.yRot -= 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_NUMPAD2) this.yRot += 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_NUMPAD9) this.zRot -= 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_NUMPAD3) this.zRot += 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_LEFT) this.xOffset -= 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_RIGHT) this.xOffset += 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_UP) this.zOffset -= 1.0f;
     else if(e.getKeyCode() == KeyEvent.VK_DOWN) this.zOffset += 1.0f;

   }
   
   /** Invoked when a key has been released.
    * See the class description for {@link KeyEvent} for a definition of
    * a key released event.
    * @param e The KeyEvent.
    */
   public void keyReleased(KeyEvent e) {}
   public void keyTyped(KeyEvent e) {}
   
   public chaosRenderer(){
	    Frame frame = new Frame("Chaos Renderer");
	    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
	    canvas.addGLEventListener(this);
	    frame.add(canvas);
	    frame.setSize(640, 480);
	    animator = new Animator(canvas); 
	    
	    frame.addWindowListener(new WindowAdapter()
	    {
	      public void windowClosing(WindowEvent e)
	      {
	        animator.stop();
	        System.exit(0);
	      }
	    });
	    frame.show();
	    canvas.requestFocus();
	   
   }
   
   public void prepareBufferArray(int index){
	this.chaosBuffer = BufferUtils.newFloatBuffer(this.chaosObjArray[index].points.length);
	this.chaosBuffer.put(this.chaosObjArray[index].points);
		  
	gl.glVertexPointer(3, GL.GL_POINTS, 0, this.chaosBuffer);
   }
   
   public static void main(String[] args)  {
	    
	    chaosRenderer myChaosRenderer = new chaosRenderer();
	    
	    myChaosRenderer.chaosObjArray[0] = new chaosObject(0.5f, 2000);
	    myChaosRenderer.chaosObjArray[0].addHomingPoint(0.0f, 1.0f, 0.0f);
	    myChaosRenderer.chaosObjArray[0].addHomingPoint(-0.707f, -0.3f, 0.0f);
	    myChaosRenderer.chaosObjArray[0].addHomingPoint(0.707f, -0.3f, 0.0f);

	    myChaosRenderer.chaosObjArray[0].calculatePoints();
	    myChaosRenderer.prepareBufferArray(0);
	    
	    myChaosRenderer.animator.start();
	    //chaosRenderer.chaosObjArray[0].reveal();
	    
	  }
}

As the code stands, I get a NULL POINTER EXCEPTION in the prepareBufferArray() method. I think this is caused by the gl.glVertexPointer() command, because if I move that command into the display() method the program will run, but I am faced with a blank (black) screen.

Should this gl.glVertexPointer() command be in the display() method, or do I have it in the right place? Any thoughts on the blank screen?

the myChaosRenderer.calculatePoints() fills the public float[] myChaosRenderer.points with [x1, y1, z1, x2, y2, z2, …, xN, yN, zN] data, where N is the number specified at the end of the constructor. You also get to set the increment value (how far towards each of the homing points you move each time, see chaos game link above). I would like to plot this as Points, all the same color (maybe a green eggshell? lol).

I would like to take this data and buffer it so the program isn’t so slow once I start drawing the “object” and looking around it. The object is actually pretty cool, when I had it working by drawing each point in a FOR loop, but terribly slow.

I guess I’ll also need to add a camera somehow, but I haven’t even begun looking for that yet.

I know the code is terribly messy, and I am interested in any and all corrections/optimizations you guys offer. At the end, I would like to present this program to some engineer geeks (mandatory assignment), so any extra features you think would be cool I’m interested in as well. The only extra feature I can think of (and have sort of designed for) is to precalculate a bunch of chaosObjects with differing increment values, and then just pop those arrays into the buffer so you can see the object morph from a Sierpinski triangle to some sort of snowflake thing.

Thanks again guys!

All of your OpenGL calls must be done while one of your GLEventListener’s callback methods is active on the stack. Have you looked at tutorials on vertex arrays? Pepijn’s port of Nehe Lesson 45 at http://pepijn.fab4.be/nehe/ might be helpful though it deals with Vertex Buffer Objects rather than vertex arrays.