/*
 * 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_*/

