Refactoring
This commit is contained in:
parent
e7336cbf82
commit
805671ebb1
30 changed files with 120 additions and 111 deletions
|
@ -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")
|
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -37,17 +37,12 @@ public class Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Component> T getComponent(Class<T> componentClass) {
|
public <T extends Component> T getComponent(Class<T> componentClass) {
|
||||||
for (Component c : components) {
|
return components
|
||||||
if (componentClass.isAssignableFrom(c.getClass())) {
|
.stream()
|
||||||
try {
|
.filter(c -> componentClass.isAssignableFrom(c.getClass()))
|
||||||
return componentClass.cast(c);
|
.findFirst()
|
||||||
} catch (ClassCastException e) {
|
.map(componentClass::cast)
|
||||||
e.printStackTrace();
|
.orElse(null);
|
||||||
assert false : "Error: Casting component.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Component> void removeComponent(Class<T> componentClass) {
|
public <T extends Component> void removeComponent(Class<T> componentClass) {
|
||||||
|
|
|
@ -94,7 +94,10 @@ public class Scene {
|
||||||
return filteredEntities;
|
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);
|
return entitiesByName.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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())
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
||||||
|
|
|
@ -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();
|
||||||
|
|
|
@ -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())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
@ -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());
|
|
@ -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();
|
||||||
|
}
|
|
@ -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;
|
||||||
}
|
}
|
|
@ -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"))
|
||||||
}
|
}
|
|
@ -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")
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.*;
|
||||||
|
@ -21,14 +23,24 @@ import java.net.URL;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
private final static String DEFAULT_SHADER_NAME = "default";
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
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 window = new GLWindow(1200, 720, "Test Window");
|
||||||
window.setVsync(1);
|
window.setVsync(1);
|
||||||
|
|
||||||
ShaderManager shaderManager = loadShaders();
|
ShaderManager shaderManager = loadShaders(vertexShader, fragmentShader);
|
||||||
Scene scene = loadScene(window, shaderManager);
|
Scene scene = loadScene(window, shaderManager, testTexture, testModel);
|
||||||
Entity camera = scene.getByName("Camera").stream().findFirst().orElse(null);
|
Entity camera = scene.getByName("Camera");
|
||||||
Entity Model = scene.getByName("Model").stream().findFirst().orElse(null);
|
Entity Model = scene.getByName("Model");
|
||||||
|
|
||||||
GLRenderer forwardRenderer = new GLRenderer(scene, shaderManager);
|
GLRenderer forwardRenderer = new GLRenderer(scene, shaderManager);
|
||||||
forwardRenderer.activeCamera = camera.getComponent(Camera.class);
|
forwardRenderer.activeCamera = camera.getComponent(Camera.class);
|
||||||
|
@ -38,9 +50,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());
|
||||||
|
|
||||||
|
@ -55,21 +65,17 @@ public class Main {
|
||||||
window.destroy();
|
window.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ShaderManager loadShaders() throws IOException {
|
private static ShaderManager loadShaders(URL vertexShader, URL fragmentShader) throws IOException {
|
||||||
ShaderManager shaderManager = new ShaderManager();
|
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(
|
shaderManager.addShader(new GLShader(
|
||||||
"default",
|
DEFAULT_SHADER_NAME,
|
||||||
Main.class.getResourceAsStream("/dev/euph/game/shader/default.vs.glsl"),
|
vertexShader,
|
||||||
Main.class.getResourceAsStream("/dev/euph/game/shader/default.fs.glsl")
|
fragmentShader
|
||||||
));
|
));
|
||||||
return shaderManager;
|
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();
|
Scene scene = new Scene();
|
||||||
Entity cameraEntity = new Entity("Camera")
|
Entity cameraEntity = new Entity("Camera")
|
||||||
.addComponent(Transform.FromPosition(0, 0, 3))
|
.addComponent(Transform.FromPosition(0, 0, 3))
|
||||||
|
@ -85,9 +91,9 @@ 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"));
|
Texture uvTexture = TextureLoader.loadTexture(testTexture, GLTexture::new);
|
||||||
Texture uvTexture = GLTextureLoader.loadTexture(Main.class.getResource("/dev/euph/game/textures/uv.png"));
|
Mesh modelMesh = MeshLoader.loadTexturedMesh(testModel, GLTexturedMesh::new);
|
||||||
Material modelMaterial = new Material(shaderManager.getShader("default"))
|
Material modelMaterial = new Material(shaderManager.getShader(DEFAULT_SHADER_NAME))
|
||||||
.setColor(new Color(7, 77, 255, 255))
|
.setColor(new Color(7, 77, 255, 255))
|
||||||
.setAlbedoTexture(uvTexture);
|
.setAlbedoTexture(uvTexture);
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue