commit 419db20c4a118d93bbfcf305056acc1ccf44dfcf
parent 80aad0bd141673316e9ef3b2bbb24e2c563cf6e7
Author: Yoann <yoann.b87@voila.fr>
Date: Sun, 18 Dec 2011 18:46:25 +0100
Passage des sommets en flottant.
Diffstat:
6 files changed, 86 insertions(+), 92 deletions(-)
diff --git a/triangle.cpp b/triangle.cpp
@@ -21,8 +21,8 @@ std::ostream& operator<<(std::ostream& os, const Triangle& t) {
return os << "Triangle " << t.v1 << "--" << t.v2 << "--" << t.v3 << "-- cycle";
}
-Vertexf Triangle::normal(Vertex v1, Vertex v2, Vertex v3) {
- Vertexf normal = (v1 - v2)*(v2 - v3);
+Vertex Triangle::normal(Vertex v1, Vertex v2, Vertex v3) {
+ Vertex normal = (v1 - v2)*(v2 - v3);
return normal / normal.norm();
}
@@ -59,7 +59,7 @@ void Triangle::display() {
// glVertex3d(v.x+vnormal.x*50,v.y+vnormal.y*50,v.z+vnormal.z*50);
// glEnd( );
// glEnable(GL_LIGHTING);
-
+
View::setColor(r,g,b);
glNormal3d(vnormal.x,vnormal.y,vnormal.z);
// glBegin(GL_TRIANGLES);
diff --git a/triangle.hh b/triangle.hh
@@ -11,7 +11,7 @@ class Triangle {
unsigned char r;
unsigned char g;
unsigned char b;
- Vertexf vnormal;
+ Vertex vnormal;
public :
friend std::ostream& operator<<(std::ostream& os, const Triangle* t);
@@ -26,7 +26,7 @@ class Triangle {
void display();
private :
- Vertexf normal(Vertex v1, Vertex v2, Vertex v3);
+ Vertex normal(Vertex v1, Vertex v2, Vertex v3);
};
#endif
diff --git a/vertex.cpp b/vertex.cpp
@@ -2,7 +2,7 @@
Vertex::Vertex() {}
-Vertex::Vertex(int x, int y, int z): x(x), y(y), z(z) {}
+Vertex::Vertex(float x, float y, float z): x(x), y(y), z(z) {}
float Vertex::norm() { return std::sqrt(x*x + y*y + z*z); }
@@ -31,7 +31,7 @@ Vertex Vertex::projectOn(Vertex v) {
return Vertex(((int64)v.x) * scalaire / normecarre, ((int64)v.y) * scalaire / normecarre, 0);
}
-Vertex Vertex::setNorm(int n) {
+Vertex Vertex::setNorm(float n) {
int64 current = norm();
return Vertex((int64)x * (int64)n / current, (int64)y * (int64)n / current, 0);
}
@@ -45,12 +45,13 @@ float Vertex::cosAngle(Vertex v) {
return ((double)(this->x*v.x + this->y*v.y)) / (((double)norm())*((double)v.norm()));
}
-Vertex::operator Vertexf() { return Vertexf(x,y,z); }
+Vertex::operator Vertex() { return Vertex(x,y,z); }
std::ostream& operator<<(std::ostream& os, const Vertex& v) {
return os << "(" << v.x << "," << v.y << "," << v.z << ")";
}
+
Vertex operator+(const Vertex& u, const Vertex& v) {
return Vertex(u.x + v.x, u.y + v.y, u.z + v.z);
}
@@ -59,19 +60,11 @@ Vertex operator-(const Vertex& u, const Vertex& 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& u, const Vertexf& v) {
- return Vertex(u.x - v.x, u.y - v.y, u.z - v.z);
-}
-
Vertex operator-(const Vertex& v) {
return Vertex(-v.x, -v.y, -v.z);
}
-Vertex operator*(const Vertex& v, const int n) {
+Vertex operator*(const Vertex& v, const float n) {
return Vertex(v.x * n, v.y * n, v.z * n);
}
@@ -103,63 +96,66 @@ Vertex Vertex::fromSpherical(float r, float xAngle, float yAngle) {
-Vertexf::Vertexf() {}
+/*
-Vertexf::Vertexf(float x, float y, float z): x(x), y(y), z(z) {}
+Vertex::Vertex() {}
+
+Vertex::Vertex(float x, float y, float z): x(x), y(y), z(z) {}
-float Vertexf::norm() { return std::sqrt(x*x + y*y + z*z); }
+float Vertex::norm() { return std::sqrt(x*x + y*y + z*z); }
-Vertexf::operator Vertex() { return Vertex(x,y,z); }
+Vertex::operator Vertex() { return Vertex(x,y,z); }
-std::ostream& operator<<(std::ostream& os, const Vertexf& v) {
+std::ostream& operator<<(std::ostream& os, const Vertex& 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);
+Vertex operator+(const Vertex& u, const Vertex& v) {
+ return Vertex(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);
+Vertex operator-(const Vertex& u, const Vertex& v) {
+ return Vertex(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);
+Vertex operator+(const Vertex& u, const Vertex& v) {
+ return Vertex(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);
+Vertex operator-(const Vertex& u, const Vertex& v) {
+ return Vertex(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);
+Vertex operator-(const Vertex& v) {
+ return Vertex(-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);
+Vertex operator*(const Vertex& v, const int n) {
+ return Vertex(v.x * n, v.y * n, v.z * n);
}
-Vertexf operator*(const Vertexf& u, const Vertexf& v) {
- return Vertexf(
+Vertex operator*(const Vertex& u, const Vertex& v) {
+ return Vertex(
(u.y * v.z) - (u.z * v.y),
(u.z * v.x) - (u.x * v.z),
(u.x * v.y) - (u.y * v.x)
);
}
-Vertexf operator/(const Vertexf& v, const int n) {
- return Vertexf(v.x / n, v.y / n, v.z / n);
+Vertex operator/(const Vertex& v, const int n) {
+ return Vertex(v.x / n, v.y / n, v.z / n);
}
-Vertexf operator/(const Vertexf& v, const float f) {
- return Vertexf(v.x / f, v.y / f, v.z / f);
+Vertex operator/(const Vertex& v, const float f) {
+ return Vertex(v.x / f, v.y / f, v.z / f);
}
-Vertexf Vertexf::fromSpherical(float r, float xAngle, float yAngle) {
+Vertex Vertex::fromSpherical(float r, float xAngle, float yAngle) {
// http://electron9.phys.utk.edu/vectors/3dcoordinates.htm
- return Vertexf(
+ 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
@@ -2,63 +2,61 @@
#define _VERTEX_HH_
#include "all_includes.hh"
-class Vertexf;
class Vertex {
public :
- int x;
- int y;
- int z;
+ float x;
+ float y;
+ float z;
public :
Vertex();
- Vertex(int x, int y, int z);
+ Vertex(float x, float y, float z);
float norm();
Vertex projectOn(Vertex v);
- Vertex setNorm(int n);
+ Vertex setNorm(float n);
Vertex perpendicular(); // Perpendiculaire 2D dans le sens contraire des aiguilles d'une montre.
float cosAngle(Vertex v); // cosinus de l'angle entre this et v.
static Vertex fromSpherical(float r, float xAngle, float yAngle);
friend Vertex intersection(Vertex a, Vertex b, Vertex c, Vertex d); // Intersection entre (a,b) et (c,d).
public :
- operator Vertexf();
+ operator Vertex();
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& v);
- friend Vertex operator*(const Vertex& v, const int n);
+ friend Vertex operator*(const Vertex& v, const float n);
friend Vertex operator*(const Vertex& u, const Vertex& v);
friend Vertex operator/(const Vertex& v, const int n);
friend Vertex operator/(const Vertex& v, const float f);
- friend Vertex operator+(const Vertex& u, const Vertexf& v);
- friend Vertex operator-(const Vertex& u, const Vertexf& v);
};
-class Vertexf {
+/*
+class Vertex {
public :
float x;
float y;
float z;
public :
- Vertexf();
- Vertexf(float x, float y, float z);
+ Vertex();
+ Vertex(float x, float y, float z);
float norm();
- static Vertexf fromSpherical(float r, float xAngle, float yAngle);
+ static Vertex fromSpherical(float r, float xAngle, float yAngle);
public :
operator Vertex();
- friend std::ostream& operator<<(std::ostream& os, const Vertexf& 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& u, const Vertexf& v);
- friend Vertexf operator/(const Vertexf& v, const int n);
- friend Vertexf operator/(const Vertexf& v, const float f);
- friend Vertexf operator+(const Vertexf& u, const Vertex& v);
- friend Vertexf operator-(const Vertexf& u, 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& v);
+ friend Vertex operator*(const Vertex& v, const int n);
+ friend Vertex operator*(const Vertex& u, const Vertex& v);
+ friend Vertex operator/(const Vertex& v, const int n);
+ friend Vertex operator/(const Vertex& v, const float f);
+ friend Vertex operator+(const Vertex& u, const Vertex& v);
+ friend Vertex operator-(const Vertex& u, const Vertex& v);
};
-
+*/
#endif
diff --git a/view.cpp b/view.cpp
@@ -1,7 +1,7 @@
#include "all_includes.hh"
-// camera(Camera(Vertexf(1000,1000,2000),45,100,1000,0.6)
-View::View(Chose* root) : root(root), camera(Camera(Vertexf(9600,10000,15300),0,179,1000,0.6)) {
+// camera(Camera(Vertex(1000,1000,2000),45,100,1000,0.6)
+View::View(Chose* root) : root(root), camera(Camera(Vertex(9600,10000,15300),0,179,1000,0.6)) {
initWindow();
mainLoop();
}
@@ -25,17 +25,17 @@ void View::initWindow() {
gluPerspective(70,(double)windowWidth/windowHeight,1,100000); // back frustum : 1km
glEnable(GL_DEPTH_TEST);
glewInit();
-
+
float MatSpec[4] = {0.0f, 0.0f, 0.0f, 1.0f};
float MatDif[4] = {0.5f, 0.5f, 0.5f, 1.0f};
float MatAmb[4] = {0.3f, 0.3f, 0.6f, 1.0f};
float shininess = 128.0f;
-
+
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,MatSpec);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,MatDif);
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,MatAmb);
glMaterialfv(GL_FRONT,GL_SHININESS,&shininess);
-
+
glEnable(GL_LIGHTING); // Active l'éclairage
glEnable(GL_LIGHT0); // Active la lumière 0;
}
@@ -62,13 +62,13 @@ void View::displayAxes() {
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(2500.0f, 0.0f, 0.0f); // ending point of the line
glEnd( );
-
+
glBegin(GL_LINES);
glColor3ub(0,255,0);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
glVertex3f(0.0f, 2500.0f, 0.0f); // ending point of the line
glEnd( );
-
+
glBegin(GL_LINES);
glColor3ub(0,0,255);
glVertex3f(0.0f, 0.0f, 0.0f); // origin of the line
@@ -81,18 +81,18 @@ void View::displayAxes() {
void View::renderScene(int lastTime, int currentTime) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
-
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;
-
+
camera.animation(currentTime-lastTime);
camera.setCamera();
-
+
setLight();
//displayAxes();
glBegin(GL_TRIANGLES);
root->display();
glEnd();
-
+
glFlush();
SDL_GL_SwapBuffers();
}
@@ -107,7 +107,7 @@ void View::mainLoop() {
int lastTime = SDL_GetTicks() - 30;
int currentTime = 0;
-
+
while (continuer) {
lastTime = currentTime;
currentTime = SDL_GetTicks();
@@ -133,9 +133,9 @@ void View::mainLoop() {
SDL_Quit();
}
-Camera::Camera(Vertexf pos, float xA, float yA, int moveSensitivity, float mouseSensitivity)
+Camera::Camera(Vertex pos, float xA, float yA, int moveSensitivity, float mouseSensitivity)
: cameraCenter(pos),
- cameraSight(cameraCenter + Vertexf::fromSpherical(100,yA,xA)),
+ cameraSight(cameraCenter + Vertex::fromSpherical(100,yA,xA)),
xAngle(xA),
yAngle(yA),
moveDist(moveSensitivity),
@@ -151,7 +151,7 @@ std::ostream& Camera::print(std::ostream& os) const {
}
void Camera::setCamera() {
- cameraSight = cameraCenter + Vertexf::fromSpherical(100, yAngle, xAngle);
+ cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle);
gluLookAt(cameraCenter.x,cameraCenter.y,cameraCenter.z, cameraSight.x, cameraSight.y, cameraSight.z,0,0,1);
}
@@ -214,17 +214,17 @@ void Camera::keyboard(const SDL_KeyboardEvent &eventKey) {
void Camera::animation(int elapsedTime) {
float diff = ((float)(elapsedTime+1)/1000.)*(float)moveDist;
-
+
if(up)
- cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, yAngle, xAngle);
+ cameraCenter = cameraCenter + Vertex::fromSpherical(diff, yAngle, xAngle);
if(down)
- cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, yAngle, xAngle);
+ cameraCenter = cameraCenter - Vertex::fromSpherical(diff, yAngle, xAngle);
if(left)
- cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, 90, xAngle - 90);
+ cameraCenter = cameraCenter - Vertex::fromSpherical(diff, 90, xAngle - 90);
if(right)
- cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, 90, xAngle - 90);
+ cameraCenter = cameraCenter + Vertex::fromSpherical(diff, 90, xAngle - 90);
if(pageUp)
- cameraCenter = cameraCenter - Vertexf::fromSpherical(diff, yAngle + 90, xAngle);
+ cameraCenter = cameraCenter - Vertex::fromSpherical(diff, yAngle + 90, xAngle);
if(pageDown)
- cameraCenter = cameraCenter + Vertexf::fromSpherical(diff, yAngle + 90, xAngle);
+ cameraCenter = cameraCenter + Vertex::fromSpherical(diff, yAngle + 90, xAngle);
}
diff --git a/view.hh b/view.hh
@@ -11,8 +11,8 @@
class Camera {
public :
- Vertexf cameraCenter;
- Vertexf cameraSight;
+ Vertex cameraCenter;
+ Vertex cameraSight;
private :
float xAngle;
@@ -27,7 +27,7 @@ class Camera {
bool pageDown;
public :
- Camera(Vertexf pos, float xA, float yA, int moveSensitivity, float mouseSensitivity);
+ Camera(Vertex pos, float xA, float yA, int moveSensitivity, float mouseSensitivity);
void setCamera();
void mouseMotion(const SDL_MouseMotionEvent &event);
void keyboard(const SDL_KeyboardEvent &event);