trying to add better lighting
This commit is contained in:
1 parent
2b1fa60212
commit
ce888f5ac0
17 files changed
+129
-12362
No files matched your search
@@ -18,7 +18,7 @@ public class Engine
|
||||
public static final float Z_FAR = 1000.0f;
|
||||
public static final float Z_NEAR = 0.1f;
|
||||
public static final float FOV = (float) Math.toRadians(90.0f);
|
||||
|
||||
public static final boolean WIREFRAME_MODE = false;
|
||||
public Engine(String window_title, Window.WindowOptions opts, GameLogic gameLogic) {
|
||||
window = new Window(window_title, opts, () -> {
|
||||
resize();
|
||||
@@ -28,7 +28,7 @@ public class Engine
|
||||
target_ups = opts.ups;
|
||||
|
||||
this.gameLogic = gameLogic;
|
||||
this.render = new Render();
|
||||
this.render = new Render(window);
|
||||
this.world = new World();
|
||||
|
||||
gameLogic.init(window, world, render);
|
||||
|
||||
@@ -66,8 +66,13 @@ public class Entity
|
||||
try {
|
||||
shader.createUniform("projection");
|
||||
shader.createUniform("model");
|
||||
shader.createUniform("lightPos");
|
||||
shader.createUniform("lightColor");
|
||||
shader.createUniform("objectColor");
|
||||
//shader.createUniform("view_pos");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
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;
|
||||
|
||||
@@ -11,6 +12,7 @@ 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.*;
|
||||
@@ -22,8 +24,7 @@ public class Mesh {
|
||||
|
||||
public int vertex_count;
|
||||
public int vao_id;
|
||||
public int vbo_id;
|
||||
public int ebo_id;
|
||||
public List<Integer> vbo_id_list;
|
||||
|
||||
public Float[] vertices;
|
||||
public Float[] normals;
|
||||
@@ -38,6 +39,7 @@ public class Mesh {
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
@@ -49,7 +51,7 @@ public class Mesh {
|
||||
ArrayList<Integer> temp_normal_indices = new ArrayList<>();
|
||||
|
||||
Scanner scanner;
|
||||
InputStream obj;
|
||||
File obj;
|
||||
|
||||
String line;
|
||||
String[] face_data_temp;
|
||||
@@ -58,7 +60,7 @@ public class Mesh {
|
||||
|
||||
// Try to open the file
|
||||
try {
|
||||
obj = this.getClass().getResourceAsStream(file_path);
|
||||
obj = new File(file_path);
|
||||
scanner = new Scanner(obj);
|
||||
} catch (Exception e) {
|
||||
System.out.println("ERROR: OBJ File \"" + file_path + "\" not found.");
|
||||
@@ -87,7 +89,7 @@ public class Mesh {
|
||||
|
||||
for (i = 1; i < 4; i++) {
|
||||
face_data_temp = data[i].split("//");
|
||||
temp_vertice_indices.add(Integer.parseInt(face_data_temp[0]));
|
||||
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]));
|
||||
@@ -124,27 +126,37 @@ public class Mesh {
|
||||
this.vao_id = glGenVertexArrays();
|
||||
glBindVertexArray(this.vao_id);
|
||||
|
||||
this.vbo_id = glGenBuffers();
|
||||
this.ebo_id = glGenBuffers();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, this.vbo_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)
|
||||
{
|
||||
// TODO : @0x01fe probably should free this (memory leaks are like bad or smthng)
|
||||
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));
|
||||
|
||||
System.out.println(indice_buffer);
|
||||
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this.ebo_id);
|
||||
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 {
|
||||
@@ -154,15 +166,18 @@ public class Mesh {
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanUp() {
|
||||
public void render() {
|
||||
glBindVertexArray(this.vao_id);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glDrawElements(GL_TRIANGLES, this.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
|
||||
// Delete the VBO
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDeleteBuffers(this.vbo_id);
|
||||
|
||||
// Delete the VAO
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
public void cleanUp() {
|
||||
vbo_id_list.forEach(GL30::glDeleteBuffers);
|
||||
glDeleteVertexArrays(this.vao_id);
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,16 @@ import static org.lwjgl.opengl.GL30.*;
|
||||
public class Render {
|
||||
|
||||
private Matrix4f projection_matrix;
|
||||
public Render() {
|
||||
public Render(Window window) {
|
||||
GL.createCapabilities();
|
||||
|
||||
float aspect_ratio = (float) window.getWidth() / window.getHeight();
|
||||
projection_matrix = new Matrix4f().perspective(Engine.FOV, aspect_ratio, Engine.Z_NEAR, Engine.Z_FAR);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
if (Engine.WIREFRAME_MODE)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
@@ -28,32 +36,22 @@ public class Render {
|
||||
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_NEAR, Engine.Z_FAR);
|
||||
|
||||
// System.out.println(projection_matrix.toString());
|
||||
|
||||
Vector3f light_position = new Vector3f(0, 0, -8);
|
||||
|
||||
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("view", new Matrix4f().identity());
|
||||
// entity.shader.setUniform("lightPos", world.lights.get(0).position);
|
||||
// entity.shader.setUniform("light_power", world.lights.get(0).power);
|
||||
// entity.shader.setUniform("lightColor", world.lights.get(0).color);
|
||||
// entity.shader.setUniform("objectColor", new Vector3f(1.0f, 1.0f, 1.0f));
|
||||
// entity.shader.setUniform("viewPos", new Vector3f(0.0f, 0.0f, 0.0f));
|
||||
entity.shader.setUniform("lightPos", light_position);
|
||||
entity.shader.setUniform("lightColor", new Vector3f(1, 1, 1));
|
||||
entity.shader.setUniform("objectColor", new Vector3f(0.9f, 0.9f, 0.9f));
|
||||
// entity.shader.setUniform("viewPos", new Vector3f(0, 0, 0));
|
||||
|
||||
entity.mesh.render();
|
||||
|
||||
glBindVertexArray(entity.mesh.vao_id);
|
||||
// glEnableVertexAttribArray(1);
|
||||
glDrawElements(GL_TRIANGLES, entity.mesh.vertice_indices.length, GL_UNSIGNED_INT, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
entity.shader.unbind();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
|
||||
public class World
|
||||
{
|
||||
ArrayList<Entity> entities;
|
||||
public ArrayList<Entity> entities;
|
||||
|
||||
ArrayList<Light> lights;
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ import org.xfe.engine.*;
|
||||
|
||||
public class Main implements GameLogic {
|
||||
|
||||
private float ang;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Main main = new Main();
|
||||
Engine engine = new Engine("Test", new Window.WindowOptions(), main);
|
||||
@@ -18,23 +20,9 @@ public class Main implements GameLogic {
|
||||
|
||||
@Override
|
||||
public void init(Window window, World world, Render render) {
|
||||
Entity cube = new Entity("/objs/cube.obj", "resources/shaders/shader.frag", "resources/shaders/shader.vert");
|
||||
Entity cube = new Entity("resources/objs/cube.obj", "resources/shaders/shader.frag", "resources/shaders/shader.vert");
|
||||
cube.model = new Matrix4f().identity().translate(0, 0, -10);
|
||||
|
||||
// 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);
|
||||
|
||||
System.out.println(cube.model.toString());
|
||||
world.addEntity(cube);
|
||||
}
|
||||
@@ -46,6 +34,10 @@ public class Main implements GameLogic {
|
||||
|
||||
@Override
|
||||
public void update(Window window, World world, long diffTimeMillis) {
|
||||
|
||||
world.entities.get(0).model = new Matrix4f()
|
||||
.identity()
|
||||
.translate(0, 0, -10)
|
||||
.rotate(ang, 0,1, 0);
|
||||
ang += 1.0f/100.0f;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user