Add the demo directory

git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@9 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2009-01-26 23:05:20 +00:00
parent f01d5f01f6
commit e8de11f141
10 changed files with 626 additions and 0 deletions

58
sources/demo/Context.cpp Executable file
View File

@ -0,0 +1,58 @@
// Libraries
#include "Context.h"
#include <iostream>
#include <vector>
// Constructor of the class Context
Context::Context() {
// We add some objects in the context at the beginning ---> THESE THINGS WILL BE STORE IN A TEXT FILE
Cube * cube1 = new Cube(Object::Position(-2.0, 1.0, -6.0), 2.0);
Cube * cube2 = new Cube(Object::Position(0.0, 1.5, 6.0), 3.0);
Cube * cube3 = new Cube(Object::Position(4.0, 4.0, -2.0), 2.0);
Plane * plane1 = new Plane(Object::Position(0.0, 0.0, 0.0), 20.0, 30.0, Vector(-1.0, 0.0, 0.0), Vector(0.0, 0.0, 1.0));
addObject(cube1);
addObject(cube2);
addObject(cube3);
addObject(plane1);
}
// Destructor of the class Context
Context::~Context() {
// Delete all the objects in vectObjects
for(int i=0; i<vectObjects.size(); ++i) {
delete vectObjects[0];
}
}
// Method to get an object from the context
Object& Context::getObject(int objectIndex) const {
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Return the object from the context
return (*vectObjects.at(objectIndex)); // THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
}
// Method for adding an object into the context
void Context::addObject(Object* object) {
if (object != NULL) {
// Add the object into the context
vectObjects.push_back(object);
}
}
// Method to remove an object from the context
void Context::removeObject(int objectIndex) {
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Restore the memory of the element
delete vectObjects[objectIndex];
// Erase the element in the vector
vectObjects.erase(vectObjects.begin()+objectIndex);
}

28
sources/demo/Context.h Executable file
View File

@ -0,0 +1,28 @@
#ifndef CONTEXT_H
#define CONTEXT_H
// Libraries
#include "Objects.h"
#include <vector>
// Class Context
class Context {
private :
std::vector<Object*> vectObjects; // Vector of Objects in the simulation
public :
Context(); // Constructor of the class
~Context(); // Destructor of the class
int getNbObjects() const; // Return the number of objects in the context
Object& getObject(int objectIndex) const; // Get an object from the context
void addObject(Object* object); // Add an object into the context
void removeObject(int objectIndex); // Remove an object from the context
};
// Method (inline) to get the number of objects in the context
inline int Context::getNbObjects() const {
return vectObjects.size();
}
#endif

128
sources/demo/Objects.cpp Executable file
View File

