-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.java
More file actions
74 lines (62 loc) · 2.9 KB
/
Mesh.java
File metadata and controls
74 lines (62 loc) · 2.9 KB
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
import gmaths.*;
import java.nio.*;
import com.jogamp.common.nio.*;
import com.jogamp.opengl.*;
public class Mesh {
private float[] vertices;
private int[] indices;
private int vertexStride = 8;
private int vertexXYZFloats = 3;
private int vertexNormalFloats = 3;
private int vertexTexFloats = 2;
private int[] vertexBufferId = new int[1];
private int[] vertexArrayId = new int[1];
private int[] elementBufferId = new int[1];
public Mesh(GL3 gl, float[] vertices, int[] indices) {
this.vertices = vertices;
this.indices = indices;
fillBuffers(gl);
}
public void render(GL3 gl) {
gl.glBindVertexArray(vertexArrayId[0]);
gl.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_INT, 0);
gl.glBindVertexArray(0);
}
private void fillBuffers(GL3 gl) {
gl.glGenVertexArrays(1, vertexArrayId, 0);
gl.glBindVertexArray(vertexArrayId[0]);
gl.glGenBuffers(1, vertexBufferId, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferId[0]);
FloatBuffer fb = Buffers.newDirectFloatBuffer(vertices);
gl.glBufferData(GL.GL_ARRAY_BUFFER, Float.BYTES * vertices.length, fb, GL.GL_STATIC_DRAW);
int stride = vertexStride;
int numXYZFloats = vertexXYZFloats;
int offset = 0;
gl.glVertexAttribPointer(0, numXYZFloats, GL.GL_FLOAT, false, stride*Float.BYTES, offset);
gl.glEnableVertexAttribArray(0);
int numNormalFloats = vertexNormalFloats; // x,y,z for each vertex
offset = numXYZFloats*Float.BYTES; // the normal values are three floats after the three x,y,z values
// so change the offset value
gl.glVertexAttribPointer(1, numNormalFloats, GL.GL_FLOAT, false, stride*Float.BYTES, offset);
// the vertex shader uses location 1 (sometimes called index 1)
// for the normal information
// location, size, type, normalize, stride, offset
// offset is relative to the start of the array of data
gl.glEnableVertexAttribArray(1);// Enable the vertex attribute array at location 1
// now do the texture coordinates in vertex attribute 2
int numTexFloats = vertexTexFloats;
offset = (numXYZFloats+numNormalFloats)*Float.BYTES;
gl.glVertexAttribPointer(2, numTexFloats, GL.GL_FLOAT, false, stride*Float.BYTES, offset);
gl.glEnableVertexAttribArray(2);
gl.glGenBuffers(1, elementBufferId, 0);
IntBuffer ib = Buffers.newDirectIntBuffer(indices);
gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, elementBufferId[0]);
gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, Integer.BYTES * indices.length, ib, GL.GL_STATIC_DRAW);
gl.glBindVertexArray(0);
}
public void dispose(GL3 gl) {
gl.glDeleteBuffers(1, vertexBufferId, 0);
gl.glDeleteVertexArrays(1, vertexArrayId, 0);
gl.glDeleteBuffers(1, elementBufferId, 0);
}
}