Getting Started with OpenGL for Windows

For setting up OpenGL in Ubuntu Linux, please see this post, otherwise for Windows / Visual Studio environments, please use these following instructions:

1. Download the latest Windows drivers

As stated on the OpenGLs wiki page, OpenGL more or less comes with the Windows operating system. You will need to ensure your PC has the latest drivers for your graphics hardware, however. Without these drivers, you will probably default to the software version of OpenGL 1.1 which is considerably slower.

The Windows drivers I chose to download and install were from the Intel site which will automatically identify the latest updates and install them on your machine:

And then run the executable after choosing to download it:

Restart the computer when the installation is finished.

2. Obtain GLUT for Windows (OpenGL Utility Toolkit):

Download this from Nate Robins’ web page. I put it on my C drive.

The current documentation for GLUT tells you to go into the glut project folder and build the glut.dsw Visual Studio project which is a little misleading since there is no glut.dsw file and all the dll, lib etc files you need are already there! So don’t worry about doing this, just download it.

3. Create a Visual Studio Project

Create a new Console Application.

For Windows XP: paste the glut32.dll file into: C:\Windows\System32.
For Windows 7: place the glut32.dll inside the C:\Windows\SysWOW64 folder instead.

In the Project properties, select C/C++ -> General -> Additional Include Directories and add the location of the glut.h header file:

In the Project properties, select Linker -> General -> Additional Library Directories and add the location of the glut libraries:

In the Project properties, select Linker -> Input -> Additional Dependencies and add “glut32.lib”:

4. Try it with an example application

It should now compile when you try this example “Cube” application:


// OpenGL1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <glut.h>

GLfloat light_diffuse[]  = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */

GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
                     { -1.0, 0.0,  0.0 }, 
                     { 0.0,  1.0,  0.0 }, 
                     { 1.0,  0.0,  0.0 },
                     { 0.0, -1.0,  0.0 }, 
                     { 0.0,  0.0,  1.0 }, 
                     { 0.0,  0.0, -1.0 }  };

GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
                      {0, 1, 2, 3}, 
                      {3, 2, 6, 7}, 
                      {7, 6, 5, 4},
                      {4, 5, 1, 0}, 
                      {5, 6, 2, 1}, 
                      {7, 4, 0, 3}  };

GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

void drawBox(void)
{
   int i;

   for (i = 0; i < 6; i++) 
   {
     glBegin(GL_QUADS);
     glNormal3fv(&n[i][0]);
     glVertex3fv(&v[faces[i][0]][0]);
     glVertex3fv(&v[faces[i][1]][0]);
     glVertex3fv(&v[faces[i][2]][0]);
     glVertex3fv(&v[faces[i][3]][0]);
     glEnd();
   }
}

void display(void)
{
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   drawBox();
   glutSwapBuffers();
}

void init(void)
{
   /* Setup cube vertex data. */
   v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
   v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
   v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
   v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
   v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
   v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

   /* Enable a single OpenGL light. */
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glEnable(GL_LIGHT0);
   glEnable(GL_LIGHTING);

   /* Use depth buffering for hidden surface elimination. */
   glEnable(GL_DEPTH_TEST);

   /* Setup the view of the cube. */
   glMatrixMode(GL_PROJECTION);
   gluPerspective( /* field of view in degree */ 40.0,
                   /* aspect ratio */ 1.0,
                   /* Z near */ 1.0, 
                   /* Z far */ 10.0 );

   glMatrixMode( GL_MODELVIEW );
   gluLookAt( 0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
              0.0, 0.0, 0.0,  /* center is at (0,0,0) */
              0.0, 1.0, 0.);  /* up is in positive Y direction */

   /* Adjust cube position to be asthetic angle. */
   glTranslatef(0.0, 0.0, -1.0);
   glRotatef(60, 1.0, 0.0, 0.0);
   glRotatef(-20, 0.0, 0.0, 1.0);
}

int _tmain(int argc, char **argv )
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutCreateWindow("red 3D lighted cube");
   glutDisplayFunc(display);
   init();
   glutMainLoop();

   return 0;
}

Which on running this example will give you the following output:



Related link

Getting Started with OpenGL in Linux




Leave a Reply