PDA

View Full Version : Using GL_QUADS does not generate a rectangle



macromate
07-07-2010, 02:24 PM
Hey, i'm currently trying to learn OpenGL and therefore I want to write a extremly simple pong game. I'm using Mac OSX 10.6.4 and Cocoa. This is what the output of the code looks like:
http://img257.imageshack.us/img257/5677/glquads.png
And this is my code:

@implementation OpenGLView
- (void)prepareOpenGL {
glClearColor(0, 0, 0, 1);
}

- (void)reshape {
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
}

- (void)drawRect:(NSRect)dirtyRect {
CGFloat width = self.bounds.size.width;
CGFloat height = self.bounds.size.height;

glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1, 1, 1);
glBegin(GL_LINES);
{
glVertex2f(0, height/2.0);
glVertex2f(width, height/2.0);
}
glEnd();

glBegin(GL_QUADS);
{
glVertex2f(paddle1.offset, 0);
glVertex2f(paddle1.offset + paddle1.width, 0);
glVertex2f(paddle1.offset, paddle1.height);
glVertex2f(paddle1.offset + paddle1.width, paddle1.height);
}
glEnd();

glBegin(GL_QUADS);
{
glVertex2f(100, 100);
glVertex2f(200, 100);
glVertex2f(100, 200);
glVertex2f(200, 200);
}
glEnd();
glFlush();
}

@end


I think this is not supposed to look like this, I just want a simple rectangle. Can somebody please tell me what I'm doing wrong?

thank you!

Rosario Leonardi
07-07-2010, 02:31 PM
You must send the vertex in counter clockwise order.


A--------D
| |
| |
B--------C

OpenGl automatically split the quad in two triangles

A B C

A C D

macromate
07-07-2010, 02:42 PM
Thank you very much. I guess that was a really stupid mistake.