-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathHelloTriangle.java
268 lines (190 loc) · 8 KB
/
HelloTriangle.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
package gl3;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.KeyListener;
import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.*;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.GLBuffers;
import framework.Semantic;
import glm.mat.Mat4x4;
import glm.vec._2.Vec2;
import glm.vec._3.Vec3;
import uno.glsl.Program;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import static com.jogamp.opengl.GL.GL_ARRAY_BUFFER;
import static com.jogamp.opengl.GL.GL_DEPTH_TEST;
import static com.jogamp.opengl.GL.GL_ELEMENT_ARRAY_BUFFER;
import static com.jogamp.opengl.GL.GL_FLOAT;
import static com.jogamp.opengl.GL.GL_STATIC_DRAW;
import static com.jogamp.opengl.GL.GL_TRIANGLES;
import static com.jogamp.opengl.GL.GL_UNSIGNED_SHORT;
import static com.jogamp.opengl.GL2ES2.GL_STREAM_DRAW;
import static com.jogamp.opengl.GL2ES3.*;
import static glm.GlmKt.glm;
import static uno.buffer.UtilKt.destroyBuffers;
import static uno.gl.GlErrorKt.checkError;
/**
* Created by elect on 04/03/17.
*/
public class HelloTriangle implements GLEventListener, KeyListener {
private static GLWindow window;
private static Animator animator;
public static void main(String[] args) {
new HelloTriangle().setup();
}
private float[] vertexData = {
-1, -1, 1, 0, 0,
+0, +2, 0, 0, 1,
+1, -1, 0, 1, 0};
private short[] elementData = {0, 2, 1};
private interface Buffer {
int VERTEX = 0;
int ELEMENT = 1;
int GLOBAL_MATRICES = 2;
int MAX = 3;
}
private IntBuffer bufferName = GLBuffers.newDirectIntBuffer(Buffer.MAX);
private IntBuffer vertexArrayName = GLBuffers.newDirectIntBuffer(1);
private FloatBuffer clearColor = GLBuffers.newDirectFloatBuffer(4);
private FloatBuffer clearDepth = GLBuffers.newDirectFloatBuffer(1);
private FloatBuffer matBuffer = GLBuffers.newDirectFloatBuffer(16);
private Program program;
private long start;
private void setup() {
GLProfile glProfile = GLProfile.get(GLProfile.GL3);
GLCapabilities glCapabilities = new GLCapabilities(glProfile);
window = GLWindow.create(glCapabilities);
window.setTitle("Hello Triangle (enhanced)");
window.setSize(1024, 768);
window.setVisible(true);
window.addGLEventListener(this);
window.addKeyListener(this);
animator = new Animator(window);
animator.start();
window.addWindowListener(new WindowAdapter() {
@Override
public void windowDestroyed(WindowEvent e) {
animator.stop();
System.exit(1);
}
});
}
@Override
public void init(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
initBuffers(gl);
initVertexArray(gl);
initProgram(gl);
gl.glEnable(GL_DEPTH_TEST);
start = System.currentTimeMillis();
}
private void initBuffers(GL3 gl) {
FloatBuffer vertexBuffer = GLBuffers.newDirectFloatBuffer(vertexData);
ShortBuffer elementBuffer = GLBuffers.newDirectShortBuffer(elementData);
gl.glGenBuffers(Buffer.MAX, bufferName);
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferName.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, bufferName.get(Buffer.ELEMENT));
gl.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer.capacity() * Short.BYTES, elementBuffer, GL_STATIC_DRAW);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.GLOBAL_MATRICES));
gl.glBufferData(GL_UNIFORM_BUFFER, Mat4x4.SIZE * 2, null, GL_STREAM_DRAW);
gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
gl.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.GLOBAL_MATRICES, bufferName.get(Buffer.GLOBAL_MATRICES));
destroyBuffers(vertexBuffer, elementBuffer);
checkError(gl, "initBuffers");
}
private void initVertexArray(GL3 gl) {
gl.glGenVertexArrays(1, vertexArrayName);
gl.glBindVertexArray(vertexArrayName.get(0));
{
gl.glBindBuffer(GL_ARRAY_BUFFER, bufferName.get(Buffer.VERTEX));
{
int stride = Vec2.SIZE + Vec3.SIZE;
int offset = 0;
gl.glEnableVertexAttribArray(Semantic.Attr.POSITION);
gl.glVertexAttribPointer(Semantic.Attr.POSITION, Vec2.length, GL_FLOAT, false, stride, offset);
offset = Vec2.SIZE;
gl.glEnableVertexAttribArray(Semantic.Attr.COLOR);
gl.glVertexAttribPointer(Semantic.Attr.COLOR, Vec3.length, GL_FLOAT, false, stride, offset);
}
gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
gl.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferName.get(Buffer.ELEMENT));
}
gl.glBindVertexArray(0);
checkError(gl, "initVao");
}
private void initProgram(GL3 gl) {
program = new Program(gl, getClass(), "shaders/gl3", "hello-triangle.vert", "hello-triangle.frag", "model");
int globalMatricesBI = gl.glGetUniformBlockIndex(program.name, "GlobalMatrices");
if (globalMatricesBI == -1) {
System.err.println("block index 'GlobalMatrices' not found!");
}
gl.glUniformBlockBinding(program.name, globalMatricesBI, Semantic.Uniform.GLOBAL_MATRICES);
checkError(gl, "initProgram");
}
@Override
public void display(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
// view matrix
{
Mat4x4 view = new Mat4x4();
view.to(matBuffer);
gl.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.GLOBAL_MATRICES));
gl.glBufferSubData(GL_UNIFORM_BUFFER, Mat4x4.SIZE, Mat4x4.SIZE, matBuffer);
gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
gl.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0f).put(1, .33f).put(2, 0.66f).put(3, 1f));
gl.glClearBufferfv(GL_DEPTH, 0, clearDepth.put(0, 1f));
gl.glUseProgram(program.name);
gl.glBindVertexArray(vertexArrayName.get(0));
// model matrix
{
long now = System.currentTimeMillis();
float diff = (float) (now - start) / 1_000f;
Mat4x4 model = new Mat4x4();
model
.scale(0.5f)
.rotate(diff, 0f, 0f, 1f)
.to(matBuffer);
gl.glUniformMatrix4fv(program.get("model"), 1, false, matBuffer);
}
gl.glDrawElements(GL_TRIANGLES, elementData.length, GL_UNSIGNED_SHORT, 0);
gl.glUseProgram(0);
gl.glBindVertexArray(0);
checkError(gl, "display");
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL3 gl = drawable.getGL().getGL3();
glm.ortho(-1f, 1f, -1f, 1f, 1f, -1f).to(matBuffer);
gl.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.GLOBAL_MATRICES));
gl.glBufferSubData(GL_UNIFORM_BUFFER, 0, Mat4x4.SIZE, matBuffer);
gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
gl.glViewport(x, y, width, height);
}
@Override
public void dispose(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
gl.glDeleteProgram(program.name);
gl.glDeleteVertexArrays(1, vertexArrayName);
gl.glDeleteBuffers(Buffer.MAX, bufferName);
destroyBuffers(vertexArrayName, bufferName, matBuffer, clearColor, clearDepth);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
new Thread(() -> {
window.destroy();
}).start();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}