diff --git a/sources/demo/Context.cpp b/sources/demo/Context.cpp index a067eba2..cefb64f9 100755 --- a/sources/demo/Context.cpp +++ b/sources/demo/Context.cpp @@ -1,3 +1,21 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ // Libraries #include "Context.h" @@ -8,13 +26,13 @@ // 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)); - + 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, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0)); + addObject(cube1); addObject(cube2); addObject(cube3); @@ -26,7 +44,7 @@ Context::Context() { Context::~Context() { // Delete all the objects in vectObjects for(int i=0; i. * + ***************************************************************************/ + #ifndef CONTEXT_H #define CONTEXT_H @@ -10,14 +29,14 @@ class Context { private : std::vector 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 + void removeObject(int objectIndex); // Remove an object from the context }; // Method (inline) to get the number of objects in the context diff --git a/sources/demo/Objects.cpp b/sources/demo/Objects.cpp index c811a088..a1b5b1b5 100755 --- a/sources/demo/Objects.cpp +++ b/sources/demo/Objects.cpp @@ -1,3 +1,21 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ // Libraries #include "Objects.h" @@ -6,23 +24,6 @@ #include #include - -// ----- 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 @@ -77,7 +78,7 @@ void Cube::draw() const { // ----- Class Plane ----- // // Constructor of the class Plane -Plane::Plane(const Position& position, float width, float height, const Vector& d1, const Vector& d2) +Plane::Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2) :Object(position) { this->width = width; this->height = height; @@ -85,13 +86,7 @@ Plane::Plane(const Position& position, float width, float height, const Vector& 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; + normalVector = d1.crossProduct(d2).getUnit(); } // Destructor of the class Plane @@ -110,17 +105,17 @@ void Plane::draw() const { // 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); + glVertex3f(position.x + d1.getX() * halfWidth + d2.getX() * halfHeight , position.y + d1.getY() * halfWidth + d2.getY() * halfHeight + , position.z + d1.getZ() * halfWidth + d2.getZ() * halfHeight); + glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); + glVertex3f(position.x + d1.getX() * halfWidth - d2.getX() * halfHeight , position.y + d1.getY() * halfWidth - d2.getY() * halfHeight + , position.z + d1.getZ() * halfWidth - d2.getZ() * halfHeight); + glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); + glVertex3f(position.x - d1.getX() * halfWidth - d2.getX() * halfHeight , position.y - d1.getY() * halfWidth - d2.getY() * halfHeight + , position.z - d1.getZ() * halfWidth - d2.getZ() * halfHeight); + glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); + glVertex3f(position.x - d1.getX() * halfWidth + d2.getX() * halfHeight , position.y - d1.getY() * halfWidth + d2.getY() * halfHeight + , position.z - d1.getZ() * halfWidth + d2.getZ() * halfHeight); glEnd(); } diff --git a/sources/demo/Objects.h b/sources/demo/Objects.h index 3c588d43..5e68f769 100755 --- a/sources/demo/Objects.h +++ b/sources/demo/Objects.h @@ -1,20 +1,35 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + #ifndef OBJECTS_H #define OBJECTS_H + +// Libraries +#include "../reactphysics3d/reactphysics3d.h" + +// Namespaces +using namespace reactphysics3d; /* 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 { @@ -53,10 +68,10 @@ 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 + Vector3D d1; // Unit vector in the plane + Vector3D d2; // Unit vector in the plane + Vector3D normalVector; // Unit normal vector of the plane + Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2); // Constructor of the class Plane virtual ~Plane(); // Destructor of the class Plane virtual void draw() const; // Method to draw the plane }; diff --git a/sources/demo/ReactDemo.h b/sources/demo/ReactDemo.h index 25120535..dc5a56ea 100755 --- a/sources/demo/ReactDemo.h +++ b/sources/demo/ReactDemo.h @@ -1,8 +1,27 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + #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 +const int WINWIDTH = 640; // Width of the OpenGL windows +const int WINHEIGHT = 480; // Height of the OpenGL windows #endif diff --git a/sources/demo/Scene.cpp b/sources/demo/Scene.cpp index 8e836e95..9e85d364 100755 --- a/sources/demo/Scene.cpp +++ b/sources/demo/Scene.cpp @@ -1,3 +1,21 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ // Librairies #include "Scene.h" @@ -10,36 +28,32 @@ Scene::Scene() { 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 @@ -47,7 +61,7 @@ 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 @@ -55,10 +69,11 @@ void Scene::init() { 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 + + glEnable(GL_LIGHTING); // Activate the lighting + glEnable(GL_LIGHT0); // Activate a light source + glEnable(GL_DEPTH_TEST); // Activate the Depth buffer + //glEnable(GL_CULL_FACE); } // Display method @@ -69,25 +84,25 @@ void Scene::display(const Context& context) const { 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); - + //gluLookAt(30,10,0,0,0,0,0,1,0); + double x = outsideCamera.getPosition().getX(); + double y = outsideCamera.getPosition().getY(); + double z = outsideCamera.getPosition().getZ(); + gluLookAt(x,y,z,0,0,0,0,1,0); + // Draw all objects in the context for(int i=0; i. * + ***************************************************************************/ + #ifndef SCENE_H #define SCENE_H // Libraries #include "Context.h" +#include "OutSideCamera.h" #include #include // Used only to draw cubes @@ -17,40 +37,25 @@ class Scene { 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 + GLfloat white_light[4]; // White light color + OutSideCamera outsideCamera; // OutSide camera (Camera that can move around the scene) 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 + void reshape(int width, int height); // Reshape the window + OutSideCamera& getOutSideCamera() ; // Return a reference to the outside camera 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; + +// Return a reference to the camera +inline OutSideCamera& Scene::getOutSideCamera() { + return outsideCamera; } #endif diff --git a/sources/demo/Simulation.cpp b/sources/demo/Simulation.cpp index 36a290df..76a2facc 100755 --- a/sources/demo/Simulation.cpp +++ b/sources/demo/Simulation.cpp @@ -1,51 +1,86 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ // Libraries #include "Simulation.h" #include "ReactDemo.h" - +#include // Constructor of the class Simulation -Simulation::Simulation() { +Simulation::Simulation() { simRunning = false; - mouseButtonPressed = false; + mouseButtonPressed = false; + fps = 0.0; } // 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; + simRunning = true; + + // Get the current time + currentFrameTime = SDL_GetTicks(); // 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); + scene.display(context); + + // Compute the fps (framerate) + computeFps(); + + //std::cout << fps << std::endl; } } // This method checks if an events occur and call the apropriate method void Simulation::checkEvents() { - SDL_Event event; // An SDL event + SDL_Event event; // An SDL event + + // Zoom of the outside camera + if (SDL_GetKeyState(NULL)[SDLK_UP]) { + scene.getOutSideCamera().decreaseDistance(fps); + } + else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) { + scene.getOutSideCamera().increaseDistance(fps); + } // 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; + case SDL_QUIT: simRunning = false; break; // A keyboard key has been pushed @@ -57,14 +92,26 @@ void Simulation::checkEvents() { // 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; + + // If the mouse moved + case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) { + // Rotation of the outSideCamera + // TODO : Problem here when we try to implement fps indepence (if we try to + // replace 60 by the variable fps + scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, 30); + scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, 30); + } } } +} + +// Compute the framerate (fps) of the application +void Simulation::computeFps() { + double lastFrameTime = currentFrameTime; + + // Get the current time + currentFrameTime = SDL_GetTicks(); + + // Compute the new framerate + fps = 1000 / double(currentFrameTime - lastFrameTime); } diff --git a/sources/demo/Simulation.h b/sources/demo/Simulation.h index 678993c1..2130620b 100755 --- a/sources/demo/Simulation.h +++ b/sources/demo/Simulation.h @@ -1,3 +1,22 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ + #ifndef SIMULATION_H #define SIMULATION_H @@ -11,14 +30,17 @@ class Simulation { 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 - + bool mouseButtonPressed; // True if the left mouse button is pressed + double currentFrameTime; // Current frame time + double fps; // Framerate of the application + + void computeFps(); // Compute the framerate of the application + 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 + ~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 +#endif diff --git a/sources/demo/main.cpp b/sources/demo/main.cpp index e04ac727..16195677 100755 --- a/sources/demo/main.cpp +++ b/sources/demo/main.cpp @@ -1,5 +1,24 @@ +/**************************************************************************** + * Copyright (C) 2009 Daniel Chappuis * + **************************************************************************** + * This file is part of ReactPhysics3D. * + * * + * ReactPhysics3D is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * ReactPhysics3D is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with ReactPhysics3D. If not, see . * + ***************************************************************************/ -// Libraries +// Libraries +#include "../reactphysics3d/reactphysics3d.h" #include "Simulation.h" #include "ReactDemo.h" @@ -68,7 +87,7 @@ int initSDL() { } // Define the window title and the window icon - SDL_WM_SetCaption("React Demo 0.0.1", NULL); + SDL_WM_SetCaption("React Demo 0.01", NULL); // Get the state of the Double Buffer parameter int nValue;