still no idea why it isn't working
This commit is contained in:
1 parent
8da13e805b
commit
b73d614393
6 files changed
+56
-20
No files matched your search
@@ -6,7 +6,7 @@ import java.util.Scanner;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
Mesh mesh;
|
||||
public Mesh mesh;
|
||||
|
||||
public Matrix4f model;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.xfe.engine;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -24,10 +25,16 @@ public class Mesh {
|
||||
public int vbo_id;
|
||||
public int ebo_id;
|
||||
|
||||
Float[] vertices;
|
||||
Float[] normals;
|
||||
Integer[] vertice_indices;
|
||||
Integer[] normal_indices;
|
||||
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) {
|
||||
@@ -98,41 +105,48 @@ public class Mesh {
|
||||
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 = MemoryUtil.memAllocFloat(this.vertices.length);
|
||||
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(ArrayUtils.toPrimitive(this.vertices));
|
||||
vertices_buffer.put(0, ArrayUtils.toPrimitive(this.vertices));
|
||||
|
||||
this.vao_id = glGenVertexArrays();
|
||||
glBindVertexArray(this.vao_id);
|
||||
|
||||
this.vbo_id = glGenBuffers();
|
||||
this.ebo_id = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this.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)
|
||||
{
|
||||
// TODO : @0x01fe probably should free this (memory leaks are like bad or smthng)
|
||||
IntBuffer indice_buffer = MemoryUtil.memAllocInt(vertice_indices.length);
|
||||
indice_buffer.put(ArrayUtils.toPrimitive(vertice_indices));
|
||||
IntBuffer indice_buffer = stack.callocInt(vertice_indices.length);
|
||||
indice_buffer.put(0, ArrayUtils.toPrimitive(vertice_indices));
|
||||
|
||||
ebo_id = glGenBuffers();
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_id);
|
||||
System.out.println(indice_buffer);
|
||||
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.ebo_id);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
} finally {
|
||||
if (vertices_buffer != null) {
|
||||
MemoryUtil.memFree(vertices_buffer);
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.xfe.engine;
|
||||
import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.lwjgl.glfw.GLFW.*;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
import static org.lwjgl.opengl.GL15.*;
|
||||
@@ -22,17 +25,21 @@ public class Render {
|
||||
|
||||
public void render(Window window, World world) {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glViewport(0, 0, window.getWidth(), window.getHeight());
|
||||
|
||||
|
||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
|
||||
float aspect_ratio = (float) window.getWidth() / window.getHeight();
|
||||
projection_matrix = new Matrix4f().perspective(Engine.FOV, aspect_ratio, Engine.Z_FAR, Engine.Z_NEAR);
|
||||
|
||||
// System.out.println(projection_matrix.toString());
|
||||
|
||||
for (Entity entity : world.entities) {
|
||||
entity.shader.bind();
|
||||
|
||||
entity.shader.setUniform("projection", projection_matrix);
|
||||
entity.shader.setUniform("model", entity.model);
|
||||
entity.shader.setUniform("model", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("model", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("view", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("lightPos", world.lights.get(0).position);
|
||||
@@ -43,9 +50,11 @@ public class Render {
|
||||
|
||||
|
||||
glBindVertexArray(entity.mesh.vao_id);
|
||||
glEnableVertexAttribArray(0);
|
||||
// glEnableVertexAttribArray(1);
|
||||
glDrawElements(GL_TRIANGLES, entity.mesh.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
entity.shader.unbind();
|
||||
}
|
||||
|
||||
// glfwSwapBuffers(window.getWindowHandle());
|
||||
|
||||
@@ -19,8 +19,21 @@ public class Main implements GameLogic {
|
||||
@Override
|
||||
public void init(Window window, World world, Render render) {
|
||||
Entity cube = new Entity("/objs/cube.obj", "/shaders/shader.frag", "/shaders/shader.vert");
|
||||
cube.model = new Matrix4f().identity().translate(0, 0, -10);
|
||||
System.out.println(cube.model.toString());
|
||||
// cube.model = new Matrix4f().identity().translate(0, 0, 10);
|
||||
|
||||
Float[] v = new Float[]{
|
||||
-0.5f, 0.5f, 0.0f,
|
||||
-0.5f, -0.5f, 0.0f,
|
||||
0.5f, -0.5f, 0.0f,
|
||||
0.5f, 0.5f, 0.0f,
|
||||
};
|
||||
Integer[] i = new Integer[]{
|
||||
0, 1, 3, 3, 1, 2,
|
||||
};
|
||||
|
||||
cube.mesh = new Mesh(v, i);
|
||||
|
||||
// System.out.println(cube.model.toString());
|
||||
world.addEntity(cube);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user