Page 1 of 1

OpenGL with C++ Tutorial on how to obtain, install, code and run OpenGL applications u Rate Topic: ****- 1 Votes

#1 violaris  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 14
  • Joined: 30-November 08

Post icon  Posted 08 December 2008 - 06:30 AM

OpenGL - Computer Graphics using C++ and OpenGL Libraries
PART ONE

OpenGL stands for Open (as in open source) Graphics Language. Unlike other graphics engines such as DirectX, OpenGL's code is available to anyone in a number of languages. In this tutorial we will focus on the use of OpenGL libraries with the C++ programming language.

As OpenGL is an open source graphics language, its code is device independent. This means that no matter what operating system you are running, you can compile the code without problems. Nevertheless, for the purpose of this tutorial, the information on obtaining and using the libraries will be seen from the point of view of a Windows user, but remember that using OpenGL with Unix/Linux/Mac/FreeBSD etc. is very similar.


1. Obtain and Install OpenGL Libraries

As with anything else open source, downloading OpenGL is completely free. You may find these libraries at the OpenGL project website.

Namely, the libraries you are going to need are:
  • OpenGL (the API tool)
  • GLU (the OpenGL Utility Library)
  • GLUT (OpenGL Utility Toolkit) - handles window system operations

Also, along with each libraries, some additional files are required. These are, a header file .h, a library file .lib and in some systems a dynamically linked library file .dll.
Once downloaded, these files must be copied in the following directories on your system. Following you can see a list of the files you need to download and the directories you need to copy them to.

NOTE: The directories I give here are specific to Microsoft's Visual C++. If this is not the compiler you are using then you will need to find the specifications from your compiler in order to find where to include these files.

glut.h : copy in <MSDevDir>\..\..\VC98\include\GL
glut32.lib : copy in <MSDevDir>\..\..\VC98\lib
glut32.dll : copy in <Windows Directory>\System


2. Adding Header Files

When you have obtained the OpenGL libraries and header files, and installed them in the correct directories of your compiler, you're set to go.
You should have the three files Gl.h, Glu.h and Glut.h in a GL subdirectory of the include directory of your compiler. The first four lines in a program should include the header files for each library we are using. They should look like this:

#include <windows.h> // Header file for Windows
#include <gl\gl.h> // Header file for the OpenGL32 Library
#include <gl\glu.h> // Header file for the GLU32 Library
#include <gl\glut.h> // Header file for the Glut32 Library



3. Event Driven Programs

Windows based programs are event driven. This means that the program responds to various events, such as:
  • mouse click
  • keypress
  • window resizing, maximazing, minimazing etc

The programmer organizes a set of callback functions that are executed when events occur. For example the statement
glutMouseFunc(myMouse);
registers the function myMouse() as the function to be executed when a mouse event occurs. The programmer puts code in myMouse() to handle all of the possible mouse actions that are of interest.
There are four principal types of events and a "glut" function is available for each. The prefix "glut" indicates that this function is part of the OpenGL Utility Toolkit.
  • glutDisplayFunc(myDisplay); Whenever the system determines that a window should be redrawn on the screen, it issues a "redraw" event. This happens when the window is first opened and when the window becomes reactivated. Here the function myDisplay() is registered as the callback function for the redraw event.

  • glutReshapeFunc(myReshape); Screen windows can be reshaped by the user. Moving the window does not produce a reshape event. Here the function myReshape() is automatically passing arguments that report the new width and height of the reshaped window.

  • glutMouseFunc(myMouse); When one of the mouse buttons is pressed or released, arguments that describe the location of the mouse and the nature of the action initiated are passed to the function myMouse().

  • glutKeyboardFunc(myKeyboard); When one of the keys on the keyboard are pressed or released. the function myKeyboard() is automatically passing arguments that tell which key was pressed. It also passes data indicating the location of the mouse at the time the key was pressed.

  • glutMainLoop(); The program draws an initial picture and enters a loop, in which it simply wait for events to occur.


4. Opening a window for drawing.

Opening a window for drawing is system dependent. OpenGL functions are system independent and they do not provide any support for controlling windows on specific systems. But OpenGL Utility Toolkit is the library that includes functions to open windows on whatever system you are using. Consider the following program:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

void myInit (void)
{
	 glClearColor(1.0,1.0,1.0,0.0) // sets background color to white
	 glColor3f(0.0f,0.0f,0.0f) // setsthe drawing colour
	 glPointSize(4.0) // sets a point to be 4x4 pixels
	 glMatrixMode(GL_PROJECTION);
	 glLoadIdentity();
	 gluOrtho2D(0.0, 500.0, 0.0, 400.0); // the display area in world coordinates.
}

void myDisplay(void)
{
	 glClear(GL_COLOR_BUFFER_BIT); // clears the screen
	 glBegin(GL_POINTS);
		  glVertex2i (100, 50);
		  glVertex2i (100, 130);
		  glVertex2i (150, 130);
	 glEnd();
	 glFlush(); // sends all output to display;
}

void main (int argc, char **argv)
{
	 glutInit (&argc, argv); // to initialize the toolkit;
	 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // sets the display mode
	 glutInitWindowSize (640, 480); // sets the window size
	 glutInitWindowPosition (10, 10); // sets the starting position for the window
	 glutCreateWindow ("My first OpenGL program!"); // creates the window and sets the title

	 glutDisplayFunc (myDisplay);

	 myInit(); // additional initializations as necessary
	 glutMainLoop(); // go into a loop until event occurs
}


