Skip to content

Commit f64a7ee

Browse files
committed
Added randomising of speed
1 parent 4bf4906 commit f64a7ee

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ as the basics of batch rendering in graphics programming.
4545

4646
## Planned
4747

48-
- Add more complex algorithms
49-
- Abstract algorithm computation to be faster
48+
- [x] Add more complex algorithms
49+
- [x] Abstract algorithm computation to be faster

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.hobbeos</groupId>
88
<artifactId>SortingVisualiser</artifactId>
9-
<version>0.4</version>
9+
<version>0.5</version>
1010

1111
<url>https://github.com/cianjinks/Sorting-Visualizer</url>
1212

src/main/java/Application.java

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import imgui.gl3.ImGuiImplGl3;
99
import org.joml.Matrix4f;
1010
import org.joml.Vector4f;
11-
import org.lwjgl.BufferUtils;
1211
import org.lwjgl.glfw.*;
1312
import org.lwjgl.opengl.*;
1413
import org.lwjgl.system.*;
@@ -29,6 +28,7 @@ public class Application {
2928
public static int WINDOW_WIDTH = 1280;
3029
public static int WINDOW_HEIGHT = 720;
3130
public ArrayList<Quad> quads;
31+
private static int MAX_QUADS = 100;
3232
public ArrayList<Integer> barHeights;
3333
public static String WINDOW_TITLE = "Sorting Visualizer";
3434

@@ -40,6 +40,7 @@ public class Application {
4040
private boolean selection = false;
4141
private boolean quick = false;
4242
private boolean merge = false;
43+
private boolean reset = true;
4344

4445
ArrayList<ArrayList<int[]>> bubbleSimulation;
4546
private int bubbleFrame = 0;
@@ -111,25 +112,25 @@ private void init() {
111112
}
112113
}
113114
if(key == GLFW_KEY_B && action == GLFW_RELEASE) {
114-
if(!sorting) {
115+
if(!sorting && reset) {
115116
sorting = true;
116117
bubble = true;
117118
}
118119
}
119120
if(key == GLFW_KEY_I && action == GLFW_RELEASE) {
120-
if(!sorting) {
121+
if(!sorting && reset) {
121122
sorting = true;
122123
selection = true;
123124
}
124125
}
125126
if(key == GLFW_KEY_Q && action == GLFW_RELEASE) {
126-
if(!sorting) {
127+
if(!sorting && reset) {
127128
sorting = true;
128129
quick = true;
129130
}
130131
}
131132
if(key == GLFW_KEY_M && action == GLFW_RELEASE) {
132-
if(!sorting) {
133+
if(!sorting && reset) {
133134
sorting = true;
134135
merge = true;
135136
}
@@ -206,25 +207,6 @@ private void loop() {
206207
quads = new ArrayList<>();
207208
setupQuads();
208209

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-
228210
// VAO (Vertex Array Object)
229211
int vaoID = GL30.glGenVertexArrays();
230212
GL30.glBindVertexArray(vaoID);
@@ -233,16 +215,19 @@ private void loop() {
233215
int vboID = GL30.glGenBuffers();
234216
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, vboID);
235217
// 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);
237219
GL30.glBufferData(GL30.GL_ARRAY_BUFFER, vboBuffer.capacity() * Float.BYTES, GL30.GL_DYNAMIC_DRAW);
238220
GL30.glVertexAttribPointer(0, Vertex.positionElementCount, Vertex.type, false, Vertex.stride, Vertex.positionOffset);
239221
GL30.glVertexAttribPointer(1, Vertex.colorElementCount, Vertex.type, false, Vertex.stride, Vertex.colorOffset);
240222

241223
GL30.glBindVertexArray(0);
242224

225+
// IBO (Index Buffer Object)
243226
int iboID = GL30.glGenBuffers();
244227
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);
246231
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, 0);
247232

248233
Shader shaderHandler = new Shader();
@@ -251,6 +236,7 @@ private void loop() {
251236

252237
Matrix4f mvp = new Matrix4f().ortho(0.0f, 1280.0f, 0.0f, 720.0f, -1.0f, 1.0f);
253238

239+
// Simulate the sorting algorithms
254240
runSimulation();
255241

256242
// GUI
@@ -269,6 +255,7 @@ private void loop() {
269255
} else {
270256
bubble = false;
271257
sorting = false;
258+
reset = false;
272259
bubbleFrame = 0;
273260
}
274261
}
@@ -280,6 +267,7 @@ else if(selection) {
280267
} else {
281268
selection = false;
282269
sorting = false;
270+
reset = false;
283271
selectionFrame = 0;
284272
}
285273
}
@@ -292,6 +280,7 @@ else if(quick) {
292280
} else {
293281
quick = false;
294282
sorting = false;
283+
reset = false;
295284
quickFrame = 0;
296285
}
297286
}
@@ -303,6 +292,7 @@ else if(merge) {
303292
} else {
304293
merge = false;
305294
sorting = false;
295+
reset = false;
306296
mergeFrame = 0;
307297
}
308298
}
@@ -325,23 +315,50 @@ else if(merge) {
325315

326316
// Bind VBO and dynamically fill it with data
327317
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);
329320

330321
// Bind VAO
331322
GL30.glBindVertexArray(vaoID);
332323
GL30.glEnableVertexAttribArray(0);
333324
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();
334343
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);
335346

336347
// Draw the vertices
337348
GL30.glDrawElements(GL30.GL_TRIANGLES, Quad.indicesCount, GL_UNSIGNED_INT, 0);
338349

339350
// Unbind VAO
340351
GL30.glBindBuffer(GL30.GL_ELEMENT_ARRAY_BUFFER, 0);
352+
GL30.glBindBuffer(GL30.GL_ARRAY_BUFFER, 0);
341353
GL30.glDisableVertexAttribArray(0);
342354
GL30.glDisableVertexAttribArray(1);
343355
GL30.glBindVertexArray(0);
344356

357+
// Clear the VBO
358+
vboBuffer.clear();
359+
// Clear the IBO
360+
iboBuffer.clear();
361+
345362
// GUI
346363
final double currentTime = glfwGetTime();
347364
final double deltaTime = (time > 0) ? (currentTime - time) : 1f / 60f;
@@ -359,27 +376,27 @@ else if(merge) {
359376
ImGui.setNextWindowPos(25, 25, ImGuiCond.Once);
360377
ImGui.begin("Controls");
361378
if(ImGui.button("Bubble Sort", 125f, 30f)) {
362-
if(!sorting) {
379+
if(!sorting && reset) {
363380
sorting = true;
364381
bubble = true;
365382
}
366383
}
367384
ImGui.sameLine(0f, -1f);
368385
if(ImGui.button("Selection Sort", 125f, 30f)) {
369-
if(!sorting) {
386+
if(!sorting && reset) {
370387
sorting = true;
371388
selection = true;
372389
}
373390
}
374391
if(ImGui.button("Merge Sort", 125f, 30f)) {
375-
if(!sorting) {
392+
if(!sorting && reset) {
376393
sorting = true;
377394
merge = true;;
378395
}
379396
}
380397
ImGui.sameLine(0f, -1f);
381398
if(ImGui.button("Quick Sort", 125f, 30f)) {
382-
if(!sorting) {
399+
if(!sorting && reset) {
383400
sorting = true;
384401
quick = true;;
385402
}
@@ -388,26 +405,28 @@ else if(merge) {
388405
if(!sorting) {
389406
resetBars();
390407
runSimulation();
408+
reset = true;
391409
}
392410
}
393-
/**if(ImGui.button("Randomise Speed", 125f, 30f)) {
411+
ImGui.sameLine(0f, -1f);
412+
if(ImGui.button("Randomise Speed", 125f, 30f)) {
394413
if(!sorting) {
395414
barHeights = new ArrayList<>();
396415
numBars = random.nextInt(96) + 5;
397-
for(int index = 0; index < numBars; index++) {
416+
for (int index = 0; index < numBars; index++) {
398417
barHeights.add(Math.round(random.nextFloat() * 500.0f));
399418
}
400419
resetBars();
401-
}**/
420+
runSimulation();
421+
reset = true;
422+
}
423+
}
402424

403425
ImGui.end();
404426

405427
ImGui.render();
406428
imGuiGl3.render(ImGui.getDrawData());
407429

408-
// Clear the VBO
409-
vboBuffer.clear();
410-
411430
shaderHandler.unBindProgram();
412431

413432
glfwSwapBuffers(window); // swap the color buffers

0 commit comments

Comments
 (0)