/
DepthClamping.java
304 lines (229 loc) · 11.3 KB
/
DepthClamping.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
package main.tut05;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.opengl.GL3;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLRunnable;
import com.jogamp.opengl.util.GLBuffers;
import glm.vec._3.Vec3;
import glm.vec._4.Vec4;
import main.framework.Framework;
import main.framework.Semantic;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import static com.jogamp.opengl.GL.*;
import static com.jogamp.opengl.GL2ES3.GL_COLOR;
import static com.jogamp.opengl.GL2ES3.GL_DEPTH;
import static com.jogamp.opengl.GL3.GL_DEPTH_CLAMP;
import static uno.buffer.UtilKt.destroyBuffers;
import static uno.glsl.UtilKt.programOf;
/**
* @author gbarbieri
*/
public class DepthClamping extends Framework {
public static void main(String[] args) {
new DepthClamping().setup("Tutorial 05 - Depth Clamping");
}
private interface Buffer {
int VERTEX = 0;
int INDEX = 1;
int MAX = 2;
}
private int theProgram, offsetUniform, perspectiveMatrixUnif;
private final int numberOfVertices = 36;
private FloatBuffer perspectiveMatrix = GLBuffers.newDirectFloatBuffer(16);
private float frustumScale = 1.0f;
private IntBuffer bufferObject = GLBuffers.newDirectIntBuffer(Buffer.MAX), vao = GLBuffers.newDirectIntBuffer(1);
private boolean depthClampingActive = false;
@Override
public void init(GL3 gl) {
initializeProgram(gl);
initializeBuffers(gl);
gl.glGenVertexArrays(1, vao);
gl.glBindVertexArray(vao.get(0));
int colorData = Float.BYTES * 3 * numberOfVertices;
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferObject.get(Buffer.VERTEX));
gl.glEnableVertexAttribArray(Semantic.Attr.POSITION);
gl.glEnableVertexAttribArray(Semantic.Attr.COLOR);
gl.glVertexAttribPointer(Semantic.Attr.POSITION, Vec3.length, GL_FLOAT, false, Vec3.SIZE, 0);
gl.glVertexAttribPointer(Semantic.Attr.COLOR, Vec4.length, GL_FLOAT, false, Vec4.SIZE, colorData);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject.get(Buffer.INDEX));
gl.glBindVertexArray(0);
gl.glEnable(GL_CULL_FACE);
gl.glCullFace(GL_BACK);
gl.glFrontFace(GL_CW);
gl.glEnable(GL_DEPTH_TEST);
gl.glDepthMask(true);
gl.glDepthFunc(GL_LESS);
gl.glDepthRange(0.0f, 1.0f);
}
private void initializeProgram(GL3 gl) {
theProgram = programOf(gl, getClass(), "tut05", "standard.vert", "standard.frag");
offsetUniform = gl.glGetUniformLocation(theProgram, "offset");
perspectiveMatrixUnif = gl.glGetUniformLocation(theProgram, "perspectiveMatrix");
float zNear = 1.0f, zFar = 3.0f;
perspectiveMatrix.put(0, frustumScale);
perspectiveMatrix.put(5, frustumScale);
perspectiveMatrix.put(10, (zFar + zNear) / (zNear - zFar));
perspectiveMatrix.put(14, (2 * zFar * zNear) / (zNear - zFar));
perspectiveMatrix.put(11, -1.0f);
gl.glUseProgram(theProgram);
gl.glUniformMatrix4fv(perspectiveMatrixUnif, 1, false, perspectiveMatrix);
gl.glUseProgram(0);
}
private void initializeBuffers(GL3 gl) {
FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
ShortBuffer indexBuffer = GLBuffers.newDirectShortBuffer(indexData);
gl.glGenBuffers(Buffer.MAX, bufferObject);
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferObject.get(Buffer.VERTEX));
gl.glBufferData(GL_ARRAY_BUFFER, vertexBuffer.capacity() * Float.BYTES, vertexBuffer, GL_STATIC_DRAW);
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject.get(Buffer.INDEX));
gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * Short.BYTES, indexBuffer, GL_STATIC_DRAW);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
destroyBuffers(vertexBuffer, indexBuffer);
}
@Override
public void display(GL3 gl) {
gl.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0.0f).put(1, 0.0f).put(2, 0.0f).put(3, 0.0f));
gl.glClearBufferfv(GL_DEPTH, 0, clearDepth.put(0, 1.0f));
gl.glUseProgram(theProgram);
gl.glBindVertexArray(vao.get(0));
gl.glUniform3f(offsetUniform, 0.0f, 0.0f, 0.5f);
gl.glDrawElements(GL_TRIANGLES, indexData.length, GL_UNSIGNED_SHORT, 0);
gl.glUniform3f(offsetUniform, 0.0f, 0.0f, -1.0f);
gl.glDrawElementsBaseVertex(GL_TRIANGLES, indexData.length, GL_UNSIGNED_SHORT, 0, numberOfVertices / 2);
gl.glBindVertexArray(0);
gl.glUseProgram(0);
}
@Override
public void reshape(GL3 gl, int w, int h) {
perspectiveMatrix.put(0, frustumScale * (h / (float) w));
perspectiveMatrix.put(5, frustumScale);
gl.glUseProgram(theProgram);
gl.glUniformMatrix4fv(perspectiveMatrixUnif, 1, false, perspectiveMatrix);
gl.glUseProgram(0);
gl.glViewport(0, 0, w, h);
}
@Override
public void end(GL3 gl) {
gl.glDeleteProgram(theProgram);
gl.glDeleteBuffers(Buffer.MAX, bufferObject);
gl.glDeleteVertexArrays(1, vao);
destroyBuffers(vao, bufferObject, perspectiveMatrix);
}
@Override
public void keyPressed(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
quit();
break;
case KeyEvent.VK_SPACE:
window.invoke(false, new GLRunnable() {
@Override
public boolean run(final GLAutoDrawable glAutoDrawable) {
GL3 gl = glAutoDrawable.getGL().getGL3();
if(depthClampingActive)
gl.glDisable(GL_DEPTH_CLAMP);
else
gl.glEnable(GL_DEPTH_CLAMP);
depthClampingActive = !depthClampingActive;
return false;
}
});
break;
}
}
private final float RIGHT_EXTENT = 0.8f, LEFT_EXTENT = -RIGHT_EXTENT, TOP_EXTENT = 0.20f, MIDDLE_EXTENT = 0.0f,
BOTTOM_EXTENT = -TOP_EXTENT, FRONT_EXTENT = -1.25f, REAR_EXTENT = -1.75f;
private final float[] GREEN_COLOR = {0.75f, 0.75f, 1.0f, 1.0f}, BLUE_COLOR = {0.0f, 0.5f, 0.0f, 1.0f},
RED_COLOR = {1.0f, 0.0f, 0.0f, 1.0f}, GREY_COLOR = {0.8f, 0.8f, 0.8f, 1.0f},
BROWN_COLOR = {0.5f, 0.5f, 0.0f, 1.0f};
private float[] vertexData = {
//Object 1 positions
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT,
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT,
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT,
LEFT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT,
RIGHT_EXTENT, MIDDLE_EXTENT, FRONT_EXTENT,
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
LEFT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
LEFT_EXTENT, TOP_EXTENT, REAR_EXTENT,
RIGHT_EXTENT, TOP_EXTENT, REAR_EXTENT,
RIGHT_EXTENT, BOTTOM_EXTENT, REAR_EXTENT,
//Object 2 positions
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT,
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT,
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT,
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT,
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT,
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT,
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
MIDDLE_EXTENT, RIGHT_EXTENT, FRONT_EXTENT,
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT,
MIDDLE_EXTENT, LEFT_EXTENT, FRONT_EXTENT,
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT,
BOTTOM_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
TOP_EXTENT, RIGHT_EXTENT, REAR_EXTENT,
TOP_EXTENT, LEFT_EXTENT, REAR_EXTENT,
BOTTOM_EXTENT, LEFT_EXTENT, REAR_EXTENT,
//Object 1 colors
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
//Object 2 colors
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
RED_COLOR[0], RED_COLOR[1], RED_COLOR[2], RED_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BROWN_COLOR[0], BROWN_COLOR[1], BROWN_COLOR[2], BROWN_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
BLUE_COLOR[0], BLUE_COLOR[1], BLUE_COLOR[2], BLUE_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREEN_COLOR[0], GREEN_COLOR[1], GREEN_COLOR[2], GREEN_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3],
GREY_COLOR[0], GREY_COLOR[1], GREY_COLOR[2], GREY_COLOR[3]};
private short[] indexData = {
0, 2, 1,
3, 2, 0,
4, 5, 6,
6, 7, 4,
8, 9, 10,
11, 13, 12,
14, 16, 15,
17, 16, 14};
}