Getting Started With OpenGL In Visual C++

  

Introduction

I have been coding OpenGL in C++ from a year but I used other IDE's. In this article we will see about setting up the Visual Studio for OpenGL to run OpenGL programs using Visual C++. So what is OpenGL?

OpenGL is a software interface to graphics hardware. This interface consists of about 150 distinct commands that you use to specify the objects and operations needed to produce interactive three-dimensional applications.

OpenGL is designed as a streamlined, hardware-independent interface to be implemented on many different hardware platforms.

In essence, it is a 3D graphics and modeling library that is extremely portable and very fast. Using OpenGL, you can create elegant and beautiful 3D graphics with nearly the visual quality of a raytracer.

The greatest advantage to using OpenGL is that it is orders of magnitude faster than a ray-tracer. It uses algorithms carefully developed and optimized by Silicon Graphics, Inc. (SGI), an acknowledged world leader in computer graphics and animation.

To run OpenGL programs using VC++ you need OpenGL header files & libraries.

Most of the common OpenGL libraries are:

  • gl (Graphics Library),
  • glu (Graphics Library Utility) &
  • glut (Graphics Library Utility Toolkit).

In this article we will also see some important methods.

The following are the articles for basics of OpenGL in C#.

The syntax is same as in C++ but in C++ there is no class.

Procedure

First you need freeglut libraries, otherwise you cannot run OpenGL program that contain glut functions.

Microsoft SDK provides gl & glu libraries but don't glut, so you must add it externally to your project.

Go to the following links for website of freeglut library. 

 
or directly download freeglut libraries for Windows for MSVC or MinGW. 
 
 

Once you downloaded the freeglut libraries extract all files. Now,

  1. Copy freeglut.dll file from freeglut-MSVC-3.0.0-2.mp\freeglut\bin folder to C:\Windows folder.

  2. Create freeglut named folder in C:\Program Files\Common Files.

  3. Copy include & lib folders from freeglut-MSVC-3.0.0-2.mp\freeglut folder to C:\Program Files\Common Files\freeglut folder because you want to add these header files & libraries to your project. There is no restriction to copy these folders only in C:\Program Files\Common Files path. You can copy or can use these files from wherever you want in your project.

Once these all actions are completed then let's start setting up Visual Studio for glut.

  1. Start Visual Studio & click on New Project.

  2. Select Visual C++ & Win32 Console Application & enter your project name & click OK.

  3. Win32 Application Wizard - Click Next.

  4. Uncheck the Precompiled header checkbox. We don't need this.

  5. Click Finish.

    Once project is created.

  6. Go to your Project Properties.

  7. Select VC++ Directories as in the following image.

    Select VC

    As you can see in the above image, Include Directories & Library Directories. In Include Directories we need to add the path of include folder which we have just copied to C:\Program Files\Common Files\freeglut path.

    Same action for Library Directories for lib folder.

  8. So first let's add freeglut header files to Include Directories. Click on Edit as shown in above image.

    Now, copy and paste path of include folder of freeglut folder to Include Directories as in the following image:

    Here my path is C:\Program Files\Common Files\freeglut\include.

    freeglut

  9. Same action for Library Directories.

    Copy and paste path of lib folder of freeglut folder to Library Directories as in the following image:
    Here my path is C:\Program Files\Common Files\freeglut\lib.

    Click OK

  10. Once it is completed Click Apply & OK.

Coding

First you must define the Windows.h header file before defining the OpenGL header files otherwise error will occur.

You need 4 header files,

  1. #include<Windows.h>    
  2. #include<gl/GL.h>    
  3. #include<gl/GLU.h>    
  4. #include<gl/glut.h>    
In this code we will draw some common objects defined by glut like Sphere, Teapot, Cone, etc.
Read all comments in the code for better understanding.

Let's see some important functions:

 

  • glClearColor(): This function clear the background with color RGB.
  • glPushMatrix(): This function pushes the coordinates onto stack.
  • glTranslatef(): This function set the position to the next object to be drawn for float.
  • glColor3f(): It specify the color to the object & takes 3 float values of RGB.
  • glutWireTeapot(); This function draws a wired Teapot. If you want to draw solid then useglutSolidTeapot().

Same for other objects. See in the following complete code.

You can see syntax when the mouse hovers on that specific function in Visual Studio.

As you know every C++ program has a main function, here it takes a command line argument.

Here's the main function that can be defined in OpenGL C++ to create a Window & set all methods for execute.

  1. int main(int argc, char** argv)    
  2. {    
  3.     glutInit(&argc, argv);    
  4.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);    
  5.     glutInitWindowSize(700, 500);    
  6.     glutInitWindowPosition(250, 50);    
  7.     glutCreateWindow("OpenGL Demo");    
  8.     Init_OpenGL();    
  9.     glutDisplayFunc(Display_Objects);    
  10.     glutReshapeFunc(Reshape);    
  11.     glutMainLoop();    
  12.     return 0;    
  13. }   

 

  • glutInit(): This function initialize the glut & takes to parameters.
  • glutInitDisplayMode(): This set's the mode of the display whether they are RGB or other.
  • glutInitWindowSize(): This set's the size(width & height) to the created window.
  • glutInitWindowPosition(): Set position to the created window.
  • glutCreateWindow(): This creates a window with text.
  • glutDisplayFunc(): This function displays all the objects on window that is drawn & takes a function name as a parameter.
  • glutReshapeFunc(): This function is used when the drawn objects are to be resize automatically when window is resized.it also take function name as a parameter.
  • glutMainLoop(): This function is used to redisplay the objects on the window when window is resize or maximize.

