added normal calculation, it doesn't seem to work but it looks
cool!
This commit is contained in:
1 parent
58aa033b02
commit
bc88572abf
6 files changed
+4529
-22
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package org.xfe.engine.world;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
@@ -8,6 +9,7 @@ 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.*;
|
||||
@@ -44,6 +46,9 @@ public class Mesh {
|
||||
this.indices = obj.getIndices();
|
||||
this.vertex_count = obj.getVertexCount();
|
||||
|
||||
// OBJ Didn't have normals, calculate them.
|
||||
if (this.normals.length == 0)
|
||||
this.calculateNormals();
|
||||
|
||||
|
||||
this.init();
|
||||
@@ -51,21 +56,15 @@ public class Mesh {
|
||||
|
||||
private void init() {
|
||||
try {
|
||||
MemoryStack stack = MemoryStack.stackPush();
|
||||
|
||||
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);
|
||||
|
||||
MemoryUtil.memFree(vertices_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Set Position Attribute
|
||||
glEnableVertexAttribArray(0);
|
||||
@@ -73,30 +72,16 @@ public class Mesh {
|
||||
|
||||
// Bind Normals into a VBO
|
||||
System.out.println(this.normals.length);
|
||||
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);
|
||||
glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
|
||||
|
||||
// Set Normal Attribute
|
||||
glEnableVertexAttribArray(1);
|
||||
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
|
||||
if (this.indices.length > 0) {
|
||||
|
||||
@@ -120,6 +105,31 @@ public class Mesh {
|
||||
}
|
||||
}
|
||||
|
||||
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
|
||||
private void calculateNormals() {
|
||||
|
||||
this.normals = new float[this.indices.length];
|
||||
|
||||
// Loop through all tris
|
||||
for (int i = 0; i < this.indices.length; i += 3) {
|
||||
|
||||
int i1 = this.indices[i] * 3;
|
||||
int i2 = this.indices[i + 1] * 3;
|
||||
int i3 = this.indices[i + 2] * 3;
|
||||
|
||||
Vector3f v1 = new Vector3f(vertices[i1], vertices[i1 + 1], vertices[i1 + 2]);
|
||||
Vector3f v2 = new Vector3f(vertices[i2], vertices[i2 + 1], vertices[i2 + 2]);
|
||||
Vector3f v3 = new Vector3f(vertices[i3], vertices[i3 + 1], vertices[i3 + 2]);
|
||||
|
||||
Vector3f U = v2.sub(v1);
|
||||
Vector3f V = v3.sub(v1);
|
||||
|
||||
this.normals[i] = (U.y * V.z) - (U.z * V.y);
|
||||
this.normals[i + 1] = (U.z * V.x) - (U.x * V.z);
|
||||
this.normals[i + 2] = (U.x * V.y) - (U.y * V.x);
|
||||
}
|
||||
}
|
||||
|
||||
public void render() {
|
||||
glBindVertexArray(this.vao_id);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
Reference in new issue
Block a user