[orx-camera] add defaults() to Camera2D and OrbitalCamera.

Add DemoCamera2DManual01.kt
This commit is contained in:
Abe Pazos
2025-08-08 12:50:18 +02:00
parent 7b101297c8
commit d9e50d46de
6 changed files with 111 additions and 18 deletions

View File

@@ -21,8 +21,8 @@ fun main() = application {
colorBuffer()
depthBuffer()
}
// Create camera and apply an initial transformation
// so the origin is no longer in the top left corner.
// Create a camera and apply an initial transformation
// so the origin is no longer in the top-left corner.
val cam = Camera2D()
cam.view *= transform {
translate(width * 0.5, height * 1.0)

View File

@@ -1,26 +1,17 @@
import org.openrndr.PresentationMode
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.isolatedWithTarget
import org.openrndr.draw.loadFont
import org.openrndr.draw.renderTarget
import org.openrndr.extra.camera.Camera2D
import org.openrndr.extra.camera.Camera2DManual
import org.openrndr.math.Vector2
import org.openrndr.math.transforms.transform
import org.openrndr.shape.Rectangle
import kotlin.math.sin
/**
* Demonstrate the use of `Camera2DManual` for manual camera control.
*
* The application is configured with a 720x720 window size. Within the program, a custom camera (`Camera2DManual`)
* is initialized and used to create isolated drawing scopes. The `isolated` method is utilized to overlay different
* is initialized and used to create isolated drawing scopes. The `isolated` method is used to overlay different
* drawing operations while maintaining individual camera states, ensuring proper transformations for specific elements.
*
* A pink circle is drawn at the center of the canvas with varying radii using isolated and non-isolated camera states.
* The outermost and innermost circles are affected by the `Camera2DManual` isolated scope, while the middle circle
* is outside of the camera's isolated scope, creating a layered visual effect.
* Three circles are drawn on the canvas: a small pink one, a medium white one and a large pink one.
* Only the pink ones are affected by the interactive `Camera2DManual`, while the middle white circle is outside
* the camera's isolated scope.
*/
fun main() = application {
configure {

View File

@@ -0,0 +1,69 @@
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.camera.Camera2DManual
import org.openrndr.extra.noise.shapes.uniform
import org.openrndr.extra.noise.uniform
import org.openrndr.math.Vector2
import org.openrndr.math.transforms.transform
import org.openrndr.shape.Rectangle
import org.openrndr.shape.contains
/**
* Demonstrate the use of `Camera2DManual` to independently translate, scale and rotate one contour
* in a collection.
*
* When the mouse is clicked, the active contour is transformed using the camera view matrix,
* then the camera is reset to its default state and whatever shape is under the mouse becomes
* the new active contour.
*
* As the mouse is dragged or its wheel scrolled, the camera is updated, affecting
* how the active contour is rendered.
*/
fun main() = application {
configure {
width = 720
height = 720
}
program {
val camera = Camera2DManual()
// Create a mutable list of rectangular contours with random transformations
// applied to them. This is the initial state for the contours.
val contours = MutableList(16) {
Rectangle.fromCenter(Vector2.ZERO, 85.0).contour.transform(
transform {
translate(drawer.bounds.uniform())
scale(Double.uniform(0.5, 2.0))
rotate(Double.uniform(0.0, 360.0))
}
)
}
var activeContour = -1
extend {
// Draw all contours. The active contour is drawn in pink and
// affected by the camera's transformations.
contours.forEachIndexed { i, c ->
if (i == activeContour) {
camera.isolated {
drawer.fill = ColorRGBa.PINK
drawer.contour(c)
}
} else {
drawer.fill = ColorRGBa.GRAY
drawer.contour(c)
}
}
}
mouse.buttonDown.listen {
// Apply the camera view matrix to the active contour
if (activeContour >= 0) contours[activeContour] = contours[activeContour].transform(camera.view)
// Reset the camera to its default state
camera.defaults()
// Make the contour under the mouse the active contour
activeContour = contours.indexOfLast { mouse.position in it }
}
}
}

View File

@@ -54,5 +54,10 @@ fun main() = application {
drawer.circle(0.0, 0.0, 50.0)
}
}
keyboard.keyDown.listen {
if (it.name == "r") {
camera.defaults()
}
}
}
}