@ -0,0 +1,128 @@
// Libraries
#include "Objects.h"
//#include <windows.h> // To avoid an error due to the #include <GL/glut.h>
#include <GL/freeglut.h>
#include <math.h>
// ----- Structure Vector ----- //
// Constructor without arguments of the structure Vector
Vector::Vector() {
x = 0.0;
y = 0.0;
z = 0.0;
}
// Constructor of the structure Vector
Vector::Vector(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
};
// ----- Class Object ----- //
// Constructor of the class Object
Object::Object(const Position& position) {
this->position = position;
}
// Destructor of the class Object
Object::~Object() {
}
// ----- Structure Position ----- //
// Constructor without arguments of the structure Position
Object::Position::Position() {
x = 0.0;
y = 0.0;
z = 0.0;
}
// Constructor of the structure Position
Object::Position::Position(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
};
// ----- Class Cube ----- //
// Constructor of the class Cube
Cube::Cube(const Position& position, float size)
:Object(position) {
this->size = size;
}
// Destructor of the classe Cube
Cube::~Cube() {
}
// Method to draw the cube
void Cube::draw() const {
// Translation of the cube to its position
glTranslatef(position.x, position.y, position.z);
// Draw the cube
glutSolidCube(size);
}
// ----- Class Plane ----- //
// Constructor of the class Plane
Plane::Plane(const Position& position, float width, float height, const Vector& d1, const Vector& d2)
:Object(position) {
this->width = width;
this->height = height;
this->d1 = d1;
this->d2 = d2;
// Compute the unit normal vector of the plane by a cross product
normalVector.x = d1.y * d2.z - d1.z * d2.y;
normalVector.y = d1.z * d2.x - d1.x * d2.z;
normalVector.z = d1.x * d2.y - d1.y * d2.x;
float length = sqrt(normalVector.x * normalVector.x + normalVector.y * normalVector.y + normalVector.z * normalVector.z);
normalVector.x = normalVector.x / length;
normalVector.y = normalVector.y / length;
normalVector.z = normalVector.z / length;
}
// Destructor of the class Plane
Plane::~Plane() {
}
// Method used to draw the plane
void Plane::draw() const {
// Translation of the cube to its position
glTranslatef(position.x, position.y, position.z);
float halfWidth = width / 2.0;
float halfHeight = height / 2.0;
// Draw the plane
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(position.x + d1.x * halfWidth + d2.x * halfHeight , position.y + d1.y * halfWidth + d2.y * halfHeight
, position.z + d1.z * halfWidth + d2.z * halfHeight);
glNormal3f(normalVector.x, normalVector.y, normalVector.z);
glVertex3f(position.x + d1.x * halfWidth - d2.x * halfHeight , position.y + d1.y * halfWidth - d2.y * halfHeight
, position.z + d1.z * halfWidth - d2.z * halfHeight);
glNormal3f(normalVector.x, normalVector.y, normalVector.z);
glVertex3f(position.x - d1.x * halfWidth - d2.x * halfHeight , position.y - d1.y * halfWidth - d2.y * halfHeight
, position.z - d1.z * halfWidth - d2.z * halfHeight);
glNormal3f(normalVector.x, normalVector.y, normalVector.z);
glVertex3f(position.x - d1.x * halfWidth + d2.x * halfHeight , position.y - d1.y * halfWidth + d2.y * halfHeight
, position.z - d1.z * halfWidth + d2.z * halfHeight);
glEnd();
}

64
sources/demo/Objects.h Executable file
View File

@ -0,0 +1,64 @@
#ifndef OBJECTS_H
#define OBJECTS_H
/*
Here we define all the objects that can appear in the simulation like cube, sphere, plane, ...
*/
struct Vector {
double x; // x component
double y; // y component
double z; // z component
// Methods
Vector(); // Constructor without arguments of the structure Vector
Vector(double x, double y, double z); // Constructor of the structure Vector
};
// ----- Class Object (abstract) ----- //
// Represent an object of the simulation
class Object {
public :
// Structure Object::Position
struct Position {
double x; // x coordinate
double y; // y coordinate
double z; // z coordinate
// Methods
Position(); // Constructor without arguments of the structure Position
Position(double x, double y, double z); // Constructor of the structure Position
} position; // Position of the object
Object(const Position& position); // Constructor of the class Object
virtual ~Object(); // Destructor of the class Object
virtual void draw() const =0; // pure virtual method to draw the object
};
// ----- Class Cube ----- //
// Represente a Cube in the simulation
class Cube : public Object {
private :
float size; // Size of a side in the cube
public :
Cube(const Position& position, float size); // Constructor of the class cube
virtual ~Cube(); // Destructor of the class cube
virtual void draw() const; // Method to draw the cube
};
// ----- Class Plane ---- //
// Represent a plane in the simulation
class Plane : public Object {
public :
float width; // Width of the plane
float height; // Height of the plane
Vector d1; // Unit vector in the plane
Vector d2; // Unit vector in the plane
Vector normalVector; // Unit normal vector of the plane
Plane(const Position& position, float width, float height, const Vector& d1, const Vector& d2); // Constructor of the class Plane
virtual ~Plane(); // Destructor of the class Plane
virtual void draw() const; // Method to draw the plane
};
#endif

8
sources/demo/ReactDemo.h Executable file
View File

@ -0,0 +1,8 @@
#ifndef REACT_DEMO_H
#define REACT_DEMO_H
// Constants
const int WINWIDTH = 640; // Width of the OpenGL windows
const int WINHEIGHT = 480; // Height of the OpenGL windows
#endif

103
sources/demo/Scene.cpp Executable file
View File

