From 60457309aecb14d6c4d34ca5a8a4c91a302aa7e2 Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Thu, 26 Sep 2024 12:21:54 -0500 Subject: [PATCH] everything works when you palce the light in the right spot --- resources/shaders/shader.frag | 3 +- resources/shaders/shader.vert | 3 +- src/main/java/org/xfe/engine/Render.java | 2 +- src/main/java/org/xfe/engine/files/Obj.java | 34 ++++++++++++-------- src/main/java/org/xfe/engine/world/Mesh.java | 34 +++++++++++++------- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/resources/shaders/shader.frag b/resources/shaders/shader.frag index 3a6aaf0..8c8343e 100644 --- a/resources/shaders/shader.frag +++ b/resources/shaders/shader.frag @@ -29,7 +29,8 @@ void main() vec3 ambient = ambientStrength * lightColor; - fragColor = vec4((ambient + diffuse) * objectColor, 1.0); + vec3 result = (ambient + diffuse) * objectColor; + fragColor = vec4(result, 1.0); // vec3 lightDir = LightPos - FragPos; // float distance = length(lightDir); diff --git a/resources/shaders/shader.vert b/resources/shaders/shader.vert index 30aed74..1550252 100644 --- a/resources/shaders/shader.vert +++ b/resources/shaders/shader.vert @@ -16,7 +16,8 @@ void main() // * view * model gl_Position = projection * model * vec4(position, 1.0); FragPos = vec3(model * vec4(position, 1.0)); - Normal = normal; + Normal = transpose(inverse(mat3(model))) * normal; LightPos = lightPos; +// Normal = normal; } diff --git a/src/main/java/org/xfe/engine/Render.java b/src/main/java/org/xfe/engine/Render.java index 87d205d..d0aa138 100644 --- a/src/main/java/org/xfe/engine/Render.java +++ b/src/main/java/org/xfe/engine/Render.java @@ -34,7 +34,7 @@ public class Render { // System.out.println(projection_matrix.toString()); - Vector3f light_position = new Vector3f(0, 0, -8); + Vector3f light_position = new Vector3f(0, 0, 0); for (Entity entity : world.entities) { entity.shader.bind(); diff --git a/src/main/java/org/xfe/engine/files/Obj.java b/src/main/java/org/xfe/engine/files/Obj.java index 4ffffbc..8f3948c 100644 --- a/src/main/java/org/xfe/engine/files/Obj.java +++ b/src/main/java/org/xfe/engine/files/Obj.java @@ -4,13 +4,15 @@ import org.apache.commons.lang3.ArrayUtils; import java.io.File; import java.io.FileNotFoundException; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Scanner; public class Obj { int vertex_count; - float[] vbo; + float[] vertex_vbo; + float[] normal_vbo; int[] indices; String file_path; @@ -26,7 +28,8 @@ public class Obj { ArrayList v = new ArrayList<>(); ArrayList vn = new ArrayList<>(); ArrayList i = new ArrayList<>(); - ArrayList temp_vbo = new ArrayList<>(); + ArrayList temp_v_vbo = new ArrayList<>(); + ArrayList temp_n_vbo = new ArrayList<>(); String line; String[] face_data_temp; @@ -73,21 +76,21 @@ public class Obj { boolean vbo_el_found = false; // We must look for the vertex//normal pair in the existing VBO buffer - for (int a = 0; a < temp_vbo.size(); a += 6) + for (int a = 0; a < temp_v_vbo.size(); a += 3) { boolean vertex_found = true; boolean normal_found = true; // look for vertex for (int b = 0; b < 3; b++) - if (temp_vbo.get(a + b) != v.get(vertex_index + b)) { + if (temp_n_vbo.get(a + b) != v.get(vertex_index + b)) { vertex_found = false; break; } // look for normal - for (int b = 3; b < 6; b++) - if (temp_vbo.get(a + b) != vn.get(normal_index + b - 3)) { + for (int b = 0; b < 3; b++) + if (temp_n_vbo.get(a + b) != vn.get(normal_index + b)) { normal_found = false; break; } @@ -104,14 +107,14 @@ public class Obj { { // Add vertex for (int a = 0; a < 3; a++) - temp_vbo.add(v.get(vertex_index + a)); + temp_v_vbo.add(v.get(vertex_index + a)); // Add normal for (int a = 0; a < 3; a++) - temp_vbo.add(vn.get(normal_index + a)); + temp_n_vbo.add(vn.get(normal_index + a)); // Add new index to indices - i.add((temp_vbo.size() / 6) - 1); + i.add((temp_v_vbo.size() / 3) - 1); } } @@ -121,12 +124,17 @@ public class Obj { } this.indices = ArrayUtils.toPrimitive(i.toArray(Integer[]::new)); - this.vbo = ArrayUtils.toPrimitive(temp_vbo.toArray(Float[]::new)); - this.vertex_count = this.vbo.length / 6; + this.vertex_vbo = ArrayUtils.toPrimitive(temp_v_vbo.toArray(Float[]::new)); + this.normal_vbo = ArrayUtils.toPrimitive(temp_n_vbo.toArray(Float[]::new)); + this.vertex_count = this.vertex_vbo.length / 3; } - public float[] getVbo() { - return vbo; + public float[] getVertexVbo() { + return vertex_vbo; + } + + public float[] getNormalVbo() { + return normal_vbo; } public int[] getIndices() { diff --git a/src/main/java/org/xfe/engine/world/Mesh.java b/src/main/java/org/xfe/engine/world/Mesh.java index b11f104..a492cb0 100644 --- a/src/main/java/org/xfe/engine/world/Mesh.java +++ b/src/main/java/org/xfe/engine/world/Mesh.java @@ -7,7 +7,6 @@ import org.xfe.engine.files.Obj; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import static org.lwjgl.opengl.GL11.*; @@ -21,13 +20,14 @@ public class Mesh { public int vao_id; public List vbo_id_list; - public float[] vbo_buffer; + public float[] vertices; + public float[] normals; public int[] indices; public Mesh(float[] vbo_buffer, int[] i) { this.vbo_id_list = new ArrayList<>(); - this.vbo_buffer = vbo_buffer; + this.vertices = vbo_buffer; this.indices = i; this.init(); @@ -38,7 +38,8 @@ public class Mesh { this.vbo_id_list = new ArrayList<>(); Obj obj = new Obj(obj_file_path); - this.vbo_buffer = obj.getVbo(); + this.vertices = obj.getVertexVbo(); + this.normals = obj.getNormalVbo(); this.indices = obj.getIndices(); this.vertex_count = obj.getVertexCount(); @@ -48,26 +49,35 @@ public class Mesh { } private void init() { - FloatBuffer vertices_buffer = null; - try { MemoryStack stack = MemoryStack.stackPush(); - // Bind our vertices into the VBO - vertices_buffer = stack.callocFloat(this.vbo_buffer.length); - - // Convert from our object list to the primitive float array - vertices_buffer.put(0, this.vbo_buffer); - this.vao_id = glGenVertexArrays(); glBindVertexArray(this.vao_id); + // Bind Vertices into a VBO + FloatBuffer vertices_buffer = stack.callocFloat(this.vertices.length); + vertices_buffer.put(0, this.vertices); int vbo_id = glGenBuffers(); + vbo_id_list.add(vbo_id); glBindBuffer(GL_ARRAY_BUFFER, vbo_id); glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW); + + // Set Position Attribute glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); + + // Bind Normals into a VBO + FloatBuffer normals_buffer = stack.callocFloat(this.normals.length); + normals_buffer.put(0, this.normals); + vbo_id = glGenBuffers(); + + vbo_id_list.add(vbo_id); + glBindBuffer(GL_ARRAY_BUFFER, vbo_id); + glBufferData(GL_ARRAY_BUFFER, normals_buffer, GL_STATIC_DRAW); + + // Set Normal Attribute glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);