8
8
import imgui .gl3 .ImGuiImplGl3 ;
9
9
import org .joml .Matrix4f ;
10
10
import org .joml .Vector4f ;
11
- import org .lwjgl .BufferUtils ;
12
11
import org .lwjgl .glfw .*;
13
12
import org .lwjgl .opengl .*;
14
13
import org .lwjgl .system .*;
@@ -29,6 +28,7 @@ public class Application {
29
28
public static int WINDOW_WIDTH = 1280 ;
30
29
public static int WINDOW_HEIGHT = 720 ;
31
30
public ArrayList <Quad > quads ;
31
+ private static int MAX_QUADS = 100 ;
32
32
public ArrayList <Integer > barHeights ;
33
33
public static String WINDOW_TITLE = "Sorting Visualizer" ;
34
34
@@ -40,6 +40,7 @@ public class Application {
40
40
private boolean selection = false ;
41
41
private boolean quick = false ;
42
42
private boolean merge = false ;
43
+ private boolean reset = true ;
43
44
44
45
ArrayList <ArrayList <int []>> bubbleSimulation ;
45
46
private int bubbleFrame = 0 ;
@@ -111,25 +112,25 @@ private void init() {
111
112
}
112
113
}
113
114
if (key == GLFW_KEY_B && action == GLFW_RELEASE ) {
114
- if (!sorting ) {
115
+ if (!sorting && reset ) {
115
116
sorting = true ;
116
117
bubble = true ;
117
118
}
118
119
}
119
120
if (key == GLFW_KEY_I && action == GLFW_RELEASE ) {
120
- if (!sorting ) {
121
+ if (!sorting && reset ) {
121
122
sorting = true ;
122
123
selection = true ;
123
124
}
124
125
}
125
126
if (key == GLFW_KEY_Q && action == GLFW_RELEASE ) {
126
- if (!sorting ) {
127
+ if (!sorting && reset ) {
127
128
sorting = true ;
128
129
quick = true ;
129
130
}
130
131
}
131
132
if (key == GLFW_KEY_M && action == GLFW_RELEASE ) {
132
- if (!sorting ) {
133
+ if (!sorting && reset ) {
133
134
sorting = true ;
134
135
merge = true ;
135
136
}
@@ -206,25 +207,6 @@ private void loop() {
206
207
quads = new ArrayList <>();
207
208
setupQuads ();
208
209
209
- // IBO (Index Buffer Object)
210
- // 768 for 16kb of vertex memory
211
- int [] indices = new int [quads .size () * Quad .indicesPerQuad ];
212
- int offset = 0 ;
213
- for (int i = 0 ; i < indices .length ; i += 6 ) {
214
- indices [i + 0 ] = 0 + offset ;
215
- indices [i + 1 ] = 1 + offset ;
216
- indices [i + 2 ] = 2 + offset ;
217
-
218
- indices [i + 3 ] = 2 + offset ;
219
- indices [i + 4 ] = 3 + offset ;
220
- indices [i + 5 ] = 0 + offset ;
221
-
222
- offset += 4 ;
223
- }
224
- IntBuffer iboBuffer = BufferUtils .createIntBuffer (indices .length );
225
- iboBuffer .put (indices );
226
- iboBuffer .flip ();
227
-
228
210
// VAO (Vertex Array Object)
229
211
int vaoID = GL30 .glGenVertexArrays ();
230
212
GL30 .glBindVertexArray (vaoID );
@@ -233,16 +215,19 @@ private void loop() {
233
215
int vboID = GL30 .glGenBuffers ();
234
216
GL30 .glBindBuffer (GL30 .GL_ARRAY_BUFFER , vboID );
235
217
// Call is now dynamic and so we allocate memory (16kB) or 512 vertices (8 floats per vertex) (768 indices)
236
- FloatBuffer vboBuffer = MemoryUtil .memAllocFloat (2 * Quad .verticesPerQuad * 512 );
218
+ FloatBuffer vboBuffer = MemoryUtil .memAllocFloat (2 * Quad .verticesPerQuad * 1024 );
237
219
GL30 .glBufferData (GL30 .GL_ARRAY_BUFFER , vboBuffer .capacity () * Float .BYTES , GL30 .GL_DYNAMIC_DRAW );
238
220
GL30 .glVertexAttribPointer (0 , Vertex .positionElementCount , Vertex .type , false , Vertex .stride , Vertex .positionOffset );
239
221
GL30 .glVertexAttribPointer (1 , Vertex .colorElementCount , Vertex .type , false , Vertex .stride , Vertex .colorOffset );
240
222
241
223
GL30 .glBindVertexArray (0 );
242
224
225
+ // IBO (Index Buffer Object)
243
226
int iboID = GL30 .glGenBuffers ();
244
227
GL30 .glBindBuffer (GL30 .GL_ELEMENT_ARRAY_BUFFER , iboID );
245
- GL30 .glBufferData (GL30 .GL_ELEMENT_ARRAY_BUFFER , iboBuffer , GL30 .GL_STATIC_DRAW );
228
+ // Just like with VBO call is now dynamic
229
+ IntBuffer iboBuffer = MemoryUtil .memAllocInt (2 * Quad .indicesPerQuad * 1024 );
230
+ GL30 .glBufferData (GL30 .GL_ELEMENT_ARRAY_BUFFER , iboBuffer .capacity () * Integer .BYTES , GL30 .GL_DYNAMIC_DRAW );
246
231
GL30 .glBindBuffer (GL30 .GL_ELEMENT_ARRAY_BUFFER , 0 );
247
232
248
233
Shader shaderHandler = new Shader ();
@@ -251,6 +236,7 @@ private void loop() {
251
236
252
237
Matrix4f mvp = new Matrix4f ().ortho (0.0f , 1280.0f , 0.0f , 720.0f , -1.0f , 1.0f );
253
238
239
+ // Simulate the sorting algorithms
254
240
runSimulation ();
255
241
256
242
// GUI
@@ -269,6 +255,7 @@ private void loop() {
269
255
} else {
270
256
bubble = false ;
271
257
sorting = false ;
258
+ reset = false ;
272
259
bubbleFrame = 0 ;
273
260
}
274
261
}
@@ -280,6 +267,7 @@ else if(selection) {
280
267
} else {
281
268
selection = false ;
282
269
sorting = false ;
270
+ reset = false ;
283
271
selectionFrame = 0 ;
284
272
}
285
273
}
@@ -292,6 +280,7 @@ else if(quick) {
292
280
} else {
293
281
quick = false ;
294
282
sorting = false ;
283
+ reset = false ;
295
284
quickFrame = 0 ;
296
285
}
297
286
}
@@ -303,6 +292,7 @@ else if(merge) {
303
292
} else {
304
293
merge = false ;
305
294
sorting = false ;
295
+ reset = false ;
306
296
mergeFrame = 0 ;
307
297
}
308
298
}
@@ -325,23 +315,50 @@ else if(merge) {
325
315
326
316
// Bind VBO and dynamically fill it with data
327
317
GL30 .glBindBuffer (GL30 .GL_ARRAY_BUFFER , vboID );
328
- GL30 .glBufferSubData (GL30 .GL_ARRAY_BUFFER , 0 , vboBuffer );
318
+ //GL30.glBufferSubData(GL30.GL_ARRAY_BUFFER, 0, vboBuffer);
319
+ GL30 .glBufferData (GL30 .GL_ARRAY_BUFFER , vboBuffer , GL30 .GL_DYNAMIC_DRAW );
329
320
330
321
// Bind VAO
331
322
GL30 .glBindVertexArray (vaoID );
332
323
GL30 .glEnableVertexAttribArray (0 );
333
324
GL30 .glEnableVertexAttribArray (1 );
325
+
326
+ // Bind IBO and dynamically fill it with data
327
+ // 768 for 16kb of vertex memory
328
+ int [] indices = new int [MAX_QUADS * Quad .indicesPerQuad ];
329
+ int offset = 0 ;
330
+ for (int i = 0 ; i < indices .length ; i += 6 ) {
331
+ indices [i + 0 ] = 0 + offset ;
332
+ indices [i + 1 ] = 1 + offset ;
333
+ indices [i + 2 ] = 2 + offset ;
334
+
335
+ indices [i + 3 ] = 2 + offset ;
336
+ indices [i + 4 ] = 3 + offset ;
337
+ indices [i + 5 ] = 0 + offset ;
338
+
339
+ offset += 4 ;
340
+ }
341
+ iboBuffer .put (indices );
342
+ iboBuffer .flip ();
334
343
GL30 .glBindBuffer (GL30 .GL_ELEMENT_ARRAY_BUFFER , iboID );
344
+ GL30 .glBufferSubData (GL30 .GL_ELEMENT_ARRAY_BUFFER , 0 , iboBuffer );
345
+ //GL30.glBufferData(GL30.GL_ELEMENT_ARRAY_BUFFER, iboBuffer, GL30.GL_DYNAMIC_DRAW);
335
346
336
347
// Draw the vertices
337
348
GL30 .glDrawElements (GL30 .GL_TRIANGLES , Quad .indicesCount , GL_UNSIGNED_INT , 0 );
338
349
339
350
// Unbind VAO
340
351
GL30 .glBindBuffer (GL30 .GL_ELEMENT_ARRAY_BUFFER , 0 );
352
+ GL30 .glBindBuffer (GL30 .GL_ARRAY_BUFFER , 0 );
341
353
GL30 .glDisableVertexAttribArray (0 );
342
354
GL30 .glDisableVertexAttribArray (1 );
343
355
GL30 .glBindVertexArray (0 );
344
356
357
+ // Clear the VBO
358
+ vboBuffer .clear ();
359
+ // Clear the IBO
360
+ iboBuffer .clear ();
361
+
345
362
// GUI
346
363
final double currentTime = glfwGetTime ();
347
364
final double deltaTime = (time > 0 ) ? (currentTime - time ) : 1f / 60f ;
@@ -359,27 +376,27 @@ else if(merge) {
359
376
ImGui .setNextWindowPos (25 , 25 , ImGuiCond .Once );
360
377
ImGui .begin ("Controls" );
361
378
if (ImGui .button ("Bubble Sort" , 125f , 30f )) {
362
- if (!sorting ) {
379
+ if (!sorting && reset ) {
363
380
sorting = true ;
364
381
bubble = true ;
365
382
}
366
383
}
367
384
ImGui .sameLine (0f , -1f );
368
385
if (ImGui .button ("Selection Sort" , 125f , 30f )) {
369
- if (!sorting ) {
386
+ if (!sorting && reset ) {
370
387
sorting = true ;
371
388
selection = true ;
372
389
}
373
390
}
374
391
if (ImGui .button ("Merge Sort" , 125f , 30f )) {
375
- if (!sorting ) {
392
+ if (!sorting && reset ) {
376
393
sorting = true ;
377
394
merge = true ;;
378
395
}
379
396
}
380
397
ImGui .sameLine (0f , -1f );
381
398
if (ImGui .button ("Quick Sort" , 125f , 30f )) {
382
- if (!sorting ) {
399
+ if (!sorting && reset ) {
383
400
sorting = true ;
384
401
quick = true ;;
385
402
}
@@ -388,26 +405,28 @@ else if(merge) {
388
405
if (!sorting ) {
389
406
resetBars ();
390
407
runSimulation ();
408
+ reset = true ;
391
409
}
392
410
}
393
- /**if(ImGui.button("Randomise Speed", 125f, 30f)) {
411
+ ImGui .sameLine (0f , -1f );
412
+ if (ImGui .button ("Randomise Speed" , 125f , 30f )) {
394
413
if (!sorting ) {
395
414
barHeights = new ArrayList <>();
396
415
numBars = random .nextInt (96 ) + 5 ;
397
- for(int index = 0; index < numBars; index++) {
416
+ for (int index = 0 ; index < numBars ; index ++) {
398
417
barHeights .add (Math .round (random .nextFloat () * 500.0f ));
399
418
}
400
419
resetBars ();
401
- }**/
420
+ runSimulation ();
421
+ reset = true ;
422
+ }
423
+ }
402
424
403
425
ImGui .end ();
404
426
405
427
ImGui .render ();
406
428
imGuiGl3 .render (ImGui .getDrawData ());
407
429
408
- // Clear the VBO
409
- vboBuffer .clear ();
410
-
411
430
shaderHandler .unBindProgram ();
412
431
413
432
glfwSwapBuffers (window ); // swap the color buffers
0 commit comments