@ -0,0 +1,103 @@
// Librairies
#include "Scene.h"
#include "Objects.h"
// Constructor of the class Scene
Scene::Scene() {
// Initialise the material specular color
mat_specular[0] = 1.0;
mat_specular[1] = 1.0;
mat_specular[2] = 1.0;
mat_specular[3] = 1.0;
// Initialize the material shininess
mat_shininess[0] = 50.0;
// Initialise the light source position
light_position[0] = 20.0;
light_position[1] = 9.0;
light_position[2] = 15.0;
light_position[3] = 0.0;
// Initialise the ambient color of the light
ambient_color[0] = 1.0;
ambient_color[1] = 1.0;
ambient_color[2] = 1.0;
ambient_color[3] = 0.7;
// Initialise the diffuse light color
white_light[0] = 1.0;
white_light[1] = 1.0;
white_light[2] = 1.0;
white_light[3] = 1.0;
// Initialise the camera angles
camera_angle1 = 0.0;
camera_angle2 = 0.0;
}
// Destructor of the class Scene
Scene::~Scene() {
}
// Init method
void Scene::init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
// Lighting settings
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color of the material
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // Shininess of the material
glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Position of the light source
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); // Ambient color of the light
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); // Diffuse color of the light
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); // Specular color of the light
glEnable(GL_LIGHTING); // Activate the lighting
glEnable(GL_LIGHT0); // Activate a light source
glEnable(GL_DEPTH_TEST); // Activate the Depth buffer
}
// Display method
void Scene::display(const Context& context) const {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Define the position and the direction of the camera
gluLookAt(30,10,0,0,0,0,0,1,0);
// Rotation of the camera due to the mouse mouvement
glRotatef(camera_angle2, 0.0, 0.0, 1.0);
glRotatef(camera_angle1, 0.0, 1.0, 0.0);
// Draw all objects in the context
for(int i=0; i<context.getNbObjects(); ++i)
{
// Copy the active matrix on the matrix stack
glPushMatrix();
// Draw the object
context.getObject(i).draw();
// Remove the matrix on the top of the matrix stack
glPopMatrix();
}
// Change the buffers
SDL_GL_SwapBuffers();
}
// Reshape the window
void Scene::reshape(int width, int height) {
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified
glLoadIdentity(); // Load the identity matrix before the transformations
gluPerspective(45.0, (float) width/height, 0.1f, 150.0f);
glMatrixMode(GL_MODELVIEW); // Specify the matrix that will be modified
glLoadIdentity(); // Load the identity matrix before the transformations
}

56
sources/demo/Scene.h Executable file
View File

@ -0,0 +1,56 @@
#ifndef SCENE_H
#define SCENE_H
// Libraries
#include "Context.h"
#include <SDL/SDL.h>
#include <GL/freeglut.h> // Used only to draw cubes
#include <GL/gl.h>
#include <GL/glu.h>
// Scene class
class Scene {
private :
GLfloat mat_specular[4]; // Material specular light color
GLfloat mat_shininess[1]; // Material shininess
GLfloat light_position[4]; // Position of the light source
GLfloat ambient_color[4]; // Ambient color of the light
GLfloat white_light[4]; // White light color
float camera_angle1; // Camera angle
float camera_angle2; // Camera angle
public :
Scene(); // constructor of the class
~Scene(); // Destructor of the class
void init(); // Initialize the values of OpenGL
void display(const Context& context) const; // display the scene
void reshape(int width, int height); // Reshape the window
float getCameraAngle1() const; // Return the angle of the camera
float getCameraAngle2() const; // Return the angle of the camera
void setCameraAngle1(float angle); // Set the angle of the camera
void setCameraAngle2(float angle); // Set the angle of the camera
};
// Return the angle of the camera (inline)
inline float Scene::getCameraAngle1() const {
return camera_angle1;
}
// Return the angle of the camera (inline)
inline float Scene::getCameraAngle2() const {
return camera_angle2;
}
// Set the angle of the camera (inline)
inline void Scene::setCameraAngle1(float angle) {
camera_angle1 = angle;
}
// Set the angle of the camera (inline)
inline void Scene::setCameraAngle2(float angle) {
camera_angle2 = angle;
}
#endif

70
sources/demo/Simulation.cpp Executable file
View File

