git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@17 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2009-02-02 13:55:12 +00:00
parent e0ea6f4a43
commit 6ae5fbd15b
10 changed files with 315 additions and 141 deletions

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "Context.h"
@ -13,7 +31,7 @@ Context::Context() {
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));
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);

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef CONTEXT_H
#define CONTEXT_H

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "Objects.h"
@ -6,23 +24,6 @@
#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
@ -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();
}

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#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
};

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef REACT_DEMO_H
#define REACT_DEMO_H

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Librairies
#include "Scene.h"
@ -31,10 +49,6 @@ Scene::Scene() {
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
@ -59,6 +73,7 @@ void Scene::init() {
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,11 +84,11 @@ 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<context.getNbObjects(); ++i)

View File

@ -1,8 +1,28 @@
/****************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef SCENE_H
#define SCENE_H
// Libraries
#include "Context.h"
#include "OutSideCamera.h"
#include <SDL/SDL.h>
#include <GL/freeglut.h> // Used only to draw cubes
@ -18,8 +38,7 @@ class Scene {
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
OutSideCamera outsideCamera; // OutSide camera (Camera that can move around the scene)
public :
Scene(); // constructor of the class
@ -27,30 +46,16 @@ class Scene {
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
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

View File

@ -1,13 +1,32 @@
/****************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "Simulation.h"
#include "ReactDemo.h"
#include <iostream>
// Constructor of the class Simulation
Simulation::Simulation() {
simRunning = false;
mouseButtonPressed = false;
fps = 0.0;
}
// Destructor of the class Simulation
@ -26,6 +45,9 @@ void Simulation::start() {
// Activation of the simulation
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
@ -33,6 +55,11 @@ void Simulation::start() {
// Display the actual scene
scene.display(context);
// Compute the fps (framerate)
computeFps();
//std::cout << fps << std::endl;
}
}
@ -40,6 +67,14 @@ void Simulation::start() {
void Simulation::checkEvents() {
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
@ -60,11 +95,23 @@ void Simulation::checkEvents() {
// 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;
// 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);
}

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
#ifndef SIMULATION_H
#define SIMULATION_H
@ -12,6 +31,10 @@ class 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
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
@ -20,5 +43,4 @@ class Simulation {
void checkEvents(); // Check if SDL events occured and make the apropriate actions
};
#endif SIMULATION_H
#endif

View File

@ -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 <http://www.gnu.org/licenses/>. *
***************************************************************************/
// 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;