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
|
public class Entity
|
||||||
{
|
{
|
||||||
Mesh mesh;
|
public Mesh mesh;
|
||||||
|
|
||||||
public Matrix4f model;
|
public Matrix4f model;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.xfe.engine;
|
package org.xfe.engine;
|
||||||
|
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
import org.lwjgl.system.MemoryStack;
|
||||||
import org.lwjgl.system.MemoryUtil;
|
import org.lwjgl.system.MemoryUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -24,10 +25,16 @@ public class Mesh {
|
|||||||
public int vbo_id;
|
public int vbo_id;
|
||||||
public int ebo_id;
|
public int ebo_id;
|
||||||
|
|
||||||
Float[] vertices;
|
public Float[] vertices;
|
||||||
Float[] normals;
|
public Float[] normals;
|
||||||
Integer[] vertice_indices;
|
public Integer[] vertice_indices;
|
||||||
Integer[] normal_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
|
// Creates a new Mesh object from an wavefront .obj file
|
||||||
public Mesh(String obj_file_path) {
|
public Mesh(String obj_file_path) {
|
||||||
@@ -98,41 +105,48 @@ public class Mesh {
|
|||||||
init();
|
init();
|
||||||
|
|
||||||
System.out.println(Arrays.toString(this.vertices));
|
System.out.println(Arrays.toString(this.vertices));
|
||||||
|
System.out.println(Arrays.toString(this.vertice_indices));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
FloatBuffer vertices_buffer = null;
|
FloatBuffer vertices_buffer = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
MemoryStack stack = MemoryStack.stackPush();
|
||||||
|
|
||||||
// Bind our vertices into the VBO
|
// 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;
|
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(ArrayUtils.toPrimitive(this.vertices));
|
vertices_buffer.put(0, ArrayUtils.toPrimitive(this.vertices));
|
||||||
|
|
||||||
this.vao_id = glGenVertexArrays();
|
this.vao_id = glGenVertexArrays();
|
||||||
glBindVertexArray(this.vao_id);
|
glBindVertexArray(this.vao_id);
|
||||||
|
|
||||||
this.vbo_id = glGenBuffers();
|
this.vbo_id = glGenBuffers();
|
||||||
|
this.ebo_id = glGenBuffers();
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, this.vbo_id);
|
glBindBuffer(GL_ARRAY_BUFFER, this.vbo_id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, vertices_buffer, GL_STATIC_DRAW);
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
|
||||||
|
|
||||||
// Bind indices in EBO
|
// Bind indices in EBO
|
||||||
if (this.vertice_indices.length > 0)
|
if (this.vertice_indices.length > 0)
|
||||||
{
|
{
|
||||||
// TODO : @0x01fe probably should free this (memory leaks are like bad or smthng)
|
// TODO : @0x01fe probably should free this (memory leaks are like bad or smthng)
|
||||||
IntBuffer indice_buffer = MemoryUtil.memAllocInt(vertice_indices.length);
|
IntBuffer indice_buffer = stack.callocInt(vertice_indices.length);
|
||||||
indice_buffer.put(ArrayUtils.toPrimitive(vertice_indices));
|
indice_buffer.put(0, ArrayUtils.toPrimitive(vertice_indices));
|
||||||
|
|
||||||
ebo_id = glGenBuffers();
|
System.out.println(indice_buffer);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_id);
|
|
||||||
|
|
||||||
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.ebo_id);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indice_buffer, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
} finally {
|
} finally {
|
||||||
if (vertices_buffer != null) {
|
if (vertices_buffer != null) {
|
||||||
MemoryUtil.memFree(vertices_buffer);
|
MemoryUtil.memFree(vertices_buffer);
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ 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 java.util.Arrays;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
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.GL15.*;
|
||||||
@@ -22,17 +25,21 @@ public class Render {
|
|||||||
|
|
||||||
public void render(Window window, World world) {
|
public void render(Window window, World world) {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
glViewport(0, 0, window.getWidth(), window.getHeight());
|
||||||
|
|
||||||
|
|
||||||
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
|
||||||
float aspect_ratio = (float) window.getWidth() / window.getHeight();
|
float aspect_ratio = (float) window.getWidth() / window.getHeight();
|
||||||
projection_matrix = new Matrix4f().perspective(Engine.FOV, aspect_ratio, Engine.Z_FAR, Engine.Z_NEAR);
|
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) {
|
for (Entity entity : world.entities) {
|
||||||
entity.shader.bind();
|
entity.shader.bind();
|
||||||
|
|
||||||
entity.shader.setUniform("projection", projection_matrix);
|
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("model", new Matrix4f().identity());
|
||||||
// entity.shader.setUniform("view", new Matrix4f().identity());
|
// entity.shader.setUniform("view", new Matrix4f().identity());
|
||||||
// entity.shader.setUniform("lightPos", world.lights.get(0).position);
|
// entity.shader.setUniform("lightPos", world.lights.get(0).position);
|
||||||
@@ -43,9 +50,11 @@ public class Render {
|
|||||||
|
|
||||||
|
|
||||||
glBindVertexArray(entity.mesh.vao_id);
|
glBindVertexArray(entity.mesh.vao_id);
|
||||||
glEnableVertexAttribArray(0);
|
|
||||||
// glEnableVertexAttribArray(1);
|
// glEnableVertexAttribArray(1);
|
||||||
glDrawElements(GL_TRIANGLES, entity.mesh.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, entity.mesh.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
|
glBindVertexArray(0);
|
||||||
|
entity.shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
// glfwSwapBuffers(window.getWindowHandle());
|
// glfwSwapBuffers(window.getWindowHandle());
|
||||||
|
|||||||
@@ -19,8 +19,21 @@ public class Main implements GameLogic {
|
|||||||
@Override
|
@Override
|
||||||
public void init(Window window, World world, Render render) {
|
public void init(Window window, World world, Render render) {
|
||||||
Entity cube = new Entity("/objs/cube.obj", "/shaders/shader.frag", "/shaders/shader.vert");
|
Entity cube = new Entity("/objs/cube.obj", "/shaders/shader.frag", "/shaders/shader.vert");
|
||||||
cube.model = new Matrix4f().identity().translate(0, 0, -10);
|
// cube.model = new Matrix4f().identity().translate(0, 0, 10);
|
||||||
System.out.println(cube.model.toString());
|
|
||||||
|
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);
|
world.addEntity(cube);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
out vec4 FragColor;
|
out vec4 fragColor;
|
||||||
|
|
||||||
|
|
||||||
//uniform float light_power;
|
//uniform float light_power;
|
||||||
@@ -7,7 +7,7 @@ out vec4 FragColor;
|
|||||||
//uniform vec3 objectColor;
|
//uniform vec3 objectColor;
|
||||||
//uniform vec3 viewPos;
|
//uniform vec3 viewPos;
|
||||||
|
|
||||||
in vec3 FragPos;
|
// in vec3 FragPos;
|
||||||
//in vec3 Normal;
|
//in vec3 Normal;
|
||||||
//in vec3 LightPos;
|
//in vec3 LightPos;
|
||||||
|
|
||||||
@@ -18,6 +18,6 @@ const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB c
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ layout (location = 0) in vec3 aPos;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
// uniform vec3 lightPos;
|
// uniform vec3 lightPos;
|
||||||
|
|
||||||
out vec3 FragPos;
|
// out vec3 FragPos;
|
||||||
// out vec3 Normal;
|
// out vec3 Normal;
|
||||||
// out vec3 LightPos;
|
// out vec3 LightPos;
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ void main()
|
|||||||
{
|
{
|
||||||
// * view * model
|
// * view * model
|
||||||
gl_Position = projection * model * vec4(aPos, 1.0);
|
gl_Position = projection * model * vec4(aPos, 1.0);
|
||||||
FragPos = vec3(vec4(aPos, 1.0));
|
// FragPos = vec3(vec4(aPos, 1.0));
|
||||||
// Normal = mat3(transpose(inverse(view * model))) * aNormal;
|
// Normal = mat3(transpose(inverse(view * model))) * aNormal;
|
||||||
// LightPos = vec3(view * vec4(lightPos, 1.0));
|
// LightPos = vec3(view * vec4(lightPos, 1.0));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user