|
| 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 | +} |
0 commit comments