I'm learning OpenGL and I've got a problem with incorrect rendering. This is the problem I'm having, there are gaps between sides of pyramids.
Here are the vertices I'm using to construct.
// Create Pyramid Vertex Buffer
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(3 * 12);
vertexBuffer.put(new float[]
{
// Side one
0f, 0.8f, 0f,
-0.8f, -0.8f, 0.8f,
0.8f, -0.8f, 0.8f,
// Side two
0f, 0.8f, 0f,
0.8f, -0.8f, 0.8f,
0.8f, -0.8f, -0.8f,
// Side three
0f, 0.8f, 0f,
0.8f, -0.8f, -0.8f,
-0.8f, -0.8f, -0.8f,
// Side four
0f, 0.8f, 0f,
-0.8f, -0.8f, -0.8f,
-0.8f, -0.8f, 0.8f
});
vertexBuffer.rewind();
I thought that this was because the model comes out of screen. So I thought translating on the z-axis should be fine and I added this line before calling glDrawArrays
glTranslatef(0f, 0f, -1f);
This came up with this strange behaviour, having more sides being drawn.
The three sides are rendered but it is only supposed to view only two! How can I solve this problem? I have also enabled the depth test.
Here's how I initialize OpenGL
// Initialize OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, Display.getWidth(), Display.getHeight());