summaryrefslogtreecommitdiffstats
path: root/Triangle.h
blob: ddb9d976e930d56df2c65cfff3b76aaab4b011fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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;}
    
    Vertex 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 (Vector(v[0])+Vector(v[1])+Vector(v[2]))/3;
    }

  private:
    Vertex v[3];
    Color c;
};

#endif /*_TRIANGLE_H_*/