1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| #include <windows.h> // Header File For Windows
#include <gl\glut.h>
bool keys[256];
bool skeys[256];
float deep = -6.0f; //profondeur
float xrot = 0.0f; //rotation sur l'axe x
float yrot = 0.0f; //rotation sur l'axe y
float xspeed = 0.0f; //vitesse de rotation x
float yspeed = 0.0f; //vitesse de rotation y
double translation_x,translation_y,translation_z;
void glutResize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* modify this line to change perspective values */
gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glMatrixMode(GL_PROJECTION);
// Set the persepctive. View angel 45, aspect ration 1, near plance 1
// and the far clipping plane at 200. The last two mean to start drawing
// from 1 unit in front of you up to 200 units far.
gluPerspective(45, 1.3, 0.1, 200.0);
// Change to the modelview matrix.
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Cette fonction permet d'effacer
//l'écran avec la couleur spécifiée
return TRUE; // Initialization Went OK
}
void DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
if (skeys[GLUT_KEY_PAGE_UP])
{
deep -= 0.02f;
}
if (skeys[GLUT_KEY_PAGE_DOWN])
{
deep += 0.02f;
}
if (skeys[GLUT_KEY_LEFT])
yspeed -= 0.005f;
if (skeys[GLUT_KEY_RIGHT])
yspeed += 0.005f;
if (skeys[GLUT_KEY_UP])
xspeed -= 0.005f;
if (skeys[GLUT_KEY_DOWN])
xspeed += 0.005f;
if (keys['r'])
{
xrot = 0.0f;
yrot = 0.0f;
xspeed = 0.0f;
yspeed = 0.0f;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); //on 'reset' la matrice de l'écran: origine au centre de la fenetre
glPushMatrix ();
glTranslated(translation_x,translation_y,translation_z); //translation (zoom)
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(0.1f, 0.1f, 0.1f); glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.1f, 0.1f, 0.1f); glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(0.1f, 0.1f, 0.1f); glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();
glPopMatrix ();
xrot += xspeed;
yrot += yspeed;
glutSwapBuffers(); //changement des buffers
glutPostRedisplay(); //reaffiche le plan
}
void keydown(unsigned char key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP :
translation_y+=0.5;
case GLUT_KEY_DOWN :
translation_y-=0.5;
case GLUT_KEY_LEFT :
translation_x-=0.5;
case GLUT_KEY_RIGHT :
translation_x+=0.5;
}
}
void keyup(unsigned char key, int x, int y)
{
keys[key] = false;
}
void specdown(int key, int x, int y)
{
skeys[key] = true;
switch(key)
{
case GLUT_KEY_F1:
glutFullScreen();
}
}
void specup(int key, int x, int y)
{
skeys[key] = false;
switch(key)
{
}
}
int main(int arg, char **argc)
{
//ici, on va initialiser les propriétés de la fenetre que l'on va créer
//commen on va afficher (le '|' permet d'utiliser plusieurs éléments dans
//un seul argument grace au code binaire: chaque élément a le '1' dans une rangéé,
//et avec '|' (or), plusieurs rangées auront des 1 pour avoir plusieurs fonctionnalités
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
//ici, on définit les coordonnées de la position de la fenetre dans l'écran
glutInitWindowPosition( 0, 0 );
//ici, la taille de la fenetre
glutInitWindowSize( 800, 600 );
//ici on initialize le tout par rapport aux ressources (args) et on crée la fenetre
glutInit(&arg, argc);
glutCreateWindow("première fenetre glut");
//ici, on appelle la fonction InitGL(), que l'on va implémenter plus tard
// et qui va contenir toutes les initialisations de l'opengl: emplacement des textures,
//des 'lights', du 'blending' etc...
InitGL();
//ici, on dit à glut quelles fonction il devra utiliser dans la boucle principale (glutMainLoop())
//en mettant comme argument le pointeur de la fonction écrite. Elles seront appelées dans la boucle
//principale quand il y en aura besoin
glutDisplayFunc(DrawGLScene); //fonction d'affichage: c'est le code principal de votre programme
glutReshapeFunc(glutResize); //appelée en cas de 'resize' de la fenetre
glutKeyboardFunc(keydown); //appelée quand l'utilisateur appuie sur une touche normale
glutKeyboardUpFunc(keyup); //relache la touche normale
glutSpecialFunc(specdown); //appuie sur une touche spéciale (F1, flèches...)
glutSpecialUpFunc(specup); //relache la touche spéciale
glutMainLoop();//boucle du jeu
translation_x=translation_y=translation_z=0.0;
return 1;
} |
Partager