Refactoring

This commit is contained in:
Snoweuph 2024-08-28 22:19:35 +02:00 committed by Snoweuph
parent e7336cbf82
commit ecb3e735da
No known key found for this signature in database
GPG key ID: 4550DEC769DB1E76
28 changed files with 90 additions and 85 deletions

View file

@ -5,3 +5,5 @@ include(":opengl")
project(":opengl").projectDir = file("src/opengl") project(":opengl").projectDir = file("src/opengl")
include(":rendering") include(":rendering")
project(":rendering").projectDir = file("src/rendering") project(":rendering").projectDir = file("src/rendering")
include(":resources")
project(":resources").projectDir = file("src/resources")

View file

@ -33,9 +33,6 @@ dependencies {
implementation("org.lwjgl", "lwjgl-glfw") implementation("org.lwjgl", "lwjgl-glfw")
runtimeOnly("org.lwjgl", "lwjgl-glfw", classifier = lwjglNatives) runtimeOnly("org.lwjgl", "lwjgl-glfw", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-stb")
runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives)
implementation("org.joml", "joml", jomlVersion) implementation("org.joml", "joml", jomlVersion)
implementation("org.joml", "joml-primitives", jomlPrimitivesVersion) implementation("org.joml", "joml-primitives", jomlPrimitivesVersion)

View file

@ -19,9 +19,6 @@ public class EntitiesByNameStorage implements SceneStorage<String, Entity> {
@Override @Override
public void add(Entity entity, Set<Class<? extends Component>> componentClasses) { public void add(Entity entity, Set<Class<? extends Component>> componentClasses) {
Set<Entity> entities = this.entitiesByName.computeIfAbsent(entity.getName(), _ -> new HashSet<>()); Set<Entity> entities = this.entitiesByName.computeIfAbsent(entity.getName(), _ -> new HashSet<>());
if (entities == null) {
entities = new HashSet<>();
}
entities.add(entity); entities.add(entity);
this.entitiesByName.put(entity.getName(), entities); this.entitiesByName.put(entity.getName(), entities);
} }

View file

@ -27,6 +27,7 @@ dependencies {
implementation("javax.annotation:javax.annotation-api:1.3.2") implementation("javax.annotation:javax.annotation-api:1.3.2")
compileOnly(project(":core", configuration = "shadow")) compileOnly(project(":core", configuration = "shadow"))
compileOnly(project(":resources", configuration = "shadow"))
compileOnly(project(":rendering", configuration = "shadow")) compileOnly(project(":rendering", configuration = "shadow"))
implementation("org.lwjgl", "lwjgl-assimp") implementation("org.lwjgl", "lwjgl-assimp")

View file

@ -1,6 +1,6 @@
package dev.euph.engine.opengl; package dev.euph.engine.opengl;
import dev.euph.engine.rendering.resources.Mesh; import dev.euph.engine.resources.Mesh;
import static org.lwjgl.opengl.GL30.glBindVertexArray; import static org.lwjgl.opengl.GL30.glBindVertexArray;

View file

@ -12,7 +12,7 @@ import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays; import static org.lwjgl.opengl.GL30.glGenVertexArrays;
public class GLMeshHelper { public class GLMeshHelper {
private final static int VERTICE_INDEX = 0; private final static int VERTICES_INDEX = 0;
private final static int TEXTURE_COORDINATE_INDEX = 1; private final static int TEXTURE_COORDINATE_INDEX = 1;
protected static int getVao() { protected static int getVao() {
@ -35,7 +35,7 @@ public class GLMeshHelper {
buffer.put(vertices).flip(); buffer.put(vertices).flip();
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
glVertexAttribPointer(VERTICE_INDEX, 3, GL_FLOAT, false, 0, 0); glVertexAttribPointer(VERTICES_INDEX, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }

View file

@ -10,7 +10,7 @@ import dev.euph.engine.rendering.Shader;
import dev.euph.engine.rendering.ShaderManager; import dev.euph.engine.rendering.ShaderManager;
import dev.euph.engine.rendering.components.Camera; import dev.euph.engine.rendering.components.Camera;
import dev.euph.engine.rendering.components.MeshRenderer; import dev.euph.engine.rendering.components.MeshRenderer;
import dev.euph.engine.rendering.resources.Mesh; import dev.euph.engine.resources.Mesh;
import dev.euph.engine.rendering.utils.ProjectionMatrix; import dev.euph.engine.rendering.utils.ProjectionMatrix;
import dev.euph.engine.rendering.utils.ViewMatrix; import dev.euph.engine.rendering.utils.ViewMatrix;
import lombok.Getter; import lombok.Getter;

View file

@ -1,7 +1,7 @@
package dev.euph.engine.opengl; package dev.euph.engine.opengl;
import dev.euph.engine.rendering.Shader; import dev.euph.engine.rendering.Shader;
import dev.euph.engine.rendering.resources.Texture; import dev.euph.engine.resources.Texture;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Vector2f; import org.joml.Vector2f;
import org.joml.Vector3f; import org.joml.Vector3f;

View file

@ -1,34 +1,40 @@
package dev.euph.engine.opengl; package dev.euph.engine.opengl;
import dev.euph.engine.core.data.ImageLoader; import dev.euph.engine.resources.Texture;
import dev.euph.engine.rendering.resources.Texture; import lombok.Getter;
import java.net.URL; import java.nio.ByteBuffer;
import static dev.euph.engine.opengl.GL.*; import static dev.euph.engine.opengl.GL.*;
public class GLTextureLoader { @Getter
public static Texture loadTexture(URL imageResource) { public class GLTexture implements Texture {
int textureID = GL.glGenTextures(); private final int id;
glBindTexture(GL_TEXTURE_2D, textureID); private final int height;
private final int width;
public GLTexture(int width, int height, ByteBuffer data) {
this.id = GL.glGenTextures();
glBindTexture(GL_TEXTURE_2D, this.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ImageLoader.Image image = ImageLoader.loadImage(imageResource.getPath());
glTexImage2D( glTexImage2D(
GL_TEXTURE_2D, GL_TEXTURE_2D,
0, 0,
GL_RGBA, GL_RGBA,
image.width(), image.height(), width, height,
0, 0,
GL_RGBA, GL_RGBA,
GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE,
image.data() data
); );
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
return new Texture(textureID, image.width(), image.height()); this.width = width;
this.height = height;
} }
} }

View file

@ -1,6 +1,6 @@
package dev.euph.engine.opengl; package dev.euph.engine.opengl;
import dev.euph.engine.rendering.resources.Mesh; import dev.euph.engine.resources.Mesh;
import static org.lwjgl.opengl.GL30.glBindVertexArray; import static org.lwjgl.opengl.GL30.glBindVertexArray;

View file

@ -27,6 +27,7 @@ dependencies {
implementation("javax.annotation:javax.annotation-api:1.3.2") implementation("javax.annotation:javax.annotation-api:1.3.2")
compileOnly(project(":core", configuration = "shadow")) compileOnly(project(":core", configuration = "shadow"))
compileOnly(project(":resources", configuration = "shadow"))
implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion")) implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion"))
shadow(localGroovy()) shadow(localGroovy())

View file

@ -1,6 +1,6 @@
package dev.euph.engine.rendering; package dev.euph.engine.rendering;
import dev.euph.engine.rendering.resources.Texture; import dev.euph.engine.resources.Texture;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

View file

@ -1,6 +1,6 @@
package dev.euph.engine.rendering; package dev.euph.engine.rendering;
import dev.euph.engine.rendering.resources.Texture; import dev.euph.engine.resources.Texture;
import lombok.Setter; import lombok.Setter;
@Setter @Setter

View file

@ -1,6 +1,6 @@
package dev.euph.engine.rendering; package dev.euph.engine.rendering;
import dev.euph.engine.rendering.resources.Texture; import dev.euph.engine.resources.Texture;
import lombok.Getter; import lombok.Getter;
import org.joml.Vector2f; import org.joml.Vector2f;
import org.joml.Vector3f; import org.joml.Vector3f;

View file

@ -4,7 +4,7 @@ import dev.euph.engine.core.transform.Transform;
import dev.euph.engine.core.ecs.Component; import dev.euph.engine.core.ecs.Component;
import dev.euph.engine.core.ecs.Requires; import dev.euph.engine.core.ecs.Requires;
import dev.euph.engine.rendering.Material; import dev.euph.engine.rendering.Material;
import dev.euph.engine.rendering.resources.Mesh; import dev.euph.engine.resources.Mesh;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;

View file

@ -1,23 +0,0 @@
package dev.euph.engine.rendering.resources;
import lombok.Getter;
@Getter
public class Texture {
public final static int ALBEDO_INDEX = 0;
public final static int NORMAL_INDEX = 1;
public final static int METALLIC_INDEX = 2;
public final static int ROUGHNESS_INDEX = 3;
public final static int AMBIENT_OCCLUSION_INDEX = 4;
private final int id;
private final int height;
private final int width;
public Texture(int id, int width, int height) {
this.id = id;
this.width = width;
this.height = height;
}
}

View file

@ -4,7 +4,7 @@ import dev.euph.engine.core.window.Window;
import dev.euph.engine.rendering.components.Camera; import dev.euph.engine.rendering.components.Camera;
import org.joml.Matrix4f; import org.joml.Matrix4f;
public class ProjectionMatrix extends Matrix4f { public final class ProjectionMatrix extends Matrix4f {
public ProjectionMatrix(Camera camera) { public ProjectionMatrix(Camera camera) {
super(); super();

View file

@ -6,7 +6,7 @@ import org.joml.Math;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.joml.Vector3f; import org.joml.Vector3f;
public class ViewMatrix extends Matrix4f { public final class ViewMatrix extends Matrix4f {
public ViewMatrix(Camera camera) { public ViewMatrix(Camera camera) {
super(); super();

View file

@ -26,13 +26,14 @@ dependencies {
annotationProcessor("javax.annotation:javax.annotation-api:1.3.2") annotationProcessor("javax.annotation:javax.annotation-api:1.3.2")
implementation("javax.annotation:javax.annotation-api:1.3.2") implementation("javax.annotation:javax.annotation-api:1.3.2")
compileOnly(project(":core", configuration = "shadow"))
implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion")) implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion"))
implementation("org.lwjgl", "lwjgl-stb")
runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives)
implementation("org.lwjgl", "lwjgl-assimp") implementation("org.lwjgl", "lwjgl-assimp")
runtimeOnly("org.lwjgl", "lwjgl-assimp", classifier = lwjglNatives) runtimeOnly("org.lwjgl", "lwjgl-assimp", classifier = lwjglNatives)
shadow(localGroovy()) shadow(localGroovy())
shadow(gradleApi()) shadow(gradleApi())
} }

View file

@ -1,4 +1,4 @@
package dev.euph.engine.rendering.resources; package dev.euph.engine.resources;
public interface Mesh { public interface Mesh {
void bind(); void bind();

View file

@ -1,6 +1,5 @@
package dev.euph.engine.opengl; package dev.euph.engine.resources;
import dev.euph.engine.rendering.resources.Mesh;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.lwjgl.assimp.*; import org.lwjgl.assimp.*;
@ -10,9 +9,19 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class GLMeshLoader{ public class MeshLoader {
public static Mesh loadMesh(URL modelResource) { @FunctionalInterface
public interface meshConstructor {
Mesh create(float[] vertices, int[] indices);
}
@FunctionalInterface
public interface texturedMeshConstructor {
Mesh create(float[] vertices, int[] indices, float[] textureCoordinates);
}
public static Mesh loadMesh(URL modelResource, meshConstructor constructor) {
try { try {
AIMesh mesh = loadMeshIntoAssimp(modelResource); AIMesh mesh = loadMeshIntoAssimp(modelResource);
@ -23,17 +32,16 @@ public class GLMeshLoader{
float[] vertices = listToFloatArray(verticesList); float[] vertices = listToFloatArray(verticesList);
int[] indices = listToIntArray(loadIndices(mesh)); int[] indices = listToIntArray(loadIndices(mesh));
return new GLMesh(vertices, indices); return constructor.create(vertices, indices);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Error loading mesh: " + modelResource.getPath()); throw new RuntimeException("Error loading mesh: " + modelResource.getPath());
} }
} }
public static Mesh loadTexturedMesh(URL modelResource) { public static Mesh loadTexturedMesh(URL modelResource, texturedMeshConstructor constructor) {
try { try {
AIMesh mesh = loadMeshIntoAssimp(modelResource); AIMesh mesh = loadMeshIntoAssimp(modelResource);
List<Float> verticesList = new ArrayList<>(); List<Float> verticesList = new ArrayList<>();
List<Float> textureCoordinatesList = new ArrayList<>(); List<Float> textureCoordinatesList = new ArrayList<>();
@ -45,7 +53,7 @@ public class GLMeshLoader{
int[] indices = listToIntArray(loadIndices(mesh)); int[] indices = listToIntArray(loadIndices(mesh));
float[] texCoords = listToFloatArray(textureCoordinatesList); float[] texCoords = listToFloatArray(textureCoordinatesList);
return new GLTexturedMesh(vertices, indices, texCoords); return constructor.create(vertices, indices, texCoords);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("Error loading mesh: " + modelResource.getPath()); throw new RuntimeException("Error loading mesh: " + modelResource.getPath());

View file

@ -0,0 +1,13 @@
package dev.euph.engine.resources;
public interface Texture {
int ALBEDO_INDEX = 0;
int NORMAL_INDEX = 1;
int METALLIC_INDEX = 2;
int ROUGHNESS_INDEX = 3;
int AMBIENT_OCCLUSION_INDEX = 4;
int getId();
int getHeight();
int getWidth();
}

View file

@ -1,26 +1,28 @@
package dev.euph.engine.core.data; package dev.euph.engine.resources;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
import static org.lwjgl.stb.STBImage.*; import static org.lwjgl.stb.STBImage.*;
public class TextureLoader {
public class ImageLoader { @FunctionalInterface
public record Image(ByteBuffer data, int width, int height) { public interface textureConstructor {
Texture create(int height, int width, ByteBuffer data);
} }
public static Texture loadTexture(URL imageResource, textureConstructor constructor) {
public static Image loadImage(String path) {
IntBuffer width = BufferUtils.createIntBuffer(1); IntBuffer width = BufferUtils.createIntBuffer(1);
IntBuffer height = BufferUtils.createIntBuffer(1); IntBuffer height = BufferUtils.createIntBuffer(1);
IntBuffer channels = BufferUtils.createIntBuffer(1); IntBuffer channels = BufferUtils.createIntBuffer(1);
ByteBuffer imageBuffer = stbi_load(path, width, height, channels, 0);
ByteBuffer imageBuffer = stbi_load(imageResource.getPath(), width, height, channels, 0);
if(imageBuffer == null) { if(imageBuffer == null) {
throw new RuntimeException("Failed to load image: " + stbi_failure_reason()); throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
} }
Image image = new Image(imageBuffer, width.get(0), height.get(0)); Texture image = constructor.create(width.get(0), height.get(0), imageBuffer);
stbi_image_free(imageBuffer); stbi_image_free(imageBuffer);
return image; return image;
} }

View file

@ -70,4 +70,5 @@ dependencies {
implementation(project(":core", configuration = "shadow")) implementation(project(":core", configuration = "shadow"))
implementation(project(":rendering", configuration = "shadow")) implementation(project(":rendering", configuration = "shadow"))
implementation(project(":opengl", configuration = "shadow")) implementation(project(":opengl", configuration = "shadow"))
implementation(project(":resources", configuration = "shadow"))
} }

View file

@ -6,3 +6,5 @@ include(":rendering")
project(":rendering").projectDir = file("../Engine/src/rendering") project(":rendering").projectDir = file("../Engine/src/rendering")
include(":opengl") include(":opengl")
project(":opengl").projectDir = file("../Engine/src/opengl") project(":opengl").projectDir = file("../Engine/src/opengl")
include(":resources")
project(":resources").projectDir = file("../Engine/src/resources")

View file

@ -20,9 +20,6 @@ public class CameraController extends Component {
Transform transform; Transform transform;
private static final float THETA = 0.0001f; private static final float THETA = 0.0001f;
private float movementSpeed = 1.0f;
private float mouseSensitivity = 0.2f;
private Vector2f mouseDelta = new Vector2f(); private Vector2f mouseDelta = new Vector2f();
private Vector2f prevMousePos; private Vector2f prevMousePos;
private Vector2f inputVectorWASD = new Vector2f(); private Vector2f inputVectorWASD = new Vector2f();
@ -98,7 +95,7 @@ public class CameraController extends Component {
// Calculate new position based on movement speed and delta time, only if there's input // Calculate new position based on movement speed and delta time, only if there's input
if (movementDir.length() > 0) { if (movementDir.length() > 0) {
Vector3f newPos = transform.getPosition().add(movementDir.mul(movementSpeed * deltaTime * 60)); Vector3f newPos = transform.getPosition().add(movementDir.mul( deltaTime * 60));
transform.setPosition(newPos); transform.setPosition(newPos);
} }
} }

View file

@ -11,8 +11,10 @@ import dev.euph.engine.rendering.components.Camera;
import dev.euph.engine.rendering.components.MeshRenderer; import dev.euph.engine.rendering.components.MeshRenderer;
import dev.euph.engine.rendering.components.lights.DirectionalLight; import dev.euph.engine.rendering.components.lights.DirectionalLight;
import dev.euph.engine.rendering.components.lights.PointLight; import dev.euph.engine.rendering.components.lights.PointLight;
import dev.euph.engine.rendering.resources.Mesh; import dev.euph.engine.resources.Mesh;
import dev.euph.engine.rendering.resources.Texture; import dev.euph.engine.resources.Texture;
import dev.euph.engine.resources.MeshLoader;
import dev.euph.engine.resources.TextureLoader;
import org.joml.Vector3f; import org.joml.Vector3f;
import java.awt.*; import java.awt.*;
@ -38,9 +40,7 @@ public class Main {
float rotationSpeed = 20f; float rotationSpeed = 20f;
scene.start(); scene.start();
AtomicBoolean running = new AtomicBoolean(true); AtomicBoolean running = new AtomicBoolean(true);
window.addCloseCallback(() -> { window.addCloseCallback(() -> running.set(false));
running.set(false);
});
while (running.get()) { while (running.get()) {
scene.update(window.getTime().getDeltaTime()); scene.update(window.getTime().getDeltaTime());
@ -85,8 +85,8 @@ public class Main {
.addComponent(new PointLight(Color.red, new Vector3f(10))) .addComponent(new PointLight(Color.red, new Vector3f(10)))
.addToScene(scene); .addToScene(scene);
Mesh modelMesh = GLMeshLoader.loadTexturedMesh(Main.class.getResource("/dev/euph/game/models/human_rigged.obj")); Mesh modelMesh = MeshLoader.loadTexturedMesh(Main.class.getResource("/dev/euph/game/models/human_rigged.obj"), GLTexturedMesh::new);
Texture uvTexture = GLTextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png")); Texture uvTexture = TextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png"), GLTexture::new);
Material modelMaterial = new Material(shaderManager.getShader("default")) Material modelMaterial = new Material(shaderManager.getShader("default"))
.setColor(new Color(7, 77, 255, 255)) .setColor(new Color(7, 77, 255, 255))
.setAlbedoTexture(uvTexture); .setAlbedoTexture(uvTexture);

View file

@ -16,7 +16,7 @@ public class OctreeTest {
public static void main(String[] args) { public static void main(String[] args) {
// Create an octree with a center at (0, 0, 0) and a half-size of 20 units // Create an octree with a center at (0, 0, 0) and a half-size of 20 units
Octree<String> octree = new Octree<>(new OctreeNode<String>( Octree<String> octree = new Octree<>(new OctreeNode<>(
8, 8,
new Vector3f(), new Vector3f(),
20 20