#ifndef _TRIANGLE_H_ #define _TRIANGLE_H_ #include "gl.h" #include "Color.h" #include "Vector.h" #include "Vertex.h" #include "Matrix.h" class Triangle { public: Triangle(const Vertex &v1, const Vertex &v2, const Vertex &v3, const Color &c0) : c(c0) { v[0] = v1; v[1] = v2; v[2] = v3; } const Vertex& getVertex(int i) const {return v[i];} const Color& getColor() const {return c;} Vector getNormal() const { Vector v1 = v[0]-v[2]; Vector v2 = v[0]-v[1]; return v1.cross(v2).normalize(); } void render() const { //glColor4f(c.getR(), c.getG(), c.getB(), c.getA()); float color[] = {c.getR(), c.getG(), c.getB(), c.getA()}; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color); for(int i = 0; i < 3; ++i) { color[i] /= 2; } glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color); Vector normal = getNormal(); glNormal3f(normal.getX(), normal.getY(), normal.getZ()); for(int i = 0; i < 3; ++i) { glVertex3f(v[i].getX(), v[i].getY(), v[i].getZ()); } } void transform(const Matrix &m) { for(int i = 0; i < 3; ++i) { v[i] = m*v[i]; } } Vertex getCenter() const { return (v[0]+v[1]+v[2])/3; } private: Vertex v[3]; Color c; }; #endif /*_TRIANGLE_H_*/