Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
I am working on an example out of the OpenGL super bible and I am having some problems. I am trying to draw a rectangle/square on top of the background.
Here is my code:
Code:
- (void)drawRect:(NSRect)dirtyRect {
	//Clear the view with the current clearing color
	glClear(GL_COLOR_BUFFER_BIT);
	//Set the current drawing color to red
	//         R      G    B
	glColor3f(1.0f, 0.0f, 0.0f);
	//Draw a filled rectangle with the current color
	glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
	//Flush drawing commands
	glFlush();
	//Set up the rendering state
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

This is not working however. The book suggests I should use GLUT for my interface, however I like Cocoa. Does anyone know why this code is not working?
Thanks
Blake
 
Define 'not working'

Doesn't compile? Runs but doesn't draw what you want?

Dumb question, but have you already initialized OpenGL elsewhere in your program?
 
Dumb question, but have you already initialized OpenGL elsewhere in your program?

Not a dumb question, first thing I thought of.

This appears to be a subclass of NSView or some other NSView subclass. Generally one uses CGContext**** commands to draw in this method...
 
Yes, OpenGL is imported. The code works, but doesn't display the proper thing. It should display a small rectangle. It does not. I know the OpenGL is working because I can draw my background. Please help and thanks!
 
Yes, OpenGL is imported. The code works, but doesn't display the proper thing. It should display a small rectangle. It does not. I know the OpenGL is working because I can draw my background. Please help and thanks!

Your projection or modelview matrix is wrong.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.