1

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.

enter image description here

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.

enter image description here

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());
genpfault
  • 51,148
  • 11
  • 85
  • 139
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • Can you post your viewport creation code? I can't tell if the triangle is getting cut off by your near frustum or maybe far frustum or even a combination of both. I think you are seeing the inside of the pyramid in your second picture, and the back of the world in the black spot of the first picture. However, that seems counter intuative since moving further away should result in what you get in the first picture and you should see the inside of the pyramid if you move closer. Also, do you have any other transforms? And what vert parameter is mapped to which color? X->Red, Y->Green? – zero298 Sep 11 '13 at 03:06
  • @zero298 I've included the code for initializing opengl. Also I'm not having any shaders. I've got it working with changing the glOrtho function but I still didn't understand why. – Sri Harsha Chilakapati Sep 11 '13 at 03:13

1 Answers1

2

glOrtho sets your viewing area. Since your shape fits inside of 1 square unit, but you want to be able to see the whole thing, you can either have your near and far set to enclose the whole shape (which the 2, -2 do) or you can set your glOrtho to something like glOrtho(-1, 1, -1, 1, 0, -5); and translate your shape backwards so that it is then in the viewing field.

Another (bad) option is to change the absolute position of your verts so that they fall within the near and far fields but I wouldn't recomend that since that is the point of the translates and transforms in general.

Do you mean to have everything in an orthographic view or do you mean to have perspective? The orthographic view is why translating didn't help originally.

zero298
  • 25,467
  • 10
  • 75
  • 100
  • On a side note, it looks like depth testing is not enabled. That should probably be enabled to; remember to add the depth buffer bit to glClear then, i.e. `glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);` – datenwolf Sep 11 '13 at 09:15