30
30
import me .oskarmendel .mass .gfx .*;
31
31
import me .oskarmendel .mass .gfx .light .DirectionalLight ;
32
32
import me .oskarmendel .mass .gfx .light .PointLight ;
33
+ import me .oskarmendel .mass .gfx .light .SpotLight ;
33
34
import me .oskarmendel .mass .util .OBJLoader ;
34
35
import org .joml .Vector3f ;
35
36
import org .lwjgl .glfw .GLFWErrorCallback ;
@@ -90,6 +91,10 @@ public class Game {
90
91
private PointLight pointLight ;
91
92
private DirectionalLight directionalLight ;
92
93
private float lightAngle ;
94
+
95
+ private SpotLight spotLight ;
96
+ private float spotAngle = 0 ;
97
+ private float spotInc = 1 ;
93
98
94
99
/**
95
100
* Default constructor for the game.
@@ -166,6 +171,15 @@ public void init() {
166
171
pointLight = new PointLight (lightColour , lightPosition , lightIntensity );
167
172
PointLight .Attenuation att = new PointLight .Attenuation (0.0f , 0.0f , 1.0f );
168
173
pointLight .setAttenuation (att );
174
+
175
+ // Spot light example.
176
+ lightPosition = new Vector3f (0.0f , 0.0f , 10f );
177
+ pointLight = new PointLight (lightColour , lightPosition , lightIntensity );
178
+ att = new PointLight .Attenuation (0.0f , 0.0f , 0.02f );
179
+ pointLight .setAttenuation (att );
180
+ Vector3f coneDir = new Vector3f (0 , 0 , -1 );
181
+ float cutOff = (float ) Math .cos (Math .toRadians (140 ));
182
+ spotLight = new SpotLight (pointLight , coneDir , cutOff );
169
183
170
184
// Directional light example.
171
185
lightPosition = new Vector3f (-1 , 0 ,0 );
@@ -238,11 +252,11 @@ private void input() {
238
252
cameraInc .y = 1 ;
239
253
}
240
254
241
- float lightPos = pointLight .getPosition ().z ;
255
+ float lightPos = spotLight . getPointLight () .getPosition ().z ;
242
256
if (screen .isKeyPressed (GLFW_KEY_N )) {
243
- this .pointLight .getPosition ().z = lightPos + 0.1f ;
257
+ this .spotLight . getPointLight () .getPosition ().z = lightPos + 0.1f ;
244
258
} else if (screen .isKeyPressed (GLFW_KEY_M )) {
245
- this .pointLight .getPosition ().z = lightPos - 0.1f ;
259
+ this .spotLight . getPointLight () .getPosition ().z = lightPos - 0.1f ;
246
260
}
247
261
}
248
262
@@ -260,6 +274,18 @@ public void update() {
260
274
// Do something for every loaded entity.
261
275
entity .getRotation ().y += 1.0f ;
262
276
}
277
+
278
+ // Update spot light direction.
279
+ spotAngle += spotInc * 0.05f ;
280
+ if (spotAngle > 2 ) {
281
+ spotInc = -1 ;
282
+ } else if (spotAngle < -2 ) {
283
+ spotInc = 1 ;
284
+ }
285
+
286
+ double spotAngleRad = Math .toRadians (spotAngle );
287
+ Vector3f coneDir = spotLight .getConeDirection ();
288
+ coneDir .y = (float ) Math .sin (spotAngleRad );
263
289
264
290
// Update directional light direction.
265
291
lightAngle += 1.1f ;
@@ -289,6 +315,7 @@ public void update() {
289
315
* Renders the game.
290
316
*/
291
317
public void render () {
292
- renderer .render (this .camera , this .entities , ambientLight , pointLight , directionalLight );
318
+ renderer .render (this .camera , this .entities , ambientLight ,
319
+ pointLight , spotLight , directionalLight );
293
320
}
294
321
}
0 commit comments