Page 1 of 1

Using OpenGL/GLUT to Create 3D Scenes Part 1 Rate Topic: -----

#1 xPurplex   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 93
  • Joined: 17-June 09

Posted 26 July 2009 - 06:13 AM

If you notice similarities between the code below and code from here, it is because what I didn't learn from trial-and-error, I learned from there. I also give full credit to for the handleResize() function. If you would rather watch videos to learn OpenGL, check the site out!


I assume that you know C++ (fairly) well and know the basics of OOP.


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
	 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);
	 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);
	 // 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);
	 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);
	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
	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:

Attached Image

(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:

This post has been edited by xPurplex: 28 July 2009 - 07:53 AM

Is This A Good Question/Topic? 4
  • +

Replies To: Using OpenGL/GLUT to Create 3D Scenes

#2 athlon32   User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Posted 02 November 2009 - 07:16 PM

very nice tutorial :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1