/* * Triangle.h * * Copyright (C) 2009 Matthias Schiffer * * 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 . */ #ifndef ZOOM_TRIANGLE_H_ #define ZOOM_TRIANGLE_H_ #include #include 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; normal = 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, bool update = true) : 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; if(update) updateNormal(); else normal = vmml::vec3f::ZERO; } const vmml::vec3f& getVertex(int i) const {return vertices[i];} void setVertex(int i, vmml::vec3f v, bool update = true) { vertices[i] = v; if(update) updateNormal(); } void updateNormal() { normal = vertices[0].compute_normal(vertices[1], vertices[2]); } const vmml::vec3f& getVertexNormal(int i) const {return normals[i];} vmml::vec3f& getVertexNormal(int i) {return normals[i];} void setVertexNormal(int i, vmml::vec3f n) { normals[i] = n; } const vmml::vec3f& getNormal() const {return normal;} 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; } bool isDegenerate() const { return (normal.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::vec3f normal; vmml::vec2f texcoords[3]; vmml::vec4f color; unsigned texture; }; } #endif /*ZOOM_TRIANGLE_H_*/