Début d'implémentation de la flycam.

This commit is contained in:
Georges Dupéron 2011-11-28 12:58:10 +01:00
parent 0d467384f3
commit 82491c509d
4 changed files with 36 additions and 15 deletions

View File

@ -1,4 +1,4 @@
#include "vertex.hh" #include "all_includes.hh"
Vertex::Vertex() {} Vertex::Vertex() {}
@ -13,7 +13,7 @@ Vertex operator+(const Vertex& u, const Vertex& v) {
} }
Vertex operator-(const Vertex& u, const Vertex& v) { 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& v) { Vertex operator-(const Vertex& v) {
@ -27,3 +27,12 @@ Vertex operator*(const Vertex& v, const int n) {
Vertex operator/(const Vertex& v, const int n) { Vertex operator/(const Vertex& v, const int n) {
return Vertex(v.x / n, v.y / n, v.z / n); return Vertex(v.x / n, v.y / n, v.z / n);
} }
Vertex Vertex::fromSpherical(float r, float xAngle, float yAngle) {
// http://electron9.phys.utk.edu/vectors/3dcoordinates.htm
return Vertex(
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)
);
}

View File

@ -1,7 +1,7 @@
#ifndef _VERTEX_HH_ #ifndef _VERTEX_HH_
#define _VERTEX_HH_ #define _VERTEX_HH_
#include <iostream> #include "all_includes.hh"
class Vertex { class Vertex {
public: public:
@ -11,6 +11,7 @@ public:
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);
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);

View File

@ -1,6 +1,6 @@
#include "all_includes.hh" #include "all_includes.hh"
View::View(Chose* root) : root(root), cameraDist(300), xSight(0), ySight(0), zSight(0), xAngle(0), yAngle(0), moveDist(10) { View::View(Chose* root) : root(root), cameraCenter(500,500,10), cameraDist(300), xSight(0), ySight(0), zSight(0), xAngle(0), yAngle(0), moveDist(10) {
initWindow(); initWindow();
mainLoop(); mainLoop();
} }
@ -59,6 +59,21 @@ void View::displayAxes() {
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(0.0f, 0.0f, -2500.0f); // ending point of the line glVertex3f(0.0f, 0.0f, -2500.0f); // ending point of the line
glEnd( ); glEnd( );
Vertex dest = Vertex::fromSpherical(100, xAngle, yAngle);
glBegin(GL_LINES);
glColor3ub(255,0,255);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3d(dest.x, dest.y, dest.z); // ending point of the line
glEnd( );
dest = cameraCenter - dest;
glBegin(GL_LINES);
glColor3ub(255,255,0);
glVertex3d(cameraCenter.x, cameraCenter.y, cameraCenter.z); // origin of the line
glVertex3d(dest.x, dest.y, dest.z); // ending point of the line
glEnd( );
glEnable(GL_LIGHTING); glEnable(GL_LIGHTING);
} }
@ -71,9 +86,10 @@ void View::renderScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
//gluLookAt(0,0,cameraDist, 0, 0, 0,0,1,0); //gluLookAt(0,0,cameraDist, 0, 0, 0,0,1,0);
glTranslated(-xSight,-ySight,-(zSight+cameraDist)); // glTranslated(-xSight,-ySight,-(zSight+cameraDist));
glRotatef(-yAngle,1,0,0); glRotatef(-yAngle,1,0,0);
glRotatef(-xAngle,0,0,1); glRotatef(-xAngle,0,0,1);
glTranslated(-cameraCenter.x, -cameraCenter.y, -cameraCenter.z);
displayAxes(); displayAxes();
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
@ -84,7 +100,6 @@ void View::renderScene() {
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
} }
void View::mainLoop() { void View::mainLoop() {
short continuer = 1; short continuer = 1;
SDL_Event event; SDL_Event event;
@ -98,18 +113,11 @@ void View::mainLoop() {
case SDL_KEYDOWN: case SDL_KEYDOWN:
switch(event.key.keysym.sym) { switch(event.key.keysym.sym) {
case SDLK_DOWN: case SDLK_DOWN:
ySight -= moveDist; cameraCenter = cameraCenter + Vertex::fromSpherical(10, xAngle, yAngle);
break; break;
case SDLK_UP: case SDLK_UP:
ySight += moveDist; cameraCenter = cameraCenter - Vertex::fromSpherical(10, xAngle, yAngle);
break; break;
case SDLK_LEFT:
xSight -= moveDist;
break;
case SDLK_RIGHT:
xSight += moveDist;
break;
default: default:
break; break;
} }

View File

@ -15,6 +15,9 @@ private:
static const int windowWidth = 1024; static const int windowWidth = 1024;
static const int windowHeight = 768; static const int windowHeight = 768;
Vertex cameraCenter;
int cameraDist; int cameraDist;
int xSight; int xSight;