Refactoring

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

View file

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

View file

@ -37,17 +37,12 @@ public class Entity {
}
public <T extends Component> T getComponent(Class<T> componentClass) {
for (Component c : components) {
if (componentClass.isAssignableFrom(c.getClass())) {
try {
return componentClass.cast(c);
} catch (ClassCastException e) {
e.printStackTrace();
assert false : "Error: Casting component.";
}
}
}
return null;
return components
.stream()
.filter(c -> componentClass.isAssignableFrom(c.getClass()))
.findFirst()
.map(componentClass::cast)
.orElse(null);
}
public <T extends Component> void removeComponent(Class<T> componentClass) {

View file

@ -94,7 +94,10 @@ public class Scene {
return filteredEntities;
}
public final Set<Entity> getByName(String name) {
public final Entity getByName(String name) {
return entitiesByName.get(name).stream().findFirst().orElse(null);
}
public final Set<Entity> getAllByName(String name) {
return entitiesByName.get(name);
}

View file

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

View file

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

View file

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

View file

@ -12,7 +12,7 @@ import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;
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;
protected static int getVao() {
@ -35,7 +35,7 @@ public class GLMeshHelper {
buffer.put(vertices).flip();
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);
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

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.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;

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 org.joml.Matrix4f;
public class ProjectionMatrix extends Matrix4f {
public final class ProjectionMatrix extends Matrix4f {
public ProjectionMatrix(Camera camera) {
super();

View file

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

View file

@ -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())
}

View file

@ -1,4 +1,4 @@
package dev.euph.engine.rendering.resources;
package dev.euph.engine.resources;
public interface Mesh {
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.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<Float> verticesList = new ArrayList<>();
List<Float> 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());

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 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;
}

View file

@ -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"))
}

View file

@ -6,3 +6,5 @@ include(":rendering")
project(":rendering").projectDir = file("../Engine/src/rendering")
include(":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;
private static final float THETA = 0.0001f;
private float movementSpeed = 1.0f;
private float mouseSensitivity = 0.2f;
private Vector2f mouseDelta = new Vector2f();
private Vector2f prevMousePos;
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
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);
}
}

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.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.*;
@ -21,14 +23,24 @@ import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;
public class Main {
private final static String DEFAULT_SHADER_NAME = "default";
public static void main(String[] args) throws IOException {
URL vertexShader = Main.class.getResource("/dev/euph/game/shader/default.vs.glsl");
URL fragmentShader = Main.class.getResource("/dev/euph/game/shader/default.fs.glsl");
URL testTexture = Main.class.getResource("/dev/euph/game/textures/uv.png");
URL testModel = Main.class.getResource("/dev/euph/game/models/human_rigged.obj");
assert vertexShader != null;
assert fragmentShader != null;
Window window = new GLWindow(1200, 720, "Test Window");
window.setVsync(1);
ShaderManager shaderManager = loadShaders();
Scene scene = loadScene(window, shaderManager);
Entity camera = scene.getByName("Camera").stream().findFirst().orElse(null);
Entity Model = scene.getByName("Model").stream().findFirst().orElse(null);
ShaderManager shaderManager = loadShaders(vertexShader, fragmentShader);
Scene scene = loadScene(window, shaderManager, testTexture, testModel);
Entity camera = scene.getByName("Camera");
Entity Model = scene.getByName("Model");
GLRenderer forwardRenderer = new GLRenderer(scene, shaderManager);
forwardRenderer.activeCamera = camera.getComponent(Camera.class);
@ -38,9 +50,7 @@ public class Main {
float rotationSpeed = 20f;
scene.start();
AtomicBoolean running = new AtomicBoolean(true);
window.addCloseCallback(() -> {
running.set(false);
});
window.addCloseCallback(() -> running.set(false));
while (running.get()) {
scene.update(window.getTime().getDeltaTime());
@ -55,21 +65,17 @@ public class Main {
window.destroy();
}
private static ShaderManager loadShaders() throws IOException {
private static ShaderManager loadShaders(URL vertexShader, URL fragmentShader) throws IOException {
ShaderManager shaderManager = new ShaderManager();
URL fragmentShader = Main.class.getResource("/dev/euph/game/shader/default.fs.glsl");
URL vertexShader = Main.class.getResource("/dev/euph/game/shader/default.vs.glsl");
assert vertexShader != null;
assert fragmentShader != null;
shaderManager.addShader(new GLShader(
"default",
Main.class.getResourceAsStream("/dev/euph/game/shader/default.vs.glsl"),
Main.class.getResourceAsStream("/dev/euph/game/shader/default.fs.glsl")
DEFAULT_SHADER_NAME,
vertexShader,
fragmentShader
));
return shaderManager;
}
private static Scene loadScene(Window window, ShaderManager shaderManager) {
private static Scene loadScene(Window window, ShaderManager shaderManager, URL testTexture, URL testModel) {
Scene scene = new Scene();
Entity cameraEntity = new Entity("Camera")
.addComponent(Transform.FromPosition(0, 0, 3))
@ -85,9 +91,9 @@ 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"));
Material modelMaterial = new Material(shaderManager.getShader("default"))
Texture uvTexture = TextureLoader.loadTexture(testTexture, GLTexture::new);
Mesh modelMesh = MeshLoader.loadTexturedMesh(testModel, GLTexturedMesh::new);
Material modelMaterial = new Material(shaderManager.getShader(DEFAULT_SHADER_NAME))
.setColor(new Color(7, 77, 255, 255))
.setAlbedoTexture(uvTexture);

View file

@ -16,7 +16,7 @@ public class OctreeTest {
public static void main(String[] args) {
// 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,
new Vector3f(),
20