@ -0,0 +1,70 @@
// Libraries
#include "Simulation.h"
#include "ReactDemo.h"
// Constructor of the class Simulation
Simulation::Simulation() {
simRunning = false;
mouseButtonPressed = false;
}
// Destructor of the class Simulation
Simulation::~Simulation() {
}
// Method to start the simulation
void Simulation::start() {
// Initialisation of the OpenGL settings for the scene
scene.init();
// Reshape the windows for the first time
scene.reshape(WINWIDTH, WINHEIGHT);
// Activation of the simulation
simRunning = true;
// Main loop of the simulation
while(simRunning) {
// Check if an SDL event occured and make the apropriate actions
checkEvents();
// Display the actual scene
scene.display(context);
}
}
// This method checks if an events occur and call the apropriate method
void Simulation::checkEvents() {
SDL_Event event; // An SDL event
// Check in the stack of events
while(SDL_PollEvent(&event)) {
// Check an event
switch(event.type) {
// An QUIT event occur
case SDL_QUIT: simRunning = false;
break;
// A keyboard key has been pushed
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
if (event.key.keysym.sym == SDLK_ESCAPE)
simRunning = false;
break;
// The size of the windows changed then we reshape the windows
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
break;
// If the mouse moved
case SDL_MOUSEMOTION: if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
// If the left mouse button is pressed then change the angle
scene.setCameraAngle1(scene.getCameraAngle1() + event.motion.xrel % 360);
scene.setCameraAngle2(scene.getCameraAngle2() + event.motion.yrel % 360);
}
break;
}
}
}

24
sources/demo/Simulation.h Executable file
View File

@ -0,0 +1,24 @@
#ifndef SIMULATION_H
#define SIMULATION_H
// Librairies
#include "Context.h"
#include "Scene.h"
// Class Simulation
class Simulation {
private :
Scene scene; // Scene object for displaying the simulation
Context context; // Context of the simulation
bool simRunning; // True if the simulation is running and false otherwise
bool mouseButtonPressed; // True if the left mouse button is pressed
public :
Simulation(); // Constructor of the class
~Simulation(); // Destructor of the class
void start(); // Start the simulation
void checkEvents(); // Check if SDL events occured and make the apropriate actions
};
#endif SIMULATION_H

87
sources/demo/main.cpp Executable file
View File

@ -0,0 +1,87 @@
// Libraries
#include "Simulation.h"
#include "ReactDemo.h"
#include <iostream>
#include <SDL/SDL.h>
// Prototypes
int initSDL();
// global variables
SDL_Surface * pScreen; // Pointer to the SDL windows
// Main function
int main(int argc, char** argv) {
if(initSDL() > 0) {
// If initSDL return an error then we exit the program
return EXIT_FAILURE;
}
// Initialize the glut library. We will use glut only to display shapes like cube or sphere
glutInit(&argc, argv);
// Create a Simulation object used to simulate a physic world
Simulation simulation;
// Start the simulation
simulation.start();
std::cerr << "Fin normale du programme" << std::endl;
// To avoid warnings notifying that argc et argv aren't used
(argc);
(argv);
return (EXIT_SUCCESS);
return 0;
}
// Methode to initialise the SDL and the windows
int initSDL() {
// Initialisation of SDL
if( SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "Echec SDL_Init : " << SDL_GetError() << std::endl;
return 1;
}
// Select the method to exit
atexit( SDL_Quit );
// Active double buffer mode
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// Select the Depth Buffer size
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
// Activation of displaying in windows mode
SDL_Surface* pScreen = SDL_SetVideoMode(WINWIDTH, WINHEIGHT, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_RESIZABLE | SDL_SWSURFACE);
// Check that displaying is activated
if( !pScreen ) {
std::cerr << "Echec de creation de la fenetre en 640x480 : " << SDL_GetError() << std::endl;
return 1;
}
// Define the window title and the window icon
SDL_WM_SetCaption("React Demo 0.0.1", NULL);
// Get the state of the Double Buffer parameter
int nValue;
if( SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &nValue) < 0) {
std::cerr << "Echec de recuperation du parametre SDL_GL_DOUBLEBUFFER : " << SDL_GetError() << std::endl;
return 1;
}
// Check that Double Buffer mode is activated
if(nValue != 1) {
std::cerr << "Erreur : SDL_GL_DOUBLEBUFFER inactif" << std::endl;
return 1;
}
return 0;
}