C++:Creating a Glut project in Visual Studio 2010

From Progzoo
Jump to: navigation, search

How to create a simple GLUT program in Visual Studio 2010:

Short Version

  1. Create a new Visual C++, Win32 project. Make sure it is an Empty project
  2. Find a simple example from the lecture notes or online. Add the file cube.cpp to the project.
  3. Change the properties of the project:
    1. Add C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include to the Include Directories
    2. Set the entry point to mainCRTStartup

Long Version

  • From Visual Studio 2010 choose File/New/Project
  • Pick Visual C++, Win32 Project

VS2010File New Project.png

  • Click next to set the Applications Settings
    • Tick the Empty project check box

VS2010Application Settings.png

  • Add a new item to the project. The new item is a cpp file called cube.cpp

VS2010AddNewItem.png

  • Copy the following code into cube.cpp (any glut programme you find should also work)
#include <GL/glut.h>


GLint faces[6][4] = {  
	{0, 1, 3, 2}, {4, 5, 7, 6}, //Back face and front 
	{0, 4, 6, 2}, {1, 3, 7, 5}, //Left face and right face
	{0, 1, 5, 4}, {2, 3, 7, 6}, //Bottom face and top face
};
GLfloat n[6][3] = {   // Normals
	{0,0,-1}, {0,0,1},   //Back and front
	{1,0,0},{-1,0,0},   //Left and right
	{0,1,0}, {0,-1,0}}; //Bottom and top
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */

void drawBox(void)
{
	int i;

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

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

void init(void)
{
	for (int i=0;i<8;i++)
	{
	//Set the x, y and z coordinates
	v[i][0] = (i & 1)*2-1;
	v[i][1] = (i>>1 & 1)*2-1;
	v[i][2] = (i>>2 & 1)*2-1;
	}

	GLfloat light_diffuse[] = {1.0, 1.0, 1.0};
	GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);

	glEnable(GL_DEPTH_TEST);

	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 */ 

	glTranslatef(0.0, 0.0, -1.0);
	glRotatef(-20, 1.0, 0.0, .0);
	glRotatef(20, 0.0, 1.0, .0);
}

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutCreateWindow("Cube");
	glutDisplayFunc(display);
	init();
	glutMainLoop();
	return 0;
}

Outside JKCC

If you are working on your own machine or in B56 you will need the following steps... Download glut-3.7.6-bin.zip from http://www.xmission.com/~nate/glut.html - you will find the following files:

  • glut.h
    • Copy this to C:\Program Files\Microsoft SDKs\Windows\v7.0A\include - create folders if required
    • ( on 64bit C:\Program Files (x68)\Microsoft SDKs\Windows\v7.0A\include )
  • glut.lib
    • Copy this to C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib - create folders if required
    • ( on 64bit C:\Program Files (x68)\Microsoft SDKs\Windows\v7.0A\lib )
  • glut.dll
    • Copy this to C:\Windows\System32 ( on 64bit Windows 7 I had to put this in C:\Windows\SysWOW64 )

All machines

  • From Project Properties find Include Directories and add C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include

VS2010Include.png VS2010IncludeF.png

  • From Project Properties find Configuration Properties/Linker/General/Additional Library Directories
    • Add in the directory C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib
  • Go to Project Properties/Configuration Properties/Linker/Advanced/Entry Point
  • Set the Entry Point to mainCRTStartup

VS2010EntryPoint.png

  • Click the green arrow [Start Debugging] to compile and run