I have made some progress with this and the problem is not with the buffer objects but to do differences between the way OpenGL and Processing deal with the camera and perspective.
I am trying to display a simple cube using OpenGL commands. I found some code that jeffg was working on and I am trying to modify it to display a cube.
Unfortunately I just get a black screen and for the life of me I can't see the problem, hopefully someone else can.
Code:import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.BufferUtil;
import processing.core.PApplet;
import processing.opengl.*;
PGraphicsOpenGL pgl;
GL gl;
GLU glu;
float aspect, camZ;
// Must be arrays of size 1 for glGenBuffers
int[] vindex_vbo = new int[1];
int[] vertices_vbo = new int[1];
int[] colors_vbo = new int[1];
boolean dataInited = false;
void setup(){
size(400,400,OPENGL);
// aspect and camZ are integral to both p5 and GL camera settings
aspect=(float)width/(float)height;
// (but p5 includes them automatically, see "camera()"
camZ= (float) (2.0f * ((height/2.0f) / Math.tan(PI*60.0f/360.0f)));
}
void draw(){
pgl = (PGraphicsOpenGL) g;
gl = pgl.beginGL();
glu = pgl.glu;
gl.glClearColor(0,0,0,0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glShadeModel(GL.GL_SMOOTH);
// tell GL that the camera is being set up now:
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
// Equivalent to P5 perspective()
glu.gluPerspective(60.0, aspect, camZ/10.0, camZ*10.0);
// Equivalent to P5 camera()
glu.gluLookAt(0, 0, 200, 0, 0, 0, 0, 1, 0);
// tell GL the following rotations will be applied to models, not the camera
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
// gl.glEnable(GL.GL_BLEND);
if(dataInited == false)
initData(gl);
else
drawBox(gl);
pgl.endGL();
}
public void initData(GL gl){
FloatBuffer pos = BufferUtil.newFloatBuffer( 24 );
float[] verts = new float[] {
-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
-1, -1, -1,
1, -1, -1,
1, 1, -1,
-1, 1, -1 };
for(int i = 0; i < verts.length; i++){
verts[i] *= 10.0f;
pos.put(verts[i]);
}
gl.glGenBuffers( 1, vertices_vbo, 0 );
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vertices_vbo[0] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, 8 * 3 * BufferUtil.SIZEOF_FLOAT, pos, GL.GL_STATIC_DRAW );
pos = null;
verts = null;
FloatBuffer cols = BufferUtil.newFloatBuffer( 72 );
for(int i = 0; i < 24; i++){
cols.put(random(1.0f));
cols.put(random(1.0f));
cols.put(1.0f);
}
gl.glGenBuffers( 1, colors_vbo, 0 );
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, colors_vbo[0] );
gl.glBufferData( GL.GL_ARRAY_BUFFER, 24 * 3 * BufferUtil.SIZEOF_FLOAT, cols, GL.GL_STATIC_DRAW );
cols = null;
IntBuffer faces = BufferUtil.newIntBuffer( 24 );
int[] facePts = new int[] {
0,3,2,1,
5,6,7,4,
4,7,3,0,
1,2,6,5,
4,0,1,5,
3,7,6,2 };
faces.put(facePts);
gl.glGenBuffers( 1, vindex_vbo, 0 );
gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, vindex_vbo[0] );
gl.glBufferData( GL.GL_ELEMENT_ARRAY_BUFFER, 24 * BufferUtil.SIZEOF_INT, faces, GL.GL_STATIC_DRAW );
faces = null;
facePts = null;
dataInited = true;
}
public void drawBox(GL gl){
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, colors_vbo[0] );
gl.glColorPointer(3,GL.GL_FLOAT,0,0);
gl.glBindBuffer( GL.GL_ARRAY_BUFFER, vertices_vbo[0] );
gl.glVertexPointer(3,GL.GL_FLOAT,0,0);
gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, vindex_vbo[0] );
gl.glDrawElements( GL.GL_QUADS, 24, GL.GL_UNSIGNED_INT, 0 );
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);
}
public void finalize(){
System.out.println("Bye bye");
gl.glDeleteBuffers( 1, vertices_vbo, 0 );
gl.glDeleteBuffers( 1, colors_vbo, 0 );
gl.glDeleteBuffers( 1, vindex_vbo, 0 );
}