everything works when you palce the light in the right spot

This commit is contained in:
JISAUAY committed 2024-09-26 12:21:54 -05:00
1 parent 83feee2706
commit 60457309ae
5 files changed
+48 -28

No files matched your search

+2 -1
View File
@@ -29,7 +29,8 @@ void main()
vec3 ambient = ambientStrength * lightColor; 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; // vec3 lightDir = LightPos - FragPos;
// float distance = length(lightDir); // float distance = length(lightDir);
+2 -1
View File
@@ -16,7 +16,8 @@ void main()
// * view * model // * view * model
gl_Position = projection * model * vec4(position, 1.0); gl_Position = projection * model * vec4(position, 1.0);
FragPos = vec3(model * vec4(position, 1.0)); FragPos = vec3(model * vec4(position, 1.0));
Normal = normal; Normal = transpose(inverse(mat3(model))) * normal;
LightPos = lightPos; LightPos = lightPos;
// Normal = normal;
} }
+1 -1
View File
@@ -34,7 +34,7 @@ public class Render {
// System.out.println(projection_matrix.toString()); // 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) { for (Entity entity : world.entities) {
entity.shader.bind(); entity.shader.bind();
+21 -13
View File
@@ -4,13 +4,15 @@ import org.apache.commons.lang3.ArrayUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
public class Obj { public class Obj {
int vertex_count; int vertex_count;
float[] vbo; float[] vertex_vbo;
float[] normal_vbo;
int[] indices; int[] indices;
String file_path; String file_path;
@@ -26,7 +28,8 @@ public class Obj {
ArrayList<Float> v = new ArrayList<>(); ArrayList<Float> v = new ArrayList<>();
ArrayList<Float> vn = new ArrayList<>(); ArrayList<Float> vn = new ArrayList<>();
ArrayList<Integer> i = new ArrayList<>(); ArrayList<Integer> i = new ArrayList<>();
ArrayList<Float> temp_vbo = new ArrayList<>(); ArrayList<Float> temp_v_vbo = new ArrayList<>();
ArrayList<Float> temp_n_vbo = new ArrayList<>();
String line; String line;
String[] face_data_temp; String[] face_data_temp;
@@ -73,21 +76,21 @@ public class Obj {
boolean vbo_el_found = false; boolean vbo_el_found = false;
// We must look for the vertex//normal pair in the existing VBO buffer // 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 vertex_found = true;
boolean normal_found = true; boolean normal_found = true;
// look for vertex // look for vertex
for (int b = 0; b < 3; b++) 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; vertex_found = false;
break; break;
} }
// look for normal // look for normal
for (int b = 3; b < 6; b++) for (int b = 0; b < 3; b++)
if (temp_vbo.get(a + b) != vn.get(normal_index + b - 3)) { if (temp_n_vbo.get(a + b) != vn.get(normal_index + b)) {
normal_found = false; normal_found = false;
break; break;
} }
@@ -104,14 +107,14 @@ public class Obj {
{ {
// Add vertex // Add vertex
for (int a = 0; a < 3; a++) 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 // Add normal
for (int a = 0; a < 3; a++) 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 // 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.indices = ArrayUtils.toPrimitive(i.toArray(Integer[]::new));
this.vbo = ArrayUtils.toPrimitive(temp_vbo.toArray(Float[]::new)); this.vertex_vbo = ArrayUtils.toPrimitive(temp_v_vbo.toArray(Float[]::new));
this.vertex_count = this.vbo.length / 6; this.normal_vbo = ArrayUtils.toPrimitive(temp_n_vbo.toArray(Float[]::new));
this.vertex_count = this.vertex_vbo.length / 3;
} }
public float[] getVbo() { public float[] getVertexVbo() {
return vbo; return vertex_vbo;
}
public float[] getNormalVbo() {
return normal_vbo;
} }
public int[] getIndices() { public int[] getIndices() {
+22 -12
View File
@@ -7,7 +7,6 @@ import org.xfe.engine.files.Obj;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
@@ -21,13 +20,14 @@ public class Mesh {
public int vao_id; public int vao_id;
public List<Integer> vbo_id_list; public List<Integer> vbo_id_list;
public float[] vbo_buffer; public float[] vertices;
public float[] normals;
public int[] indices; public int[] indices;
public Mesh(float[] vbo_buffer, int[] i) { public Mesh(float[] vbo_buffer, int[] i) {
this.vbo_id_list = new ArrayList<>(); this.vbo_id_list = new ArrayList<>();
this.vbo_buffer = vbo_buffer; this.vertices = vbo_buffer;
this.indices = i; this.indices = i;
this.init(); this.init();
@@ -38,7 +38,8 @@ public class Mesh {
this.vbo_id_list = new ArrayList<>(); this.vbo_id_list = new ArrayList<>();
Obj obj = new Obj(obj_file_path); 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.indices = obj.getIndices();
this.vertex_count = obj.getVertexCount(); this.vertex_count = obj.getVertexCount();
@@ -48,26 +49,35 @@ public class Mesh {
} }
private void init() { private void init() {
FloatBuffer vertices_buffer = null;
try { try {
MemoryStack stack = MemoryStack.stackPush(); 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(); this.vao_id = glGenVertexArrays();
glBindVertexArray(this.vao_id); 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(); int vbo_id = glGenBuffers();
vbo_id_list.add(vbo_id); vbo_id_list.add(vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id); glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW);
// Set Position Attribute
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 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); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);