cube got mutilated

This commit is contained in:
0x01FE committed 2024-09-26 10:43:54 -05:00
1 parent 47580ee155
commit 83feee2706
2 files changed
+94 -60

No files matched your search

+68 -31
View File
@@ -10,9 +10,8 @@ import java.util.Scanner;
public class Obj { public class Obj {
int vertex_count; int vertex_count;
float[] vertices; float[] vbo;
float[] normals; int[] indices;
int[] vertex_indices;
String file_path; String file_path;
@@ -24,14 +23,13 @@ public class Obj {
void loadObj(File file) { void loadObj(File file) {
ArrayList<Float> temp_vertices = new ArrayList<>(); ArrayList<Float> v = new ArrayList<>();
ArrayList<Float> temp_normals = new ArrayList<>(); ArrayList<Float> vn = new ArrayList<>();
ArrayList<Integer> temp_vertex_indices = new ArrayList<>(); ArrayList<Integer> i = new ArrayList<>();
ArrayList<Float> temp_real_normals = new ArrayList<>(); ArrayList<Float> temp_vbo = new ArrayList<>();
String line; String line;
String[] face_data_temp; String[] face_data_temp;
int i;
Scanner scanner; Scanner scanner;
@@ -50,50 +48,89 @@ public class Obj {
switch (data[0]) { switch (data[0]) {
case "v": case "v":
temp_vertices.add(Float.valueOf(data[1])); v.add(Float.valueOf(data[1]));
temp_vertices.add(Float.valueOf(data[2])); v.add(Float.valueOf(data[2]));
temp_vertices.add(Float.valueOf(data[3])); v.add(Float.valueOf(data[3]));
break; break;
case "vn": case "vn":
temp_normals.add(Float.valueOf(data[1])); vn.add(Float.valueOf(data[1]));
temp_normals.add(Float.valueOf(data[2])); vn.add(Float.valueOf(data[2]));
temp_normals.add(Float.valueOf(data[3])); vn.add(Float.valueOf(data[3]));
break; break;
case "f": case "f":
// Sample face data : "f 5//1 3//1 1//1" where each thing is v//normal_v // Sample face data : "f 5//1 3//1 1//1" where each thing is v//normal_v
for (i = 1; i < 4; i++) { for (int index = 1; index < 4; index++) {
face_data_temp = data[i].split("//");
temp_vertex_indices.add(Integer.parseInt(face_data_temp[0]) - 1);
if (face_data_temp.length > 1) // "//" means "vertex index//normal index"
if (line.contains("//"))
{ {
face_data_temp = data[index].split("//");
int vertex_index = (Integer.parseInt(face_data_temp[0]) - 1) * 3;
int normal_index = (Integer.parseInt(face_data_temp[1]) - 1) * 3; int normal_index = (Integer.parseInt(face_data_temp[1]) - 1) * 3;
for (int c = 0; c < 3; c++) boolean vbo_el_found = false;
temp_real_normals.add(temp_normals.get(normal_index + c));
// We must look for the vertex//normal pair in the existing VBO buffer
for (int a = 0; a < temp_vbo.size(); a += 6)
{
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)) {
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)) {
normal_found = false;
break;
}
if (vertex_found && normal_found)
{
i.add(a / 6);
vbo_el_found = true;
}
}
// If we didn't find a match vertex//normal pair it must be added to the VBO
if (!vbo_el_found)
{
// Add vertex
for (int a = 0; a < 3; a++)
temp_vbo.add(v.get(vertex_index + a));
// Add normal
for (int a = 0; a < 3; a++)
temp_vbo.add(vn.get(normal_index + a));
// Add new index to indices
i.add((temp_vbo.size() / 6) - 1);
}
}
} }
break; break;
} }
} }
this.vertices = ArrayUtils.toPrimitive(temp_vertices.toArray(Float[]::new)); this.indices = ArrayUtils.toPrimitive(i.toArray(Integer[]::new));
this.normals = ArrayUtils.toPrimitive(temp_real_normals.toArray(Float[]::new)); this.vbo = ArrayUtils.toPrimitive(temp_vbo.toArray(Float[]::new));
this.vertex_indices = ArrayUtils.toPrimitive(temp_vertex_indices.toArray(Integer[]::new)); this.vertex_count = this.vbo.length / 6;
this.vertex_count = this.vertices.length / 3;
} }
public float[] getVertices() { public float[] getVbo() {
return vertices; return vbo;
}
public float[] getNormals() {
return normals;
} }
public int[] getIndices() { public int[] getIndices() {
return vertex_indices; return indices;
} }
public int getVertexCount() { public int getVertexCount() {
+26 -29
View File
@@ -1,6 +1,5 @@
package org.xfe.engine.world; package org.xfe.engine.world;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.opengl.GL30; import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryStack;
import org.xfe.engine.files.Obj; import org.xfe.engine.files.Obj;
@@ -22,15 +21,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[] vertices; public float[] vbo_buffer;
public float[] normals; public int[] indices;
public int[] vertex_indices;
public Mesh(float[] v, int[] i) { public Mesh(float[] vbo_buffer, int[] i) {
this.vbo_id_list = new ArrayList<>(); this.vbo_id_list = new ArrayList<>();
this.vertices = v; this.vbo_buffer = vbo_buffer;
this.vertex_indices = i; this.indices = i;
this.init(); this.init();
} }
@@ -40,13 +38,11 @@ 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.vertices = obj.getVertices(); this.vbo_buffer = obj.getVbo();
this.normals = obj.getNormals(); this.indices = obj.getIndices();
this.vertex_indices = obj.getIndices();
this.vertex_count = obj.getVertexCount(); this.vertex_count = obj.getVertexCount();
System.out.println(Arrays.toString(this.vertices));
System.out.println(Arrays.toString(this.normals));
this.init(); this.init();
} }
@@ -58,11 +54,10 @@ public class Mesh {
MemoryStack stack = MemoryStack.stackPush(); MemoryStack stack = MemoryStack.stackPush();
// Bind our vertices into the VBO // Bind our vertices into the VBO
vertices_buffer = stack.callocFloat(this.vertices.length); vertices_buffer = stack.callocFloat(this.vbo_buffer.length);
vertex_count = this.vertices.length / 3;
// Convert from our object list to the primitive float array // Convert from our object list to the primitive float array
vertices_buffer.put(0, this.vertices); vertices_buffer.put(0, this.vbo_buffer);
this.vao_id = glGenVertexArrays(); this.vao_id = glGenVertexArrays();
glBindVertexArray(this.vao_id); glBindVertexArray(this.vao_id);
@@ -73,26 +68,28 @@ public class Mesh {
glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
if (normals.length > 0) {
vbo_id = glGenBuffers();
vbo_id_list.add(vbo_id);
FloatBuffer normals_buffer = stack.callocFloat(normals.length);
normals_buffer.put(0, this.normals);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
glBufferData(GL_ARRAY_BUFFER, normals_buffer, vbo_id);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0); glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
}
// if (normals.length > 0) {
// vbo_id = glGenBuffers();
// vbo_id_list.add(vbo_id);
//
// FloatBuffer normals_buffer = stack.callocFloat(normals.length);
// normals_buffer.put(0, this.normals);
// glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
// glBufferData(GL_ARRAY_BUFFER, normals_buffer, vbo_id);
// glEnableVertexAttribArray(1);
// glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
// }
// Bind indices in EBO // Bind indices in EBO
if (this.vertex_indices.length > 0) { if (this.indices.length > 0) {
vbo_id = glGenBuffers(); vbo_id = glGenBuffers();
vbo_id_list.add(vbo_id); vbo_id_list.add(vbo_id);
IntBuffer indice_buffer = stack.callocInt(vertex_indices.length); IntBuffer indice_buffer = stack.callocInt(indices.length);
indice_buffer.put(0, this.vertex_indices); indice_buffer.put(0, this.indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW);
@@ -111,7 +108,7 @@ public class Mesh {
glBindVertexArray(this.vao_id); glBindVertexArray(this.vao_id);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, this.vertex_indices.length, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, this.indices.length, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1); glDisableVertexAttribArray(1);