Une version améliorée de la freeFly.
This commit is contained in:
parent
fee757a5ad
commit
80aa4e35d5
|
@ -2,11 +2,6 @@
|
||||||
#define _TRIANGLE_HH_
|
#define _TRIANGLE_HH_
|
||||||
|
|
||||||
#include "all_includes.hh"
|
#include "all_includes.hh"
|
||||||
struct Vertexf {
|
|
||||||
float x;
|
|
||||||
float y;
|
|
||||||
float z;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Triangle {
|
class Triangle {
|
||||||
public:
|
public:
|
||||||
|
|
56
vertex.cpp
56
vertex.cpp
|
@ -16,6 +16,14 @@ Vertex operator-(const Vertex& u, const Vertex& v) {
|
||||||
return Vertex(u.x - v.x, u.y - v.y, u.z - v.z);
|
return Vertex(u.x - v.x, u.y - v.y, u.z - v.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vertex operator+(const Vertex& u, const Vertexf& v) {
|
||||||
|
return Vertex(u.x + v.x, u.y + v.y, u.z + v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertex operator-(const Vertex& u, const Vertexf& v) {
|
||||||
|
return Vertex(u.x - v.x, u.y - v.y, u.z - v.z);
|
||||||
|
}
|
||||||
|
|
||||||
Vertex operator-(const Vertex& v) {
|
Vertex operator-(const Vertex& v) {
|
||||||
return Vertex(-v.x, -v.y, -v.z);
|
return Vertex(-v.x, -v.y, -v.z);
|
||||||
}
|
}
|
||||||
|
@ -36,3 +44,51 @@ Vertex Vertex::fromSpherical(float r, float xAngle, float yAngle) {
|
||||||
r * std::cos(xAngle / 180 * 3.14159)
|
r * std::cos(xAngle / 180 * 3.14159)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Vertexf::Vertexf() {}
|
||||||
|
|
||||||
|
Vertexf::Vertexf(float x, float y, float z): x(x), y(y), z(z) {}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Vertexf& v) {
|
||||||
|
return os << "(" << v.x << "," << v.y << "," << v.z << ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator+(const Vertexf& u, const Vertexf& v) {
|
||||||
|
return Vertexf(u.x + v.x, u.y + v.y, u.z + v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator-(const Vertexf& u, const Vertex& v) {
|
||||||
|
return Vertexf(u.x - v.x, u.y - v.y, u.z - v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator+(const Vertexf& u, const Vertex& v) {
|
||||||
|
return Vertexf(u.x + v.x, u.y + v.y, u.z + v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator-(const Vertexf& u, const Vertexf& v) {
|
||||||
|
return Vertexf(u.x - v.x, u.y - v.y, u.z - v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator-(const Vertexf& v) {
|
||||||
|
return Vertexf(-v.x, -v.y, -v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator*(const Vertexf& v, const int n) {
|
||||||
|
return Vertexf(v.x * n, v.y * n, v.z * n);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf operator/(const Vertexf& v, const int n) {
|
||||||
|
return Vertexf(v.x / n, v.y / n, v.z / n);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertexf Vertexf::fromSpherical(float r, float xAngle, float yAngle) {
|
||||||
|
// http://electron9.phys.utk.edu/vectors/3dcoordinates.htm
|
||||||
|
return Vertexf(
|
||||||
|
r * std::sin(xAngle / 180 * 3.14159) * std::cos(yAngle / 180 * 3.14159),
|
||||||
|
r * std::sin(xAngle / 180 * 3.14159) * std::sin(yAngle / 180 * 3.14159),
|
||||||
|
r * std::cos(xAngle / 180 * 3.14159)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
33
vertex.hh
33
vertex.hh
|
@ -2,23 +2,50 @@
|
||||||
#define _VERTEX_HH_
|
#define _VERTEX_HH_
|
||||||
|
|
||||||
#include "all_includes.hh"
|
#include "all_includes.hh"
|
||||||
|
class Vertexf;
|
||||||
|
|
||||||
class Vertex {
|
class Vertex {
|
||||||
public:
|
public:
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
int z;
|
int z;
|
||||||
public:
|
|
||||||
|
public:
|
||||||
Vertex();
|
Vertex();
|
||||||
Vertex(int x, int y, int z);
|
Vertex(int x, int y, int z);
|
||||||
static Vertex fromSpherical(float r, float xAngle, float yAngle);
|
static Vertex fromSpherical(float r, float xAngle, float yAngle);
|
||||||
public:
|
|
||||||
|
public:
|
||||||
friend std::ostream& operator<<(std::ostream& os, const Vertex& v);
|
friend std::ostream& operator<<(std::ostream& os, const Vertex& v);
|
||||||
friend Vertex operator+(const Vertex& u, const Vertex& v);
|
friend Vertex operator+(const Vertex& u, const Vertex& v);
|
||||||
friend Vertex operator-(const Vertex& u, const Vertex& v);
|
friend Vertex operator-(const Vertex& u, const Vertex& v);
|
||||||
friend Vertex operator-(const Vertex& v);
|
friend Vertex operator-(const Vertex& v);
|
||||||
friend Vertex operator*(const Vertex& v, const int n);
|
friend Vertex operator*(const Vertex& v, const int n);
|
||||||
friend Vertex operator/(const Vertex& v, const int n);
|
friend Vertex operator/(const Vertex& v, const int n);
|
||||||
|
friend Vertex operator+(const Vertex& u, const Vertexf& v);
|
||||||
|
friend Vertex operator-(const Vertex& u, const Vertexf& v);
|
||||||
|
};
|
||||||
|
|
||||||
|
class Vertexf {
|
||||||
|
public:
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Vertexf();
|
||||||
|
Vertexf(float x, float y, float z);
|
||||||
|
static Vertexf fromSpherical(float r, float xAngle, float yAngle);
|
||||||
|
|
||||||
|
public:
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const Vertex& v);
|
||||||
|
friend Vertexf operator+(const Vertexf& u, const Vertexf& v);
|
||||||
|
friend Vertexf operator-(const Vertexf& u, const Vertexf& v);
|
||||||
|
friend Vertexf operator-(const Vertexf& v);
|
||||||
|
friend Vertexf operator*(const Vertexf& v, const int n);
|
||||||
|
friend Vertexf operator/(const Vertexf& v, const int n);
|
||||||
|
friend Vertexf operator+(const Vertexf& u, const Vertex& v);
|
||||||
|
friend Vertexf operator-(const Vertexf& u, const Vertex& v);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
99
view.cpp
99
view.cpp
|
@ -1,7 +1,6 @@
|
||||||
#include "all_includes.hh"
|
#include "all_includes.hh"
|
||||||
|
|
||||||
View::View(Chose* root) : root(root), cameraCenter(127,14,128), xAngle(44), yAngle(101), moveDist(4), mouseSensitivity(0.4) {
|
View::View(Chose* root) : root(root), camera(Camera(Vertexf(127,14,128),44,101,40,0.6)) {
|
||||||
cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle);
|
|
||||||
initWindow();
|
initWindow();
|
||||||
mainLoop();
|
mainLoop();
|
||||||
}
|
}
|
||||||
|
@ -73,14 +72,15 @@ void View::displayAxes() {
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
void View::renderScene() {
|
void View::renderScene(int lastTime, int currentTime) {
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
|
||||||
|
|
||||||
cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle);
|
camera.animation(currentTime-lastTime);
|
||||||
gluLookAt(cameraCenter.x,cameraCenter.y,cameraCenter.z, cameraSight.x, cameraSight.y, cameraSight.z,0,0,1);
|
camera.setCamera();
|
||||||
|
|
||||||
setLight();
|
setLight();
|
||||||
displayAxes();
|
displayAxes();
|
||||||
root->display();
|
root->display();
|
||||||
|
@ -96,15 +96,21 @@ void View::mainLoop() {
|
||||||
SDL_WM_GrabInput(SDL_GRAB_ON);
|
SDL_WM_GrabInput(SDL_GRAB_ON);
|
||||||
SDL_ShowCursor(SDL_DISABLE);
|
SDL_ShowCursor(SDL_DISABLE);
|
||||||
|
|
||||||
|
int lastTime = SDL_GetTicks() - 30;
|
||||||
|
int currentTime = 0;
|
||||||
|
|
||||||
while (continuer) {
|
while (continuer) {
|
||||||
|
lastTime = currentTime;
|
||||||
|
currentTime = SDL_GetTicks();
|
||||||
while ( SDL_PollEvent(&event) ) {
|
while ( SDL_PollEvent(&event) ) {
|
||||||
switch(event.type) {
|
switch(event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
continuer = 0;
|
continuer = 0;
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
switch(event.key.keysym.sym) {
|
case SDL_KEYUP:
|
||||||
|
camera.keyboard(event.key);
|
||||||
|
/*switch(event.key.keysym.sym) {
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
cameraCenter = cameraCenter - Vertex::fromSpherical(moveDist, yAngle, xAngle);
|
cameraCenter = cameraCenter - Vertex::fromSpherical(moveDist, yAngle, xAngle);
|
||||||
break;
|
break;
|
||||||
|
@ -133,16 +139,11 @@ void View::mainLoop() {
|
||||||
std::cout << "Camera = " << cameraCenter << " xAngle = " << xAngle << " yAngle = " << yAngle << std::endl;
|
std::cout << "Camera = " << cameraCenter << " xAngle = " << xAngle << " yAngle = " << yAngle << std::endl;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}*/
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
xAngle -= event.motion.xrel*mouseSensitivity;
|
camera.mouseMotion(event.motion);
|
||||||
yAngle += event.motion.yrel*mouseSensitivity;
|
|
||||||
if(yAngle > 179)
|
|
||||||
yAngle = 179;
|
|
||||||
else if(yAngle < 1)
|
|
||||||
yAngle = 1;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -150,8 +151,76 @@ void View::mainLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderScene();
|
renderScene(lastTime,currentTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Camera::Camera(Vertexf pos, float xA, float yA, int moveSensitivity, float mouseSensitivity) {
|
||||||
|
cameraCenter = pos;
|
||||||
|
xAngle = xA;
|
||||||
|
yAngle = yA;
|
||||||
|
cameraSight = cameraCenter + Vertexf::fromSpherical(100,yA,xA);
|
||||||
|
moveDist = moveSensitivity;
|
||||||
|
this->mouseSensitivity = mouseSensitivity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::setCamera() {
|
||||||
|
cameraSight = cameraCenter + Vertexf::fromSpherical(100, yAngle, xAngle);
|
||||||
|
gluLookAt(cameraCenter.x,cameraCenter.y,cameraCenter.z, cameraSight.x, cameraSight.y, cameraSight.z,0,0,1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::mouseMotion(const SDL_MouseMotionEvent &event) {
|
||||||
|
xAngle -= event.xrel*mouseSensitivity;
|
||||||
|
yAngle += event.yrel*mouseSensitivity;
|
||||||
|
if(yAngle > 179)
|
||||||
|
yAngle = 179;
|
||||||
|
else if(yAngle < 1)
|
||||||
|
yAngle = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::keyboard(const SDL_KeyboardEvent &eventKey) {
|
||||||
|
switch(eventKey.keysym.sym) {
|
||||||
|
case SDLK_UP:
|
||||||
|
up = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_DOWN:
|
||||||
|
down = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_LEFT:
|
||||||
|
left = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_RIGHT:
|
||||||
|
right = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_PAGEUP:
|
||||||
|
pageUp = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_PAGEDOWN:
|
||||||
|
pageDown = (eventKey.type == SDL_KEYDOWN);
|
||||||
|
break;
|
||||||
|
case SDLK_ESCAPE:
|
||||||
|
exit(0);
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::animation(int elapsedTime) {
|
||||||
|
float diff = ((float)(elapsedTime+1)/1000.)*(float)moveDist;
|
||||||
|
|
||||||
|
if(up)
|
||||||
|
cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, yAngle, xAngle);
|
||||||
|
if(down)
|
||||||
|
cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, yAngle, xAngle);
|
||||||
|
if(left)
|
||||||
|
cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, 90, xAngle - 90);
|
||||||
|
if(right)
|
||||||
|
cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, 90, xAngle - 90);
|
||||||
|
if(pageUp)
|
||||||
|
cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, yAngle + 90, xAngle);
|
||||||
|
if(pageDown)
|
||||||
|
cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, yAngle + 90, xAngle);
|
||||||
|
}
|
||||||
|
|
55
view.hh
55
view.hh
|
@ -9,28 +9,43 @@
|
||||||
// Calcul correct des normales dans triangle.cpp
|
// Calcul correct des normales dans triangle.cpp
|
||||||
// Prendre en compte tous les évènements X en attente avant de relancer le rendu.
|
// Prendre en compte tous les évènements X en attente avant de relancer le rendu.
|
||||||
|
|
||||||
class Camera;
|
class Camera {
|
||||||
|
|
||||||
class View {
|
|
||||||
private:
|
private:
|
||||||
Chose* root;
|
Vertexf cameraCenter;
|
||||||
|
Vertexf cameraSight;
|
||||||
static const int windowWidth = 1024;
|
|
||||||
static const int windowHeight = 768;
|
|
||||||
|
|
||||||
Vertex cameraCenter;
|
|
||||||
Vertex cameraSight;
|
|
||||||
|
|
||||||
float xAngle;
|
float xAngle;
|
||||||
float yAngle;
|
float yAngle;
|
||||||
int moveDist;
|
int moveDist;
|
||||||
float mouseSensitivity;
|
float mouseSensitivity;
|
||||||
|
bool up;
|
||||||
|
bool down;
|
||||||
|
bool left;
|
||||||
|
bool right;
|
||||||
|
bool pageUp;
|
||||||
|
bool pageDown;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Camera(Vertexf pos, float xA, float yA, int moveSensitivity, float mouseSensitivity);
|
||||||
|
void setCamera();
|
||||||
|
void mouseMotion(const SDL_MouseMotionEvent &event);
|
||||||
|
void keyboard(const SDL_KeyboardEvent &event);
|
||||||
|
void animation(int elapsedTime);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class View {
|
||||||
|
private:
|
||||||
|
Chose* root;
|
||||||
|
Camera camera;
|
||||||
|
|
||||||
|
static const int windowWidth = 1024;
|
||||||
|
static const int windowHeight = 768;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
View(Chose* root);
|
View(Chose* root);
|
||||||
void initWindow();
|
void initWindow();
|
||||||
void mainLoop();
|
void mainLoop();
|
||||||
void renderScene();
|
void renderScene(int lastTime, int currentTime);
|
||||||
void displayAxes();
|
void displayAxes();
|
||||||
|
|
||||||
static void setColor(unsigned char r, unsigned char g, unsigned char b);
|
static void setColor(unsigned char r, unsigned char g, unsigned char b);
|
||||||
|
@ -39,20 +54,4 @@ class View {
|
||||||
void setLight();
|
void setLight();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Camera {
|
|
||||||
private:
|
|
||||||
Vertex cameraCenter;
|
|
||||||
Vertex cameraSight;
|
|
||||||
float xAngle;
|
|
||||||
float yAngle;
|
|
||||||
int moveDist;
|
|
||||||
float mouseSensitivity;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Camera(Vertex pos, float xA, float yA, int moveSensitivity, float mouseSensitivity);
|
|
||||||
void setCamera() const;
|
|
||||||
void mouseMotion(const SDL_MouseMotionEvent &event);
|
|
||||||
void keyboard(const SDL_KeyboardEvent &event);
|
|
||||||
void animation() const;
|
|
||||||
};
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user