There are many functions provided by glut library like glutKeyboardFunc() & glutMouseFunc() etc.

Here's the complete code.

Copy & paste the following code to your created project code.

If everything is fine and there are no errors then run your project.

  1. #include<Windows.h>    
  2. // first include Windows.h header file which is required    
  3. #include<stdio.h>    
  4. #include<gl/GL.h>   // GL.h header file    
  5. #include<gl/GLU.h> // GLU.h header file    
  6. #include<gl/glut.h>  // glut.h header file from freeglut\include\GL folder    
  7. #include<conio.h>    
  8. #include<stdio.h>    
  9. #include<math.h>    
  10. #include<string.h>    
  11. // Init_OpenGL() function    
  12. void Init_OpenGL()    
  13. {    
  14.     // set background color to Black    
  15.     glClearColor(0.0, 0.0, 0.0, 0.0);    
  16.     // set shade model to Flat    
  17.     glShadeModel(GL_FLAT);    
  18. }    
  19.     
  20. // Display_Objects() function    
  21. void Display_Objects(void)    
  22. {    
  23.     // clearing the window or remove all drawn objects    
  24.     glClear(GL_COLOR_BUFFER_BIT);    
  25.     /*glPushMatrix(), which copies the current matrix and adds  
  26.     the copy to the top of the stack, and  
  27.     glPopMatrix(), which discards the top matrix on the stack*/    
  28.     glPushMatrix();    
  29.     //the glTranslatef() routine in the display list alters the position of the next object to be drawn    
  30.     glTranslatef(0.0, 0.0, 0.0);    
  31.     // set color to object glColor3f(red,green,blue);    
  32.     glColor3f(1.0, 0.8, 0.0);    
  33.     // draw a wire tea pot    
  34.     glutWireTeapot(1.0);    
  35.     
  36.     // draw a wire sphere    
  37.     glTranslatef(-2.5, 0.0, 0.0);    
  38.     glColor3f(0.0, 1.0, 0.0);    
  39.     glutWireSphere(0.8, 30, 30);    
  40.     
  41.     // draw a wire cone    
  42.     glTranslatef(5.0, 0.0, 0.0);    
  43.     glColor3f(0.0, 0.6, 1.0);    
  44.     glutWireCone(0.8, 1.5, 20, 20);    
  45.     
  46.     // draw a wire cube    
  47.     glTranslatef(-1.0, 1.4, 0.0);    
  48.     glColor3f(1.0, 0.3, 0.0);    
  49.     glutWireCube(1.0);    
  50.     
  51.     // draw a wire torus    
  52.     glTranslatef(-3.0, 0.4, 0.0);    
  53.     glColor3f(1.0, 0.3, 1.0);    
  54.     glutWireTorus(0.2, 0.6, 20, 20);    
  55.     
  56.     // draw a text    
  57.     glTranslatef(-2.5, -4.0, 0.0);    
  58.     
  59.     char str[] = {"OpenGL Demo in Visual C++"};    
  60.     
  61.     glColor3f(1.0, 1.0, 1.0);    
  62.     // set position to text    
  63.     glRasterPos2f(2.0, 0.0);    
  64.     
  65.     for (int i = 0; i < strlen(str); i++)    
  66.     {    
  67.         // draw each character    
  68.         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);    
  69.     }    
  70.     
  71.     //you can draw many objects here like polygons,lines,triangles etc    
  72.     
  73.     glPopMatrix();    
  74.     glutSwapBuffers();    
  75. }    
  76. // Reshape() function    
  77. void Reshape(int w, int h)    
  78. {    
  79.     //adjusts the pixel rectangle for drawing to be the entire new window    
  80.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);    
  81.     //matrix specifies the projection transformation    
  82.     glMatrixMode(GL_PROJECTION);    
  83.     // load the identity of matrix by clearing it.    
  84.     glLoadIdentity();    
  85.     gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);    
  86.     //matrix specifies the modelview transformation    
  87.     glMatrixMode(GL_MODELVIEW);    
  88.     // again  load the identity of matrix    
  89.     glLoadIdentity();    
  90.     // gluLookAt() this function is used to specify the eye.    
  91.     // it is used to specify the coordinates to view objects from a specific position    
  92.     gluLookAt(-0.3, 0.5, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    
  93. }    
  94.     
  95. // main function    
  96. int main(int argc, char** argv)    
  97. {    
  98.     // initialize glut    
  99.     glutInit(&argc, argv);    
  100.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);    
  101.     // set window size    
  102.     glutInitWindowSize(700, 500);    
  103.     // set window location    
  104.     glutInitWindowPosition(250, 50);    
  105.     // create window with window text    
  106.     glutCreateWindow("OpenGL Demo");    
  107.     // call Init_OpenGL() function    
  108.     Init_OpenGL();    
  109.     // call glutDisplayFunc() function & pass parameter as Display_Objects() function    
  110.     glutDisplayFunc(Display_Objects);    
  111.     // call glutReshapeFunc() function & pass parameter as Reshape() function    
  112.     glutReshapeFunc(Reshape);    
  113.     //glutMainLoop() is used to redisplay the objects    
  114.     glutMainLoop();    
  115.     return 0;    
  116. }    

 

To create a 3D Planetary System in OpenGL :-     3D Planetary System In OpenGL  
 
To create a 3D Game Scene in OpenGL :-             Drawing A 3D Scene Using OpenGL in C/C++
 
 
 


Similar Articles