summaryrefslogtreecommitdiffstats
path: root/src/de/gamezock/metacraft/data/Triangle.java
blob: 2049c6f3e48a567918cae98b04c70c13f54e9702 (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
package de.gamezock.metacraft.data;

import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

public class Triangle implements Cloneable {
  private Point3f[] vertices;
  private Vector3f[] normals;
  private Vector3f normal;
  
  
  public Triangle(Point3f v1, Point3f v2, Point3f v3) {
    vertices = new Point3f[3];
    
    vertices[0] = v1;
    vertices[1] = v2;
    vertices[2] = v3;
    
    Vector3f edge1 = new Vector3f(), edge2 = new Vector3f();
    edge1.sub(v1, v2);
    edge2.sub(v3, v2);
    
    normal = new Vector3f();
    normal.cross(edge1, edge2);
    normal.normalize();
    
    normals = new Vector3f[3];
    normals[0] = new Vector3f(normal);
    normals[1] = new Vector3f(normal);
    normals[2] = new Vector3f(normal);
  }
  
  public Point3f getVertex(int i) {
    return vertices[i];
  }
  
  public Vector3f getVertexNormal(int i) {
    return normals[i];
  }
  
  public Vector3f getNormal() {
    return normal;
  }
  
  public void translate(Vector3f v) {
    for(Point3f p : vertices) {
      p.add(v);
    }
  }
  
  public Object clone() {
    Triangle t = new Triangle((Point3f)vertices[0].clone(), (Point3f)vertices[1].clone(), (Point3f)vertices[2].clone());
    
    for(int i = 0; i < 3; ++i) {
      t.normals[i] = (Vector3f)normals[i].clone();
    }
    
    return t;
  }
}