From 929679914e2080735d21af41bc5a08b5e9d49814 Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Wed, 4 Feb 2009 14:43:33 +0000 Subject: [PATCH] git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@57 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- sources/demo/Camera.cpp | 34 +++++++++ sources/demo/Camera.h | 91 ++++++++++++++++++++++++ sources/demo/Context.cpp | 76 ++++++++++++++++++++ sources/demo/Context.h | 47 +++++++++++++ sources/demo/Objects.cpp | 123 +++++++++++++++++++++++++++++++++ sources/demo/Objects.h | 79 +++++++++++++++++++++ sources/demo/OutSideCamera.cpp | 115 ++++++++++++++++++++++++++++++ sources/demo/OutSideCamera.h | 64 +++++++++++++++++ sources/demo/ReactDemo.h | 27 ++++++++ sources/demo/Scene.cpp | 118 +++++++++++++++++++++++++++++++ sources/demo/Scene.h | 61 ++++++++++++++++ sources/demo/Simulation.cpp | 117 +++++++++++++++++++++++++++++++ sources/demo/Simulation.h | 46 ++++++++++++ sources/demo/main.cpp | 106 ++++++++++++++++++++++++++++ 14 files changed, 1104 insertions(+) create mode 100644 sources/demo/Camera.cpp create mode 100644 sources/demo/Camera.h create mode 100755 sources/demo/Context.cpp create mode 100755 sources/demo/Context.h create mode 100755 sources/demo/Objects.cpp create mode 100755 sources/demo/Objects.h create mode 100644 sources/demo/OutSideCamera.cpp create mode 100644 sources/demo/OutSideCamera.h create mode 100755 sources/demo/ReactDemo.h create mode 100755 sources/demo/Scene.cpp create mode 100755 sources/demo/Scene.h create mode 100755 sources/demo/Simulation.cpp create mode 100755 sources/demo/Simulation.h create mode 100755 sources/demo/main.cpp diff --git a/sources/demo/Camera.cpp b/sources/demo/Camera.cpp new file mode 100644 index 00000000..4d9e6eec --- /dev/null +++ b/sources/demo/Camera.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** + * 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 "Camera.h" + +// Initialization of static variables +double Camera::speed=0.5; + +// Constructor of the camera +Camera::Camera() { + +} + +// Destructor of the camera +Camera::~Camera() { + +} diff --git a/sources/demo/Camera.h b/sources/demo/Camera.h new file mode 100644 index 00000000..7ca15361 --- /dev/null +++ b/sources/demo/Camera.h @@ -0,0 +1,91 @@ +/**************************************************************************** + * 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 CAMERA_H +#define CAMERA_H + +// Libraries +#include "../reactphysics3d/reactphysics3d.h" // We want the mathematics stuff of reactphysics3d + +// Namespaces +using namespace reactphysics3d; + +// Class Camera (abstract class) +// In the project we will use two different camera. This is the superclass of the two +// cameras. We will use a OutSideCamera that move arround the scene and an OnBoardCamera +// that will simulate the deplacement of a viewer inside the scene +class Camera { + protected : + Vector3D position; // Position of the camera + Vector3D lookAtPoint; // Point where the camera is looking + Vector3D viewVector; // Vector from the camera position to the view point + static double speed; // Speed movement of the camera + + public : + Camera(); // Constructor + Camera(const Camera& camera); // Copy-constructor + virtual ~Camera(); // Destructor + virtual Vector3D getPosition() const; // Get the position of the camera + virtual void setPosition(const Vector3D& pos); // Set the position of the camera + virtual Vector3D getLookAtPoint() const; // Return the point where the camera is looking + virtual Vector3D getViewVector() const; // Return the view vector of the camera + virtual void updateViewVector()=0; // Update the view vector of the camera + static void increaseSpeed(); // Increase the speed of camera movement + static void decreaseSpeed(); // Decrease the speed of camera movement +}; + +// Get the position of the camera (inline function) +inline Vector3D Camera::getPosition() const { + // Return the position of the camera + return position; +} + +// Set the position of the camera (inline function) +inline void Camera::setPosition(const Vector3D& pos) { + // Set the position of the camera + position = pos; +} + +// Return the point where the camera is looking (inline function) +inline Vector3D Camera::getLookAtPoint() const { + return lookAtPoint; +} + +// Return the view vector of the camera (inline function) +inline Vector3D Camera::getViewVector() const { + return viewVector; +} + +// Increase the speed movement of the camera (inline function) +inline void Camera::increaseSpeed() +{ + speed= speed+0.05; + if(speed>1) + speed=1; +} + +// Decrease the speep movement of the camera (inline function) +inline void Camera::decreaseSpeed() +{ + speed = speed-0.05; + if(speed<=0) + speed=0.005; +} + +#endif diff --git a/sources/demo/Context.cpp b/sources/demo/Context.cpp new file mode 100755 index 00000000..cefb64f9 --- /dev/null +++ b/sources/demo/Context.cpp @@ -0,0 +1,76 @@ +/**************************************************************************** + * 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" + +#include +#include + + +// 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, Vector3D(-1.0, 0.0, 0.0), Vector3D(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. * + ***************************************************************************/ + +#ifndef CONTEXT_H +#define CONTEXT_H + +// Libraries +#include "Objects.h" + +#include + +// Class Context +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 +}; + +// Method (inline) to get the number of objects in the context +inline int Context::getNbObjects() const { + return vectObjects.size(); +} + +#endif diff --git a/sources/demo/Objects.cpp b/sources/demo/Objects.cpp new file mode 100755 index 00000000..a1b5b1b5 --- /dev/null +++ b/sources/demo/Objects.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** + * 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" + +//#include // To avoid an error due to the #include +#include +#include + +// ----- 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 Vector3D& d1, const Vector3D& 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 = d1.crossProduct(d2).getUnit(); +} + +// 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.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 new file mode 100755 index 00000000..5e68f769 --- /dev/null +++ b/sources/demo/Objects.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * 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, ... +*/ + +// ----- 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 + 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 +}; + +#endif diff --git a/sources/demo/OutSideCamera.cpp b/sources/demo/OutSideCamera.cpp new file mode 100644 index 00000000..b830c6ab --- /dev/null +++ b/sources/demo/OutSideCamera.cpp @@ -0,0 +1,115 @@ +/**************************************************************************** + * 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 "OutSideCamera.h" +#include + +// Constants +const double PI = 3.141592; + +// Constructor +OutSideCamera::OutSideCamera() { + + // Initialize the attributes + heightFromFloor = 20.0; + horizontalAngleRotation = 0; + verticalAngleRotation = 45; + distanceFromOrigin = 10.0; + lookAtPoint.setAllValues(0.0, 0.0, 0.0); + + // Update the position of the camera + updatePosition(); + + // Update the view vector of the camera + updateViewVector(); +} + +// Destructor +OutSideCamera::~OutSideCamera() { + +} + +// Compute the new position of the camera +void OutSideCamera::updatePosition() { + + // Compute the floor distance from origin + double floorDistance = distanceFromOrigin * cos(PI/180.0 * verticalAngleRotation); + + // Update the position of the camera + position.setAllValues(floorDistance*cos(PI/180.0 * horizontalAngleRotation), distanceFromOrigin*sin(PI/180*verticalAngleRotation), + floorDistance*sin(PI/180.0 * horizontalAngleRotation)); +} + +// Set the camera rotation angle and update the position of the camera +void OutSideCamera::modifyHorizontalAngleRotation(int screenDistance, float fps) { + + // Update the horizontal rotation angle of the camera + horizontalAngleRotation = (horizontalAngleRotation + int(screenDistance * 60.0 / fps)) % 360; + + // Update the position and the view vector of the camera + updatePosition(); + updateViewVector(); +} + +// Set the vertical camera rotation angle +void OutSideCamera::modifyVerticalAngleRotation(int screenDistance, float fps) { + + // Update the vertical rotation angle of the camera + verticalAngleRotation = verticalAngleRotation + (screenDistance * 60.0 / fps); + + // Vertical angle limits + if (verticalAngleRotation > 89) { + verticalAngleRotation = 89; + } + if (verticalAngleRotation < 1) { + verticalAngleRotation = 1; + } + + // Update the position and the view vector of the camera + updatePosition(); + updateViewVector(); +} + +// Increase the distance from origine of the camera (used for the zoom) +void OutSideCamera::increaseDistance(float fps) { + + // Increase the distance from origin + distanceFromOrigin = distanceFromOrigin + (speed * 60 / fps); + + // Update the position and the view vector of the camera + updatePosition(); + updateViewVector(); +} + +// Decrease the distance from origine of the camera (used for the zoom) +void OutSideCamera::decreaseDistance(float fps) { + + // Decrease the distance from origin + distanceFromOrigin = distanceFromOrigin - (speed * 60 / fps); + + // Limit condition + if(distanceFromOrigin < 1) { + distanceFromOrigin=1; + } + + // Update the position and the view vector of the camera + updatePosition(); + updateViewVector(); +} diff --git a/sources/demo/OutSideCamera.h b/sources/demo/OutSideCamera.h new file mode 100644 index 00000000..b0a61027 --- /dev/null +++ b/sources/demo/OutSideCamera.h @@ -0,0 +1,64 @@ +/**************************************************************************** + * 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 OUTSIDE_CAMERA_H +#define OUTSIDE_CAMERA_H + +// Libraries +#include "Camera.h" + +// ---- Class OutSideCamera ---- +// This camera can be move everywhere around the scene to obtain a outside view of the house +class OutSideCamera : public Camera { + private : + double heightFromFloor; // Height from the floor + int horizontalAngleRotation; // Horizontal rotation angle (in degree) + int verticalAngleRotation; // Vertical rotation angle (in degree) + double distanceFromOrigin; // Distance of the camera from the origin (used to zoom) + + public : + OutSideCamera(); // Constructor + OutSideCamera(const OutSideCamera& camera); // Copy-constructor + virtual ~OutSideCamera(); // Destructor + void updatePosition(); // Compute the new position of the camera + void updateViewVector(); // Update the view vector of the camera + double getHeightFromFloor() const; // Get the height of the camera from the floor + void setHeightFromFloor(double height); // Set the height of the camera from the floor + void modifyHorizontalAngleRotation(int screenDistance, float fps); // Modify the horizontal camera rotation angle + void modifyVerticalAngleRotation(int screenDistance, float fps); // Modify the vertical camera rotation angle + void increaseDistance(float fps); // Increase the distance of the camera from the origin + void decreaseDistance(float fps); // Decrease the distance of the camera from the origin +}; + +// Compute the new view vector of the camera (inline function) +inline void OutSideCamera::updateViewVector() { + viewVector = Vector3D(0.0, 0.0, 0.0) - position; +} + +// Get the height of the camera from the floor (inline function) +inline double OutSideCamera::getHeightFromFloor() const { + return heightFromFloor; +} + +// Set the height of the camera from the floor (inline function) +inline void OutSideCamera::setHeightFromFloor(double height) { + heightFromFloor = height; +} + +#endif diff --git a/sources/demo/ReactDemo.h b/sources/demo/ReactDemo.h new file mode 100755 index 00000000..dc5a56ea --- /dev/null +++ b/sources/demo/ReactDemo.h @@ -0,0 +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 + +#endif diff --git a/sources/demo/Scene.cpp b/sources/demo/Scene.cpp new file mode 100755 index 00000000..9e85d364 --- /dev/null +++ b/sources/demo/Scene.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** + * 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" +#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; +} + +// 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 + //glEnable(GL_CULL_FACE); +} + +// 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); + 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 +#include +#include + + +// 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 + 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 + 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 a reference to the camera +inline OutSideCamera& Scene::getOutSideCamera() { + return outsideCamera; +} + +#endif diff --git a/sources/demo/Simulation.cpp b/sources/demo/Simulation.cpp new file mode 100755 index 00000000..76a2facc --- /dev/null +++ b/sources/demo/Simulation.cpp @@ -0,0 +1,117 @@ +/**************************************************************************** + * 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() { + simRunning = 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; + + // 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); + + // 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 + + // 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; + 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)) { + // 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 new file mode 100755 index 00000000..2130620b --- /dev/null +++ b/sources/demo/Simulation.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * 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 + +// 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 + 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 +}; + +#endif diff --git a/sources/demo/main.cpp b/sources/demo/main.cpp new file mode 100755 index 00000000..16195677 --- /dev/null +++ b/sources/demo/main.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** + * 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 "../reactphysics3d/reactphysics3d.h" +#include "Simulation.h" +#include "ReactDemo.h" + +#include +#include + + +// 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.01", 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; +}