Find REAL3D on facebook

OpenGL Tutorial - 1

How to install OpenGL? or Installation guide for OpenGL? or How can I compile my first OpenGL program using MS Visual Studio 2005, 2008, 2010, 2011, 2012?

In this tutorial you are going to learn...

  • How to install OpenGL in windows?
  • How can you compile your first OpenGL program(in which a wireframe cube is rendered) using MS Visual Studio?

  • Windows Installation:

    I am assuming that you have Microsoft Visual Studio 2010 in your system. So, here we go, just download the precomposed package which I made a long ago, I always need this package whenever I reinstall my windows.

    File Name Type Size Release Date
    OpenLibraries *.zip 482 KB 2012.07.11

    After downloading the above zip file...

    Step - 1:
    Extract it. You will find three folders named dll, lib, include.
    Now what you need to do is...
    Step - 2:
    Copy all the *.dll files from the "ddl" folder and paste into "C:\Windows\System32" folder.
    Step - 3:
    Copy all the *.lib files from the "lib" folder and paste into "C:\Program Files\Microsoft Visual Studio 10.0\VC\lib" folder.
    If you are working in Visual Studio 2005 then paste all *.lib files into "C:\Program Files\Microsoft Visual Studio 8\VC\lib" folder.
    Step - 4:
    Copy both folders "GL" and "AL" from the include folder and paste into "C:\Program Files\Microsoft Visual Studio 10.0\VC\include" folder.
    If you are working in Visual Studio 2005 then paste both folder into "C:\Program Files\Microsoft Visual Studio 8\VC\include" folder.

    Congratulations, you have successfully installed OpenGL and OpenAL in your system. OpenAL will help you to play sounds (audio files) in your OpenGL applications.

    Now you can easily compile your first OpenGL program in MS VS 2010. Download my sample VS2010 Project and run it.

    File Name Type Size Release Date
    OpenGLTest_VS2010Proj *.zip 662 KB 2012.06.17

    If you don´t want to use my sample project then here is the method to make a new VS2010 project for OpenGL programs.

    Step - 1:
    Start MS Visual Studio 2010 C++ and create a new empty project of type Win32 Console Application" from "File -> New -> Project -> Visual C++ -> Win32 -> Win32 Console Application -> Write Name of your New Project -> Press OK -> Press Next -> Select Empty Project -> Press Finish".
    Step - 2:
    Now on the left pannel, right click on "Source Files then Add -> New Item -> Now select C++ File (.cpp) -> Write name of your .cpp file (for example: opengltest.cpp)-> Press Add".
    Step - 3:
    Now copy and paste my sample OpenGL code into your newly created *.cpp file.
    Step - 4:
    Finally, Rebuild (Ctrl+Alt+F7) and run the program by pressing Ctrl+F5 or go to Debug menu and press "Start Without Debugging".

    From here you can download the opengltest.cpp file. The file contains a full OpenGL program in which a Cube is rendered.

    File Name Type Size Release Date
    OpenGLTest *.cpp 4.27 KB 2012.06.17

    	Created:	2012/06/17
    	Created:	17:6:2012   23:24
    	Filename: 	opengltest.cpp
    	Author:		Furqan Ullah
    	Disclaimer:	This tutorial has been written for learning purposes only.
    #include < stdlib.h>
    #include < GL/glut.h>
    /* Initialization of OpenGL                                                                     */
    int initOpenGL (GLvoid) 
    	glClearColor(0.0f, 0.28f, 0.49f, 0.0f);
    	glShadeModel (GL_SMOOTH);
    	return true;
    /* This is the main OpenGL function. Whatever you want to draw in your OpenGL application, you will need to write in this function only. */
    /* This is a real-time activated function, this function operates like a loop function,  */
    void display(void)
    	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    		glRotatef(15.0, 1.0, 0.0, 0.0);
    		glRotatef(30.0, 0.0, 1.0, 0.0);
    		glRotatef(0, 0.0, 0.0, 1.0);
    /* This function defines the camera location, type of projection, and window shape after each frame. */
    void reshape(int w, int h)
    	glViewport(0, 0, w, h);
    	gluPerspective(45.0, (GLdouble)w / (GLdouble)h, 0.1, 10000.0);
    	gluLookAt(0.0, 0.0, 100.0,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
    /*  This is the main function, from where the compiler start compiling your code. 
     *  Think in this way.. your compiler starts reading your code from first line of your main function. */
    /*  So, the compiler reads your code from up to down. 
     *  It means that you will need to declare all relevant variables before that specific function. */
    /*  Just read from first line to last one. Its not that much difficult, everything is clear. 
     *  Do not think too much, just concentrate on the variable names. You will know the meaning of that variable soon. */
    int main(int argc, char** argv)
    	int WINDOW_SIZE_W = 1024/2; // width of your OpenGL window
    	int WINDOW_SIZE_H = 768/2; // height of your OpenGL window : it will be your choice, whatever size you want, just change the values.
    	int screenW = glutGet(GLUT_SCREEN_WIDTH); // get the width of your desktop window, or in other words, the resolution you are working on.
    	int screenH = glutGet(GLUT_SCREEN_HEIGHT); // get the height of the your desktop window
    	int windowPosX = (screenW - WINDOW_SIZE_W) / 2; // calculation for placing the OpenGL window at your monitor's/LCD's center.
    	int windowPosY = (screenH - WINDOW_SIZE_H) / 2; // calculation for placing the OpenGL window at your monitor's/LCD's center.
    	glutInitWindowSize (WINDOW_SIZE_W, WINDOW_SIZE_H); // Initialization of OpenGL window
    	glutInitWindowPosition (windowPosX, windowPosY); // OpenGL windows position at the center of LCD
    	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE); // Initialization of display mode. Multisample for anti-aliasing.
    	glutCreateWindow("OpenGL Test by Furqan"); // Create OpenGL window with a title
    	initOpenGL(); // Initialization of OpenGL with default operations, for example, set the background color of the OpenGL window, Enable Lights etc.
    	glutDisplayFunc(display); // Main display function of OpenGL, whatever you want to display in your application you have to write in display() Func.
    	glutReshapeFunc(reshape); // Reshape controller of the OpenGL window
    	glutMainLoop(); // Main loop of GLUT.
    	return 0;

    opengltest.cpp Output

    Output result of opengltest.cpp file.

    Now you have learned your first lesson just in 10 minutes. wow. ;-). Now spend some more time and play with OpenGL by changing different variables, functions etc. Good Luck.