TP-rendu/TP1-2/helloteapot.cpp
2023-06-22 20:30:57 +02:00

137 lines
3.1 KiB
C++
Executable file

#include <stdlib.h>
// for mac osx
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else
// only for windows
#ifdef _WIN32
#include <windows.h>
#endif
// for windows and linux
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <math.h>
#endif
bool solidMode = false;
double angle = 0;
// function called everytime the windows is refreshed
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// define the projection transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 10);
// define the viewing transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3 * cos(angle), 0, 3 * sin(angle), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// clear window
glClear(GL_COLOR_BUFFER_BIT);
// draw scene
// glutWireTeapot(.5);
// glutSolidTeacup(0.5);
if (solidMode)
{
glutSolidTorus(0.25, 0.5, 100, 100);
}
else
{
glutWireTorus(0.25, 0.5, 100, 100);
}
// flush drawing routines to the window
glFlush();
angle += 0.001;
glutPostRedisplay();
}
// Function called everytime a key is pressed
void key(unsigned char key, int x, int y)
{
switch (key)
{
case 'r':
angle += 0.1;
break;
case 'w':
solidMode = !solidMode;
break;
// the 'esc' key
case 27:
// the 'q' key
case 'q':
exit(EXIT_SUCCESS);
break;
default:
break;
}
glutPostRedisplay();
}
// Function called every time the main window is resized
void reshape(int width, int height)
{
// define the viewport transformation;
glViewport(0, 0, width, height);
}
// Main routine
int main(int argc, char *argv[])
{
// initialize GLUT, using any commandline parameters passed to the
// program
glutInit(&argc, argv);
// setup the size, position, and display mode for new windows
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_RGB);
// create and set up a window
glutCreateWindow("Hello, teapot!");
// Set up the callback functions:
// for display
glutDisplayFunc(display);
// for the keyboard
glutKeyboardFunc(key);
// for reshaping
glutReshapeFunc(reshape);
// lights from tp3
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; // the ambient component
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; // the diffuse component
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; // the specular component
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; // the light position
// set the components to the first light
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// activate lighting effects
glEnable(GL_LIGHTING);
// turn on the first light
glEnable(GL_LIGHT0);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// tell GLUT to wait for events
glutMainLoop();
}