Refactoring

This commit is contained in:
Snoweuph 2024-08-28 22:19:35 +02:00
parent e7336cbf82
commit 9516001bb3
Signed by: Snoweuph
GPG key ID: 0021ADF278B2F49B
13 changed files with 33 additions and 19 deletions

View file

@ -4,4 +4,6 @@ project(":core").projectDir = file("src/core")
include(":opengl") 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

@ -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

@ -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,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

@ -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

@ -26,13 +26,11 @@ 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-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

@ -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

@ -5,4 +5,6 @@ project(":core").projectDir = file("../Engine/src/core")
include(":rendering") 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

@ -11,8 +11,9 @@ 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.rendering.resources.Texture;
import dev.euph.engine.resources.MeshLoader;
import org.joml.Vector3f; import org.joml.Vector3f;
import java.awt.*; import java.awt.*;
@ -85,7 +86,7 @@ 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 = GLTextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png"));
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))