The first five functions in main() initialize and display the screen window in which the program will produce graphics:
  • glutInit(&argc, argv); Initializes the OpenGL Utility Toolkit. Its arguments are standard ones for passing information about the command line.

  • glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); Specifies how the display should be initialized. GLUT_SINGLE and GLUT_RGB are build in constants which are ORed together and indicate that a single display buffer should be allocated and that colours are specified using desired amounts of red, green and blue. Later on you may explore double buffering, for making smooth animations.

  • glutInitWindowSize (640, 480); Specifies tha the screen window should initially be 640 pixels wide by 480 pixels high. Once the program is running the user can resize these values.

  • glutInitWindowPosition (10, 10); Specifies that the window's upper left corner should be positioned on the screen 10 pixels over from the left edge and 20 pixels down from top. When the program is running the user can move the window.

  • glutCreateWindow ("My first OpenGL program!"); Opens and displays the screen window, putting the title "My first OpenGL program!" in the title bar.

TIP: I strongly suggest that you compile and run the previous program so you can get a better understanding of what is going on. It is the best way to get the best out of this tutorial.


5. Drawing Basic Graphics Primitives

The drawing commands will be placed in the callback function assiciated with a redraw event, i.e. myDisplay().
The list of objects to be drawn are placed between the two OpenGL function calls, glBegin() and glEnd(). The argument of glBegin() determines which object is drawn. For example the program I showed you before will draw in a window three points (4 by 4 pixels each). These points are drawn using the commands:

glBegin(GL_POINTS);
	  glVertex2i (100, 50);
	  glVertex2i (100, 130);
	  glVertex2i (150, 130);
glEnd();



The constant GL_POINTS means that it will draw points. To draw other primitives, GL_LINES, GL_POLYGON, etc can be used. A full list of primitives can be found in the OpenGL documentation right here.

Many functions in OpenGL, such as Vertex2i() have many variations, which depends on the number and type of the arguments passed to the function.
The prefix "gl" indicates a function from the OpenGL library (as opposed to "glut" for the GL Utility Toolkit). Then comes the basic command root, followed by the number of arguments being send top the function (often 3 and 4), and finally, the type of argument (i for an integer, f or d for a floating point value).


6. OpenGL Data Types

OpenGL works with specific data types. For example, functions like glVertex2i() expect integers of a certain size (32 bits). Some systems treat the C or C++ data type int as 16-bit quantity (this depents on the operating system), whereas others treat it as 32-bit. There is no standard size for float or double either. To ensure that openGL functions receive the proper data types, we should use the built in names, such as GLint or GLfloat. The OpenGL types are:
  • GLbyte - signed char (8-bit integer)
  • GLshort - short (16-bit integer)
  • GLint, GLsizei - int or long (32-bit integer)
  • GLfloat, Glclampf - float (32-bit floating point)
  • GLdouble, GLclampd - double (64-bit floating point)
  • GLubyte, GLboolean - unsigned char (8-bit unsigned number)
  • GLushort - unsigned short (16-bit unsigned number)
  • GLuint, GLenum, GLbitfield - unsigned int or unsigned long (32-bit unsigned number)

Thus, the function to display a point should be as follows:
void drawDot (GLint x, GLint y)
{
	glBegin(GL_POINTS);
		glVertex2i(x,y);
	glEnd();
}



Note that the function glVertex2i(x,y) has a suffix i and thus "expects" a 32-bit integer, but if you dclare x and y with int then your system might translate them as 16-bit integers, depending on your system, and this will cause problems. So be careful when using variable declarations with OpenGL.

----------------End of PART ONE------------------


Well, that's all for now, I'll be back with the second part of the tutorial which will include:
  • OpenGL Functions
  • Establishing the Coordinate System
  • More examples with code
  • Making Line Drawings
  • Drawing Polylines and Polygons
  • Reading Values from files
  • Building a polyline drawer
  • Type of Polygons
  • Alligned 2-Dimensional Rectangles
  • Creating a simple game


Is This A Good Question/Topic? 4
  • +

Replies To: OpenGL with C++

#2 Quantify  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-April 09

Posted 14 April 2009 - 03:04 PM

Great job introducing the basics of setting up the window etc. The only thing I noticed that is wrong is that the first code snippet you have is missing 3 semi-colons at the beginning here:
void myInit (void)
{
	 glClearColor(1.0,1.0,1.0,0.0); // sets background color to white
	 glColor3f(0.0f,0.0f,0.0f); // setsthe drawing colour
	 glPointSize(4.0); // sets a point to be 4x4 pixels
	 glMatrixMode(GL_PROJECTION);
	 glLoadIdentity();
	 gluOrtho2D(0.0, 500.0, 0.0, 400.0); // the display area in world coordinates.
} 


glClearColor, glColor3f(0.0f,0.0f,0.0f); and glPointSize(4.0) needed the semicolons.

Thanks a ton!
Was This Post Helpful? 0
  • +
  • -

#3 SpooksDF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 06-April 09

Posted 03 July 2009 - 06:15 AM

What is the linker for OpenGL?
Was This Post Helpful? 0
  • +
  • -

#4 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 30 October 2009 - 10:54 PM

great tutorial :D gonna make anymore?
Was This Post Helpful? 0
  • +
  • -

#5 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Posted 27 August 2010 - 12:53 PM

Quote

What is the linker for OpenGL?


opengl32.lib
glu32.lib
glut32.lib (Assuming your compiler came with OpenGL Toolkit. Otherwise you will need to download freeglut)

This post has been edited by Splatocaster: 27 August 2010 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Anon*


Reputation:

Posted 25 September 2010 - 12:40 AM

looking at the code, the "windows.h" isn't really needed, right?
Was This Post Helpful? 0

#7 strandedviking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-July 10

Posted 14 October 2010 - 12:03 PM

The post was helpful as I am trying to figure out how to paint pixels
in c++ 4.0(Microsoft). No one seems to have the answers for me. Now
I am gonna try to down load the Open Gl files. I am assuming Open GL
will work with microsoft Visual C++ 4.0?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1