PDA

View Full Version : OpenGL in draw rect




Duke Leto
Jul 18, 2009, 09:43 AM
I have a circle drawn in an NSOpenGL view, but the OpenGL units and the frame units are very different.

Given a square size, how can I make a circle to fit the frame? It seems like a guessing game!

I am very new to OpenGL, as I started to figure some things out just yesterday, so go easy on me please :D

Almost forgot, here's the code:

void glCircle3i3d(GLint x, GLint y, GLint rad, GLdouble red, GLdouble green, GLdouble blue) {
float angle;
glPushMatrix();
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glLineWidth(1.0f);
glBegin(GL_POLYGON);
glColor3d(red, green, blue);
for(int i = 0; i < 100; i++) {
angle = i*2*M_PI/100;
glVertex2f(x + (cos(angle) * rad), y + (sin(angle) * rad));
}
glEnd();
glEnable(GL_TEXTURE_2D);
glPopMatrix();
}

- (void)drawRect:(NSRect)rect
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // Reset the current modelview matrix
glCircle3i3d(0, 0, rect.size.width/2, myColor.red, myColor.green,
myColor.blue);
[[self openGLContext] flushBuffer];
}



autorelease
Jul 18, 2009, 04:00 PM
Are you using orthographic or perspective projection?

If you're using perspective projection, you won't be able to make a circle fit the frame without some very serious math.

If this is a 2D application, you should use orthographic projection, because you can make pixel coordinates correspond easily to world coordinates. If you set up your projection matrix with
gluOrtho2D(0.0, viewWidth, 0.0, viewHeight);
then world coordinates will be identical to pixel coordinates.

OpenGL is not easy. It can take skilled programmers months or even years to master it.