From: Svjatoslav Agejenko Date: Mon, 23 Mar 2026 19:59:34 +0000 (+0200) Subject: refactor(demos): adopt new transform API and simplify initialization X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=e8e6f138e52d8fbd49958aa8d03cc7d5e8f911a4;p=sixth-3d-demos.git refactor(demos): adopt new transform API and simplify initialization Update all demos to use the 6-parameter Transform.fromAngles and set methods that accept full Euler rotation (x, y, z, yaw, pitch, roll). Remove explicit render thread start calls since ViewPanel now auto-starts on component events. Add initial repaint signals where needed. OctreeDemo now adds lights to both raytracer and rasterizer lighting systems for consistent illumination. Rename DiamondSquareLandscape to TerrainDemo for clarity. Increase voxel opacity in fractal from 100 to 200 for better visibility. --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/MinimalExample.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/MinimalExample.java index 91c368c..41e6e53 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/MinimalExample.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/MinimalExample.java @@ -28,7 +28,9 @@ public class MinimalExample { ViewFrame viewFrame = new ViewFrame(); ShapeCollection shapes = viewFrame.getViewPanel().getRootShapeCollection(); - Transform boxTransform = Transform.fromAngles(new Point3D(0, 0, 0), 0, 0); + viewFrame.getViewPanel().getCamera().getTransform().setTranslation(new Point3D(0, -100, -300)); + + Transform boxTransform = Transform.fromAngles(0, 0, 0, 0, 0, 0); SolidPolygonRectangularBox box = new SolidPolygonRectangularBox( new Point3D(-50, -50, -50), new Point3D(50, 50, 50), @@ -37,8 +39,6 @@ public class MinimalExample { box.setTransform(boxTransform); shapes.addShape(box); - viewFrame.getViewPanel().getCamera().getTransform().setTranslation(new Point3D(0, -100, -300)); - - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } } \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/OctreeDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/OctreeDemo.java index 14b62a3..e2ed165 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/OctreeDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/OctreeDemo.java @@ -14,10 +14,10 @@ import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.renderer.octree.IntegerPoint; import eu.svjatoslav.sixth.e3d.renderer.octree.OctreeVolume; import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.RaytracingCamera; -import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.LightSource; import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.RayTracer; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; +import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightSource; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.LightSourceMarker; @@ -45,7 +45,7 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { private static final double magnification = 5; private final LineAppearance gridAppearance = new LineAppearance(40, new Color(255, 0, 0, 60)); - private final Vector lights = new Vector<>(); + private final Vector lights = new Vector<>(); private OctreeVolume octreeVolume; private ShapeCollection shapeCollection; private ViewPanel viewPanel; @@ -59,20 +59,21 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { } /** - * Adds a light source to both the visual scene and the raytracer. - * @param location position of the light + * Adds a light source to both the raytracer and rasterizer lighting systems. + * @param location position in octree space (will be scaled by magnification for display) * @param color color of the light * @param brightness intensity of the light */ - private void addLight(final Point3D location, final Color color, - final float brightness) { + private void addLightToBothSystems(final Point3D location, final Color color, + final float brightness) { shapeCollection.addShape(new LightSourceMarker(new Point3D(location) .scaleUp(magnification), color)); - final LightSource lightSource = new LightSource(location, color, - brightness); + lights.add(new eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.LightSource( + location, color, brightness)); - lights.add(lightSource); + viewPanel.getLightingManager().addLight(new LightSource( + new Point3D(location).scaleUp(magnification), color, brightness * 50)); } /** Creates a colorful spiral pattern of voxels in the octree. */ @@ -110,7 +111,7 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { putRect( new IntegerPoint( x - size, y - size, z - size), new IntegerPoint( x + size, y + size, z + size), - new Color((int) c1, (int) c2, (int) c3, 100)); + new Color((int) c1, (int) c2, (int) c3, 200)); if (size > 1) { fractal(x, y - (size * 3), z, size / 2, step + 1); @@ -125,7 +126,9 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { final ViewFrame viewFrame = new ViewFrame(); viewPanel = viewFrame.getViewPanel(); - viewPanel.getCamera().getTransform().setTranslation(new Point3D(0, -30, -300)); + viewPanel.getCamera().getTransform().set(104.13, -65.04, -370.53, 0.12, 0.14, 0); + + viewPanel.getLightingManager().setAmbientLight(new Color(50, 50, 50)); octreeVolume = new OctreeVolume(); @@ -135,11 +138,8 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { new Point3D(-10000, -10000, -10000), new Point3D(10000, 10000, 10000), 4000, gridAppearance)); - // yellow light - addLight(new Point3D(20, -450, 240), new Color(255, 255, 255, 255), 100); - - // red light - addLight(new Point3D(-150, -116, 141), new Color(255, 0, 0, 255), 10); + addLightToBothSystems(new Point3D(20, -450, 240), new Color(255, 255, 255), 100); + addLightToBothSystems(new Point3D(-150, -116, 141), new Color(255, 0, 0), 10); dotSpiral(); @@ -167,9 +167,6 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { viewPanel.getKeyboardFocusStack().pushFocusOwner(this); viewPanel.repaintDuringNextViewUpdate(); - - // Ensure the render thread is started - viewPanel.ensureRenderThreadStarted(); } /** @@ -210,12 +207,11 @@ public class OctreeDemo extends WorldNavigationUserInputTracker { * @param color the color to fill */ private void putRect(IntegerPoint p1, IntegerPoint p2, final Color color) { - - shapeCollection - .addShape(new SolidPolygonRectangularBox( - new Point3D(p1).scaleUp(magnification), - new Point3D(p2).scaleUp(magnification), color)); - + final SolidPolygonRectangularBox box = new SolidPolygonRectangularBox( + new Point3D(p1).scaleUp(magnification), + new Point3D(p2).scaleUp(magnification), color); + box.setShadingEnabled(true); + shapeCollection.addShape(box); octreeVolume.fillRectangle(p1, p2, color); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/RainingNumbersDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/RainingNumbersDemo.java index 84cc65e..da0b33d 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/RainingNumbersDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/RainingNumbersDemo.java @@ -79,6 +79,8 @@ public class RainingNumbersDemo implements FrameListener { private void run() { final ViewFrame viewFrame = new ViewFrame(); + viewFrame.getViewPanel().getCamera().getTransform().set(-129.75, 228.27, -220.69, -0.55, 0.54, 0); + final ShapeCollection geometryCollection = viewFrame.getViewPanel() .getRootShapeCollection(); @@ -101,7 +103,6 @@ public class RainingNumbersDemo implements FrameListener { viewFrame.getViewPanel().addFrameListener(this); - // Ensure the render thread is started - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java index 7c04f2f..54d25f4 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/RandomPolygonsDemo.java @@ -88,6 +88,8 @@ public class RandomPolygonsDemo { final ViewFrame viewFrame = new ViewFrame(); + viewFrame.getViewPanel().getCamera().getTransform().set(-52.96, -239.61, -1293.29, -0.09, -0.36, 0); + final ShapeCollection shapeCollection = viewFrame.getViewPanel() .getRootShapeCollection(); @@ -102,8 +104,7 @@ public class RandomPolygonsDemo { for (int i = 0; i < POLYGON_COUNT; i++) addRandomPolygon(shapeCollection); - // Ensure the render thread is started - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/ShadedShapesDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/ShadedShapesDemo.java index f8232c5..e9dc664 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/ShadedShapesDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/ShadedShapesDemo.java @@ -3,6 +3,7 @@ package eu.svjatoslav.sixth.e3d.examples; import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.gui.ViewFrame; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; +import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightSource; @@ -16,6 +17,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Random; +import static eu.svjatoslav.sixth.e3d.math.Transform.fromAngles; + /** * Demo showing a shaded sphere, cube, pyramid, and cylinder with multiple colored light sources. * Ten light sources orbit around the shapes on different paths to demonstrate dynamic lighting. @@ -40,6 +43,8 @@ public class ShadedShapesDemo { final ViewPanel viewPanel = viewFrame.getViewPanel(); final ShapeCollection shapes = viewPanel.getRootShapeCollection(); + viewPanel.getCamera().getTransform().setTranslation(new Point3D(200, -250, -900)); + // Use the global lighting manager from ViewPanel viewPanel.getLightingManager().setAmbientLight(new Color(25, 25, 25)); @@ -101,16 +106,10 @@ public class ShadedShapesDemo { cylinder.setShadingEnabled(true); shapes.addShape(cylinder); - // Camera - viewPanel.getCamera().getTransform().setTranslation(new Point3D(200, -250, -900)); - final MultiLightAnimator animator = new MultiLightAnimator(orbitingLights); viewPanel.addFrameListener(animator); viewPanel.repaintDuringNextViewUpdate(); - - // Ensure the render thread is started - viewPanel.ensureRenderThreadStarted(); } /** @@ -199,7 +198,8 @@ public class ShadedShapesDemo { Point3D newPosition = new Point3D(x, y + 50, z); orbitingLight.light.setPosition(newPosition); - orbitingLight.marker.setTransform(eu.svjatoslav.sixth.e3d.math.Transform.fromAngles(newPosition, 0, 0)); + orbitingLight.marker.setTransform(fromAngles( + newPosition.x, newPosition.y, newPosition.z, 0, 0, 0)); } return true; } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/SineHeightmap.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/SineHeightmap.java index 91993f9..dd8cee5 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/SineHeightmap.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/SineHeightmap.java @@ -83,13 +83,13 @@ public class SineHeightmap { final ShapeCollection geometryCollection = viewFrame.getViewPanel() .getRootShapeCollection(); + viewFrame.getViewPanel().getCamera().getTransform().setTranslation(new Point3D(0, 0, -500)); + addSphere(geometryCollection); addWobblySurface(geometryCollection, 200); addWobblySurface(geometryCollection, -200); - - setCameraLocation(viewFrame); - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } /** @@ -105,12 +105,4 @@ public class SineHeightmap { )); } - /** - * Sets the camera to an initial viewing position. - * @param viewFrame the view frame whose camera to configure - */ - private static void setCameraLocation(ViewFrame viewFrame) { - viewFrame.getViewPanel().getCamera().getTransform().setTranslation(new Point3D(0, 0, -500)); - } - } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo.java index 773fe43..748edc1 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo.java @@ -9,12 +9,10 @@ package eu.svjatoslav.sixth.e3d.examples; import eu.svjatoslav.sixth.e3d.geometry.Point2D; import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.geometry.Rectangle; -import eu.svjatoslav.sixth.e3d.gui.Camera; import eu.svjatoslav.sixth.e3d.gui.ViewFrame; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.LookAndFeel; import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.TextEditComponent; -import eu.svjatoslav.sixth.e3d.math.Quaternion; import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; @@ -43,17 +41,16 @@ public class TextEditorDemo { final ViewFrame viewFrame = new ViewFrame(); final ViewPanel viewPanel = viewFrame.getViewPanel(); + viewPanel.getCamera().getTransform().set(500, -300, -800, 0.6, -0.5, 0); + final ShapeCollection shapeCollection = viewFrame.getViewPanel() .getRootShapeCollection(); - setCameraLocation(viewPanel); - addGrid(shapeCollection); addTextEditors(viewPanel, shapeCollection); - // Ensure the render thread is started - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } /** @@ -61,8 +58,7 @@ public class TextEditorDemo { * @param shapeCollection the collection to add the grid to */ private static void addGrid(ShapeCollection shapeCollection) { - final Transform transform = Transform.fromAngles(new Point3D(0, 100, 0), 0, - Math.PI / 2); + final Transform transform = Transform.fromAngles(0, 100, 0, 0, Math.PI / 2, 0); final Rectangle rectangle = new Rectangle(2000); final LineAppearance appearance = new LineAppearance(10, new Color( @@ -89,13 +85,4 @@ public class TextEditorDemo { } } - /** - * Sets the camera to an initial viewing position. - * @param viewPanel the view panel whose camera to configure - */ - private static void setCameraLocation(ViewPanel viewPanel) { - Camera camera = viewPanel.getCamera(); - camera.getTransform().setTranslation(new Point3D(500, -300, -800)); - camera.getTransform().getRotation().set(Quaternion.fromAngles(0.6, -0.5)); - } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo2.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo2.java index 75a4413..81c145e 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo2.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/TextEditorDemo2.java @@ -9,12 +9,10 @@ package eu.svjatoslav.sixth.e3d.examples; import eu.svjatoslav.sixth.e3d.geometry.Point2D; import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.geometry.Rectangle; -import eu.svjatoslav.sixth.e3d.gui.Camera; import eu.svjatoslav.sixth.e3d.gui.ViewFrame; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.LookAndFeel; import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.TextEditComponent; -import eu.svjatoslav.sixth.e3d.math.Quaternion; import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; @@ -28,6 +26,9 @@ import java.io.InputStreamReader; import java.net.URISyntaxException; import java.util.stream.Collectors; +import static eu.svjatoslav.sixth.e3d.math.Transform.fromAngles; +import static java.lang.Math.PI; + /** * "Text Editor City" demo showing a 3D city of text editor components. * Creates a grid of buildings where each building has four text editor panels @@ -65,17 +66,16 @@ public class TextEditorDemo2 { final ViewFrame viewFrame = new ViewFrame(); final ViewPanel viewPanel = viewFrame.getViewPanel(); + viewPanel.getCamera().getTransform().set(500, -300, -800, 0.6, -0.5, 0); + final ShapeCollection shapeCollection = viewFrame.getViewPanel() .getRootShapeCollection(); - setCameraLocation(viewPanel); - addGrid(shapeCollection); addCity(viewPanel, shapeCollection); - // Ensure the render thread is started - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } /** @@ -100,8 +100,7 @@ public class TextEditorDemo2 { * @param shapeCollection the collection to add the grid to */ private static void addGrid(ShapeCollection shapeCollection) { - final Transform transform = Transform.fromAngles(new Point3D(0, 100, 0), 0, - Math.PI / 2); + final Transform transform = fromAngles(0, 100, 0, 0, PI / 2, 0); final Rectangle rectangle = new Rectangle(10000); final LineAppearance appearance = new LineAppearance(10, new Color( @@ -121,17 +120,13 @@ public class TextEditorDemo2 { * @throws IOException if demo text file cannot be read */ private void addBuilding(ViewPanel viewPanel, ShapeCollection shapeCollection, double x, double z) throws URISyntaxException, IOException { - Transform transform = new Transform(new Point3D(x, -390, z-200)); - addTextEditor(viewPanel, shapeCollection, transform); + addTextEditor(viewPanel, shapeCollection, new Transform(new Point3D(x, -390, z-200))); - transform = Transform.fromAngles(new Point3D(x, -390, z+200),Math.PI, 0); - addTextEditor(viewPanel, shapeCollection, transform); + addTextEditor(viewPanel, shapeCollection, fromAngles(x, -390, z+200, PI, 0, 0)); - transform = Transform.fromAngles(new Point3D(x-200, -390, z),Math.PI/2, 0); - addTextEditor(viewPanel, shapeCollection, transform); + addTextEditor(viewPanel, shapeCollection, fromAngles(x-200, -390, z, PI/2, 0, 0)); - transform = Transform.fromAngles(new Point3D(x+200, -390, z),Math.PI/2*3f, 0); - addTextEditor(viewPanel, shapeCollection, transform); + addTextEditor(viewPanel, shapeCollection, fromAngles(x+200, -390, z, PI/2*3f, 0, 0)); } /** @@ -187,13 +182,4 @@ public class TextEditorDemo2 { } } - /** - * Sets the camera to an initial viewing position for the city scene. - * @param viewPanel the view panel whose camera to configure - */ - private static void setCameraLocation(ViewPanel viewPanel) { - Camera camera = viewPanel.getCamera(); - camera.getTransform().setTranslation(new Point3D(500, -300, -800)); - camera.getTransform().getRotation().set(Quaternion.fromAngles(0.6, -0.5)); - } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/WindingOrderDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/WindingOrderDemo.java index 3b9e893..613397b 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/WindingOrderDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/WindingOrderDemo.java @@ -40,6 +40,8 @@ public class WindingOrderDemo { ViewPanel viewPanel = viewFrame.getViewPanel(); ShapeCollection shapes = viewPanel.getRootShapeCollection(); + viewPanel.getCamera().getTransform().setTranslation(new Point3D(0, 0, -500)); + double size = 150; Point3D upperCenter = new Point3D(0, -size, 0); @@ -51,8 +53,6 @@ public class WindingOrderDemo { shapes.addShape(triangle); - viewPanel.getCamera().getTransform().setTranslation(new Point3D(0, 0, -500)); - - viewPanel.ensureRenderThreadStarted(); + viewPanel.repaintDuringNextViewUpdate(); } } \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/benchmark/GraphicsBenchmark.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/benchmark/GraphicsBenchmark.java index bc0ccbb..a5e1dcf 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/benchmark/GraphicsBenchmark.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/benchmark/GraphicsBenchmark.java @@ -115,10 +115,13 @@ public class GraphicsBenchmark implements FrameListener, KeyboardInputHandler { viewPanel = viewFrame.getViewPanel(); viewPanel.setFrameRate(0); shapes = viewPanel.getRootShapeCollection(); + + camera = viewPanel.getCamera(); + camera.getTransform().set(0, -500, -800, 0, -0.5, 0); + viewPanel.addFrameListener(this); viewPanel.getKeyboardFocusStack().pushFocusOwner(this); - camera = viewPanel.getCamera(); - viewPanel.ensureRenderThreadStarted(); + viewPanel.repaintDuringNextViewUpdate(); } private void registerTests() { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/DiamondSquareLandscape.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/DiamondSquareLandscape.java deleted file mode 100644 index 2328e4d..0000000 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/DiamondSquareLandscape.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Sixth 3D engine demos. Author: Svjatoslav Agejenko. - * This project is released under Creative Commons Zero (CC0) license. - */ - -package eu.svjatoslav.sixth.e3d.examples.diamondsquare_demo; - -import eu.svjatoslav.sixth.e3d.geometry.Point3D; -import eu.svjatoslav.sixth.e3d.gui.ViewFrame; -import eu.svjatoslav.sixth.e3d.gui.ViewPanel; -import eu.svjatoslav.sixth.e3d.renderer.raster.Color; -import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; -import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightSource; -import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightingManager; -import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.LightSourceMarker; - -/** - * Demo showing a procedurally generated mountain landscape using the diamond-square algorithm. - * Three colored light sources (warm orange, cool cyan, neutral white) illuminate the terrain - * from above, demonstrating dynamic flat shading. - */ -public class DiamondSquareLandscape { - - /** - * Entry point for the diamond-square landscape demo. - * @param args command line arguments (ignored) - */ - public static void main(final String[] args) { - final ViewFrame viewFrame = new ViewFrame(); - ViewPanel viewPanel = viewFrame.getViewPanel(); - - final ShapeCollection shapes = viewPanel.getRootShapeCollection(); - - setLights(viewFrame, shapes); - - shapes.addShape(new DiamondSquareTerrain(129, 0.0, 300.0, 42)); - - viewPanel.getCamera().getTransform() - .setTranslation(new Point3D(0, -600, -800)); - - viewPanel.ensureRenderThreadStarted(); - } - - // TODO: add javadoc here - private static void setLights(ViewFrame viewFrame, ShapeCollection shapes) { - LightingManager lightingManager = viewFrame.getViewPanel().getLightingManager(); - lightingManager.setAmbientLight(new Color(250, 150, 100)); - - final LightSource warmLight = new LightSource( - new Point3D(-400, -500, 0), - new Color(255, 180, 100), - 190.0 - ); - final LightSource coolLight = new LightSource( - new Point3D(400, -500, 0), - new Color(100, 200, 255), - 220.0 - ); - final LightSource neutralLight = new LightSource( - new Point3D(0, -600, 300), - new Color(255, 255, 255), - 250.0 - ); - - lightingManager.addLight(warmLight); - lightingManager.addLight(coolLight); - lightingManager.addLight(neutralLight); - - shapes.addShape(new LightSourceMarker(warmLight.getPosition(), new Color(255, 180, 100))); - shapes.addShape(new LightSourceMarker(coolLight.getPosition(), new Color(100, 200, 255))); - shapes.addShape(new LightSourceMarker(neutralLight.getPosition(), new Color(255, 255, 255))); - } -} \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/TerrainDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/TerrainDemo.java new file mode 100644 index 0000000..70ca2c6 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/diamondsquare_demo/TerrainDemo.java @@ -0,0 +1,76 @@ +/* + * Sixth 3D engine demos. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. + */ + +package eu.svjatoslav.sixth.e3d.examples.diamondsquare_demo; + +import eu.svjatoslav.sixth.e3d.geometry.Point3D; +import eu.svjatoslav.sixth.e3d.gui.ViewFrame; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; +import eu.svjatoslav.sixth.e3d.renderer.raster.Color; +import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; +import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightSource; +import eu.svjatoslav.sixth.e3d.renderer.raster.lighting.LightingManager; +import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.LightSourceMarker; + +/** + * Demo showing a procedurally generated mountain landscape using the diamond-square algorithm. + * Three colored light sources (warm orange, cool cyan, neutral white) illuminate the terrain + * from above, demonstrating dynamic flat shading. + */ +public class TerrainDemo { + + /** + * Entry point for the terrain demo. + * @param args command line arguments (ignored) + */ + public static void main(final String[] args) { + final ViewFrame viewFrame = new ViewFrame(); + ViewPanel viewPanel = viewFrame.getViewPanel(); + + viewPanel.getCamera().getTransform().set(-307.96, -847.50, -768.20, -0.52, -0.66, 0); + + final ShapeCollection shapes = viewPanel.getRootShapeCollection(); + + setLights(viewFrame, shapes); + + shapes.addShape(new DiamondSquareTerrain(129, 0.0, 300.0, 42)); + + viewPanel.repaintDuringNextViewUpdate(); + } + + /** + * Sets up the lighting for the terrain scene. + * @param viewFrame the view frame containing the lighting manager + * @param shapes the shape collection to add light source markers to + */ + private static void setLights(ViewFrame viewFrame, ShapeCollection shapes) { + LightingManager lightingManager = viewFrame.getViewPanel().getLightingManager(); + lightingManager.setAmbientLight(new Color(250, 150, 100)); + + final LightSource warmLight = new LightSource( + new Point3D(-400, -500, 0), + new Color(255, 180, 100), + 190.0 + ); + final LightSource coolLight = new LightSource( + new Point3D(400, -500, 0), + new Color(100, 200, 255), + 220.0 + ); + final LightSource neutralLight = new LightSource( + new Point3D(0, -600, 300), + new Color(255, 255, 255), + 250.0 + ); + + lightingManager.addLight(warmLight); + lightingManager.addLight(coolLight); + lightingManager.addLight(neutralLight); + + shapes.addShape(new LightSourceMarker(warmLight.getPosition(), new Color(255, 180, 100))); + shapes.addShape(new LightSourceMarker(coolLight.getPosition(), new Color(100, 200, 255))); + shapes.addShape(new LightSourceMarker(neutralLight.getPosition(), new Color(255, 255, 255))); + } +} \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/galaxy_demo/PointCloudDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/galaxy_demo/PointCloudDemo.java index 87b7c97..7e94245 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/galaxy_demo/PointCloudDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/galaxy_demo/PointCloudDemo.java @@ -31,16 +31,17 @@ public class PointCloudDemo { final ViewFrame viewFrame = new ViewFrame(); + viewFrame.getViewPanel().getCamera().getTransform().set(-1099.85, -2862.44, 144.32, -1.09, -0.60, 0); + final ShapeCollection geometryCollection = viewFrame.getViewPanel() .getRootShapeCollection(); - Transform transform = Transform.fromAngles(new Point3D(0, -1000, 1000), 0, 0); + Transform transform = Transform.fromAngles(0, -1000, 1000, 0, 0, 0); // add galaxy geometryCollection.addShape(new Galaxy(500, 3, 10000, transform)); - // Ensure the render thread is started - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/graph_demo/MathGraphsDemo.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/graph_demo/MathGraphsDemo.java index b69ef47..abbb2c3 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/graph_demo/MathGraphsDemo.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/graph_demo/MathGraphsDemo.java @@ -149,12 +149,12 @@ public class MathGraphsDemo { final ShapeCollection geometryCollection = viewFrame.getViewPanel() .getRootShapeCollection(); + viewFrame.getViewPanel().getCamera().getTransform().set(-613.58, -408.70, -351.41, -0.60, -0.54, -0.00); + addMathFormulas(geometryCollection); addSurfaceGraph(geometryCollection); - - setCameraLocation(viewFrame); - viewFrame.getViewPanel().ensureRenderThreadStarted(); + viewFrame.getViewPanel().repaintDuringNextViewUpdate(); } /** @@ -205,12 +205,4 @@ public class MathGraphsDemo { geometryCollection.addShape(surface); } - /** - * Sets the camera to an initial viewing position. - * @param viewFrame the view frame whose camera to configure - */ - private static void setCameraLocation(ViewFrame viewFrame) { - viewFrame.getViewPanel().getCamera().getTransform().setTranslation(new Point3D(0, 0, -500)); - } - } \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/launcher/ApplicationListPanel.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/launcher/ApplicationListPanel.java index 7e978aa..a97b692 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/launcher/ApplicationListPanel.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/launcher/ApplicationListPanel.java @@ -6,7 +6,7 @@ package eu.svjatoslav.sixth.e3d.examples.launcher; -import eu.svjatoslav.sixth.e3d.examples.diamondsquare_demo.DiamondSquareLandscape; +import eu.svjatoslav.sixth.e3d.examples.diamondsquare_demo.TerrainDemo; import eu.svjatoslav.sixth.e3d.examples.SineHeightmap; import eu.svjatoslav.sixth.e3d.examples.graph_demo.MathGraphsDemo; import eu.svjatoslav.sixth.e3d.examples.MinimalExample; @@ -68,9 +68,9 @@ class ApplicationListPanel extends JPanel { new DemoEntry("Shaded Shapes", "Shapes lit by orbiting colored light sources", new ShowShadedShapes()), - new DemoEntry("Diamond-Square Landscape", + new DemoEntry("Procedural Terrain", "Procedural mountains with 3 colored light sources", - new ShowDiamondSquareLandscape()), + new ShowTerrainDemo()), new DemoEntry("Graphics Benchmark", "Automated performance measuring FPS across modes", new ShowGraphicsBenchmark()), @@ -236,14 +236,14 @@ class ApplicationListPanel extends JPanel { } } - private static class ShowDiamondSquareLandscape extends AbstractAction { - ShowDiamondSquareLandscape() { - putValue(NAME, "Diamond-Square Landscape"); + private static class ShowTerrainDemo extends AbstractAction { + ShowTerrainDemo() { + putValue(NAME, "Procedural Terrain"); } @Override public void actionPerformed(final ActionEvent e) { - DiamondSquareLandscape.main(null); + TerrainDemo.main(null); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java index eaa2d28..b2a6b30 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/examples/life_demo/Main.java @@ -2,11 +2,9 @@ package eu.svjatoslav.sixth.e3d.examples.life_demo; import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.geometry.Rectangle; -import eu.svjatoslav.sixth.e3d.gui.Camera; import eu.svjatoslav.sixth.e3d.gui.ViewFrame; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationUserInputTracker; -import eu.svjatoslav.sixth.e3d.math.Quaternion; import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; @@ -75,9 +73,11 @@ public class Main extends WorldNavigationUserInputTracker { // create application frame visible to the user final ViewFrame viewFrame = new ViewFrame(); + final ViewPanel viewPanel = viewFrame.getViewPanel(); + + viewPanel.getCamera().getTransform().set(100, -50, -200, 0.2f, -0.7f, 0); - final ShapeCollection shapeCollection = viewFrame.getViewPanel() - .getRootShapeCollection(); + final ShapeCollection shapeCollection = viewPanel.getRootShapeCollection(); // add matrix shapeCollection.addShape(MATRIX); @@ -85,18 +85,11 @@ public class Main extends WorldNavigationUserInputTracker { // add wire-frame grid (optional) shapeCollection.addShape(createGrid()); - final ViewPanel viewPanel = viewFrame.getViewPanel(); - - setCameraOrientation(viewPanel.getCamera()); - // enable receiving of keyboard events viewPanel.getKeyboardFocusStack().pushFocusOwner(this); // Done! World is built. So ensure screen is updated too. viewPanel.repaintDuringNextViewUpdate(); - - // Ensure the render thread is started - viewPanel.ensureRenderThreadStarted(); } /** @@ -105,33 +98,16 @@ public class Main extends WorldNavigationUserInputTracker { */ private Grid2D createGrid() { return new Grid2D( - Transform.fromAngles( - new Point3D( // Grid positioning: - 0, // center - 100, // below the main scene - 0), // center + Transform.fromAngles(0, 100, 0, 0, Math.PI / 2, 0), - // Grid orientation: - 0, // no rotation along XZ axis - Math.PI / 2), // face down + new Rectangle(800), - new Rectangle(800), // large enough, square grid + 5, 5, - 5, 5, // grid will be divided to 5x5 segments - - new LineAppearance(3, // line thickness - new Color("FF000050") // red and quite transparent + new LineAppearance(3, + new Color("FF000050") ) ); } - /** - * Sets the camera to an initial viewing position for the matrix. - * @param camera the camera to configure - */ - private void setCameraOrientation(final Camera camera) { - camera.getTransform().setTranslation(new Point3D(100, -50, -200)); - camera.getTransform().getRotation().set(Quaternion.fromAngles(0.2f, -0.7f)); - } - }