From 82491c509db8dbdac6aa3f66139d239016f91d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Georges=20Dup=C3=A9ron?= Date: Mon, 28 Nov 2011 12:58:10 +0100 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20d'impl=C3=A9mentation=20de=20la=20?= =?UTF-8?q?flycam.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vertex.cpp | 13 +++++++++++-- vertex.hh | 3 ++- view.cpp | 32 ++++++++++++++++++++------------ view.hh | 3 +++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/vertex.cpp b/vertex.cpp index aa8efe0..664bea7 100644 --- a/vertex.cpp +++ b/vertex.cpp @@ -1,4 +1,4 @@ -#include "vertex.hh" +#include "all_includes.hh" Vertex::Vertex() {} @@ -13,7 +13,7 @@ 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) { @@ -27,3 +27,12 @@ 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); } + +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) + ); +} diff --git a/vertex.hh b/vertex.hh index 2912bf5..8bcc1b3 100644 --- a/vertex.hh +++ b/vertex.hh @@ -1,7 +1,7 @@ #ifndef _VERTEX_HH_ #define _VERTEX_HH_ -#include +#include "all_includes.hh" class Vertex { public: @@ -11,6 +11,7 @@ public: public: Vertex(); Vertex(int x, int y, int z); + static Vertex fromSpherical(float r, float xAngle, float yAngle); public: friend std::ostream& operator<<(std::ostream& os, const Vertex& v); friend Vertex operator+(const Vertex& u, const Vertex& v); diff --git a/view.cpp b/view.cpp index d2cbcb2..2bfbac8 100644 --- a/view.cpp +++ b/view.cpp @@ -1,6 +1,6 @@ #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(); mainLoop(); } @@ -59,6 +59,21 @@ void View::displayAxes() { glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line glVertex3f(0.0f, 0.0f, -2500.0f); // ending point of the line 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); } @@ -71,9 +86,10 @@ void View::renderScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; //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(-xAngle,0,0,1); + glTranslated(-cameraCenter.x, -cameraCenter.y, -cameraCenter.z); displayAxes(); glBegin(GL_TRIANGLES); @@ -84,7 +100,6 @@ void View::renderScene() { SDL_GL_SwapBuffers(); } - void View::mainLoop() { short continuer = 1; SDL_Event event; @@ -98,18 +113,11 @@ void View::mainLoop() { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_DOWN: - ySight -= moveDist; + cameraCenter = cameraCenter + Vertex::fromSpherical(10, xAngle, yAngle); break; case SDLK_UP: - ySight += moveDist; + cameraCenter = cameraCenter - Vertex::fromSpherical(10, xAngle, yAngle); break; - case SDLK_LEFT: - xSight -= moveDist; - break; - case SDLK_RIGHT: - xSight += moveDist; - break; - default: break; } diff --git a/view.hh b/view.hh index 0defe9f..4fb8545 100644 --- a/view.hh +++ b/view.hh @@ -15,6 +15,9 @@ private: static const int windowWidth = 1024; static const int windowHeight = 768; + + Vertex cameraCenter; + int cameraDist; int xSight;