-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.java
135 lines (113 loc) · 4.24 KB
/
Light.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
/* I declare that this code is my own work */
/* Author <Junxiang Chen> <[email protected]> */
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;
public class Light {
private Material material;
public Shader shader;
private Camera camera;
private Vec3 position;
private int[] vertexBufferId = new int[1];
private int[] vertexArrayId = new int[1];
private int[] elementBufferId = new int[1];
// ***************************************************
/* THE DATA
*/
// anticlockwise/counterclockwise ordering
private float[] vertices = new float[]{ // x,y,z
-0.5f, -0.5f, -0.5f, // 0
-0.5f, -0.5f, 0.5f, // 1
-0.5f, 0.5f, -0.5f, // 2
-0.5f, 0.5f, 0.5f, // 3
0.5f, -0.5f, -0.5f, // 4
0.5f, -0.5f, 0.5f, // 5
0.5f, 0.5f, -0.5f, // 6
0.5f, 0.5f, 0.5f // 7
};
private int[] indices = new int[]{
0, 1, 3, // x -ve
3, 2, 0, // x -ve
4, 6, 7, // x +ve
7, 5, 4, // x +ve
1, 5, 7, // z +ve
7, 3, 1, // z +ve
6, 4, 0, // z -ve
0, 2, 6, // z -ve
0, 4, 5, // y -ve
5, 1, 0, // y -ve
2, 3, 7, // y +ve
7, 6, 2 // y +ve
};
private int vertexStride = 3;
private int vertexXYZFloats = 3;
public Light(GL3 gl3) {
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);
position = new Vec3(3f, 2f, 1f);
// modelMatrix = new Mat4(1);
shader = new Shader(gl3, "shader/vs_light_01.txt", "shader/fs_light_01.txt");
fillBuffers(gl3);
}
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);
}
public void setPosition(Vec3 v) {
position.x = v.x;
position.y = v.y;
position.z = v.z;
}
public void setPosition(float x, float y, float z) {
position.x = x;
position.y = y;
position.z = z;
}
public Vec3 getPosition() {
return position;
}
public void setMaterial(Material material) {
this.material = material;
}
public Material getMaterial() {
return material;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public void dispose(GL3 gl3) {
gl3.glDeleteBuffers(1, vertexBufferId, 0);
gl3.glDeleteVertexArrays(1, vertexArrayId, 0);
gl3.glDeleteBuffers(1, elementBufferId, 0);
}
public void render(GL3 gl3) {
Mat4 modelMatrix = new Mat4(1);
modelMatrix = Mat4.multiply(Mat4Transform.scale(0.3f, 0.3f, 0.3f), modelMatrix);
Mat4 worldMatrix = Mat4.multiply(Mat4Transform.translate(position), modelMatrix);
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);
}
}