1

I'm trying to make shapes with OpenGL in LWJGL but every time I do it, it makes this triangle dent in it.

The Code:

//The Window Is 800 Wide And 600 Tall
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(800, 0);
GL11.glVertex2i(0, 600);
GL11.glVertex2i(800, 600);
GL11.glEnd();

The Result:

The OpenGL Fail

Also, the code was a lot bigger then that. I just put the code that draws the shape.

3 Answers3

5

Order of operations does matter in OpenGL. This code goes as follows:

Put down a "pencil" (I suggest you do this with a real pencil on an actual piece of paper, while reading this, i.e. following the instructions, put down the pencil and do the following movements without lifting it) – Draw a quad

GL11.glBegin(GL11.GL_QUADS);

First corner bottom left

GL11.glVertex2i(0, 0);

Second corner bottom right

GL11.glVertex2i(800, 0);

Thid corner top left

GL11.glVertex2i(0, 600);

Fourth corner top right

GL11.glVertex2i(800, 600);

Finish quad by returning with the pencil to the first point.

GL11.glEnd();

OpenGL expects you to deliver it convex geometry with a consistent winding, i.e. the vertices are drawn in clockwise or counterclockwise direction. You're switching directions inbetween, which makes the shape you define nonconvex.

I strongly suggest you keep to a counter clockwise winding:

  1. Bottom Left
  2. Bottom Right
  3. Top Right
  4. Top Left

That's also how you'd draw a quad with a pencil.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
4

Try to swap these two:

 GL11.glVertex2i(0, 600);
 GL11.glVertex2i(800, 600);

So yo have this:

GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(800, 0);
GL11.glVertex2i(800, 600);
GL11.glVertex2i(0, 600);
GL11.glEnd();

So , in your code the vertex order in the quad is : top left , top right , bottom left , bottom right.Which is not a convex shape. The correct order is : top left , top right , bottom right, bottom left.

Michael IV
  • 11,016
  • 12
  • 92
  • 223
0

Fixed it. I draw top left, top right, bottom right, bottom left. Not top left, top right, bottom left, bottom right.

  • So that is what I proposed you :) Your coordinates were : top left , top right , bottom left , bottom right. – Michael IV Dec 24 '12 at 10:55
  • FYI: Most OpenGL projection setups assume the origin, in the lower left corner, so positive Y values go up! – datenwolf Dec 24 '12 at 12:42