This repository was archived by the owner on Feb 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.java
173 lines (153 loc) · 5.8 KB
/
Mesh.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import gmaths.*;
import java.nio.*;
import com.jogamp.common.nio.*;
import com.jogamp.opengl.*;
/**
* Mesh.java
* A Mesh object for texturing the scene objects, and used in processing illumination data
*
* @author Will Garside // worgarside@gmail.com
* @author Dr. Steve Maddock
* @version 1.0 2017-12-06
*/
public abstract class Mesh {
protected float[] vertices;
protected int[] indices;
private int vertexStride = 8;
private int vertexXYZFloats = 3;
private int vertexNormalFloats = 3;
private int vertexTexFloats = 2;
private int[] vertexBufferId = new int[1];
protected int[] vertexArrayId = new int[1];
private int[] elementBufferId = new int[1];
protected Material material;
protected Shader shader;
protected Mat4 model;
protected Camera camera;
protected Mat4 perspective;
protected Light light;
/**
* Constructor for the Mesh class
*
* @param gl
*/
public Mesh(GL3 gl) {
material = new Material();
model = new Mat4(1);
}
// ------------ Setters ------------ \\
/**
* Sets the Matrix used by the Mesh
*
* @param m - the Matrix used as a Mat4
*/
public void setModelMatrix(Mat4 m) {
model = m;
}
/**
* Sets the camera used by the users
*
* @param camera - Camera object from Arty.java
*/
public void setCamera(Camera camera) {
this.camera = camera;
}
/**
*
* @param perspective
*/
public void setPerspective(Mat4 perspective) {
this.perspective = perspective;
}
/**
* Sets the Light shining on the Mesh
*
* @param light - the Light object shining on the Mesh
*/
public void setLight(Light light) {
this.light = light;
}
/**
* Removes the Mesh from memory on system exit
*
* @param gl - graphics library
*/
public void dispose(GL3 gl) {
gl.glDeleteBuffers(1, vertexBufferId, 0);
gl.glDeleteVertexArrays(1, vertexArrayId, 0);
gl.glDeleteBuffers(1, elementBufferId, 0);
}
/**
* Send data to the GPU buffers
*
* @param gl - graphics library
*/
protected 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);
}
/**
* Renders the Mesh object
*
* @param gl - graphics library
* @param model - the model of the Mesh as a Mat4
*/
public abstract void render(GL3 gl, Mat4 model);
/**
* Renders the Mesh object
*
* @param gl - graphics library
*/
public void render(GL3 gl) {
render(gl, model);
}
/**
* Sends data to the Shaders for TwoTriangles, Cube, and Sphere objects
*
* @param gl - graphics library
* @param shader - the shader recieving data
* @param i - the lightSource reference number
* @param ambient - the ambient value of the scene
*/
public void setShaderValues(GL3 gl, Shader shader, int i, Vec3 ambient) {
shader.setVec3(gl, "lightSources[" + i + "].position", light.getPosition(i));
shader.setVec3(gl, "lightSources[" + i + "].ambient", ambient);
shader.setVec3(gl, "lightSources[" + i + "].diffuse", light.getMaterial().getDiffusePoint(i));
shader.setVec3(gl, "lightSources[" + i + "].specular", light.getMaterial().getSpecularPoint(i));
shader.setFloat(gl, "lightSources[" + i + "].falloffConstant", light.getFallOffConstant(i));
shader.setFloat(gl, "lightSources[" + i + "].falloffLinear", light.getFallOffLinear(i));
shader.setFloat(gl, "lightSources[" + i + "].falloffQuadratic", light.getFallOffQuadratic(i));
shader.setVec3(gl, "lightSources[" + i + "].spotDirection", light.getDirection(i));
shader.setFloat(gl, "lightSources[" + i + "].spotCutoff", light.getCutOff(i));
shader.setFloat(gl, "lightSources[" + i + "].spotOuterCutOff", light.getOuterCutOff(i));
shader.setInt(gl, "lightSources[" + i + "].spotlight", light.getSpotlight(i));
}
}