-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLight.java
More file actions
149 lines (120 loc) · 3.86 KB
/
Light.java
File metadata and controls
149 lines (120 loc) · 3.86 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
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
import gmaths.*;
import java.nio.*;
import com.jogamp.common.nio.*;
import com.jogamp.opengl.*;
public class Light {
private Material material;
private Vec3 position;
private Mat4 model;
private Shader shader;
private Camera camera;
private Vec3 dim = new Vec3(1.0f,1.0f,1.0f);
public Light(GL3 gl) {
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,3f,3f);
model = new Mat4(1);
shader = new Shader(gl, "./shaders/vs_light.txt", "./shaders/fs_light.txt");
fillBuffers(gl);
}
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 m) {
material = m;
}
public Vec3 getDim(){
return dim;
}
public void setDim(Vec3 d){
dim = d;
}
public Material getMaterial() {
return material;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
public void render(GL3 gl) {
Mat4 model = new Mat4(1);
model = Mat4.multiply(Mat4Transform.scale(0.3f,0.3f,0.3f), model);
model = Mat4.multiply(Mat4Transform.translate(position), model);
Mat4 mvpMatrix = Mat4.multiply(camera.getPerspectiveMatrix(), Mat4.multiply(camera.getViewMatrix(), model));
shader.use(gl);
shader.setFloatArray(gl, "mvpMatrix", mvpMatrix.toFloatArrayForGLSL());
gl.glBindVertexArray(vertexArrayId[0]);
gl.glDrawElements(GL.GL_TRIANGLES, indices.length, GL.GL_UNSIGNED_INT, 0);
gl.glBindVertexArray(0);
}
public void dispose(GL3 gl) {
gl.glDeleteBuffers(1, vertexBufferId, 0);
gl.glDeleteVertexArrays(1, vertexArrayId, 0);
gl.glDeleteBuffers(1, elementBufferId, 0);
}
// ***************************************************
/* 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;
// ***************************************************
/* THE LIGHT BUFFERS
*/
private int[] vertexBufferId = new int[1];
private int[] vertexArrayId = new int[1];
private int[] elementBufferId = new int[1];
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);
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);
}
}