Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/engine/application/BasicApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void update() {
}

@Override
public void render(Graphics g) {
public void render(Graphics g) {
if (activeScene != null) {
activeScene.render(g);
}
Expand Down Expand Up @@ -170,4 +170,8 @@ public Scene getActiveScene() {
public void setActiveScene(Scene activeScene) {
this.activeScene = activeScene;
}

public void setDisplayInfoText(boolean displayInfoText) {
this.displayInfoText = displayInfoText;
}
}
16 changes: 16 additions & 0 deletions src/main/java/engine/resources/Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package engine.resources;

public class Image {

private final Object backendImage;

public Image(Object backendImage) {
this.backendImage = backendImage;
}

public Object getBackendImage() {
return backendImage;
}

// Add methods to abstract common operations like `getWidth()`, `getHeight()`, etc.
}
6 changes: 6 additions & 0 deletions src/main/java/engine/resources/ImageLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package engine.resources;

public interface ImageLoader {

Object loadImage(String path); // Returns the backend-specific image object
}
29 changes: 29 additions & 0 deletions src/main/java/engine/resources/ImageResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package engine.resources;

public class ImageResource implements Resource {

private Object image;

private String path;

@Override
public void load(String path) {
this.image = ResourceManager.getInstance().loadImage(path);
this.path = path;
}

@Override
public void unload() {
ResourceManager.getInstance().unloadImage(path);
this.image = null;
}

@Override
public boolean isLoaded() {
return image != null;
}

public Object getImage() {
return image;
}
}
10 changes: 10 additions & 0 deletions src/main/java/engine/resources/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package engine.resources;

public interface Resource {

void load(String path);

void unload();

boolean isLoaded();
}
47 changes: 47 additions & 0 deletions src/main/java/engine/resources/ResourceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package engine.resources;

import java.util.HashMap;
import java.util.Map;

public class ResourceManager {

private static ResourceManager instance;

private ImageLoader imageLoader;

private final Map<String, Image> resourceCache = new HashMap<>();

private ResourceManager() {}

public static ResourceManager getInstance() {
if (instance == null) {
instance = new ResourceManager();
}
return instance;
}

public void setImageLoader(ImageLoader loader) {
this.imageLoader = loader;
}

public Image loadImage(String path) {
if (resourceCache.containsKey(path)) {
return resourceCache.get(path); // Return cached resource
}

if (imageLoader == null) {
throw new IllegalStateException("ImageLoader is not set!");
}

Object obj = imageLoader.loadImage(path);
Image image = new Image(obj);

resourceCache.put(path, image);

return image;
}

public void unloadImage(String path) {
resourceCache.remove(path); // Optionally handle cleanup for backend-specific resources
}
}
52 changes: 47 additions & 5 deletions src/main/java/workspace/GraphicsPImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import engine.processing.LightGizmoRenderer;
import engine.processing.LightRendererImpl;
import engine.render.Material;
import engine.resources.Image;
import engine.scene.camera.Camera;
import engine.scene.light.Light;
import engine.scene.light.LightRenderer;
Expand All @@ -14,6 +15,8 @@
import mesh.Mesh3D;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import processing.opengl.PGraphicsOpenGL;
import processing.opengl.PShader;
import workspace.render.Mesh3DRenderer;
import workspace.ui.Color;
Expand All @@ -35,6 +38,10 @@ public class GraphicsPImpl implements Graphics {

private LightGizmoRenderer lightGizmoRenderer;

public static int faceCount = 0;

public static int vertexCount = 0;

@Override
public void setAmbientColor(math.Color ambientColor) {
this.ambientColor = ambientColor;
Expand Down Expand Up @@ -66,6 +73,8 @@ public GraphicsPImpl(PApplet p) {

@Override
public void fillFaces(Mesh3D mesh) {
faceCount += mesh.faces.size();
vertexCount += mesh.vertices.size();
if (wireframeMode) {
g.noFill();
stroke();
Expand Down Expand Up @@ -342,9 +351,9 @@ public void rotateZ(float angle) {

@Override
public void rotate(float rx, float ry, float rz) {
g.rotateX(rx);
g.rotateY(ry);
g.rotateZ(rz);
g.rotateX(rx);
g.rotateY(ry);
g.rotateZ(rz);
}

public void camera() {
Expand Down Expand Up @@ -460,15 +469,48 @@ public void applyCamera(Camera camera) {
if (camera == null) {
throw new IllegalArgumentException("Camera instance cannot be null.");
}


// g.resetMatrix();
// Matrix4f m = camera.getViewProjectionMatrix();
//
//
// Vector3f target = camera.getTarget();
// Vector3f eye = camera.getTransform().getPosition();
// Matrix4f look = Matrix4f.lookAt(eye, target, new Vector3f(0, 1, 0));
//
// m = m.multiply(look);

// g.getMatrix().set(m.getValues());

float fov = camera.getFieldOfView();
float aspect = camera.getAspectRatio();
float near = camera.getNearPlane();
float far = camera.getFarPlane();
g.perspective(fov, aspect, near, far);
// g.perspective(fov, aspect, near, far);

Matrix4f m = camera.getProjectionMatrix();
((PGraphicsOpenGL) g).projection.set(m.getValues());

Vector3f target = camera.getTarget();
Vector3f eye = camera.getTransform().getPosition();
g.camera(eye.x, eye.y, eye.z, target.x, target.y, target.z, 0, 1, 0);
}

@Override
public void drawImage(Image image, float x, float y) {
if (image.getBackendImage() instanceof PImage) {
g.image((PImage) image.getBackendImage(), x, y);
} else {
throw new IllegalArgumentException("Unsupported image backend.");
}
}

@Override
public void drawImage(Image image, float x, float y, float width, float height) {
if (image.getBackendImage() instanceof PImage) {
g.image((PImage) image.getBackendImage(), x, y, width, height);
} else {
throw new IllegalArgumentException("Unsupported image backend.");
}
}
}
Loading
Loading