summaryrefslogtreecommitdiffstats
path: root/src/Triangle.h
blob: 2b34c3f1cc9e71d252e9a33c9c6241d3b90df9d2 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Triangle.h
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ZOOM_TRIANGLE_H_
#define ZOOM_TRIANGLE_H_

#include <vmmlib/vector.hpp>
#include <vmmlib/matrix.hpp>

namespace Zoom {

class Triangle {
  public:
    Triangle() : color(vmml::vec4f::ONE), texture(0) {
      vertices[0] = vertices[1] = vertices[2] = vmml::vec3f::ZERO;
      normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO;
      texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO;
    }

    Triangle(const vmml::vec3f &v1, const vmml::vec3f &v2, const vmml::vec3f &v3, const vmml::vec4f &color0) : color(color0), texture(0) {
      vertices[0] = v1;
      vertices[1] = v2;
      vertices[2] = v3;

      normals[0] = normals[1] = normals[2] = vmml::vec3f::ZERO;
      texcoords[0] = texcoords[1] = texcoords[2] = vmml::vec2f::ZERO;
    }
    
    const vmml::vec3f& getVertex(int i) const {return vertices[i];}
    vmml::vec3f& getVertex(int i) {return vertices[i];}
    void setVertex(int i, vmml::vec3f v) {
      vertices[i] = v;
    }

    const vmml::vec3f& getNormal(int i) const {return normals[i];}
    vmml::vec3f& getNormal(int i) {return normals[i];}
    void setNormal(int i, vmml::vec3f n) {
      normals[i] = n;
    }

    const vmml::vec2f& getTexCoords(int i) const {return texcoords[i];}
    vmml::vec2f& getTexCoords(int i) {return texcoords[i];}
    void setTexCoords(int i, vmml::vec2f t) {
      texcoords[i] = t;
    }

    unsigned getTexture() const {
      return texture;
    }
    void setTexture(unsigned tex) {
      texture = tex;
    }

    const vmml::vec4f& getColor() const {return color;}
    vmml::vec4f& getColor() {return color;}
    void setColor(vmml::vec4f c) {
      color = c;
    }
    
    vmml::vec3f computeNormal() const {
      return vertices[0].compute_normal(vertices[1], vertices[2]);
    }
    
    bool isDegenerate() const {
      return (computeNormal().squared_length() == 0);
    }

    void transform(const vmml::mat4f &matrix) {
      for(int i = 0; i < 3; ++i) {
        vertices[i] = matrix*vertices[i];
      }
    }
    
    vmml::vec3f getCenter() const {
      return (vertices[0]+vertices[1]+vertices[2])/3;
    }

  private:
    vmml::vec3f vertices[3];
    vmml::vec3f normals[3];
    vmml::vec2f texcoords[3];
    vmml::vec4f color;
    unsigned texture;
};

}

#endif /*ZOOM_TRIANGLE_H_*/