Skip to content

Commit dd3415a

Browse files
committed
Started experimental implementation of static meshes. Increased the
chunk size of the procedural terrain to stress test the current implementation.
1 parent 8212d8b commit dd3415a

File tree

8 files changed

+160
-4
lines changed

8 files changed

+160
-4
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package engine.components;
2+
3+
import engine.render.Material;
4+
import engine.vbo.VBO;
5+
import engine.vbo.VBOFactory;
6+
import math.Bounds;
7+
import mesh.Mesh3D;
8+
import mesh.util.MeshBoundsCalculator;
9+
import workspace.ui.Graphics;
10+
11+
/**
12+
* The {@code Geometry} class represents a 3D object in a scene with a mesh and material applied to
13+
* it. It is responsible for rendering the mesh and applying the appropriate material to it. The
14+
* class also provides access to the mesh's bounding box, which is useful for purposes like culling,
15+
* spatial partitioning, and debugging.
16+
*
17+
* <p>This class implements the {@link RenderableComponent} interface, indicating that it has a
18+
* render method to be invoked during the render loop of the engine.
19+
*
20+
* @see RenderableComponent
21+
* @see Material
22+
* @see Mesh3D
23+
* @see Bounds
24+
*/
25+
public class StaticGeometry extends AbstractComponent implements RenderableComponent {
26+
27+
/** The bounding box of the mesh used for culling, spatial partitioning, and debugging. */
28+
private Bounds bounds;
29+
30+
private VBO vbo;
31+
32+
/**
33+
* Constructs a {@code StaticGeometry} with the specified mesh and a default material.
34+
*
35+
* @param mesh The {@link Mesh3D} object representing the geometry of the object.
36+
* @throws IllegalArgumentException If the mesh is {@code null}.
37+
*/
38+
public StaticGeometry(Mesh3D mesh) {
39+
this(mesh, Material.DEFAULT_WHITE);
40+
}
41+
42+
/**
43+
* Constructs a {@code StaticGeometry} with the specified mesh and material.
44+
*
45+
* @param mesh The {@link Mesh3D} object representing the geometry of the object.
46+
* @param material The {@link Material} to be applied to the mesh.
47+
* @throws IllegalArgumentException If the mesh or material is {@code null}.
48+
*/
49+
public StaticGeometry(Mesh3D mesh, Material material) {
50+
validate(mesh, material);
51+
this.bounds = MeshBoundsCalculator.calculateBounds(mesh);
52+
this.vbo = VBOFactory.getInstance().create();
53+
this.vbo.create(mesh, material);
54+
}
55+
56+
/**
57+
* Validates the mesh and material to ensure they are not {@code null}.
58+
*
59+
* @param mesh The {@link Mesh3D} object to validate.
60+
* @param material The {@link Material} to validate.
61+
* @throws IllegalArgumentException If the mesh or material is {@code null}.
62+
*/
63+
private void validate(Mesh3D mesh, Material material) {
64+
if (mesh == null) {
65+
throw new IllegalArgumentException("Mesh cannot be null.");
66+
}
67+
if (material == null) {
68+
throw new IllegalArgumentException("Material cannot be null.");
69+
}
70+
}
71+
72+
@Override
73+
public void render(Graphics g) {
74+
g.draw(vbo);
75+
}
76+
77+
@Override
78+
public void update(float tpf) {}
79+
80+
@Override
81+
public void onAttach() {}
82+
83+
@Override
84+
public void onDetach() {}
85+
}

