trying to add better lighting

This commit is contained in:
JISAUAY committed 2024-09-25 17:41:43 -05:00
1 parent 2b1fa60212
commit ce888f5ac0
17 files changed
+128 -12361

No files matched your search

+49 -9
View File
@@ -3,21 +3,61 @@ out vec4 fragColor;
//uniform float light_power;
//uniform vec3 lightColor;
//uniform vec3 objectColor;
//uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;
//uniform vec3 view_pos;
// in vec3 FragPos;
//in vec3 Normal;
//in vec3 LightPos;
in vec3 FragPos;
in vec3 Normal;
in vec3 LightPos;
// const float light_power = 12.0;
const float ambientStrength = 0.2;
const float light_power = 12.0;
const float ambientStrength = 0.1;
const float shininess = 64.0;
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
void main()
{
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
// FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
// fragColor = vec4(dot(LightPos, Normal) * lightColor, 1.0);
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(LightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 ambient = ambientStrength * lightColor;
fragColor = vec4((ambient + diffuse) * objectColor, 1.0);
// vec3 lightDir = LightPos - FragPos;
// float distance = length(lightDir);
// vec3 norm = normalize(Normal);
// vec3 ambient = ambientStrength * lightColor;
//
// distance = distance * distance;
// lightDir = normalize(lightDir);
//
// float lambertian = max(dot(norm, lightDir), 0.0);
// vec3 diffuse = lambertian * lightColor;
// float specular = 0.0;
//
// if (lambertian > 0.0)
// {
// vec3 viewDir = normalize(-FragPos);
//
// vec3 halfDir = normalize(lightDir + viewDir);
// float specAngle = max(dot(halfDir, norm), 0.0);
// specular = pow(specAngle, shininess);
// }
//
// vec3 colorLinear = (ambient + (diffuse * light_power) + (specular * light_power)) * objectColor;
//
// // vec3 colorGammaCorrected = pow(colorLinear, vec3(1.0 / screenGamma));
//
// fragColor = vec4(colorLinear, 1.0);
// vec3 norm = normalize(Normal);
}
+10 -11
View File
@@ -1,23 +1,22 @@
#version 330 core
layout (location = 0) in vec3 aPos;
// layout (location = 1) in vec3 aNormal;
//layout (location = 1) in vec3 aColor;
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
uniform mat4 model;
// uniform mat4 view;
uniform mat4 projection;
// uniform vec3 lightPos;
uniform vec3 lightPos;
// out vec3 FragPos;
// out vec3 Normal;
// out vec3 LightPos;
out vec3 FragPos;
out vec3 Normal;
out vec3 LightPos;
void main()
{
// * view * model
gl_Position = projection * model * vec4(aPos, 1.0);
// FragPos = vec3(vec4(aPos, 1.0));
// Normal = mat3(transpose(inverse(view * model))) * aNormal;
// LightPos = vec3(view * vec4(lightPos, 1.0));
gl_Position = projection * model * vec4(position, 1.0);
FragPos = vec3(model * vec4(position, 1.0));
Normal = normal;
LightPos = lightPos;
}
+2 -2
View File
@@ -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);
+5
View File
@@ -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);
}
}
}
+37 -22
View File
@@ -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);
}
}
+16 -18
View File
@@ -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();
}
+1 -1
View File
@@ -6,7 +6,7 @@ import java.util.ArrayList;
public class World
{
ArrayList<Entity> entities;
public ArrayList<Entity> entities;
ArrayList<Light> lights;
+8 -16
View File
@@ -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;
}
}
-221
View File
@@ -1,221 +0,0 @@
# Blender v2.79 (sub 0) OBJ File: 'axis.blend'
# www.blender.org
v -11.748000 -0.360000 1.268000
v -11.276000 -0.360000 1.268000
v -10.424000 -0.360000 0.228000
v -9.568000 -0.360000 1.268000
v -9.092000 -0.360000 1.268000
v -10.184000 -0.360000 -0.048000
v -9.008000 -0.360000 -1.460000
v -9.484000 -0.360000 -1.460000
v -10.424000 -0.360000 -0.332000
v -11.352000 -0.360000 -1.460000
v -11.824000 -0.360000 -1.460000
v -10.660000 -0.360000 -0.048000
v -9.484000 0.373033 -1.460000
v -10.424000 0.373033 -0.332000
v -11.748000 0.373033 1.268000
v -10.660000 0.373033 -0.048000
v -9.568000 0.373033 1.268000
v -10.184000 0.373033 -0.048000
v -11.824000 0.373033 -1.460000
v -9.008000 0.373033 -1.460000
v -10.424000 0.373033 0.228000
v -11.352000 0.373033 -1.460000
v -9.092000 0.373033 1.268000
v -11.276000 0.373033 1.268000
v -0.620000 -0.380000 11.376000
v 1.116000 -0.380000 9.000000
v -1.344000 -0.380000 9.000000
v -1.344000 -0.380000 9.352000
v 0.392000 -0.380000 9.352000
v -1.344000 -0.380000 11.728000
v 1.008000 -0.380000 11.728000
v 1.008000 -0.380000 11.376000
v 0.392000 0.384923 9.352000
v -1.344000 0.384923 11.728000
v -0.620000 0.384923 11.376000
v -1.344000 0.384923 9.352000
v 1.116000 0.384923 9.000000
v 1.008000 0.384923 11.728000
v 1.008000 0.384923 11.376000
v -1.344000 0.384923 9.000000
v -0.250000 10.788000 0.104000
v -0.250000 11.728000 -0.628000
v -0.250000 11.728000 -1.100000
v -0.250000 10.444000 -0.088000
v -0.250000 9.000000 -0.088000
v -0.250000 9.000000 0.304000
v -0.250000 10.440000 0.304000
v -0.250000 11.728000 1.316000
v -0.250000 11.728000 0.844000
v 0.304321 9.000000 -0.088000
v 0.304321 9.000000 0.304000
v 0.304321 10.440000 0.304000
v 0.304321 10.444000 -0.088000
v 0.304321 11.728000 1.316000
v 0.304321 10.788000 0.104000
v 0.304321 11.728000 -0.628000
v 0.304321 11.728000 -1.100000
v 0.304321 11.728000 0.844000
v 0.500000 -0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v 0.500000 -0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 -0.500000 -0.500000
v -0.500000 0.500000 -0.500000
v -0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v -8.500000 0.500000 0.500000
v -8.500000 -0.500000 0.500000
v -8.500000 -0.500000 -0.500000
v -8.500000 0.500000 -0.500000
v 0.500000 8.500000 -0.500000
v 0.500000 8.500000 0.500000
v -0.500000 8.500000 0.500000
v -0.500000 8.500000 -0.500000
v 0.500000 0.500000 8.500000
v 0.500000 -0.500000 8.500000
v -0.500000 -0.500000 8.500000
v -0.500000 0.500000 8.500000
s off
f 6 5 4
f 6 4 3
f 3 2 1
f 3 1 12
f 6 3 12
f 7 6 12
f 7 12 9
f 9 12 11
f 7 9 8
f 10 9 11
f 18 17 23
f 18 21 17
f 21 15 24
f 21 16 15
f 18 16 21
f 20 16 18
f 20 14 16
f 14 19 16
f 20 13 14
f 22 19 14
f 6 23 5
f 9 13 8
f 10 14 9
f 7 18 6
f 1 16 12
f 11 22 10
f 2 15 1
f 5 17 4
f 4 21 3
f 3 24 2
f 8 20 7
f 12 19 11
f 32 31 30
f 32 30 25
f 25 30 29
f 26 25 29
f 26 29 28
f 26 28 27
f 39 34 38
f 39 35 34
f 35 33 34
f 37 33 35
f 37 36 33
f 37 40 36
f 28 40 27
f 25 39 32
f 32 38 31
f 26 35 25
f 29 36 28
f 30 33 29
f 27 37 26
f 31 34 30
f 44 42 43
f 44 41 42
f 41 48 49
f 41 47 48
f 44 47 41
f 45 47 44
f 45 46 47
f 53 57 56
f 53 56 55
f 55 58 54
f 55 54 52
f 53 55 52
f 50 53 52
f 50 52 51
f 58 41 49
f 50 46 45
f 55 42 41
f 54 49 48
f 51 47 46
f 52 48 47
f 56 43 42
f 57 44 43
f 53 45 44
f 60 61 59
f 65 78 66
f 66 68 65
f 64 59 63
f 65 59 61
f 60 72 62
f 67 69 68
f 63 70 64
f 65 69 63
f 64 67 66
f 72 74 73
f 64 71 60
f 62 73 66
f 66 74 64
f 75 77 76
f 61 77 65
f 66 75 62
f 62 76 61
f 6 18 23
f 9 14 13
f 10 22 14
f 7 20 18
f 1 15 16
f 11 19 22
f 2 24 15
f 5 23 17
f 4 17 21
f 3 21 24
f 8 13 20
f 12 16 19
f 28 36 40
f 25 35 39
f 32 39 38
f 26 37 35
f 29 33 36
f 30 34 33
f 27 40 37
f 31 38 34
f 58 55 41
f 50 51 46
f 55 56 42
f 54 58 49
f 51 52 47
f 52 54 48
f 56 57 43
f 57 53 44
f 53 50 45
f 60 62 61
f 65 77 78
f 66 67 68
f 64 60 59
f 65 63 59
f 60 71 72
f 67 70 69
f 63 69 70
f 65 68 69
f 64 70 67
f 72 71 74
f 64 74 71
f 62 72 73
f 66 73 74
f 75 78 77
f 61 76 77
f 66 78 75
f 62 75 76
-30
View File
@@ -1,30 +0,0 @@
# Blender 4.1.1
# www.blender.org
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vn -0.0000 1.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
s 0
f 5//1 3//1 1//1
f 3//2 8//2 4//2
f 7//3 6//3 8//3
f 2//4 8//4 6//4
f 1//5 4//5 2//5
f 5//6 2//6 6//6
f 5//1 7//1 3//1
f 3//2 7//2 8//2
f 7//3 5//3 6//3
f 2//4 4//4 8//4
f 1//5 3//5 4//5
f 5//6 1//6 2//6
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-18
View File
@@ -1,18 +0,0 @@
# Blender 4.1.1
# www.blender.org
mtllib tetrahedron.mtl
o Tetrahedron
v 0.589834 1.455189 -0.731109
v -0.654905 0.362953 1.561846
v -1.236534 -0.687860 -0.998915
v 1.301606 -1.130282 0.168178
vn -0.3405 -0.8402 0.4221
vn -0.7515 0.6526 -0.0971
vn 0.7139 0.3971 0.5767
vn 0.3781 -0.2096 -0.9017
s 0
usemtl
f 3//1 4//1 2//1
f 2//2 1//2 3//2
f 1//3 2//3 4//3
f 1//4 4//4 3//4
-23
View File
@@ -1,23 +0,0 @@
#version 330 core
out vec4 fragColor;
//uniform float light_power;
//uniform vec3 lightColor;
//uniform vec3 objectColor;
//uniform vec3 viewPos;
// in vec3 FragPos;
//in vec3 Normal;
//in vec3 LightPos;
// const float light_power = 12.0;
const float ambientStrength = 0.2;
const float shininess = 64.0;
const float screenGamma = 2.2; // Assume the monitor is calibrated to the sRGB color space
void main()
{
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
-23
View File
@@ -1,23 +0,0 @@
#version 330 core
layout (location = 0) in vec3 aPos;
// layout (location = 1) in vec3 aNormal;
//layout (location = 1) in vec3 aColor;
uniform mat4 model;
// uniform mat4 view;
uniform mat4 projection;
// uniform vec3 lightPos;
// out vec3 FragPos;
// out vec3 Normal;
// out vec3 LightPos;
void main()
{
// * view * model
gl_Position = projection * model * vec4(aPos, 1.0);
// FragPos = vec3(vec4(aPos, 1.0));
// Normal = mat3(transpose(inverse(view * model))) * aNormal;
// LightPos = vec3(view * vec4(lightPos, 1.0));
}
-11
View File
@@ -1,11 +0,0 @@
#version 330 core
out vec4 FragColor;
uniform vec3 lightColor;
void main()
{
FragColor = vec4(lightColor, 1.0);
}
-15
View File
@@ -1,15 +0,0 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
}