summaryrefslogtreecommitdiffstats
path: root/src/de/gamezock/metacraft/data/VertexBuffer.java
blob: c2205a027ca2351d715c2911d8749e10231a827f (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 java.nio.FloatBuffer;

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

public class VertexBuffer {
  private FloatBuffer buffer;
  
  public VertexBuffer(int num) {
    buffer = FloatBuffer.allocate(2*3*num);
  }
  
  public FloatBuffer getBuffer() {
    return buffer;
  }
  
  private void setTuple(int i, Tuple3f tuple) {
    buffer.position(3*i);
    
    buffer.put(tuple.x);
    buffer.put(tuple.y);
    buffer.put(tuple.z);
  }
  
  private Point3f getTuple(int i) {
    buffer.position(3*i);
    
    return new Point3f(buffer.get(), buffer.get(), buffer.get());
  }
  
  public void setNormal(int i, Vector3f normal) {
    setTuple(2*i, normal);
  }
  
  public void setVertex(int i, Point3f vertex) {
    setTuple(2*i + 1, vertex);
  }
  
  public Vector3f getNormal(int i) {
    return new Vector3f(getTuple(2*i));
  }
  
  public Point3f getVertex(int i) {
    return getTuple(2*i + 1);
  }
  
  public int getSize() {
    return buffer.capacity() / (2*3);
  }
  
  public void dump() {
    buffer.clear();
    
    while(buffer.hasRemaining())
      System.out.println(buffer.get());
  }
}