src/main/java/engine/demos/landmass/MapGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*/
1414
public class MapGenerator {
1515

16-
private int chunkSize = 241;
16+
// private int chunkSize = 481;
17+
private int chunkSize = 961;
1718
private int mapWidth = chunkSize;
1819
private int mapHeight = chunkSize;
1920
private int seed = 221;

src/main/java/engine/demos/landmass/ProceduralLandmassDemo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import engine.application.ApplicationSettings;
44
import engine.application.BasicApplication;
5-
import engine.components.Geometry;
5+
import engine.components.StaticGeometry;
66
import engine.components.RoundReticle;
77
import engine.components.SmoothFlyByCameraControl;
88
import engine.render.Material;
@@ -41,7 +41,7 @@ public enum DrawMode {
4141
}
4242

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

@@ -103,7 +103,7 @@ private void createTerrain() {
103103
terrainMesh.apply(new ScaleModifier(3));
104104
terrainMesh.apply(new CenterAtModifier());
105105

106-
Geometry terrainGeometry = new Geometry(terrainMesh, mapMaterial);
106+
StaticGeometry terrainGeometry = new StaticGeometry(terrainMesh, mapMaterial);
107107
SceneNode terrainNode = new SceneNode();
108108
terrainNode.addComponent(terrainGeometry);
109109
scene.addNode(terrainNode);

src/main/java/engine/processing/ProcessingApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import engine.input.MouseInput;
88
import engine.resources.ResourceManager;
99
import engine.resources.TextureManager;
10+
import engine.vbo.VBOFactory;
1011
import processing.core.PApplet;
1112
import workspace.GraphicsPImpl;
1213
import workspace.ui.Graphics;
@@ -33,6 +34,8 @@ public void setup() {
3334
Graphics g = new GraphicsPImpl(this);
3435
ResourceManager.getInstance().setImageLoader(new ProcessingImageLoader(this));
3536
TextureManager.getInstance().setTextureLoader(new ProcessingTextureLoader(this));
37+
VBOFactory.getInstance()
38+
.setVBOCreationStrategy(new ProcessingVBOCreationStrategy(getGraphics()));
3639
container.setGraphics(g);
3740
getSurface().setTitle(settings.getTitle());
3841
setupInput();

src/main/java/engine/vbo/VBO.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package engine.vbo;
2+
3+
import engine.render.Material;
4+
import mesh.Mesh3D;
5+
6+
public interface VBO {
7+
void create(float[] vertices, int[] indices);
8+
9+
void create(Mesh3D mesh, Material material);
10+
11+
void bind();
12+
13+
void unbind();
14+
15+
void updateData(float[] newData);
16+
17+
void delete();
18+
19+
int getVertexCount();
20+
21+
int getFaceCount();
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package engine.vbo;
2+
3+
public interface VBOCreationStrategy {
4+
5+
public VBO create();
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package engine.vbo;
2+
3+
public class VBOFactory {
4+
5+
private static VBOFactory instance;
6+
private static VBOCreationStrategy strategy;
7+
8+
private VBOFactory() {}
9+
10+
public static VBOFactory getInstance() {
11+
if (instance == null) {
12+
instance = new VBOFactory();
13+
}
14+
return instance;
15+
}
16+
17+
public void setVBOCreationStrategy(VBOCreationStrategy strategy) {
18+
VBOFactory.strategy = strategy;
19+
}
20+
21+
public VBO create() {
22+
if (strategy == null) {
23+
System.err.println("No VBOCreationStrategy set!");
24+
return null;
25+
}
26+
return strategy.create();
27+
}
28+
}

src/main/java/workspace/GraphicsPImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import engine.processing.LightGizmoRenderer;
66
import engine.processing.LightRendererImpl;
77
import engine.processing.ProcessingTexture;
8+
import engine.processing.VBOProcessing;
89
import engine.render.Material;
910
import engine.resources.FilterMode;
1011
import engine.resources.Image;
@@ -13,6 +14,7 @@
1314
import engine.scene.camera.Camera;
1415
import engine.scene.light.Light;
1516
import engine.scene.light.LightRenderer;
17+
import engine.vbo.VBO;
1618
import math.Matrix4f;
1719
import math.Vector2f;
1820
import math.Vector3f;
@@ -104,6 +106,15 @@ public void drawFaces(Mesh3D mesh) {
104106
drawMeshFaces(mesh);
105107
}
106108

109+
@Override
110+
public void draw(VBO vbo) {
111+
faceCount += vbo.getFaceCount();
112+
vertexCount += vbo.getVertexCount();
113+
applyTexture();
114+
VBOProcessing vboProcessing = (VBOProcessing) vbo;
115+
vboProcessing.draw(g);
116+
}
117+
107118
@Override
108119
public void renderInstances(Mesh3D mesh, List<Matrix4f> instanceTransforms) {
109120
if (mesh.getFaces().isEmpty() || mesh.getVertices().isEmpty()) {

0 commit comments

Comments
 (0)