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,
- Copy freeglut.dll file from freeglut-MSVC-3.0.0-2.mp\freeglut\bin folder to C:\Windows folder.
- Create freeglut named folder in C:\Program Files\Common Files.
- 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.
- Start Visual Studio & click on New Project.
- Select Visual C++ & Win32 Console Application & enter your project name & click OK.
- Win32 Application Wizard - Click Next.
- Uncheck the Precompiled header checkbox. We don't need this.
- Click Finish.
Once project is created.
- Go to your Project Properties.
- Select VC++ Directories as in the following image.
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.
- 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.
- 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.
- 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,
- #include<Windows.h>
- #include<gl/GL.h>
- #include<gl/GLU.h>
- #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.
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(700, 500);
- glutInitWindowPosition(250, 50);
- glutCreateWindow("OpenGL Demo");
- Init_OpenGL();
- glutDisplayFunc(Display_Objects);
- glutReshapeFunc(Reshape);
- glutMainLoop();
- return 0;
- }
- 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.
- #include<Windows.h>
-
- #include<stdio.h>
- #include<gl/GL.h> // GL.h header file
- #include<gl/GLU.h> // GLU.h header file
- #include<gl/glut.h> // glut.h header file from freeglut\include\GL folder
- #include<conio.h>
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
-
- void Init_OpenGL()
- {
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
-
- glShadeModel(GL_FLAT);
- }
-
-
- void Display_Objects(void)
- {
-
- glClear(GL_COLOR_BUFFER_BIT);
-
-
-
- glPushMatrix();
-
- glTranslatef(0.0, 0.0, 0.0);
-
- glColor3f(1.0, 0.8, 0.0);
-
- glutWireTeapot(1.0);
-
-
- glTranslatef(-2.5, 0.0, 0.0);
- glColor3f(0.0, 1.0, 0.0);
- glutWireSphere(0.8, 30, 30);
-
-
- glTranslatef(5.0, 0.0, 0.0);
- glColor3f(0.0, 0.6, 1.0);
- glutWireCone(0.8, 1.5, 20, 20);
-
-
- glTranslatef(-1.0, 1.4, 0.0);
- glColor3f(1.0, 0.3, 0.0);
- glutWireCube(1.0);
-
-
- glTranslatef(-3.0, 0.4, 0.0);
- glColor3f(1.0, 0.3, 1.0);
- glutWireTorus(0.2, 0.6, 20, 20);
-
-
- glTranslatef(-2.5, -4.0, 0.0);
-
- char str[] = {"OpenGL Demo in Visual C++"};
-
- glColor3f(1.0, 1.0, 1.0);
-
- glRasterPos2f(2.0, 0.0);
-
- for (int i = 0; i < strlen(str); i++)
- {
-
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, str[i]);
- }
-
-
-
- glPopMatrix();
- glutSwapBuffers();
- }
-
- void Reshape(int w, int h)
- {
-
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
-
- glMatrixMode(GL_PROJECTION);
-
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
-
- glMatrixMode(GL_MODELVIEW);
-
- glLoadIdentity();
-
-
- gluLookAt(-0.3, 0.5, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- }
-
-
- int main(int argc, char** argv)
- {
-
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
-
- glutInitWindowSize(700, 500);
-
- glutInitWindowPosition(250, 50);
-
- glutCreateWindow("OpenGL Demo");
-
- Init_OpenGL();
-
- glutDisplayFunc(Display_Objects);
-
- glutReshapeFunc(Reshape);
-
- glutMainLoop();
- return 0;
- }