I’m trying to convert my glBegin->glEnd triangle stripped terrain to be drawn with a vertex buffer object but I’m having a bit of trouble. All of the examples I find online don’t really explain the setup too well and most are written in C/C++ so the JOGL differences aren’t spelled out at all. My app doesn’t crash or throw any exceptions at the moment, but it doesn’t display anything, either. Code to follow… any help would be appreciated.
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
GLU glu = new GLU();
renderer = new TextRenderer(new Font("Consolas", Font.BOLD, 15));
// Setup GL States
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
gl.glEnable(GL.GL_DEPTH_TEST); // Enable Depth Testing
gl.glShadeModel(GL.GL_SMOOTH); // Select Smooth Shading
// Set Perspective Calculations To Most Accurate
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
gl.glEnable(GL.GL_TEXTURE_2D); // Enable Textures
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Set The Color To White
}
public void drawTerrain(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
gl.glPushMatrix();
gl.glClear (GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
if( !vboIsSetup )
{
setupPointers(gl);
vboIsSetup = true;
}
gl.glEnableClientState( GL.GL_VERTEX_ARRAY );
gl.glEnableClientState( GL.GL_NORMAL_ARRAY );
gl.glEnableClientState( GL.GL_COLOR_ARRAY );
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vboIds[0]);
gl.glColorPointer(3, GL.GL_DOUBLE, 0, vboIds[0]);
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vboIds[0]);
gl.glNormalPointer(GL.GL_DOUBLE, 0, vboIds[0] + colorSize);
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vboIds[0]);
gl.glVertexPointer(3, GL.GL_DOUBLE, 0, vboIds[0] + normalSize);
gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, vboIds[1]);
gl.glDrawElements(GL.GL_TRIANGLE_STRIP, 1, GL.GL_UNSIGNED_INT, vboIds[1]);
// reset everything
gl.glBindBufferARB( GL.GL_ARRAY_BUFFER, 0 );
gl.glDisableClientState( GL.GL_VERTEX_ARRAY );
gl.glDisableClientState( GL.GL_NORMAL_ARRAY );
gl.glDisableClientState( GL.GL_COLOR_ARRAY );
gl.glPopMatrix();
}
private void setupPointers(GL gl)
{
colorSize = colors.size() * 3 * BufferUtil.SIZEOF_DOUBLE;
normalSize = normals.size() * 3 * BufferUtil.SIZEOF_DOUBLE;
vertexSize = vertices.size() * 3 * BufferUtil.SIZEOF_DOUBLE;
indexSize = indices.size() * BufferUtil.SIZEOF_INT;
vbo = ByteBuffer.allocate(colorSize + normalSize + vertexSize);
ibo = ByteBuffer.allocate(indexSize);
colorOffset = 0;
normalOffset = colorSize;
vertexOffset = normalSize;
for (int i = 0; i < indices.size(); i++)
{
ibo.putInt(indices.get(i));
}
for (int j = 0; j < colors.size(); j++)
{
vbo.putDouble(colors.get(j).x);
vbo.putDouble(colors.get(j).y);
vbo.putDouble(colors.get(j).z);
}
for (int j = 0; j < normals.size(); j++)
{
vbo.putDouble(normals.get(j).x);
vbo.putDouble(normals.get(j).y);
vbo.putDouble(normals.get(j).z);
}
for (int i = 0; i < vertices.size(); i++)
{
vbo.putDouble(vertices.get(i).getPosition().x);
vbo.putDouble(vertices.get(i).getPosition().y);
vbo.putDouble(vertices.get(i).getPosition().z);
}
vbo.rewind();
ibo.rewind();
gl.glEnableClientState( GL.GL_VERTEX_ARRAY );
gl.glEnableClientState( GL.GL_NORMAL_ARRAY );
gl.glEnableClientState( GL.GL_COLOR_ARRAY );
// Generate And Bind The VBO Buffer
gl.glGenBuffersARB(1, vboIds, 0); // Get A Valid Name
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vboIds[0]); // Bind The Buffer
// Load The Data
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, colorSize + normalSize + vertexSize, vbo, GL.GL_STATIC_DRAW_ARB);
// Generate And Bind The Index Buffer
gl.glGenBuffersARB(1, vboIds, 1); // Get A Valid Name
gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, vboIds[1]); // Bind The Buffer
// Load The Data
gl.glBufferDataARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, indexSize, ibo, GL.GL_STATIC_DRAW_ARB);
gl.glDisableClientState( GL.GL_VERTEX_ARRAY );
gl.glDisableClientState( GL.GL_NORMAL_ARRAY );
gl.glDisableClientState( GL.GL_COLOR_ARRAY );
//vbo.clear();
//ibo.clear();
}