-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovingLight.java
308 lines (274 loc) · 7.49 KB
/
MovingLight.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* I declare that this code is my own work */
/* Author <Junxiang Chen> <jchen115@sheffield.ac.uk> */
/*
moving light (light bulb)
*/
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL3;
import gmaths.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
/**
* constructor
*/
public class MovingLight {
private Material material;
public Shader shader;
private Camera camera;
private Vec3 localPosition;
private Vec3 worldPosition;
private Vec3 localDirection;
private Vec3 worldDirection;
private Mat4 worldMatrix;
private int[] vertexBufferId = new int[1];
private int[] vertexArrayId = new int[1];
private int[] elementBufferId = new int[1];
private float[] vertices = setLightSize();
private int[] indices = Sphere.indices;
private int vertexStride = 3;
private int vertexXYZFloats = 3;
/**
* set light bulb size
*
* @return vertices
*/
private float[] setLightSize() {
float[] vertices = Sphere.vertices.clone();
for (int i = 0; i < vertices.length; i++) {
vertices[i] = vertices[i] * 0.65f;
}
return vertices;
}
/*
parameters of spotlight
*/
private float constant;
private float linear;
private float quadratic;
private float cutOff;
private float outerCutOff;
/**
* constructor
*
* @param gl3 GL3 parameter
* @param localPosition local position (local coordinate)
* @param worldMatrix world matrix (transform local coordinate to world)
* @param localDirection local direction of light
*/
public MovingLight(GL3 gl3, Vec3 localPosition, Mat4 worldMatrix, Vec3 localDirection) {
material = new Material();
material.setAmbient(0.5f, 0.5f, 0.5f);
material.setDiffuse(0.8f, 0.8f, 0.8f);
material.setSpecular(0.8f, 0.8f, 0.8f);
this.localPosition = localPosition;
this.worldMatrix = worldMatrix;
this.localDirection = localDirection;
shader = new Shader(gl3, "shader/vs_light_01.txt", "shader/fs_light_01.txt");
fillBuffers(gl3);
}
/**
* fill buffers
*
* @param gl3 GL3 parameter
*/
public void fillBuffers(GL3 gl3) {
gl3.glGenVertexArrays(1, vertexArrayId, 0);
gl3.glBindVertexArray(vertexArrayId[0]);
gl3.glGenBuffers(1, vertexBufferId, 0);
gl3.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferId[0]);
FloatBuffer fb = Buffers.newDirectFloatBuffer(vertices);
gl3.glBufferData(GL.GL_ARRAY_BUFFER, Float.BYTES * vertices.length, fb, GL.GL_STATIC_DRAW);
int stride = vertexStride;
int numXYZFloats = vertexXYZFloats;
int offset = 0;
gl3.glVertexAttribPointer(0, numXYZFloats, GL.GL_FLOAT, false, stride * Float.BYTES, offset);
gl3.glEnableVertexAttribArray(0);
gl3.glGenBuffers(1, elementBufferId, 0);
IntBuffer ib = Buffers.newDirectIntBuffer(indices);
gl3.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, elementBufferId[0]);
gl3.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, Integer.BYTES * indices.length, ib, GL.GL_STATIC_DRAW);
gl3.glBindVertexArray(0);
}
/**
* set world position
*/
public void setWorldPosition() {
Vec4 position = new Vec4(this.localPosition, 1);
Vec4 result = Vec4.multiplyMatrix(this.worldMatrix, position);
this.worldPosition = result.toVec3();
}
/**
* get position in world coordinate
*
* @return position in world coordinate
*/
public Vec3 getWorldPosition() {
return worldPosition;
}
/**
* set world matrix
*
* @param worldMatrix Mat4 parameter
*/
public void setWorldMatrix(Mat4 worldMatrix) {
this.worldMatrix = worldMatrix;
}
/**
* get matrix that transform local coordinate to world
*
* @return the direction in world coordinate
*/
public Mat4 getWorldMatrix() {
return worldMatrix;
}
/**
* set direction from local coordinate to world
*/
public void setWorldDirection() {
Vec4 result = Vec4.multiplyMatrix(this.worldMatrix, new Vec4(localDirection, 0));
// result.x = result.x * 0.285f;
// result.z = result.z * -0.001f;
result.z = result.z * (-0.05f);
this.worldDirection = result.toVec3();
}
/**
* get direction in world coordination
*
* @return direction under world coordinate
*/
public Vec3 getWorldDirection() {
return worldDirection;
}
/**
* set material of light
*
* @param material Material parameter
*/
public void setMaterial(Material material) {
this.material = material;
}
/**
* get material information
*
* @return Material parameter
*/
public Material getMaterial() {
return material;
}
/**
* set camera
*
* @param camera Camera parameter
*/
public void setCamera(Camera camera) {
this.camera = camera;
}
/**
* set constant
*
* @param constant float parameter
*/
public void setConstant(float constant) {
this.constant = constant;
}
/**
* get constant
*
* @return float parameter
*/
public float getConstant() {
return this.constant;
}
/**
* set linear
*
* @param linear float parameter
*/
public void setLinear(float linear) {
this.linear = linear;
}
/**
* get linear
*
* @return float parameter
*/
public float getLinear() {
return this.linear;
}
/**
* set quadratic
*
* @param quadratic float parameter
*/
public void setQuadratic(float quadratic) {
this.quadratic = quadratic;
}
/**
* get quadratic
*
* @return float parameter
*/
public float getQuadratic() {
return this.quadratic;
}
/**
* set inner cut off
*
* @param cutOff float parameter
*/
public void setCutOff(float cutOff) {
this.cutOff = (float) Math.cos(Math.toRadians(cutOff));
}
/**
* get inner cut off
*
* @return float parameter
*/
public float getCutOff() {
return this.cutOff;
}
/**
* set outer cut off
*
* @param outerCutOff float parameter
*/
public void setOuterCutOff(float outerCutOff) {
this.outerCutOff = (float) Math.cos(Math.toRadians(outerCutOff));
}
/**
* get outer cut off
*
* @return float parameter
*/
public float getOuterCutOff() {
return this.outerCutOff;
}
/**
* dispose buffer
*
* @param gl3 GL3 parameter
*/
public void dispose(GL3 gl3) {
gl3.glDeleteBuffers(1, vertexBufferId, 0);
gl3.glDeleteVertexArrays(1, vertexArrayId, 0);
gl3.glDeleteBuffers(1, elementBufferId, 0);
}
/**
* render light
*
* @param gl3
*/
public void render(GL3 gl3) {
setWorldPosition();
setWorldDirection();
Mat4 mvpMatrix = Mat4.multiply(
camera.getPerspectiveMatrix(), Mat4.multiply(camera.getViewMatrix(), worldMatrix)
);
shader.use(gl3);
shader.setFloatArray(gl3, "mvpMatrix", mvpMatrix.toFloatArrayForGLSL());
gl3.glBindVertexArray(vertexArrayId[0]);
gl3.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_INT, 0);
gl3.glBindVertexArray(0);
}
}