diff --git a/Engine/settings.gradle.kts b/Engine/settings.gradle.kts index 7d8723a..7c29e34 100644 --- a/Engine/settings.gradle.kts +++ b/Engine/settings.gradle.kts @@ -4,4 +4,6 @@ project(":core").projectDir = file("src/core") include(":opengl") project(":opengl").projectDir = file("src/opengl") include(":rendering") -project(":rendering").projectDir = file("src/rendering") \ No newline at end of file +project(":rendering").projectDir = file("src/rendering") +include(":resources") +project(":resources").projectDir = file("src/resources") \ No newline at end of file diff --git a/Engine/src/core/build.gradle.kts b/Engine/src/core/build.gradle.kts index 94ea372..aa5d5a0 100644 --- a/Engine/src/core/build.gradle.kts +++ b/Engine/src/core/build.gradle.kts @@ -32,9 +32,6 @@ dependencies { implementation("org.lwjgl", "lwjgl-glfw") 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-primitives", jomlPrimitivesVersion) diff --git a/Engine/src/opengl/build.gradle.kts b/Engine/src/opengl/build.gradle.kts index 75357db..a7d76d7 100644 --- a/Engine/src/opengl/build.gradle.kts +++ b/Engine/src/opengl/build.gradle.kts @@ -27,6 +27,7 @@ dependencies { implementation("javax.annotation:javax.annotation-api:1.3.2") compileOnly(project(":core", configuration = "shadow")) + compileOnly(project(":resources", configuration = "shadow")) compileOnly(project(":rendering", configuration = "shadow")) implementation("org.lwjgl", "lwjgl-assimp") diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLMesh.java b/Engine/src/opengl/java/dev/euph/engine/opengl/GLMesh.java index d24f1c9..d2dfdd8 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLMesh.java +++ b/Engine/src/opengl/java/dev/euph/engine/opengl/GLMesh.java @@ -1,6 +1,6 @@ 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; diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLRenderer.java b/Engine/src/opengl/java/dev/euph/engine/opengl/GLRenderer.java index b39b017..c3e62ad 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLRenderer.java +++ b/Engine/src/opengl/java/dev/euph/engine/opengl/GLRenderer.java @@ -10,7 +10,7 @@ import dev.euph.engine.rendering.Shader; import dev.euph.engine.rendering.ShaderManager; import dev.euph.engine.rendering.components.Camera; 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.ViewMatrix; import lombok.Getter; diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLShader.java b/Engine/src/opengl/java/dev/euph/engine/opengl/GLShader.java index 91a0706..b6715b5 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLShader.java +++ b/Engine/src/opengl/java/dev/euph/engine/opengl/GLShader.java @@ -1,7 +1,7 @@ package dev.euph.engine.opengl; 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.Vector2f; import org.joml.Vector3f; diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLTextureLoader.java b/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexture.java similarity index 53% rename from Engine/src/opengl/java/dev/euph/engine/opengl/GLTextureLoader.java rename to Engine/src/opengl/java/dev/euph/engine/opengl/GLTexture.java index eed8810..0725319 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLTextureLoader.java +++ b/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexture.java @@ -1,34 +1,40 @@ package dev.euph.engine.opengl; -import dev.euph.engine.core.data.ImageLoader; -import dev.euph.engine.rendering.resources.Texture; +import dev.euph.engine.resources.Texture; +import lombok.Getter; -import java.net.URL; +import java.nio.ByteBuffer; import static dev.euph.engine.opengl.GL.*; -public class GLTextureLoader { - public static Texture loadTexture(URL imageResource) { - int textureID = GL.glGenTextures(); - glBindTexture(GL_TEXTURE_2D, textureID); +@Getter +public class GLTexture implements Texture { + private final int id; + 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_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - ImageLoader.Image image = ImageLoader.loadImage(imageResource.getPath()); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, - image.width(), image.height(), + width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, - image.data() + data ); glGenerateMipmap(GL_TEXTURE_2D); - return new Texture(textureID, image.width(), image.height()); + this.width = width; + this.height = height; } + } diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexturedMesh.java b/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexturedMesh.java index 6ef0e21..f67e6ce 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexturedMesh.java +++ b/Engine/src/opengl/java/dev/euph/engine/opengl/GLTexturedMesh.java @@ -1,6 +1,6 @@ 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; diff --git a/Engine/src/rendering/build.gradle.kts b/Engine/src/rendering/build.gradle.kts index 984f68b..44f0618 100644 --- a/Engine/src/rendering/build.gradle.kts +++ b/Engine/src/rendering/build.gradle.kts @@ -27,6 +27,7 @@ dependencies { implementation("javax.annotation:javax.annotation-api:1.3.2") compileOnly(project(":core", configuration = "shadow")) + compileOnly(project(":resources", configuration = "shadow")) implementation(platform("org.lwjgl:lwjgl-bom:$lwjglVersion")) shadow(localGroovy()) diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/Material.java b/Engine/src/rendering/java/dev/euph/engine/rendering/Material.java index 38879c3..8bf4de5 100644 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/Material.java +++ b/Engine/src/rendering/java/dev/euph/engine/rendering/Material.java @@ -1,6 +1,6 @@ package dev.euph.engine.rendering; -import dev.euph.engine.rendering.resources.Texture; +import dev.euph.engine.resources.Texture; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/PBRMaterial.java b/Engine/src/rendering/java/dev/euph/engine/rendering/PBRMaterial.java index d731855..efef167 100644 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/PBRMaterial.java +++ b/Engine/src/rendering/java/dev/euph/engine/rendering/PBRMaterial.java @@ -1,6 +1,6 @@ package dev.euph.engine.rendering; -import dev.euph.engine.rendering.resources.Texture; +import dev.euph.engine.resources.Texture; import lombok.Setter; @Setter diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/Shader.java b/Engine/src/rendering/java/dev/euph/engine/rendering/Shader.java index 0950def..fdbc218 100644 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/Shader.java +++ b/Engine/src/rendering/java/dev/euph/engine/rendering/Shader.java @@ -1,6 +1,6 @@ package dev.euph.engine.rendering; -import dev.euph.engine.rendering.resources.Texture; +import dev.euph.engine.resources.Texture; import lombok.Getter; import org.joml.Vector2f; import org.joml.Vector3f; diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/components/MeshRenderer.java b/Engine/src/rendering/java/dev/euph/engine/rendering/components/MeshRenderer.java index 4b411e0..31c3806 100644 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/components/MeshRenderer.java +++ b/Engine/src/rendering/java/dev/euph/engine/rendering/components/MeshRenderer.java @@ -4,7 +4,7 @@ import dev.euph.engine.core.transform.Transform; import dev.euph.engine.core.ecs.Component; import dev.euph.engine.core.ecs.Requires; import dev.euph.engine.rendering.Material; -import dev.euph.engine.rendering.resources.Mesh; +import dev.euph.engine.resources.Mesh; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/resources/Texture.java b/Engine/src/rendering/java/dev/euph/engine/rendering/resources/Texture.java deleted file mode 100644 index b224bcc..0000000 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/resources/Texture.java +++ /dev/null @@ -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; - } -} diff --git a/Engine/src/resources/build.gradle.kts b/Engine/src/resources/build.gradle.kts index e17f1e7..981f74b 100644 --- a/Engine/src/resources/build.gradle.kts +++ b/Engine/src/resources/build.gradle.kts @@ -26,13 +26,14 @@ dependencies { annotationProcessor("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("org.lwjgl", "lwjgl-stb") + runtimeOnly("org.lwjgl", "lwjgl-stb", classifier = lwjglNatives) + implementation("org.lwjgl", "lwjgl-assimp") runtimeOnly("org.lwjgl", "lwjgl-assimp", classifier = lwjglNatives) - shadow(localGroovy()) shadow(gradleApi()) } diff --git a/Engine/src/rendering/java/dev/euph/engine/rendering/resources/Mesh.java b/Engine/src/resources/java/dev/euph/engine/resources/Mesh.java similarity index 66% rename from Engine/src/rendering/java/dev/euph/engine/rendering/resources/Mesh.java rename to Engine/src/resources/java/dev/euph/engine/resources/Mesh.java index 28da075..d2086a9 100644 --- a/Engine/src/rendering/java/dev/euph/engine/rendering/resources/Mesh.java +++ b/Engine/src/resources/java/dev/euph/engine/resources/Mesh.java @@ -1,4 +1,4 @@ -package dev.euph.engine.rendering.resources; +package dev.euph.engine.resources; public interface Mesh { void bind(); diff --git a/Engine/src/opengl/java/dev/euph/engine/opengl/GLMeshLoader.java b/Engine/src/resources/java/dev/euph/engine/resources/MeshLoader.java similarity index 84% rename from Engine/src/opengl/java/dev/euph/engine/opengl/GLMeshLoader.java rename to Engine/src/resources/java/dev/euph/engine/resources/MeshLoader.java index 2af3fe2..db1751d 100644 --- a/Engine/src/opengl/java/dev/euph/engine/opengl/GLMeshLoader.java +++ b/Engine/src/resources/java/dev/euph/engine/resources/MeshLoader.java @@ -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.lwjgl.assimp.*; @@ -10,9 +9,19 @@ import java.util.ArrayList; import java.util.List; 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 { AIMesh mesh = loadMeshIntoAssimp(modelResource); @@ -23,17 +32,16 @@ public class GLMeshLoader{ float[] vertices = listToFloatArray(verticesList); int[] indices = listToIntArray(loadIndices(mesh)); - return new GLMesh(vertices, indices); + return constructor.create(vertices, indices); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Error loading mesh: " + modelResource.getPath()); } } - public static Mesh loadTexturedMesh(URL modelResource) { + public static Mesh loadTexturedMesh(URL modelResource, texturedMeshConstructor constructor) { try { AIMesh mesh = loadMeshIntoAssimp(modelResource); - List verticesList = new ArrayList<>(); List textureCoordinatesList = new ArrayList<>(); @@ -45,7 +53,7 @@ public class GLMeshLoader{ int[] indices = listToIntArray(loadIndices(mesh)); float[] texCoords = listToFloatArray(textureCoordinatesList); - return new GLTexturedMesh(vertices, indices, texCoords); + return constructor.create(vertices, indices, texCoords); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("Error loading mesh: " + modelResource.getPath()); diff --git a/Engine/src/resources/java/dev/euph/engine/resources/Texture.java b/Engine/src/resources/java/dev/euph/engine/resources/Texture.java new file mode 100644 index 0000000..38e4ffb --- /dev/null +++ b/Engine/src/resources/java/dev/euph/engine/resources/Texture.java @@ -0,0 +1,13 @@ +package dev.euph.engine.resources; + +public interface 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; + + public int getId(); + public int getHeight(); + public int getWidth(); +} diff --git a/Engine/src/core/java/dev/euph/engine/core/data/ImageLoader.java b/Engine/src/resources/java/dev/euph/engine/resources/TextureLoader.java similarity index 51% rename from Engine/src/core/java/dev/euph/engine/core/data/ImageLoader.java rename to Engine/src/resources/java/dev/euph/engine/resources/TextureLoader.java index f463f75..981f600 100644 --- a/Engine/src/core/java/dev/euph/engine/core/data/ImageLoader.java +++ b/Engine/src/resources/java/dev/euph/engine/resources/TextureLoader.java @@ -1,26 +1,28 @@ -package dev.euph.engine.core.data; +package dev.euph.engine.resources; import org.lwjgl.BufferUtils; +import java.net.URL; import java.nio.ByteBuffer; import java.nio.IntBuffer; import static org.lwjgl.stb.STBImage.*; - -public class ImageLoader { - public record Image(ByteBuffer data, int width, int height) { +public class TextureLoader { + @FunctionalInterface + public interface textureConstructor { + Texture create(int height, int width, ByteBuffer data); } - - public static Image loadImage(String path) { + public static Texture loadTexture(URL imageResource, textureConstructor constructor) { IntBuffer width = BufferUtils.createIntBuffer(1); IntBuffer height = 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) { 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); return image; } diff --git a/TestGame/build.gradle.kts b/TestGame/build.gradle.kts index eb3fb82..d3c728b 100644 --- a/TestGame/build.gradle.kts +++ b/TestGame/build.gradle.kts @@ -70,4 +70,5 @@ dependencies { implementation(project(":core", configuration = "shadow")) implementation(project(":rendering", configuration = "shadow")) implementation(project(":opengl", configuration = "shadow")) + implementation(project(":resources", configuration = "shadow")) } \ No newline at end of file diff --git a/TestGame/settings.gradle.kts b/TestGame/settings.gradle.kts index 94f30db..3ef7147 100644 --- a/TestGame/settings.gradle.kts +++ b/TestGame/settings.gradle.kts @@ -5,4 +5,6 @@ project(":core").projectDir = file("../Engine/src/core") include(":rendering") project(":rendering").projectDir = file("../Engine/src/rendering") include(":opengl") -project(":opengl").projectDir = file("../Engine/src/opengl") \ No newline at end of file +project(":opengl").projectDir = file("../Engine/src/opengl") +include(":resources") +project(":resources").projectDir = file("../Engine/src/resources") \ No newline at end of file diff --git a/TestGame/src/dev/euph/game/Main.java b/TestGame/src/dev/euph/game/Main.java index 02e0bb5..df8fcb5 100644 --- a/TestGame/src/dev/euph/game/Main.java +++ b/TestGame/src/dev/euph/game/Main.java @@ -11,8 +11,10 @@ import dev.euph.engine.rendering.components.Camera; import dev.euph.engine.rendering.components.MeshRenderer; import dev.euph.engine.rendering.components.lights.DirectionalLight; import dev.euph.engine.rendering.components.lights.PointLight; -import dev.euph.engine.rendering.resources.Mesh; -import dev.euph.engine.rendering.resources.Texture; +import dev.euph.engine.resources.Mesh; +import dev.euph.engine.resources.Texture; +import dev.euph.engine.resources.MeshLoader; +import dev.euph.engine.resources.TextureLoader; import org.joml.Vector3f; import java.awt.*; @@ -85,8 +87,8 @@ public class Main { .addComponent(new PointLight(Color.red, new Vector3f(10))) .addToScene(scene); - Mesh modelMesh = GLMeshLoader.loadTexturedMesh(Main.class.getResource("/dev/euph/game/models/human_rigged.obj")); - Texture uvTexture = GLTextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png")); + Mesh modelMesh = MeshLoader.loadTexturedMesh(Main.class.getResource("/dev/euph/game/models/human_rigged.obj"), GLTexturedMesh::new); + Texture uvTexture = TextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png"), GLTexture::new); Material modelMaterial = new Material(shaderManager.getShader("default")) .setColor(new Color(7, 77, 255, 255)) .setAlbedoTexture(uvTexture);