|
This project is much simpler than the last one. We will only be implementing a few new methods and explaining only a few OpenGL calls.
First, we have defined a new variable for kGLView called drawing_type.
@interface kGLView : NSOpenGLView {
...
GLint drawing_type;
}
...
- (IBAction)setSolid:(id)sender;
- (IBAction)setWireframe:(id)sender;
- (IBAction)setPoints:(id)sender;
- (void) drawTriangle;
@end
|
We initialise drawing_type in the initGL method. You can put this anywhere you want in the initialization.
drawing_type = GL_TRIANGLES;
|
Now we will define a new method called drawTriangle. As you can imagine, it defines a triangle in OpenGL
- (void) drawTriangle
{
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(drawing_type);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 1.0f,-1.0f, 1.0f);
glEnd();
}
|
glTranslate - you use this function to move into the space. Without this function moving into the space, our camera, located at 0,0,0 would be unable to see anything we drew.
glBegin and glEnd - these tell OpenGL what you want to draw. There are a number of OpenGL calls that are invalid between a glBegin and a glEnd. glBegin takes a parameter that tells it what to draw. You can try drawing this triangle with GL_POINTS or GL_LINE_LOOP if you'd like. Most of the other begin modes have a certain number of vertices that you must have in order to draw a complete shape.
glVertex3f - tells OpenGL where you want to draw. If you didn't use translate, you could translate within these calls, but everything would have to have the translation added to it.
glColor3f - tells OpenGL what color you want to draw in. Blending is on, so the colors will blend into one another within a particular shape.
For more information on any of these, open up your terminal and check the man pages!
But wait! We are programming in Cocoa! It is easy to add a menu item to allow us to see the effects of different things. Go into your NIB file and create a new menu called "Style" and add the submenus "Filled", "Wireframe" and "Points"
Connect these to three actions in your kGLView called setSolid, setWireframe and setPoints. Now save your NIB, and create the actions in your source code. We already put these in kGLView's header above.
- (IBAction)setSolid:(id)sender
{ drawing_type = GL_POLYGON; }
- (IBAction)setWireframe:(id)sender
{ drawing_type = GL_LINE_LOOP; }
- (IBAction)setPoints:(id)sender
{ drawing_type = GL_POINTS; }
|
And there you have it! We recieve our messages from the event system, we have looked at three styles of OpenGL drawing, and we have color.
|
|