fixed normal generation
This commit is contained in:
1 parent
bc88572abf
commit
215fb4f012
9 files changed
+50
-8698
No files matched your search
@@ -1,5 +1,6 @@
|
||||
package org.xfe.engine.world;
|
||||
|
||||
import org.joml.GeometryUtils;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
@@ -55,60 +56,53 @@ public class Mesh {
|
||||
}
|
||||
|
||||
private void init() {
|
||||
try {
|
||||
this.vao_id = glGenVertexArrays();
|
||||
glBindVertexArray(this.vao_id);
|
||||
|
||||
// Bind Vertices into a VBO
|
||||
int vbo_id = glGenBuffers();
|
||||
this.vao_id = glGenVertexArrays();
|
||||
glBindVertexArray(this.vao_id);
|
||||
|
||||
vbo_id_list.add(vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
|
||||
// Bind Vertices into a VBO
|
||||
int vbo_id = glGenBuffers();
|
||||
|
||||
// Set Position Attribute
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
||||
vbo_id_list.add(vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Bind Normals into a VBO
|
||||
System.out.println(this.normals.length);
|
||||
// Set Position Attribute
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
||||
|
||||
// Bind Normals into a VBO
|
||||
System.out.println(this.normals.length);
|
||||
vbo_id = glGenBuffers();
|
||||
|
||||
vbo_id_list.add(vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
|
||||
|
||||
// Set Normal Attribute
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
|
||||
|
||||
// Bind indices in EBO
|
||||
if (this.indices.length > 0) {
|
||||
|
||||
System.out.println(indices.length);
|
||||
vbo_id = glGenBuffers();
|
||||
|
||||
vbo_id_list.add(vbo_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
|
||||
|
||||
// Set Normal Attribute
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
|
||||
|
||||
// Bind indices in EBO
|
||||
if (this.indices.length > 0) {
|
||||
|
||||
System.out.println(indices.length);
|
||||
vbo_id = glGenBuffers();
|
||||
vbo_id_list.add(vbo_id);
|
||||
|
||||
// IntBuffer indice_buffer = stack.callocInt(indices.length);
|
||||
// indice_buffer.put(0, this.indices);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
} finally {
|
||||
// if (vertices_buffer != null) {
|
||||
// MemoryUtil.memFree(vertices_buffer);
|
||||
// }
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
}
|
||||
|
||||
// https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
|
||||
private void calculateNormals() {
|
||||
|
||||
this.normals = new float[this.indices.length];
|
||||
this.normals = new float[this.vertices.length];
|
||||
|
||||
// Loop through all tris
|
||||
for (int i = 0; i < this.indices.length; i += 3) {
|
||||
@@ -121,12 +115,22 @@ public class Mesh {
|
||||
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);
|
||||
Vector3f normal = new Vector3f();
|
||||
|
||||
GeometryUtils.normal(v1, v2, v3, normal);
|
||||
|
||||
this.normals[i1] = normal.x;
|
||||
this.normals[i1 + 1] = normal.y;
|
||||
this.normals[i1 + 2] = normal.z;
|
||||
|
||||
this.normals[i2] = normal.x;
|
||||
this.normals[i2 + 1] = normal.y;
|
||||
this.normals[i2 + 2] = normal.z;
|
||||
|
||||
this.normals[i3] = normal.x;
|
||||
this.normals[i3 + 1] = normal.y;
|
||||
this.normals[i3 + 2] = normal.z;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user