From 7ab8dd96f301b0f921afe8bdff820479f29bb49f Mon Sep 17 00:00:00 2001 From: ahbejarano Date: Thu, 26 Dec 2019 16:54:00 +0100 Subject: [PATCH] Attribute locations set-up upon Mesh creation --- .../main/java/org/lwjglb/game/Renderer.java | 37 ++++++-- .../java/org/lwjglb/engine/graph/Mesh.java | 23 ++++- .../main/java/org/lwjglb/game/Renderer.java | 14 ++- .../java/org/lwjglb/engine/graph/Mesh.java | 24 ++++- .../main/java/org/lwjglb/game/Renderer.java | 18 ++-- .../java/org/lwjglb/engine/graph/Mesh.java | 24 ++++- .../main/java/org/lwjglb/game/Renderer.java | 22 +++-- .../java/org/lwjglb/engine/graph/Mesh.java | 31 +++++-- .../java/org/lwjglb/engine/graph/Mesh.java | 31 +++++-- .../java/org/lwjglb/engine/graph/Mesh.java | 36 ++++++-- .../java/org/lwjglb/engine/graph/Mesh.java | 36 ++++++-- .../java/org/lwjglb/engine/graph/Mesh.java | 43 ++++++--- .../java/org/lwjglb/engine/graph/Mesh.java | 39 +++++--- .../java/org/lwjglb/engine/graph/Mesh.java | 39 +++++--- .../java/org/lwjglb/engine/graph/Mesh.java | 39 +++++--- .../java/org/lwjglb/engine/graph/Mesh.java | 39 +++++--- .../java/org/lwjglb/engine/graph/Mesh.java | 47 ++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 47 ++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 47 ++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 47 ++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 51 +++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 51 +++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 51 +++++++---- .../java/org/lwjglb/engine/graph/Mesh.java | 59 +++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 62 ++++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 62 ++++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 62 ++++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 62 ++++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 62 ++++++++----- .../java/org/lwjglb/engine/graph/Mesh.java | 68 ++++++++------ .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 65 ++++++------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 84 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 91 ++++++++----------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 82 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 85 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 85 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 85 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- .../lwjglb/engine/graph/InstancedMesh.java | 85 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 66 ++++++++------ .../lwjglb/engine/graph/InstancedMesh.java | 85 ++++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 66 ++++++++------ .../lwjglb/engine/graph/InstancedMesh.java | 80 +++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 60 +++++++----- .../lwjglb/engine/graph/InstancedMesh.java | 80 +++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 66 ++++++++------ .../lwjglb/engine/graph/InstancedMesh.java | 80 +++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 66 ++++++++------ .../lwjglb/engine/graph/InstancedMesh.java | 80 +++++++--------- .../java/org/lwjglb/engine/graph/Mesh.java | 76 +++++++++------- pom.xml | 2 +- 59 files changed, 1956 insertions(+), 1470 deletions(-) diff --git a/chapter04/src/main/java/org/lwjglb/game/Renderer.java b/chapter04/src/main/java/org/lwjglb/game/Renderer.java index ce42b2fd..7dc5dec2 100644 --- a/chapter04/src/main/java/org/lwjglb/game/Renderer.java +++ b/chapter04/src/main/java/org/lwjglb/game/Renderer.java @@ -1,15 +1,32 @@ package org.lwjglb.game; -import java.nio.FloatBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.Utils; import org.lwjglb.engine.Window; import org.lwjglb.engine.graph.ShaderProgram; +import java.nio.FloatBuffer; + +import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.glClear; +import static org.lwjgl.opengl.GL11.glDrawArrays; +import static org.lwjgl.opengl.GL11.glViewport; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; + public class Renderer { private int vboId; @@ -28,9 +45,9 @@ public void init() throws Exception { shaderProgram.link(); float[] vertices = new float[]{ - 0.0f, 0.5f, 0.0f, - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f + 0.0f, 0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f }; FloatBuffer verticesBuffer = null; @@ -46,6 +63,8 @@ public void init() throws Exception { vboId = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW); + // Enable location 0 + glEnableVertexAttribArray(0); // Define structure of the data glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); @@ -77,13 +96,11 @@ public void render(Window window) { // Bind to the VAO glBindVertexArray(vaoId); - glEnableVertexAttribArray(0); // Draw the vertices glDrawArrays(GL_TRIANGLES, 0, 3); // Restore state - glDisableVertexAttribArray(0); glBindVertexArray(0); shaderProgram.unbind(); diff --git a/chapter05/c05-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter05/c05-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 288ce743..30845e2e 100644 --- a/chapter05/c05-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter05/c05-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,12 +1,24 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -33,6 +45,7 @@ public Mesh(float[] positions, int[] indices) { posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Index VBO diff --git a/chapter05/c05-p1/src/main/java/org/lwjglb/game/Renderer.java b/chapter05/c05-p1/src/main/java/org/lwjglb/game/Renderer.java index acab6525..ea6ac14f 100644 --- a/chapter05/c05-p1/src/main/java/org/lwjglb/game/Renderer.java +++ b/chapter05/c05-p1/src/main/java/org/lwjglb/game/Renderer.java @@ -1,13 +1,19 @@ package org.lwjglb.game; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; import org.lwjglb.engine.Utils; import org.lwjglb.engine.Window; import org.lwjglb.engine.graph.Mesh; import org.lwjglb.engine.graph.ShaderProgram; +import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glClear; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL11.glViewport; +import static org.lwjgl.opengl.GL30.glBindVertexArray; + public class Renderer { private ShaderProgram shaderProgram; @@ -38,11 +44,9 @@ public void render(Window window, Mesh mesh) { // Draw the mesh glBindVertexArray(mesh.getVaoId()); - glEnableVertexAttribArray(0); glDrawElements(GL_TRIANGLES, mesh.getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); glBindVertexArray(0); shaderProgram.unbind(); diff --git a/chapter05/c05-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter05/c05-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 833e750d..8e7fff9a 100644 --- a/chapter05/c05-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter05/c05-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,12 +1,24 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -36,6 +48,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO @@ -44,6 +57,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO diff --git a/chapter05/c05-p2/src/main/java/org/lwjglb/game/Renderer.java b/chapter05/c05-p2/src/main/java/org/lwjglb/game/Renderer.java index 69de4b59..ea6ac14f 100644 --- a/chapter05/c05-p2/src/main/java/org/lwjglb/game/Renderer.java +++ b/chapter05/c05-p2/src/main/java/org/lwjglb/game/Renderer.java @@ -1,13 +1,19 @@ package org.lwjglb.game; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; import org.lwjglb.engine.Utils; import org.lwjglb.engine.Window; import org.lwjglb.engine.graph.Mesh; import org.lwjglb.engine.graph.ShaderProgram; +import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glClear; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL11.glViewport; +import static org.lwjgl.opengl.GL30.glBindVertexArray; + public class Renderer { private ShaderProgram shaderProgram; @@ -29,7 +35,7 @@ public void clear() { public void render(Window window, Mesh mesh) { clear(); - if ( window.isResized() ) { + if (window.isResized()) { glViewport(0, 0, window.getWidth(), window.getHeight()); window.setResized(false); } @@ -38,13 +44,9 @@ public void render(Window window, Mesh mesh) { // Draw the mesh glBindVertexArray(mesh.getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, mesh.getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); shaderProgram.unbind(); diff --git a/chapter06/c06-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter06/c06-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 833e750d..8e7fff9a 100644 --- a/chapter06/c06-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter06/c06-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,12 +1,24 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -36,6 +48,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO @@ -44,6 +57,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO diff --git a/chapter06/c06-p1/src/main/java/org/lwjglb/game/Renderer.java b/chapter06/c06-p1/src/main/java/org/lwjglb/game/Renderer.java index 382c122c..5fe8d837 100644 --- a/chapter06/c06-p1/src/main/java/org/lwjglb/game/Renderer.java +++ b/chapter06/c06-p1/src/main/java/org/lwjglb/game/Renderer.java @@ -1,14 +1,20 @@ package org.lwjglb.game; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; import org.lwjglb.engine.Utils; import org.lwjglb.engine.Window; import org.lwjglb.engine.graph.Mesh; import org.lwjglb.engine.graph.ShaderProgram; +import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glClear; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL11.glViewport; +import static org.lwjgl.opengl.GL30.glBindVertexArray; + public class Renderer { /** @@ -33,10 +39,10 @@ public void init(Window window) throws Exception { shaderProgram.createVertexShader(Utils.loadResource("/vertex.vs")); shaderProgram.createFragmentShader(Utils.loadResource("/fragment.fs")); shaderProgram.link(); - + // Create projection matrix float aspectRatio = (float) window.getWidth() / window.getHeight(); - projectionMatrix = new Matrix4f().perspective(FOV, aspectRatio, Z_NEAR, Z_FAR); + projectionMatrix = new Matrix4f().perspective(Renderer.FOV, aspectRatio, Renderer.Z_NEAR, Renderer.Z_FAR); shaderProgram.createUniform("projectionMatrix"); } @@ -47,7 +53,7 @@ public void clear() { public void render(Window window, Mesh mesh) { clear(); - if ( window.isResized() ) { + if (window.isResized()) { glViewport(0, 0, window.getWidth(), window.getHeight()); window.setResized(false); } @@ -57,14 +63,10 @@ public void render(Window window, Mesh mesh) { // Draw the mesh glBindVertexArray(mesh.getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, mesh.getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); shaderProgram.unbind(); diff --git a/chapter06/c06-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter06/c06-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index bddaf9a9..c1159767 100644 --- a/chapter06/c06-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter06/c06-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,12 +1,27 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -36,6 +51,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO @@ -44,6 +60,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -79,14 +96,10 @@ public int getVertexCount() { public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); } diff --git a/chapter07/c07-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter07/c07-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index bddaf9a9..c1159767 100644 --- a/chapter07/c07-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter07/c07-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,12 +1,27 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -36,6 +51,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, posVboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Colour VBO @@ -44,6 +60,7 @@ public Mesh(float[] positions, float[] colours, int[] indices) { colourBuffer.put(colours).flip(); glBindBuffer(GL_ARRAY_BUFFER, colourVboId); glBufferData(GL_ARRAY_BUFFER, colourBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -79,14 +96,10 @@ public int getVertexCount() { public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); } diff --git a/chapter07/c07-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter07/c07-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index d13256c5..cfb343b0 100644 --- a/chapter07/c07-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter07/c07-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, int[] indices, Texture textur posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, int[] indices, Texture textur textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Index VBO @@ -90,14 +110,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); } diff --git a/chapter08/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter08/src/main/java/org/lwjglb/engine/graph/Mesh.java index d13256c5..cfb343b0 100644 --- a/chapter08/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter08/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, int[] indices, Texture textur posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, int[] indices, Texture textur textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Index VBO @@ -90,14 +110,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); glBindVertexArray(0); } diff --git a/chapter09/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter09/src/main/java/org/lwjglb/engine/graph/Mesh.java index 914d1793..cb162cea 100644 --- a/chapter09/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter09/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,16 +1,34 @@ package org.lwjglb.engine.graph; +import org.joml.Vector3f; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import org.joml.Vector3f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -32,7 +50,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice FloatBuffer vecNormalsBuffer = null; IntBuffer indicesBuffer = null; try { - colour = DEFAULT_COLOUR; + colour = Mesh.DEFAULT_COLOUR; vertexCount = indices.length; vboIdList = new ArrayList<>(); @@ -46,6 +64,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -55,6 +74,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -64,6 +84,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -130,16 +151,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter10/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter10/src/main/java/org/lwjglb/engine/graph/Mesh.java index ad2fad21..6b62b047 100644 --- a/chapter10/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter10/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -58,6 +78,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -113,16 +134,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter11/c11-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter11/c11-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index ad2fad21..6b62b047 100644 --- a/chapter11/c11-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter11/c11-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -58,6 +78,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -113,16 +134,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter11/c11-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter11/c11-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index ad2fad21..6b62b047 100644 --- a/chapter11/c11-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter11/c11-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -58,6 +78,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -113,16 +134,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter11/c11-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter11/c11-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java index ad2fad21..6b62b047 100644 --- a/chapter11/c11-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter11/c11-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,6 +68,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -58,6 +78,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -113,16 +134,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter12/c12-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter12/c12-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0a095543..266bab93 100644 --- a/chapter12/c12-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter12/c12-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,20 +68,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,16 +139,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter12/c12-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter12/c12-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0a095543..266bab93 100644 --- a/chapter12/c12-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter12/c12-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,20 +68,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,16 +139,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter12/c12-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter12/c12-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0a095543..266bab93 100644 --- a/chapter12/c12-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter12/c12-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,20 +68,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,16 +139,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter13/c13-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter13/c13-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0a095543..266bab93 100644 --- a/chapter13/c13-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter13/c13-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,15 +1,33 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -40,6 +58,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -49,20 +68,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,16 +139,10 @@ public void render() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/chapter13/c13-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter13/c13-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 76611ba5..5d02211d 100644 --- a/chapter13/c13-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter13/c13-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,35 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +70,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -120,16 +141,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -147,7 +162,7 @@ public void renderList(List gameItems, Consumer consumer) { initRender(); for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter14/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter14/src/main/java/org/lwjglb/engine/graph/Mesh.java index b942c07e..2630f442 100644 --- a/chapter14/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter14/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,35 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +70,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -120,16 +141,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -147,7 +162,7 @@ public void renderList(List gameItems, Consumer consumer) { initRender(); for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter15/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter15/src/main/java/org/lwjglb/engine/graph/Mesh.java index b942c07e..2630f442 100644 --- a/chapter15/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter15/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,35 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +70,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -120,16 +141,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -147,7 +162,7 @@ public void renderList(List gameItems, Consumer consumer) { initRender(); for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter16/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter16/src/main/java/org/lwjglb/engine/graph/Mesh.java index 15b60a87..2630f442 100644 --- a/chapter16/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter16/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,35 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +70,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -120,16 +141,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -137,17 +152,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter17/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter17/src/main/java/org/lwjglb/engine/graph/Mesh.java index e8948061..fdb045ce 100644 --- a/chapter17/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter17/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,36 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +61,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +71,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,7 +140,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -127,16 +149,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -144,17 +160,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter18/c18-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter18/c18-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0b3b5e52..36cabaa9 100644 --- a/chapter18/c18-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter18/c18-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,36 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +61,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +71,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,7 +140,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -127,16 +149,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -144,17 +160,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter18/c18-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter18/c18-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0b3b5e52..36cabaa9 100644 --- a/chapter18/c18-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter18/c18-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,36 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +61,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +71,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,7 +140,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -127,16 +149,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -144,17 +160,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter19/c19-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter19/c19-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 0b3b5e52..36cabaa9 100644 --- a/chapter19/c19-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter19/c19-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,17 +1,36 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -42,6 +61,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -51,20 +71,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Index VBO @@ -118,7 +140,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -127,16 +149,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -144,17 +160,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter19/c19-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter19/c19-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index bbb349e3..b6116ce8 100644 --- a/chapter19/c19-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter19/c19-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,7 +46,7 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -160,20 +184,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -191,7 +205,7 @@ public void renderList(List gameItems, Consumer consumer) { initRender(); for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter19/c19-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter19/c19-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java index 3ad80ac8..7e76323b 100644 --- a/chapter19/c19-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter19/c19-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,6 +80,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO @@ -69,6 +90,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice vecNormalsBuffer.put(normals).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -78,6 +100,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -87,6 +110,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -117,7 +141,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -146,7 +170,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -155,20 +179,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -176,17 +190,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -226,7 +240,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + private static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter20/c20-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter20/c20-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 9eda67c3..b6116ce8 100644 --- a/chapter20/c20-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter20/c20-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + private static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter20/c20-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter20/c20-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 9eda67c3..b6116ce8 100644 --- a/chapter20/c20-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter20/c20-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ private void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ private void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + private void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ private void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + private static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 562208e1..b964548d 100644 --- a/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,22 +1,31 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int VECTOR4F_SIZE_BYTES = 4 * 4; - private static final int MATRIX_SIZE_BYTES = 4 * VECTOR4F_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = 4 * InstancedMesh.VECTOR4F_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; @@ -31,7 +40,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer modelLightViewBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -40,23 +49,25 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix modelViewVBO = glGenBuffers(); vboIdList.add(modelViewVBO); - this.modelViewBuffer = MemoryUtil.memAllocFloat(numInstances * MATRIX_SIZE_FLOATS); + this.modelViewBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.MATRIX_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, modelViewVBO); int start = 5; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, MATRIX_SIZE_BYTES, i * VECTOR4F_SIZE_BYTES); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.MATRIX_SIZE_BYTES, i * InstancedMesh.VECTOR4F_SIZE_BYTES); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; } // Light view matrix modelLightViewVBO = glGenBuffers(); vboIdList.add(modelLightViewVBO); - this.modelLightViewBuffer = MemoryUtil.memAllocFloat(numInstances * MATRIX_SIZE_FLOATS); + this.modelLightViewBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.MATRIX_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, modelLightViewVBO); for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, MATRIX_SIZE_BYTES, i * VECTOR4F_SIZE_BYTES); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.MATRIX_SIZE_BYTES, i * InstancedMesh.VECTOR4F_SIZE_BYTES); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; } @@ -77,28 +88,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, boolean depthMap, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { initRender(); @@ -123,10 +112,10 @@ private void renderChunkInstanced(List gameItems, boolean depthMap, Tr Matrix4f modelMatrix = transformation.buildModelMatrix(gameItem); if (!depthMap) { Matrix4f modelViewMatrix = transformation.buildModelViewMatrix(modelMatrix, viewMatrix); - modelViewMatrix.get(MATRIX_SIZE_FLOATS * i, modelViewBuffer); + modelViewMatrix.get(InstancedMesh.MATRIX_SIZE_FLOATS * i, modelViewBuffer); } Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(MATRIX_SIZE_FLOATS * i, this.modelLightViewBuffer); + modelLightViewMatrix.get(InstancedMesh.MATRIX_SIZE_FLOATS * i, this.modelLightViewBuffer); i++; } diff --git a/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter21/c21-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 58ac7f92..cce943eb 100644 --- a/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2; - - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 2; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; + + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2; + + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,28 +51,31 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -78,28 +90,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -131,21 +121,21 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T viewMatrix.transpose3x3(modelMatrix); } Matrix4f modelViewMatrix = transformation.buildModelViewMatrix(modelMatrix, viewMatrix); - if ( billBoard ) { + if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } diff --git a/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter21/c21-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 1a269fd9..97de335b 100644 --- a/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2; - - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 2; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; + + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2; + + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,64 +51,36 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } - @Override - public void cleanUp() { - super.cleanUp(); - if (this.instanceDataBuffer != null) { - MemoryUtil.memFree(this.instanceDataBuffer); - this.instanceDataBuffer = null; - } - } - - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -134,18 +115,18 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } diff --git a/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter22/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter22/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 1a269fd9..cce943eb 100644 --- a/chapter22/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter22/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2; - - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 2; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; + + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2; + + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,28 +51,31 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -78,28 +90,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 1; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -134,18 +124,18 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } diff --git a/chapter22/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter22/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter22/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter22/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 5e17b14b..2e59d116 100644 --- a/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,34 +51,38 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -85,28 +98,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -141,24 +132,24 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2 + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; this.instanceDataBuffer.put(buffPos, gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter23/c23-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 5e17b14b..2e59d116 100644 --- a/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,34 +51,38 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -85,28 +98,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -141,24 +132,24 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2 + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; this.instanceDataBuffer.put(buffPos, gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter23/c23-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter24/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter24/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 5e17b14b..2e59d116 100644 --- a/chapter24/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter24/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,34 +51,38 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -85,28 +98,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -141,24 +132,24 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2 + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; this.instanceDataBuffer.put(buffPos, gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter24/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter24/src/main/java/org/lwjglb/engine/graph/Mesh.java index e3344380..5655bee8 100644 --- a/chapter24/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter24/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -27,9 +46,9 @@ public class Mesh { private Material material; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } - + public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { FloatBuffer posBuffer = null; FloatBuffer textCoordsBuffer = null; @@ -51,6 +70,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -60,20 +80,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -83,6 +105,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -92,6 +115,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -122,7 +146,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } if (indicesBuffer != null) { MemoryUtil.memFree(indicesBuffer); - } + } } } @@ -151,7 +175,7 @@ protected void initRender() { glBindTexture(GL_TEXTURE_2D, texture.getId()); } Texture normalMap = material.getNormalMap(); - if ( normalMap != null ) { + if (normalMap != null) { // Activate first texture bank glActiveTexture(GL_TEXTURE1); // Bind the texture @@ -160,20 +184,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } - + protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -181,17 +195,17 @@ protected void endRender() { public void render() { initRender(); - + glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); endRender(); } - + public void renderList(List gameItems, Consumer consumer) { initRender(); - + for (GameItem gameItem : gameItems) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); @@ -231,7 +245,7 @@ public void deleteBuffers() { glBindVertexArray(0); glDeleteVertexArrays(vaoId); } - + protected static float[] createEmptyFloatArray(int length, float defaultValue) { float[] result = new float[length]; Arrays.fill(result, defaultValue); diff --git a/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 5e17b14b..2e59d116 100644 --- a/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,34 +51,38 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -85,28 +98,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -141,24 +132,24 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2 + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; this.instanceDataBuffer.put(buffPos, gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 6c38d514..d1ccc6ac 100644 --- a/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter25/c25-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -25,11 +44,11 @@ public class Mesh { private final int vertexCount; private Material material; - + private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -53,6 +72,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -62,20 +82,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -85,6 +107,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -94,6 +117,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -143,7 +167,7 @@ public final int getVaoId() { public int getVertexCount() { return vertexCount; } - + public float getBoundingRadius() { return boundingRadius; } @@ -151,7 +175,7 @@ public float getBoundingRadius() { public void setBoundingRadius(float boundingRadius) { this.boundingRadius = boundingRadius; } - + protected void initRender() { Texture texture = material.getTexture(); if (texture != null) { @@ -170,20 +194,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -202,7 +216,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 5e17b14b..2e59d116 100644 --- a/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS * 2 + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -42,34 +51,38 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int // Model View Matrix instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Light view matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -85,28 +98,6 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix, Matrix4f lightViewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix, lightViewMatrix); } @@ -141,24 +132,24 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (billBoard) { modelViewMatrix.scale(gameItem.getScale()); } - modelViewMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); } if (lightViewMatrix != null) { Matrix4f modelLightViewMatrix = transformation.buildModelLightViewMatrix(modelMatrix, lightViewMatrix); - modelLightViewMatrix.get(INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS, this.instanceDataBuffer); + modelLightViewMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS, this.instanceDataBuffer); } if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS * 2 + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS * 2 + 2; this.instanceDataBuffer.put(buffPos, gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 6c38d514..d1ccc6ac 100644 --- a/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter25/c25-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -25,11 +44,11 @@ public class Mesh { private final int vertexCount; private Material material; - + private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -53,6 +72,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -62,20 +82,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -85,6 +107,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -94,6 +117,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -143,7 +167,7 @@ public final int getVaoId() { public int getVertexCount() { return vertexCount; } - + public float getBoundingRadius() { return boundingRadius; } @@ -151,7 +175,7 @@ public float getBoundingRadius() { public void setBoundingRadius(float boundingRadius) { this.boundingRadius = boundingRadius; } - + protected void initRender() { Texture texture = material.getTexture(); if (texture != null) { @@ -170,20 +194,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -202,7 +216,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter26/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter26/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 3a8b738e..d191175b 100644 --- a/chapter26/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter26/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_READ; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -41,27 +50,30 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; // Model matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected or Scaling (for particles) - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -77,29 +89,7 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - - public void renderListInstanced( List gameItems, Transformation transformation, Matrix4f viewMatrix) { + public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix); } @@ -128,19 +118,19 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (viewMatrix != null && billBoard) { viewMatrix.transpose3x3(modelMatrix); } - modelMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data or scaling for billboard - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS + 2; this.instanceDataBuffer.put(buffPos, billBoard ? gameItem.getScale() : gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter26/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter26/src/main/java/org/lwjglb/engine/graph/Mesh.java index 3a3a2ca9..7e8a7aef 100644 --- a/chapter26/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter26/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -29,7 +48,7 @@ public class Mesh { private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -53,6 +72,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -62,20 +82,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -85,6 +107,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -94,6 +117,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -170,20 +194,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -202,7 +216,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 3a8b738e..d191175b 100644 --- a/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_READ; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -41,27 +50,30 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; // Model matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected or Scaling (for particles) - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -77,29 +89,7 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - - public void renderListInstanced( List gameItems, Transformation transformation, Matrix4f viewMatrix) { + public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix); } @@ -128,19 +118,19 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (viewMatrix != null && billBoard) { viewMatrix.transpose3x3(modelMatrix); } - modelMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data or scaling for billboard - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS + 2; this.instanceDataBuffer.put(buffPos, billBoard ? gameItem.getScale() : gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java index 840c2274..6362527a 100644 --- a/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter27/c27-p1/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -29,7 +48,7 @@ public class Mesh { private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -41,7 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice IntBuffer indicesBuffer = null; try { calculateBoundingRadius(positions); - + vertexCount = indices.length; vboIdList = new ArrayList<>(); @@ -55,6 +74,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -64,20 +84,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -87,6 +109,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -96,6 +119,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -129,11 +153,11 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } } } - + private void calculateBoundingRadius(float positions[]) { int length = positions.length; boundingRadius = 0; - for(int i=0; i< length; i++) { + for (int i = 0; i < length; i++) { float pos = positions[i]; boundingRadius = Math.max(Math.abs(pos), boundingRadius); } @@ -181,20 +205,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -213,7 +227,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 3a8b738e..d191175b 100644 --- a/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_READ; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -41,27 +50,30 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; // Model matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected or Scaling (for particles) - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -77,29 +89,7 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - - public void renderListInstanced( List gameItems, Transformation transformation, Matrix4f viewMatrix) { + public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix); } @@ -128,19 +118,19 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (viewMatrix != null && billBoard) { viewMatrix.transpose3x3(modelMatrix); } - modelMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data or scaling for billboard - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS + 2; this.instanceDataBuffer.put(buffPos, billBoard ? gameItem.getScale() : gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java index 840c2274..6362527a 100644 --- a/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter27/c27-p2/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -29,7 +48,7 @@ public class Mesh { private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -41,7 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice IntBuffer indicesBuffer = null; try { calculateBoundingRadius(positions); - + vertexCount = indices.length; vboIdList = new ArrayList<>(); @@ -55,6 +74,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO @@ -64,20 +84,22 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice textCoordsBuffer.put(textCoords).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - // Create empty structure + } else { + // Create empty structure vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -87,6 +109,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -96,6 +119,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -129,11 +153,11 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } } } - + private void calculateBoundingRadius(float positions[]) { int length = positions.length; boundingRadius = 0; - for(int i=0; i< length; i++) { + for (int i = 0; i < length; i++) { float pos = positions[i]; boundingRadius = Math.max(Math.abs(pos), boundingRadius); } @@ -181,20 +205,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -213,7 +227,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/chapter28/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java b/chapter28/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java index 3a8b738e..d191175b 100644 --- a/chapter28/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java +++ b/chapter28/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java @@ -1,30 +1,39 @@ package org.lwjglb.engine.graph; -import java.nio.FloatBuffer; -import java.util.List; import org.joml.Matrix4f; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL31.*; -import static org.lwjgl.opengl.GL33.*; import org.lwjgl.system.MemoryUtil; import org.lwjglb.engine.items.GameItem; +import java.nio.FloatBuffer; +import java.util.List; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_DYNAMIC_READ; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL31.glDrawElementsInstanced; +import static org.lwjgl.opengl.GL33.glVertexAttribDivisor; + public class InstancedMesh extends Mesh { private static final int FLOAT_SIZE_BYTES = 4; - private static final int VECTOR4F_SIZE_BYTES = 4 * FLOAT_SIZE_BYTES; + private static final int VECTOR4F_SIZE_BYTES = 4 * InstancedMesh.FLOAT_SIZE_BYTES; private static final int MATRIX_SIZE_FLOATS = 4 * 4; - private static final int MATRIX_SIZE_BYTES = MATRIX_SIZE_FLOATS * FLOAT_SIZE_BYTES; + private static final int MATRIX_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_FLOATS * InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_BYTES = MATRIX_SIZE_BYTES + FLOAT_SIZE_BYTES * 2 + FLOAT_SIZE_BYTES; + private static final int INSTANCE_SIZE_BYTES = InstancedMesh.MATRIX_SIZE_BYTES + InstancedMesh.FLOAT_SIZE_BYTES * 2 + InstancedMesh.FLOAT_SIZE_BYTES; - private static final int INSTANCE_SIZE_FLOATS = MATRIX_SIZE_FLOATS + 3; + private static final int INSTANCE_SIZE_FLOATS = InstancedMesh.MATRIX_SIZE_FLOATS + 3; private final int numInstances; @@ -33,7 +42,7 @@ public class InstancedMesh extends Mesh { private FloatBuffer instanceDataBuffer; public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int numInstances) { - super(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + super(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); this.numInstances = numInstances; @@ -41,27 +50,30 @@ public InstancedMesh(float[] positions, float[] textCoords, float[] normals, int instanceDataVBO = glGenBuffers(); vboIdList.add(instanceDataVBO); - instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * INSTANCE_SIZE_FLOATS); + instanceDataBuffer = MemoryUtil.memAllocFloat(numInstances * InstancedMesh.INSTANCE_SIZE_FLOATS); glBindBuffer(GL_ARRAY_BUFFER, instanceDataVBO); int start = 5; int strideStart = 0; // Model matrix for (int i = 0; i < 4; i++) { - glVertexAttribPointer(start, 4, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 4, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; - strideStart += VECTOR4F_SIZE_BYTES; + strideStart += InstancedMesh.VECTOR4F_SIZE_BYTES; } // Texture offsets - glVertexAttribPointer(start, 2, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 2, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); - strideStart += FLOAT_SIZE_BYTES * 2; + glEnableVertexAttribArray(start); + strideStart += InstancedMesh.FLOAT_SIZE_BYTES * 2; start++; // Selected or Scaling (for particles) - glVertexAttribPointer(start, 1, GL_FLOAT, false, INSTANCE_SIZE_BYTES, strideStart); + glVertexAttribPointer(start, 1, GL_FLOAT, false, InstancedMesh.INSTANCE_SIZE_BYTES, strideStart); glVertexAttribDivisor(start, 1); + glEnableVertexAttribArray(start); start++; glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -77,29 +89,7 @@ public void cleanUp() { } } - @Override - protected void initRender() { - super.initRender(); - - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glEnableVertexAttribArray(start + i); - } - } - - @Override - protected void endRender() { - int start = 5; - int numElements = 4 * 2 + 2; - for (int i = 0; i < numElements; i++) { - glDisableVertexAttribArray(start + i); - } - - super.endRender(); - } - - public void renderListInstanced( List gameItems, Transformation transformation, Matrix4f viewMatrix) { + public void renderListInstanced(List gameItems, Transformation transformation, Matrix4f viewMatrix) { renderListInstanced(gameItems, false, transformation, viewMatrix); } @@ -128,19 +118,19 @@ private void renderChunkInstanced(List gameItems, boolean billBoard, T if (viewMatrix != null && billBoard) { viewMatrix.transpose3x3(modelMatrix); } - modelMatrix.get(INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); + modelMatrix.get(InstancedMesh.INSTANCE_SIZE_FLOATS * i, instanceDataBuffer); if (text != null) { int col = gameItem.getTextPos() % text.getNumCols(); int row = gameItem.getTextPos() / text.getNumCols(); float textXOffset = (float) col / text.getNumCols(); float textYOffset = (float) row / text.getNumRows(); - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS; this.instanceDataBuffer.put(buffPos, textXOffset); this.instanceDataBuffer.put(buffPos + 1, textYOffset); } // Selected data or scaling for billboard - int buffPos = INSTANCE_SIZE_FLOATS * i + MATRIX_SIZE_FLOATS + 2; + int buffPos = InstancedMesh.INSTANCE_SIZE_FLOATS * i + InstancedMesh.MATRIX_SIZE_FLOATS + 2; this.instanceDataBuffer.put(buffPos, billBoard ? gameItem.getScale() : gameItem.isSelected() ? 1 : 0); i++; diff --git a/chapter28/src/main/java/org/lwjglb/engine/graph/Mesh.java b/chapter28/src/main/java/org/lwjglb/engine/graph/Mesh.java index f35d6612..2443650b 100644 --- a/chapter28/src/main/java/org/lwjglb/engine/graph/Mesh.java +++ b/chapter28/src/main/java/org/lwjglb/engine/graph/Mesh.java @@ -1,18 +1,37 @@ package org.lwjglb.engine.graph; +import org.lwjgl.system.MemoryUtil; +import org.lwjglb.engine.items.GameItem; + import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; -import static org.lwjgl.opengl.GL15.*; -import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; -import org.lwjgl.system.MemoryUtil; -import org.lwjglb.engine.items.GameItem; + +import static org.lwjgl.opengl.GL11.GL_FLOAT; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_TRIANGLES; +import static org.lwjgl.opengl.GL11.GL_UNSIGNED_INT; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL11.glDrawElements; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE1; +import static org.lwjgl.opengl.GL13.glActiveTexture; +import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_ELEMENT_ARRAY_BUFFER; +import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW; +import static org.lwjgl.opengl.GL15.glBindBuffer; +import static org.lwjgl.opengl.GL15.glBufferData; +import static org.lwjgl.opengl.GL15.glDeleteBuffers; +import static org.lwjgl.opengl.GL15.glGenBuffers; +import static org.lwjgl.opengl.GL20.glDisableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray; +import static org.lwjgl.opengl.GL20.glVertexAttribPointer; +import static org.lwjgl.opengl.GL30.glBindVertexArray; +import static org.lwjgl.opengl.GL30.glDeleteVertexArrays; +import static org.lwjgl.opengl.GL30.glGenVertexArrays; public class Mesh { @@ -29,7 +48,7 @@ public class Mesh { private float boundingRadius; public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices) { - this(positions, textCoords, normals, indices, createEmptyIntArray(MAX_WEIGHTS * positions.length / 3, 0), createEmptyFloatArray(MAX_WEIGHTS * positions.length / 3, 0)); + this(positions, textCoords, normals, indices, Mesh.createEmptyIntArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0), Mesh.createEmptyFloatArray(Mesh.MAX_WEIGHTS * positions.length / 3, 0)); } public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indices, int[] jointIndices, float[] weights) { @@ -41,7 +60,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice IntBuffer indicesBuffer = null; try { calculateBoundingRadius(positions); - + vertexCount = indices.length; vboIdList = new ArrayList<>(); @@ -55,33 +74,36 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice posBuffer.put(positions).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, posBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); // Texture coordinates VBO vboId = glGenBuffers(); vboIdList.add(vboId); textCoordsBuffer = MemoryUtil.memAllocFloat(textCoords.length); - if ( textCoordsBuffer.capacity() > 0 ) { + if (textCoordsBuffer.capacity() > 0) { textCoordsBuffer.put(textCoords).flip(); - } else { - // Create empty structure. Two coordinates for each 3 position coordinates - textCoordsBuffer = MemoryUtil.memAllocFloat((positions.length * 3) / 2); - } + } else { + // Create empty structure. Two coordinates for each 3 position coordinates + textCoordsBuffer = MemoryUtil.memAllocFloat((positions.length * 3) / 2); + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, textCoordsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0); // Vertex normals VBO vboId = glGenBuffers(); vboIdList.add(vboId); vecNormalsBuffer = MemoryUtil.memAllocFloat(normals.length); - if ( vecNormalsBuffer.capacity() > 0 ) { + if (vecNormalsBuffer.capacity() > 0) { vecNormalsBuffer.put(normals).flip(); - } else { - vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); - } + } else { + vecNormalsBuffer = MemoryUtil.memAllocFloat(positions.length); + } glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, vecNormalsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_FLOAT, false, 0, 0); // Weights @@ -91,6 +113,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice weightsBuffer.put(weights).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, weightsBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, false, 0, 0); // Joint indices @@ -100,6 +123,7 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice jointIndicesBuffer.put(jointIndices).flip(); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, jointIndicesBuffer, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, false, 0, 0); // Index VBO @@ -133,11 +157,11 @@ public Mesh(float[] positions, float[] textCoords, float[] normals, int[] indice } } } - + private void calculateBoundingRadius(float positions[]) { int length = positions.length; boundingRadius = 0; - for(int i=0; i< length; i++) { + for (int i = 0; i < length; i++) { float pos = positions[i]; boundingRadius = Math.max(Math.abs(pos), boundingRadius); } @@ -185,20 +209,10 @@ protected void initRender() { // Draw the mesh glBindVertexArray(getVaoId()); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(2); - glEnableVertexAttribArray(3); - glEnableVertexAttribArray(4); } protected void endRender() { // Restore state - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glDisableVertexAttribArray(2); - glDisableVertexAttribArray(3); - glDisableVertexAttribArray(4); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); @@ -217,7 +231,7 @@ public void renderList(List gameItems, Consumer consumer) { for (GameItem gameItem : gameItems) { if (gameItem.isInsideFrustum()) { - // Set up data requiered by gameItem + // Set up data required by GameItem consumer.accept(gameItem); // Render this game item glDrawElements(GL_TRIANGLES, getVertexCount(), GL_UNSIGNED_INT, 0); diff --git a/pom.xml b/pom.xml index 8b7d8bd0..90254532 100644 --- a/pom.xml +++ b/pom.xml @@ -153,7 +153,7 @@ 3.1.2 3.1.0 3.2.3 - 1.9.17 + 1.9.19