commit 46fec292f99514d6863b23fc08ede6ef9fcf410a
parent af2755ffb404ec29ad8a6ef9ff7939edc7f16898
Author: Yoann <yoann.b87@voila.fr>
Date: Tue, 29 Nov 2011 20:53:54 +0100
Ajout de la fonction de calcul de normales, placement de manière fixe de
la lumière.
Diffstat:
4 files changed, 57 insertions(+), 12 deletions(-)
diff --git a/triangle.cpp b/triangle.cpp
@@ -2,7 +2,8 @@
Triangle::Triangle(Vertex v1, Vertex v2, Vertex v3, unsigned char r, unsigned char g, unsigned char b): v1(v1), v2(v2), v3(v3), r(r), g(g), b(b) {
// TODO : calcul de la normale.
- normal = Vertex(0,0,1);
+ normal = this->normalVector(v1,v2,v3);
+
}
std::ostream& operator<<(std::ostream& os, const Triangle* t) {
@@ -13,7 +14,38 @@ std::ostream& operator<<(std::ostream& os, const Triangle& t) {
return os << "Triangle " << t.v1 << "--" << t.v2 << "--" << t.v3 << "-- cycle";
}
+Vertexf Triangle::normalVector(Vertex v1, Vertex v2, Vertex v3) {
+ int ax = v1.x - v2.x;
+ int ay = v1.y - v2.y;
+ int az = v1.z - v2.z;
+ int bx = v2.x - v3.x;
+ int by = v2.y - v3.y;
+ int bz = v2.z - v3.z;
+
+ Vertexf normal;
+
+ float x = (float)((ay * bz) - (az * by));
+ float y = (float)((az * bx) - (ax * bz));
+ float z = -(float)((ax * by) - (ay * bx));
+ float length = sqrt(x*x + y*y + z*z);
+
+ normal.x = x/length;
+ normal.y = y/length;
+ normal.z = z/length;
+
+ return normal;
+}
+
void Triangle::display() {
+ glDisable(GL_LIGHTING);
+ glDisable(GL_TEXTURE_2D);
+ glBegin(GL_LINES);
+ glColor3ub(255,255,0);
+ glVertex3d(v1.x*10,v1.y*10,v1.z*10);
+ glVertex3d(v1.x*10+normal.x*50,v1.y*10+normal.y*50,v1.z*10+normal.z*50);
+ glEnd( );
+ glEnable(GL_LIGHTING);
+
View::setColor(r,g,b);
glNormal3d(normal.x,normal.y,normal.z);
glBegin(GL_TRIANGLES);
diff --git a/triangle.hh b/triangle.hh
@@ -2,22 +2,30 @@
#define _TRIANGLE_HH_
#include "all_includes.hh"
+struct Vertexf {
+ float x;
+ float y;
+ float z;
+};
class Triangle {
-public:
+ public:
Vertex v1;
Vertex v2;
Vertex v3;
unsigned char r;
unsigned char g;
unsigned char b;
- Vertex normal;
-public:
- Triangle(Vertex v1, Vertex v2, Vertex v3, unsigned char r, unsigned char g, unsigned char b);
- void display();
-public:
+ Vertexf normal;
+
+ public:
friend std::ostream& operator<<(std::ostream& os, const Triangle* t);
friend std::ostream& operator<<(std::ostream& os, const Triangle& t);
+ Triangle(Vertex v1, Vertex v2, Vertex v3, unsigned char r, unsigned char g, unsigned char b);
+ void display();
+
+ private :
+ Vertexf normalVector(Vertex v1, Vertex v2, Vertex v3);
};
#endif
diff --git a/view.cpp b/view.cpp
@@ -30,8 +30,13 @@ void View::initWindow() {
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;
+}
- float Light1Pos[4] = {0.0f, 1.0f, 0.0f, 0.0f};
+void View::setLight() {
+ float Light1Pos[4] = {0.5f, 1.0f, 0.0f, 0.0f};
float Light1Dif[4] = {1.0f, 1.0f, 1.0f, 1.0f};
float Light1Spec[4] = {0.0f, 0.0f, 0.0f, 1.0f};
float Light1Amb[4] = {0.4f, 0.4f, 0.4f, 1.0f};
@@ -40,9 +45,6 @@ void View::initWindow() {
glLightfv(GL_LIGHT0, GL_SPECULAR, Light1Spec);
glLightfv(GL_LIGHT0, GL_AMBIENT, Light1Amb);
glLightfv(GL_LIGHT0, GL_POSITION, Light1Pos);
-
- glEnable(GL_LIGHTING); // Active l'éclairage
- glEnable(GL_LIGHT0); // Active la lumière 0;
}
void View::displayAxes() {
@@ -79,7 +81,7 @@ void View::renderScene() {
cameraSight = cameraCenter + Vertex::fromSpherical(100, yAngle, xAngle);
gluLookAt(cameraCenter.x,cameraCenter.y,cameraCenter.z, cameraSight.x, cameraSight.y, cameraSight.z,0,0,1);
-
+ setLight();
displayAxes();
root->display();
diff --git a/view.hh b/view.hh
@@ -31,6 +31,9 @@ class View {
void displayAxes();
static void setColor(unsigned char r, unsigned char g, unsigned char b);
+
+ private:
+ void setLight();
};
#endif