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
85 changes: 85 additions & 0 deletions src/main/java/engine/components/StaticGeometry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package engine.components;

import engine.render.Material;
import engine.vbo.VBO;
import engine.vbo.VBOFactory;
import math.Bounds;
import mesh.Mesh3D;
import mesh.util.MeshBoundsCalculator;
import workspace.ui.Graphics;

/**
* The {@code Geometry} class represents a 3D object in a scene with a mesh and material applied to
* it. It is responsible for rendering the mesh and applying the appropriate material to it. The
* class also provides access to the mesh's bounding box, which is useful for purposes like culling,
* spatial partitioning, and debugging.
*
* <p>This class implements the {@link RenderableComponent} interface, indicating that it has a
* render method to be invoked during the render loop of the engine.
*
* @see RenderableComponent
* @see Material
* @see Mesh3D
* @see Bounds
*/
public class StaticGeometry extends AbstractComponent implements RenderableComponent {

/** The bounding box of the mesh used for culling, spatial partitioning, and debugging. */
private Bounds bounds;

private VBO vbo;

/**
* Constructs a {@code StaticGeometry} with the specified mesh and a default material.
*
* @param mesh The {@link Mesh3D} object representing the geometry of the object.
* @throws IllegalArgumentException If the mesh is {@code null}.
*/
public StaticGeometry(Mesh3D mesh) {
this(mesh, Material.DEFAULT_WHITE);
}

/**
* Constructs a {@code StaticGeometry} with the specified mesh and material.
*
* @param mesh The {@link Mesh3D} object representing the geometry of the object.
* @param material The {@link Material} to be applied to the mesh.
* @throws IllegalArgumentException If the mesh or material is {@code null}.
*/
public StaticGeometry(Mesh3D mesh, Material material) {
validate(mesh, material);
this.bounds = MeshBoundsCalculator.calculateBounds(mesh);
this.vbo = VBOFactory.getInstance().create();
this.vbo.create(mesh, material);
}

/**
* Validates the mesh and material to ensure they are not {@code null}.
*
* @param mesh The {@link Mesh3D} object to validate.
* @param material The {@link Material} to validate.
* @throws IllegalArgumentException If the mesh or material is {@code null}.
*/
private void validate(Mesh3D mesh, Material material) {
if (mesh == null) {
throw new IllegalArgumentException("Mesh cannot be null.");
}
if (material == null) {
throw new IllegalArgumentException("Material cannot be null.");
}
}

@Override
public void render(Graphics g) {
g.draw(vbo);
}

@Override
public void update(float tpf) {}

@Override
public void onAttach() {}

@Override
public void onDetach() {}
}
3 changes: 2 additions & 1 deletion src/main/java/engine/demos/landmass/MapGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
public class MapGenerator {

private int chunkSize = 241;
// private int chunkSize = 481;
private int chunkSize = 961;
private int mapWidth = chunkSize;
private int mapHeight = chunkSize;
private int seed = 221;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import engine.application.ApplicationSettings;
import engine.application.BasicApplication;
import engine.components.Geometry;
import engine.components.StaticGeometry;
import engine.components.RoundReticle;
import engine.components.SmoothFlyByCameraControl;
import engine.render.Material;
Expand Down Expand Up @@ -41,7 +41,7 @@ public enum DrawMode {
}

// Configuration fields
private int levelOfDetail = 1; // Level of detail for the terrain mesh (0 - 6)
private int levelOfDetail = 0; // Level of detail for the terrain mesh (0 - 6)
private DrawMode drawMode = DrawMode.COLOR_MAP;
private Scene scene;

Expand Down Expand Up @@ -103,7 +103,7 @@ private void createTerrain() {
terrainMesh.apply(new ScaleModifier(3));
terrainMesh.apply(new CenterAtModifier());

Geometry terrainGeometry = new Geometry(terrainMesh, mapMaterial);
StaticGeometry terrainGeometry = new StaticGeometry(terrainMesh, mapMaterial);
SceneNode terrainNode = new SceneNode();
terrainNode.addComponent(terrainGeometry);
scene.addNode(terrainNode);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/engine/processing/ProcessingApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import engine.input.MouseInput;
import engine.resources.ResourceManager;
import engine.resources.TextureManager;
import engine.vbo.VBOFactory;
import processing.core.PApplet;
import workspace.GraphicsPImpl;
import workspace.ui.Graphics;
Expand All @@ -33,6 +34,8 @@ public void setup() {
Graphics g = new GraphicsPImpl(this);
ResourceManager.getInstance().setImageLoader(new ProcessingImageLoader(this));
TextureManager.getInstance().setTextureLoader(new ProcessingTextureLoader(this));
VBOFactory.getInstance()
.setVBOCreationStrategy(new ProcessingVBOCreationStrategy(getGraphics()));
container.setGraphics(g);
getSurface().setTitle(settings.getTitle());
setupInput();
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/engine/vbo/VBO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package engine.vbo;

import engine.render.Material;
import mesh.Mesh3D;

public interface VBO {
void create(float[] vertices, int[] indices);

void create(Mesh3D mesh, Material material);

void bind();

void unbind();

void updateData(float[] newData);

void delete();

int getVertexCount();

int getFaceCount();
}
6 changes: 6 additions & 0 deletions src/main/java/engine/vbo/VBOCreationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package engine.vbo;

public interface VBOCreationStrategy {

public VBO create();
}
28 changes: 28 additions & 0 deletions src/main/java/engine/vbo/VBOFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package engine.vbo;

public class VBOFactory {

private static VBOFactory instance;
private static VBOCreationStrategy strategy;

private VBOFactory() {}

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

public void setVBOCreationStrategy(VBOCreationStrategy strategy) {
VBOFactory.strategy = strategy;
}

public VBO create() {
if (strategy == null) {
System.err.println("No VBOCreationStrategy set!");
return null;
}
return strategy.create();
}
}
11 changes: 11 additions & 0 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.processing.ProcessingTexture;
import engine.processing.VBOProcessing;
import engine.render.Material;
import engine.resources.FilterMode;
import engine.resources.Image;
Expand All @@ -13,6 +14,7 @@
import engine.scene.camera.Camera;
import engine.scene.light.Light;
import engine.scene.light.LightRenderer;
import engine.vbo.VBO;
import math.Matrix4f;
import math.Vector2f;
import math.Vector3f;
Expand Down Expand Up @@ -104,6 +106,15 @@ public void drawFaces(Mesh3D mesh) {
drawMeshFaces(mesh);
}

@Override
public void draw(VBO vbo) {
faceCount += vbo.getFaceCount();
vertexCount += vbo.getVertexCount();
applyTexture();
VBOProcessing vboProcessing = (VBOProcessing) vbo;
vboProcessing.draw(g);
}

@Override
public void renderInstances(Mesh3D mesh, List<Matrix4f> instanceTransforms) {
if (mesh.getFaces().isEmpty() || mesh.getVertices().isEmpty()) {
Expand Down
Loading