we got cubes now
This commit is contained in:
1 parent
b73d614393
commit
2b1fa60212
17 files changed
+16485
-25
No files matched your search
@@ -2,8 +2,12 @@ package org.xfe.engine;
|
||||
|
||||
import org.joml.Matrix4f;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
|
||||
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
|
||||
|
||||
public class Entity
|
||||
{
|
||||
public Mesh mesh;
|
||||
@@ -12,11 +16,23 @@ public class Entity
|
||||
|
||||
public ShaderProgram shader;
|
||||
|
||||
public Entity() {
|
||||
this.model = new Matrix4f().identity();
|
||||
}
|
||||
|
||||
public Entity(String fragment_shader_path, String vertex_shader_path) {
|
||||
this.model = new Matrix4f().identity();
|
||||
loadShaders(fragment_shader_path, vertex_shader_path);
|
||||
}
|
||||
|
||||
public Entity(String object_file_path, String fragment_shader_path, String vertex_shader_path)
|
||||
{
|
||||
this.model = new Matrix4f().identity();
|
||||
this.mesh = new Mesh(object_file_path);
|
||||
loadShaders(fragment_shader_path, vertex_shader_path);
|
||||
}
|
||||
|
||||
// Load Shaders
|
||||
void loadShaders(String fragment_shader_path, String vertex_shader_path) {
|
||||
try {
|
||||
this.shader = new ShaderProgram();
|
||||
} catch (Exception e) {
|
||||
@@ -25,16 +41,14 @@ public class Entity
|
||||
}
|
||||
|
||||
try {
|
||||
String vertex_shader_code = new Scanner(this.getClass().getResourceAsStream(vertex_shader_path)).useDelimiter("\\Z").next();
|
||||
this.shader.createVertexShader(vertex_shader_code);
|
||||
this.shader.createShader(new File(vertex_shader_path), GL_VERTEX_SHADER);
|
||||
} catch (Exception e) {
|
||||
System.out.println("ERROR Initializing the vertex shader");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
String fragment_shader_code = new Scanner(this.getClass().getResourceAsStream(fragment_shader_path)).useDelimiter("\\Z").next();
|
||||
this.shader.createFragmentShader(fragment_shader_code);
|
||||
this.shader.createShader(new File(fragment_shader_path), GL_FRAGMENT_SHADER);
|
||||
} catch (Exception e)
|
||||
{
|
||||
System.out.println("ERROR Initializing the fragment shader");
|
||||
|
||||
@@ -148,9 +148,9 @@ public class Mesh {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
} finally {
|
||||
if (vertices_buffer != null) {
|
||||
MemoryUtil.memFree(vertices_buffer);
|
||||
}
|
||||
// if (vertices_buffer != null) {
|
||||
// MemoryUtil.memFree(vertices_buffer);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,10 @@ public class Render {
|
||||
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();
|
||||
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_NEAR, Engine.Z_FAR);
|
||||
|
||||
// System.out.println(projection_matrix.toString());
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Render {
|
||||
entity.shader.bind();
|
||||
|
||||
entity.shader.setUniform("projection", projection_matrix);
|
||||
entity.shader.setUniform("model", new Matrix4f().identity());
|
||||
entity.shader.setUniform("model", entity.model);
|
||||
// entity.shader.setUniform("model", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("view", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("lightPos", world.lights.get(0).position);
|
||||
|
||||
@@ -4,9 +4,11 @@ import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.lwjgl.opengl.GL20.*;
|
||||
|
||||
@@ -54,6 +56,11 @@ public class ShaderProgram {
|
||||
return shaderId;
|
||||
}
|
||||
|
||||
public void createShader(File file, int shader_type) throws Exception {
|
||||
String shader_code = new Scanner(file).useDelimiter("\\Z").next();
|
||||
createShader(shader_code, shader_type);
|
||||
}
|
||||
|
||||
public void link() throws Exception {
|
||||
glLinkProgram(programId);
|
||||
if (glGetProgrami(programId, GL_LINK_STATUS) == 0) {
|
||||
|
||||
@@ -18,22 +18,24 @@ 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);
|
||||
Entity cube = new Entity("/objs/cube.obj", "resources/shaders/shader.frag", "resources/shaders/shader.vert");
|
||||
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,
|
||||
};
|
||||
// Entity cube = new Entity("resources/shaders/shader.frag", "resources/shaders/shader.vert");
|
||||
//
|
||||
// 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);
|
||||
|
||||
cube.mesh = new Mesh(v, i);
|
||||
|
||||
// System.out.println(cube.model.toString());
|
||||
System.out.println(cube.model.toString());
|
||||
world.addEntity(cube);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user