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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| #include "Essai.hpp"
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL;
const Uint32 temps_par_image = 1000/FPS;
Uint32 dernier_temps;
Uint32 temps_courant,temps_ecoule;
Uint32 temps_debut;
bool maintenu;
float vertices[NB_VERT*V_SIZE] = {
-1.f, -1.f, -1.f,
1.f, -1.f, -1.f,
1.f, 1.f, -1.f,
-1.f, 1.f, -1.f,
-1.f, -1.f, 1.f,
1.f, -1.f, 1.f,
1.f, 1.f, 1.f,
-1.f, 1.f, 1.f
};
float colors[NB_VERT*C_SIZE] = {
1.f, 0.f, 0.f,
0.f, 1.f, 0.f,
0.f, 0.f, 1.f,
1.f, 1.f, 0.f,
1.f, 0.f, 1.f,
0.f, 1.f, 1.f,
0.5f, 0.2f, 0.8f,
0.8f, 0.4f, 0.1f
};
unsigned int index[NB_FACE*NB_VERT_PER_FACE] = {
0, 1, 2, 3, // Face du bas
1, 2, 6, 5, // Face de devant
2, 3, 7, 6, // Face de droite
6, 7, 4, 5, // Face du dessus
0, 1, 5, 4, // Face de gauche
0, 3, 7, 4 // Face de derrière.
};
GLuint verticesBuffer;
GLuint colorsBuffer;
GLuint indexBuffer;
SDL_Init(SDL_INIT_VIDEO);
atexit(SDL_Quit);
SDL_WM_SetCaption("Gestion avancee de camera !", NULL); // Titre fenetre
ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, NB_COULEURS, SDL_OPENGL); // Mode video
/* initialisation de glew */
glewInit();
/* creation d'un objet tampon et recuperation de son identifiant */
glGenBuffers(1, &colorsBuffer);
glGenBuffers(1, &verticesBuffer);
glGenBuffers(1, &indexBuffer);
// Chargement des données dans le colors buffer.
glBindBuffer(GL_ARRAY_BUFFER, colorsBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NB_VERT*C_SIZE, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*NB_VERT*C_SIZE, colors);
// Chargement des données dans le vertices buffer.
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*NB_VERT*V_SIZE, NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*NB_VERT*V_SIZE, vertices);
// Chargement des données dans l'index buffer.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*NB_FACE*NB_VERT_PER_FACE,
NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(unsigned int)*NB_FACE*NB_VERT_PER_FACE,
index);
// Activer le tableaux de sommet et de couleur.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(50,(double)LARGEUR_FENETRE/HAUTEUR_FENETRE,1,1000); // fovy,ratio,near,far
glEnable(GL_DEPTH_TEST); // Active le z-buffer
//glDisable(GL_CULL_FACE); // Dessine les faces de chaque cote
glEnable(GL_TEXTURE_2D); // Active les textures 2D
textureSol = loadTexture("Textures/crackles.jpg"); // Chargement de la texture du sol
camera = new Camera(Vector3D(0,0,2));
dernier_temps = SDL_GetTicks();
for (;;)
{
temps_debut = SDL_GetTicks();
while (SDL_PollEvent(&event)) // Recupere les evenements
{
switch(event.type)
{
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
exit(0);
break;
default :
camera->clavier(event.key);
}
break;
case SDL_KEYUP:
camera->clavier(event.key);
break;
case SDL_MOUSEMOTION: //la souris est bougee, ça n'interesse que la camera
camera->mouvementSouris(event.motion);
break;
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN: //tous les evenements boutons (up ou down) sont donnes a la camera
camera->clicSouris(event.button);
break;
}
}
temps_courant = SDL_GetTicks();
temps_ecoule = temps_courant - dernier_temps;
dernier_temps = temps_courant;
// gere la camera et dessine la scene
camera->animer(temps_ecoule);
dessinerTout();
// gere le temps que prend l'affichage pour eviter de pomper trop de ressources
temps_ecoule = SDL_GetTicks() - temps_debut;
if (temps_ecoule < temps_par_image)
{
SDL_Delay(temps_par_image - temps_ecoule);
}
}
// Supprimer les buffers.
glDeleteBuffers(1, &colorsBuffer);
glDeleteBuffers(1, &verticesBuffer);
glDeleteBuffers(1, &indexBuffer);
return 0;
}
/** Dessine le cube */
void dessinerCube()
{
glPushMatrix();
glRotated(angleZ,0,0,1);
glRotated(angleX,1,0,0);
glBegin(GL_QUADS); // creation de cube
//Je dessine la meme face 4 fois avec une rotation de 90 degres d'axe z
for (int i = 1; i < 5; i++)
{
glBegin(GL_QUADS);
glColor3ub(i*40,i*40,0);
//glTexCoord2d(0,1);
glVertex3d(1,1,1);
//glTexCoord2d(0,0);
glVertex3d(1,1,-1);
glColor3ub(i*40,0,i*80);
//glTexCoord2d(1,0);
glVertex3d(-1,1,-1);
//glTexCoord2d(1,1);
glVertex3d(-1,1,1);
glEnd();
glRotated(90,0,0,1);
}
//Je dessine la meme face 2 fois avec une rotation de 180 degres d'axe x
for (int i = 1; i < 3; i++)
{
glBegin(GL_QUADS);
glColor3ub(0,i*80,i*80);
//glTexCoord2d(0,0);
glVertex3d(1,-1,1);
//glTexCoord2d(1,0);
glVertex3d(1,1,1);
glColor3ub(i*20,i*20,i*80);
//glTexCoord2d(1,1);
glVertex3d(-1,1,1);
//glTexCoord2d(0,1);
glVertex3d(-1,-1,1);
glEnd();
glRotated(180,1,0,0);
}
glPopMatrix();
}
/** Dessine le sol */
void dessinerSol(void)
{
glBindTexture(GL_TEXTURE_2D, textureSol); // Choisit la texture appropriee
glBegin(GL_QUADS);
glColor3ub(255,0,0);
glTexCoord2i(0,5);
glVertex3i(-10,-10,-1);
glColor3ub(0,255,0);
glTexCoord2i(5,0);
glVertex3i(10,-10,-1);
glColor3ub(255,255,0);
glTexCoord2i(5,5);
glVertex3i(10,10,-1);
glColor3ub(255,0,255);
glTexCoord2i(0,0);
glVertex3i(-10,10,-1);
glEnd();
glColor3ub(255,255,255); //permet de pas colorier le reste avec ces couleurs
}
/** Dessine tout : le sol plus un cube */
void dessinerTout(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
camera->regarder();
//dessinerSol();
//dessinerCube();
// Spécification du buffer de couleur.
glBindBuffer(GL_ARRAY_BUFFER, colorsBuffer);
glColorPointer(C_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(0));
// Spécification du buffer de sommet.
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glVertexPointer(V_SIZE, GL_FLOAT, 0, BUFFER_OFFSET(0));
// Dessiner le carré.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_QUADS, NB_FACE, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glFlush(); // libere la memoire
SDL_GL_SwapBuffers(); // libere les tampons
}
/** Supprime la camera et quitte l'application */
void stop(void)
{
delete camera;
SDL_Quit();
} |
Partager