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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#include <iostream>
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <Newton/newton.h>
#include <SDL/SDL_framerate.h>
#include "vector.h"
void Render ();
void Prepare ();
void InitSDL ();
void InitOGL ();
void InitScene ();
void Shutdown ();
void DessinerCube ();
void GetFPS ();
int main (int argc, char *argv [])
{
InitSDL();
InitOGL();
SDL_Event event;
bool bQuit = false;
FPSmanager manager;
SDL_initFramerate (&manager);
SDL_setFramerate (&manager, 60);
while (!bQuit)
{
Prepare ();
Render ();
SDL_framerateDelay (&manager);
GetFPS ();
if (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_QUIT:
Shutdown ();
bQuit = true;
break;
default:
break;
}
}
}
return EXIT_SUCCESS;
}
void GetFPS ()
{
static char titleBar[25] = {0};
static int FPS = 0;
float nextSecond = 0.0f;
static float prevSecond = 0.0f;
FPS++;
nextSecond = SDL_GetTicks() * 0.001f;
if (nextSecond - prevSecond > 1.0f)
{
prevSecond = nextSecond;
sprintf(titleBar, "%d FPS", FPS);
SDL_WM_SetCaption (titleBar, NULL);
FPS = 0;
}
}
void InitSDL ()
{
// Initialisation de SDL
SDL_Init (SDL_INIT_VIDEO);
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); // On active le double buffering
SDL_Surface * pEcran = SDL_SetVideoMode (640, 480, 32, SDL_OPENGL);
if (pEcran == NULL)
{
std::cerr << "Erreur au paramètrage vidéo : " << SDL_GetError ();
}
atexit (SDL_Quit);
}
// PARTIE OPENGL
// Structure vecteur toute simple
typedef struct
{
GLfloat x, y, z;
} vecteur;
typedef struct
{
GLfloat matrice [4][4];
} matrix;
// Variables globales pour Newton
NewtonWorld * nWorld = NULL; // Monde
NewtonBody * nBody1 = NULL; // Corps n°1, représentant le sol statique
NewtonBody * nBody2 = NULL; // Corps n°2, représentant un cube attiré par des forces
void InitNewton ();
void ForceAndTorqueCallBack (const NewtonBody *);
void InitOGL ()
{
glClearColor(0.0, 0.0, 0.3, 0.0);
glClearDepth(1.0);
GLenum err = glewInit();
if (GLEW_OK != err)
std::cerr << "Erreur dans GLEW.";
glViewport (0, 0, 640, 480); // On règle le Viewport
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60.0, 640.0 / 480.0, 0.1, 1000.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity (); // On rétablit la matrice identité
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
GLfloat LampeDiffuse [] = {1.0f, 1.0f, 1.0f};
GLfloat LampeAmbient [] = {0.75f, 0.75f, 0.75f};
GLfloat LampePosition [] = {4.0f, 3.0f, 0.0f, 0.0f};
glLightfv (GL_LIGHT0, GL_DIFFUSE, LampeDiffuse);
glLightfv (GL_LIGHT0, GL_SPECULAR, LampeDiffuse);
glLightfv (GL_LIGHT0, GL_AMBIENT, LampeAmbient);
glLightfv (GL_LIGHT0, GL_POSITION, LampePosition);
// On définit les matériaux des objets
glEnable (GL_COLOR_MATERIAL);
GLfloat CouleurMateriel [] = {1.0f, 0.0f, 0.0f};
GLfloat Specular [] = {1.0f, 1.0f, 1.0f};
GLfloat Emission [] = {0.0f, 0.0f, 0.0f};
glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glColor3fv (CouleurMateriel);
glColorMaterial (GL_FRONT_AND_BACK, GL_SPECULAR);
glColor3fv (Specular);
glColorMaterial (GL_FRONT_AND_BACK, GL_EMISSION);
glColor3fv (Emission);
glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 100);
// On initialise Newton
InitNewton ();
}
void Prepare ()
{
static GLfloat tempsPrecedent = 0.0f;
GLfloat tempsActuel = 0.0f;
tempsPrecedent = SDL_GetTicks () - tempsPrecedent;
tempsActuel = SDL_GetTicks ();
NewtonUpdate (nWorld, (tempsActuel - tempsPrecedent));
}
void Render ()
{
glClearColor(0.0f, 0.0, 0.3, 0.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
gluLookAt (0.0f, 0.0f, 15.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// Puis quelques rotations afin de bien voir le sol
glRotatef(15, 1, 0, 0);
glRotatef(45, 0, 1, 0);
// On va d'abord dessiner le sol
// On récupère d'abord sa matrice
matrix matriceSol;
NewtonBodyGetMatrix (nBody1, &matriceSol.matrice [0][0]);
glPushMatrix (); // On sauvegarde la matrice actuelle
// Afin d'obtenir la bonne transformation du corps, on multiplie la matrice
// actuelle par la matrice du corps, qui a été changée par Newton à cause des
// forces s'exercant sur le corps
glMultMatrixf (&matriceSol.matrice [0][0]);
// On met à l'échelle, le sol ayant la taille : 10.0f, 0.5f, 10.0f
glScalef (10.0f / 2, 0.5f / 2, 10.0f / 2);
DessinerCube (); // On appelle la fonction pour dessiner le cube
glPopMatrix (); // On rétablit la matrice
// On dessine ensuite le cube ^^
matrix matriceBoite;
NewtonBodyGetMatrix (nBody2, &matriceBoite.matrice [0][0]);
glPushMatrix ();
glMultMatrixf (&matriceBoite.matrice [0][0]);
glScalef (1.0f/2, 1.0f/2, 1.0f/2);
DessinerCube ();
glPopMatrix ();
SDL_GL_SwapBuffers();
}
void Shutdown ()
{
NewtonDestroy (nWorld), nWorld = NULL;
}
void InitNewton ()
{
nWorld = NewtonCreate (NULL, NULL); // On initialise l'instance à Newton World
// On créé en premier lieu un sol, statique
NewtonCollision * collision = NULL;
// On commence par initialiser la boîte de collision, aux dimensions du sol
collision = NewtonCreateBox (nWorld, 10.0f, 1.0f, 10.0f, NULL);
// On initialise le corps, en utilisant cette instance de collision
nBody1 = NewtonCreateBody (nWorld, collision);
NewtonReleaseCollision (nWorld, collision);
// Afin d'avoir des comportements physiques réalistes, on doit définir correctement
// l'inertie du corps
// La formule utilisée est la suivante :
//Inertie.x := Masse * (Size.y * Size.y + Size.z * Size.z) / 12;
//Inertie.y := Masse * (Size.x * Size.x + Size.z * Size.z) / 12;
//Inertie.z := Masse * (Size.x * Size.x + Size.y * Size.y) / 12;
// D'abord pour le sol
vecteur inertie;
//inertie.x = 0.0f; // Ici l'inertie sera tout le temps égale à 0
//inertie.y = 0.0f;
//inertie.z = 0.0f;
// On règle la masse et l'inertie pour ce corps
NewtonBodySetMassMatrix (nBody1, 0.0f, inertie.x, inertie.y, inertie.z);
// Il nous reste plus qu'à positionner nos deux corps ^^
matrix matrice1; // On créé notre structure matrice
matrice1.matrice [0][0] = matrice1.matrice [1][1] = matrice1.matrice [2][2] = matrice1.matrice [3][3] = 1.0f;
// On récupère la matrice actuelle du corps du sol
NewtonBodyGetMatrix (nBody1, &matrice1.matrice [0][0]);
// Les valeurs de la matrice jouant sur les translations (les positions), étant
// la dernière colonne, on les change afin de positionner notre corps
// On place notre sol aux coordonnées 0, 0, 0
matrice1.matrice [3][0] = 0.0f;
matrice1.matrice [3][1] = 0.0f;
matrice1.matrice [3][2] = 0.0f;
// Puis on assigne ces valeurs à la matrice du corps
NewtonBodySetMatrix (nBody1, &matrice1.matrice [0][0]);
// On fait tout pareil, mais pour le cube
// On réinitialise la boîte de collision, aux dimensions du cube
NewtonCollision * collision2 = NewtonCreateBox (nWorld, 1.0f, 1.0f, 1.0f, NULL);
// On initialise le corps en utilisant cette instance de collision
nBody2 = NewtonCreateBody (nWorld, collision2);
// Puis on détruit l'instance de collision, elle ne nous sert plus
NewtonReleaseCollision (nWorld, collision2);
// On donne à la boîte une masse de 10.0
//inertie.x = 10.0f * (1.0f * 1.0f + 1.0f * 1.0f) / 12;
//inertie.y = 10.0f * (1.0f * 1.0f + 1.0f * 1.0f) / 12;
//inertie.z = 10.0f * (1.0f * 1.0f + 1.0f * 1.0f) / 12;
// Puis on règle la masse et l'inertie pour la boiboîte ^^
NewtonBodySetMassMatrix (nBody2, 1.0f, 1.0f, 1.0f, 1.0f);
// On fait de même avec le cube qu'on placera disons... aux coordonnées
// 2.0, 5.0, -3.0
matrix matrice2;
matrice2.matrice [0][0] = matrice2.matrice [1][1] = matrice2.matrice [2][2] = matrice2.matrice [3][3] = 1.0f;
// On récupère la matrice
NewtonBodyGetMatrix (nBody2, &matrice2.matrice [0][0]);
matrice2.matrice [3][0] = 2.0f;
matrice2.matrice [3][1] = 5.0f;
matrice2.matrice [3][2] = -3.0f;
NewtonBodySetMatrix (nBody2, &matrice2.matrice [0][0]);
// Voilà, on en a fini avec le paramétrage de nos deux corps... enfin presque !
// Il nous reste encore quelque chose à paramétrer ! Pour que le corps bouge, il faut
// qu'il soit attiré par une ou plusieurs forces. Or, il n'y en a pour l'instant
// aucune. Nous allons donc spécifier une callback pour chacun des corps. En fait,
// jusqu'à ce qu'un corps soit stable (qu'il ne bouge plus), Newton appelera
// automatiquement la fonction qu'on lui aura donné en paramètre et se chargera
// d'ajouter les forces ^^ Pour cela on utilise la fonction
// NewtonBodySetForceAndTorqueCallBack
// Pour l'objet un, le sol :
//NewtonBodySetForceAndTorqueCallback (nBody1, ForceAndTorqueCallBack);
// Pour le cube :
NewtonBodySetForceAndTorqueCallback (nBody2, ForceAndTorqueCallBack);
}
void ForceAndTorqueCallBack (const NewtonBody * nBody)
{
GLfloat masse; // Contiendra la masse de l'objet pris en paramètre par la fonction
vecteur inertie; // Contiendra l'inertie du corps
vecteur force; // Spécifiera la force appliquée sur le corps
NewtonBodyGetMassMatrix (nBody, &masse, &inertie.x, &inertie.y, &inertie.z);
force.x = 0.0f;
force.y = -masse * 9.81;
force.z = 0.0f;
NewtonBodyAddForce (nBody, &force.x); // On ajoute la force au corps
}
void DessinerCube ()
{
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f( -1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( -1.0, 1.0, 1.0);
glNormal3f(0.0, 0.0, -1.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( -1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f( -1.0, 1.0, -1.0);
glVertex3f( -1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, -1.0);
glNormal3f(0.0, -1.0, 0.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( -1.0, -1.0, 1.0);
glNormal3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glNormal3f(-1.0, 0.0, 0.0);
glVertex3f( -1.0, -1.0, -1.0);
glVertex3f( -1.0, -1.0, 1.0);
glVertex3f( -1.0, 1.0, 1.0);
glVertex3f( -1.0, 1.0, -1.0);
glEnd ();
} |
Partager