Assumptions
I assume that you know C++ (fairly) well and know the basics of OOP.
Introduction
This is the first tutorial in a set that will teach the basics of OpenGL and GLUT. We will be making a little scene by drawing a point, line, triangle, and square. If you have any questions, comments, or corrections, feel free to PM me.
What is OpenGL?
OpenGL, or Open Graphics Library, is a powerful, cross-platform, cross-language 3D graphics library.
What is GLUT?
GLUT stands for OpenGL Utility Toolkit and is used for handling input, windows, menus, etc. It is easy to use and (sorta) easy to install, so these tutorials will be using GLUT.
Setting up OpenGL/GLUT
Dev-C++ - Download this devpack.
Visual Studio - here is a guide for getting it set up.
Code::Blocks - here is a guide for setting it up.
NOTE: if you are using a IDE seperate from those stated above, I apoligize. Please PM me and I'll do my best to post a guide here.
Making a Project
Unless you're running Code::Blocks, you have yet to run an OpenGL/GLUT program. If you are running Code::Blocks and the test code ran like it should've, skip this section.
Dev-C++ - Start up Dev-C++ and go to File->New->Project and click on "glut" under "Multimedia". Erase the default code and skip ahead to "The Code".
Visual Studio - Follow the instructions in the previous section to make a project then paste the code into it. Finally, run it. If there are white shapes on a black background, pat yourself on the back; it's working!
The Code
Ahh, we're finally here! The code! Here we go...
#include <gl/glut.h> using namespace std;
For this lesson we only need to include gl/glut.h.
void init(int argc, char** argv)
{
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Window size
glutCreateWindow("Introduction to OpenGL"); //Create a window
glEnable(GL_DEPTH_TEST); //Make sure 3D drawing works when one object is in front of another
}
Here we initialize GLUT, make a new window and enable GL_DEPTH_TEST. (See comments)
//Called when the window is resized
void handleResize(int w, int h)
{
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
This function is automatically called when the user resizes the window. Don't worry about how it works too much, but notice this: all OpenGL, GLUT, and GLU (OpenGL Utility) functions have gl, glut, and glu in front of their functions, respectively. Also, the 1.0 and 200.0 are the near and far z clipping coordinates. What this means is that if an object is nearer than 1 unit from the "camera" or farther than 200 units, it will not be drawn.
//Draws the 3D scene
void draw()
{
//Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
Here's where we actually draw the shapes. We call glClear() to clear the screen black (don't worry about the parameters). Then we switch to using the modelview matrix so that we can draw on the screen. glLoadIdentity() resets the matrix and then we can start drawing.
glBegin(GL_POINTS); //Begin drawing points // a point glVertex3f(-0.75f, -0.25f, -5.0f); glEnd(); glBegin(GL_LINES); //Begin drawing lines // 2 end points on line glVertex3f(-1.0f, 1.0f, -5.0f); glVertex3f(1.0f, -1.0f, -5.0f); glEnd(); glBegin(GL_TRIANGLES); // 3 corner points on triangle glVertex3f(1.5f, 1.0f, -5.0f); glVertex3f(1.0f, 0.0f, -5.0f); glVertex3f(0.5f, 1.0f, -5.0f); glEnd(); glBegin(GL_QUADS); //Begin drawing quadrilaterals // 4 corner points on square glVertex3f(-0.5f, -2.0f, -5.0f); glVertex3f(0.5f, -2.0f, -5.0f); glVertex3f(0.5f, -1.0f, -5.0f); glVertex3f(-0.5f, -1.0f, -5.0f); glEnd(); glutSwapBuffers(); //Send scene to the screen to be shown }
glBegin() groups sets of glVertex3f() into a shape, depending on its parameter. For example, GL_POINTS groups them into single points, GL_LINES groups them into twos and connects them to form a line, etc. Also, glBegin() always has a corrisponding call to glEnd(), which takes OpenGL out of its state of grouping.
NOTE: OpenGL is a state machine, I'll explain in a later tutorial.
glVertex3f takes 3 float parameters: the x, y, and z coordinates of the vertex. Ignore the z coordinate, so 0, 0, -5 would be the center of the screen.
finally, we call glutSwapBuffers() to show our nifty little scene on the screen.
int main(int argc, char** argv) {
init(argc, argv); //Initialize rendering
//Set functions for glutMainLoop to call
glutDisplayFunc(draw);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
main. Here we call our init() function and use glut...Func() to set the functions that glutMainLoop should call. One thing: glutMainLoop() never returns, so our little return 0 is never reached. This is put in to shut our compiler up about main returning something.
So here's our output:

(sorry about the quality, download the attached image or copy the source code into a project to see the line and point better.
My next tutorial will include:
-Color
This post has been edited by xPurplex: 28 July 2009 - 07:53 AM






MultiQuote




|