Compare commits
10
Commits
ce888f5ac0
...
dac94bff2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dac94bff2d | ||
|
|
ad0ef8e1b6 | ||
|
|
83a527759e | ||
|
|
215fb4f012 | ||
|
|
bc88572abf | ||
|
|
58aa033b02 | ||
|
|
719e6993df | ||
|
|
60457309ae | ||
|
|
83feee2706 | ||
|
|
47580ee155 |
No files matched your search
-1366
File diff suppressed because it is too large.
Load diff
-1393
File diff suppressed because it is too large.
Load diff
-1396
File diff suppressed because it is too large.
Load diff
@@ -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);
|
||||||
|
|||||||
@@ -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,6 +1,6 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine;
|
||||||
|
|
||||||
import org.joml.*;
|
import org.xfe.engine.world.World;
|
||||||
|
|
||||||
import java.lang.Math;
|
import java.lang.Math;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine;
|
||||||
|
|
||||||
|
import org.xfe.engine.world.World;
|
||||||
|
|
||||||
public interface GameLogic {
|
public interface GameLogic {
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
|
|||||||
@@ -1,183 +0,0 @@
|
|||||||
package org.xfe.engine;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
|
||||||
import org.lwjgl.opengl.GL30;
|
|
||||||
import org.lwjgl.system.MemoryStack;
|
|
||||||
import org.lwjgl.system.MemoryUtil;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.FloatBuffer;
|
|
||||||
import java.nio.IntBuffer;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
|
||||||
import static org.lwjgl.opengl.GL15.*;
|
|
||||||
import static org.lwjgl.opengl.GL20.*;
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
|
||||||
|
|
||||||
public class Mesh {
|
|
||||||
|
|
||||||
public int vertex_count;
|
|
||||||
public int vao_id;
|
|
||||||
public List<Integer> vbo_id_list;
|
|
||||||
|
|
||||||
public Float[] vertices;
|
|
||||||
public Float[] normals;
|
|
||||||
public Integer[] vertice_indices;
|
|
||||||
public Integer[] normal_indices;
|
|
||||||
|
|
||||||
public Mesh(Float[] v, Integer[] i) {
|
|
||||||
this.vertices = v;
|
|
||||||
this.vertice_indices = i;
|
|
||||||
init();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creates a new Mesh object from an wavefront .obj file
|
|
||||||
public Mesh(String obj_file_path) {
|
|
||||||
this.vbo_id_list = new ArrayList<>();
|
|
||||||
this.loadObj(obj_file_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadObj(String file_path) {
|
|
||||||
|
|
||||||
ArrayList<Float> temp_vertices = new ArrayList<>();
|
|
||||||
ArrayList<Float> temp_normals = new ArrayList<>();
|
|
||||||
ArrayList<Integer> temp_vertice_indices = new ArrayList<>();
|
|
||||||
ArrayList<Integer> temp_normal_indices = new ArrayList<>();
|
|
||||||
|
|
||||||
Scanner scanner;
|
|
||||||
File obj;
|
|
||||||
|
|
||||||
String line;
|
|
||||||
String[] face_data_temp;
|
|
||||||
int[] face_data = new int[3];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
// Try to open the file
|
|
||||||
try {
|
|
||||||
obj = new File(file_path);
|
|
||||||
scanner = new Scanner(obj);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("ERROR: OBJ File \"" + file_path + "\" not found.");
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
line = scanner.nextLine();
|
|
||||||
|
|
||||||
String[] data = line.split(" ");
|
|
||||||
|
|
||||||
switch (data[0]) {
|
|
||||||
case "v":
|
|
||||||
temp_vertices.add(Float.valueOf(data[1]));
|
|
||||||
temp_vertices.add(Float.valueOf(data[2]));
|
|
||||||
temp_vertices.add(Float.valueOf(data[3]));
|
|
||||||
break;
|
|
||||||
case "vn":
|
|
||||||
temp_normals.add(Float.valueOf(data[1]));
|
|
||||||
temp_normals.add(Float.valueOf(data[2]));
|
|
||||||
temp_normals.add(Float.valueOf(data[3]));
|
|
||||||
break;
|
|
||||||
case "f":
|
|
||||||
// Sample face data : "f 5//1 3//1 1//1" where each thing is v//normal_v
|
|
||||||
|
|
||||||
for (i = 1; i < 4; i++) {
|
|
||||||
face_data_temp = data[i].split("//");
|
|
||||||
temp_vertice_indices.add(Integer.parseInt(face_data_temp[0]) - 1);
|
|
||||||
|
|
||||||
if (face_data_temp.length > 1)
|
|
||||||
temp_normal_indices.add(Integer.parseInt(face_data_temp[1]));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.vertices = temp_vertices.toArray(Float[]::new);
|
|
||||||
this.normals = temp_normals.toArray(Float[]::new);
|
|
||||||
this.vertice_indices = temp_vertice_indices.toArray(Integer[]::new);
|
|
||||||
this.normal_indices = temp_normal_indices.toArray(Integer[]::new);
|
|
||||||
|
|
||||||
// OBJ File has been read, setup openGL buffers
|
|
||||||
init();
|
|
||||||
|
|
||||||
System.out.println(Arrays.toString(this.vertices));
|
|
||||||
System.out.println(Arrays.toString(this.vertice_indices));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init() {
|
|
||||||
FloatBuffer vertices_buffer = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
MemoryStack stack = MemoryStack.stackPush();
|
|
||||||
|
|
||||||
// Bind our vertices into the VBO
|
|
||||||
vertices_buffer = stack.callocFloat(this.vertices.length);
|
|
||||||
vertex_count = this.vertices.length / 3;
|
|
||||||
|
|
||||||
// Convert from our object list to the primitive float array
|
|
||||||
vertices_buffer.put(0, ArrayUtils.toPrimitive(this.vertices));
|
|
||||||
|
|
||||||
this.vao_id = glGenVertexArrays();
|
|
||||||
glBindVertexArray(this.vao_id);
|
|
||||||
|
|
||||||
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);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
|
||||||
|
|
||||||
// Bind indices in EBO
|
|
||||||
if (this.vertice_indices.length > 0) {
|
|
||||||
vbo_id = glGenBuffers();
|
|
||||||
vbo_id_list.add(vbo_id);
|
|
||||||
|
|
||||||
IntBuffer indice_buffer = stack.callocInt(vertice_indices.length);
|
|
||||||
indice_buffer.put(0, ArrayUtils.toPrimitive(vertice_indices));
|
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id);
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (normals.length > 0) {
|
|
||||||
vbo_id = glGenBuffers();
|
|
||||||
vbo_id_list.add(vbo_id);
|
|
||||||
|
|
||||||
FloatBuffer normals_buffer = stack.callocFloat(normals.length);
|
|
||||||
normals_buffer.put(0, ArrayUtils.toPrimitive(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
} finally {
|
|
||||||
// if (vertices_buffer != null) {
|
|
||||||
// MemoryUtil.memFree(vertices_buffer);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void render() {
|
|
||||||
glBindVertexArray(this.vao_id);
|
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
glEnableVertexAttribArray(1);
|
|
||||||
glDrawElements(GL_TRIANGLES, this.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
|
||||||
|
|
||||||
glDisableVertexAttribArray(0);
|
|
||||||
glDisableVertexAttribArray(1);
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
public void cleanUp() {
|
|
||||||
vbo_id_list.forEach(GL30::glDeleteBuffers);
|
|
||||||
glDeleteVertexArrays(this.vao_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,14 +3,10 @@ package org.xfe.engine;
|
|||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
|
import org.xfe.engine.world.Entity;
|
||||||
|
import org.xfe.engine.world.World;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
import static org.lwjgl.opengl.GL15.*;
|
|
||||||
import static org.lwjgl.opengl.GL20.*;
|
|
||||||
import static org.lwjgl.opengl.GL30.*;
|
|
||||||
|
|
||||||
public class Render {
|
public class Render {
|
||||||
|
|
||||||
@@ -38,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();
|
||||||
|
|||||||
@@ -0,0 +1,178 @@
|
|||||||
|
package org.xfe.engine.files;
|
||||||
|
|
||||||
|
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.Objects;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Obj {
|
||||||
|
int vertex_count;
|
||||||
|
|
||||||
|
float[] vertex_vbo;
|
||||||
|
float[] normal_vbo;
|
||||||
|
int[] indices;
|
||||||
|
|
||||||
|
String file_path;
|
||||||
|
|
||||||
|
public Obj(String file_path) {
|
||||||
|
this.file_path = file_path;
|
||||||
|
this.loadObj(new File(file_path));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loadObj(File file) {
|
||||||
|
|
||||||
|
ArrayList<Float> v = new ArrayList<>();
|
||||||
|
ArrayList<Float> vn = new ArrayList<>();
|
||||||
|
ArrayList<Integer> i = new ArrayList<>();
|
||||||
|
ArrayList<Float> temp_v_vbo = new ArrayList<>();
|
||||||
|
ArrayList<Float> temp_n_vbo = new ArrayList<>();
|
||||||
|
|
||||||
|
String line;
|
||||||
|
String[] face_data_temp;
|
||||||
|
|
||||||
|
Scanner scanner;
|
||||||
|
|
||||||
|
try {
|
||||||
|
scanner = new Scanner(file);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("ERROR: OBJ File \"" + file_path + "\" not found.");
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
line = scanner.nextLine();
|
||||||
|
|
||||||
|
String[] data = line.split(" ");
|
||||||
|
|
||||||
|
switch (data[0]) {
|
||||||
|
case "v":
|
||||||
|
v.add(Float.valueOf(data[1]));
|
||||||
|
v.add(Float.valueOf(data[2]));
|
||||||
|
v.add(Float.valueOf(data[3]));
|
||||||
|
break;
|
||||||
|
case "vn":
|
||||||
|
vn.add(Float.valueOf(data[1]));
|
||||||
|
vn.add(Float.valueOf(data[2]));
|
||||||
|
vn.add(Float.valueOf(data[3]));
|
||||||
|
break;
|
||||||
|
case "f":
|
||||||
|
// Sample face data : "f 5//1 3//1 1//1" where each thing is v//normal_v
|
||||||
|
|
||||||
|
for (int index = 1; index < 4; index++) {
|
||||||
|
|
||||||
|
// "//" 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;
|
||||||
|
|
||||||
|
boolean vbo_el_found = false;
|
||||||
|
|
||||||
|
// We must look for the vertex//normal pair in the existing VBO buffer
|
||||||
|
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 (!Objects.equals(temp_v_vbo.get(a + b), v.get(vertex_index + b))) {
|
||||||
|
vertex_found = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!Objects.equals(temp_n_vbo.get(a + b), vn.get(normal_index + b))) {
|
||||||
|
normal_found = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vertex_found && normal_found)
|
||||||
|
{
|
||||||
|
i.add(a / 3);
|
||||||
|
vbo_el_found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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_v_vbo.add(v.get(vertex_index + a));
|
||||||
|
|
||||||
|
// Add normal
|
||||||
|
for (int a = 0; a < 3; a++)
|
||||||
|
temp_n_vbo.add(vn.get(normal_index + a));
|
||||||
|
|
||||||
|
// Add new index to indices
|
||||||
|
i.add((temp_v_vbo.size() / 3) - 1);
|
||||||
|
}
|
||||||
|
} else if (line.contains("/")) {
|
||||||
|
|
||||||
|
} else { // Means the F is just 3 vertex indexes
|
||||||
|
|
||||||
|
int vertex_index = (Integer.parseInt(data[index]) - 1) * 3;
|
||||||
|
|
||||||
|
boolean vbo_el_found = false;
|
||||||
|
for (int a = 0; a < temp_v_vbo.size(); a += 3) {
|
||||||
|
boolean vertex_found = true;
|
||||||
|
|
||||||
|
for (int b = 0; b < 3; b++) {
|
||||||
|
if (temp_v_vbo.get(a + b) != v.get(vertex_index + b)) {
|
||||||
|
vertex_found = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vertex_found) {
|
||||||
|
i.add(a / 3);
|
||||||
|
vbo_el_found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!vbo_el_found) {
|
||||||
|
for (int a = 0; a < 3; a++)
|
||||||
|
temp_v_vbo.add(v.get(vertex_index + a));
|
||||||
|
|
||||||
|
i.add((temp_v_vbo.size() / 3) - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.indices = ArrayUtils.toPrimitive(i.toArray(Integer[]::new));
|
||||||
|
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[] getVertexVbo() {
|
||||||
|
return vertex_vbo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float[] getNormalVbo() {
|
||||||
|
return normal_vbo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] getIndices() {
|
||||||
|
return indices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVertexCount() {
|
||||||
|
return vertex_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,9 +1,9 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine.world;
|
||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
|
import org.xfe.engine.ShaderProgram;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
|
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
|
||||||
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
|
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine.world;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
package org.xfe.engine.world;
|
||||||
|
|
||||||
|
import org.joml.GeometryUtils;
|
||||||
|
import org.joml.Vector3f;
|
||||||
|
import org.lwjgl.opengl.GL30;
|
||||||
|
import org.lwjgl.system.MemoryStack;
|
||||||
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
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.*;
|
||||||
|
import static org.lwjgl.opengl.GL15.*;
|
||||||
|
import static org.lwjgl.opengl.GL20.*;
|
||||||
|
import static org.lwjgl.opengl.GL30.*;
|
||||||
|
|
||||||
|
public class Mesh {
|
||||||
|
|
||||||
|
public int vertex_count;
|
||||||
|
public int vao_id;
|
||||||
|
public List<Integer> vbo_id_list;
|
||||||
|
|
||||||
|
public float[] vertices;
|
||||||
|
public float[] normals;
|
||||||
|
public int[] indices;
|
||||||
|
|
||||||
|
public Mesh(float[] vbo_buffer, int[] i) {
|
||||||
|
this.vbo_id_list = new ArrayList<>();
|
||||||
|
|
||||||
|
this.vertices = vbo_buffer;
|
||||||
|
this.indices = i;
|
||||||
|
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a new Mesh object from an wavefront .obj file
|
||||||
|
public Mesh(String obj_file_path) {
|
||||||
|
this.vbo_id_list = new ArrayList<>();
|
||||||
|
Obj obj = new Obj(obj_file_path);
|
||||||
|
|
||||||
|
this.vertices = obj.getVertexVbo();
|
||||||
|
this.normals = obj.getNormalVbo();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
|
||||||
|
this.vao_id = glGenVertexArrays();
|
||||||
|
glBindVertexArray(this.vao_id);
|
||||||
|
|
||||||
|
// Bind Vertices into a VBO
|
||||||
|
int vbo_id = glGenBuffers();
|
||||||
|
|
||||||
|
vbo_id_list.add(vbo_id);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// 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_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.vertices.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 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void render() {
|
||||||
|
glBindVertexArray(this.vao_id);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glDrawElements(GL_TRIANGLES, this.indices.length, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
glDisableVertexAttribArray(0);
|
||||||
|
glDisableVertexAttribArray(1);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
public void cleanUp() {
|
||||||
|
vbo_id_list.forEach(GL30::glDeleteBuffers);
|
||||||
|
glDeleteVertexArrays(this.vao_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-3
@@ -1,6 +1,4 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine.world;
|
||||||
|
|
||||||
import org.joml.Vector3f;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
@@ -2,6 +2,8 @@ package org.xfe.game;
|
|||||||
|
|
||||||
import org.joml.Matrix4f;
|
import org.joml.Matrix4f;
|
||||||
import org.xfe.engine.*;
|
import org.xfe.engine.*;
|
||||||
|
import org.xfe.engine.world.Entity;
|
||||||
|
import org.xfe.engine.world.World;
|
||||||
|
|
||||||
public class Main implements GameLogic {
|
public class Main implements GameLogic {
|
||||||
|
|
||||||
@@ -36,8 +38,10 @@ public class Main implements GameLogic {
|
|||||||
public void update(Window window, World world, long diffTimeMillis) {
|
public void update(Window window, World world, long diffTimeMillis) {
|
||||||
world.entities.get(0).model = new Matrix4f()
|
world.entities.get(0).model = new Matrix4f()
|
||||||
.identity()
|
.identity()
|
||||||
.translate(0, 0, -10)
|
.translate(0, 0, -5)
|
||||||
|
.translate(0, -2, 0)
|
||||||
.rotate(ang, 0,1, 0);
|
.rotate(ang, 0,1, 0);
|
||||||
|
// .rotate(ang * 2, 0, 0, 1);
|
||||||
ang += 1.0f/100.0f;
|
ang += 1.0f/100.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user