This project shows how to perform the three classical shadings: Polygon shading, Gouraud shading and Phong shading over 3D models using OpenGL.
There are two main steps to run the code of this project:
- Copy the additional OpenGL libraries (added to the project) in their respective places as listed below.
- Open the project in Visual Studio.
You need to include the OpenGL libraries in the following locations:
- C:\Program Files\Microsoft Visual Studio 10.0\VC\dll:
- glu32.dll
- glut32.dll
- opengl32.dll
- C:\Program Files\Microsoft Visual Studio 10.0\VC\include\GL:
- GL.H
- GLAux.h
- glew.h
- GLU.H
- glui.h
- glut.
- glxew.h
- wglew.h
- C:\Program Files\Microsoft Visual Studio 10.0\VC\lib:
- glew32.lib
- glew32d.lib
- glew32s.lib
- glfw3.lib
- glfw3dll.lib
- GLU32.LIB
- glui32.lib
- glut32.lib
- C:\Windows\System32:
- glu32.dll
- glut32.dll
The list above shows how to create a local installation of OpenGl. If you want to use OpenGL just for this project, include the glu32.dll and glut32.dll in the folder of the generated executable.
When running the actual control, a set of keyboard controls can be used:
- 'm': Change model from the list of models loaded by default.
- 's': Change the shading model applied in the selected 3D model.
- 'a': Activates the rotation movement of the model.
- '0': Camera zoom in.
- '9': Camera zoom out.
There are different parts of the code that are worth to mention.
The function below
void keyboard(unsigned char key,int x,int y)
includes all the controls that are available from the keyboard.
The Main code is composed as
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowPosition(60,50);
glutInitWindowSize(900,700);
glutCreateWindow("Shading examples");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
myInit();
glutMainLoop();
return 0;
}
where the default OpenGL functions are included prior to the myInit() function. If you want to add custom code, please do so in the myInit() or after it. The glut functions are standard implementations of OpenGl.
When running the program, the defaul 3D models are loaded. About the visualization:
- To change the displayed model, press 'm'.
- To start and stop the rotation movement, press 'a'.
In the /main/MultiShader/models folder, there are 15 tri models, but by default only 4 are loaded in the code. If you want to add the extra ones, you need to first (in the main.cpp file) add extra variables
TRIModel csieModel,ballModel,f18Model,csieB1;
then load the models from the files
void myInit()
{
ballModel.loadFromFile("models/balls.tri");
csieModel.loadFromFile("models/csie.tri");
f18Model.loadFromFile("models/Car_road.tri");
csieB1.loadFromFile("models/Easter.tri");
changeModel(currentModel);
...
}
and finally add them in the switch function
void changeModel(int m)
{
switch(m)
{
case 2:
TRIobj=&csieModel;
break;
case 1:
TRIobj=&ballModel;
break;
case 3:
TRIobj=&f18Model;
break;
case 4:
TRIobj=&csieB1;
}
currentModel=m;
}
A demo of the project can be seen below.