Demos: ensure all use fun main() = application {

- Adjust some demo window sizes.
- Replace Random.double by Double.uniform
- Tweak some demos so screenshots look more interesting
This commit is contained in:
Abe Pazos
2025-01-26 20:57:04 +01:00
parent 1975a820fc
commit c8f7dd52c6
116 changed files with 2889 additions and 2942 deletions

View File

@@ -5,34 +5,41 @@ import org.openrndr.extra.viewbox.viewBox
import org.openrndr.math.Vector2
import kotlin.math.cos
/**
* Demonstrates how to use a proxy program inside a [viewBox],
* how the main program can access its variables and methods,
* and execute its `extend` block by calling its `draw()` method.
*/
fun Program.program01() {
var color: ColorRGBa by this.userProperties
color = ColorRGBa.WHITE
var someFunction : ()->Unit by this.userProperties
var someFunction: () -> Unit by this.userProperties
someFunction = {
color = ColorRGBa(Math.random(), Math.random(), Math.random())
}
extend {
drawer.fill = color
drawer.circle(Vector2(width/2.0, height/2.0), cos(seconds) * 50.0 + 50.0)
drawer.circle(Vector2(width / 2.0, height / 2.0), cos(seconds) * 50.0 + 50.0)
}
}
fun main() {
application {
program {
val proxyBox = viewBox(drawer.bounds).apply { program01() }
var color: ColorRGBa by proxyBox.userProperties
var someFunction: () -> Unit by proxyBox.userProperties
color = ColorRGBa.PINK
extend {
if (Math.random() < 0.01) {
someFunction()
}
proxyBox.draw()
fun main() = application {
configure {
width = 720
height = 360
}
program {
val proxyBox = viewBox(drawer.bounds).apply { program01() }
var color: ColorRGBa by proxyBox.userProperties
val someFunction: () -> Unit by proxyBox.userProperties
color = ColorRGBa.PINK
extend {
if (Math.random() < 0.01) {
someFunction()
}
proxyBox.draw()
}
}
}
}

View File

@@ -0,0 +1,64 @@
import org.openrndr.*
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.viewbox.ViewBox
import org.openrndr.extra.viewbox.viewBox
import org.openrndr.math.Vector2
import kotlin.math.cos
/**
* Demonstrates how to use two proxy programs and
* toggle between them by clicking the mouse.
*
* programA draws a circle and can be moved by pressing the
* arrow keys.
*
* programB draws a ring located at the current mouse
* position.
*
* Note that programA keeps listening to the key events
* even if programB is currently displayed.
*/
fun Program.programA() {
var pos = drawer.bounds.center
extend {
drawer.fill = ColorRGBa.PINK
drawer.circle(pos, cos(seconds) * 50.0 + 50.0)
}
keyboard.keyDown.listen {
when (it.key) {
KEY_ARROW_UP -> pos -= Vector2.UNIT_Y * 20.0
KEY_ARROW_DOWN -> pos += Vector2.UNIT_Y * 20.0
KEY_ARROW_LEFT -> pos -= Vector2.UNIT_X * 20.0
KEY_ARROW_RIGHT -> pos += Vector2.UNIT_X * 20.0
}
}
}
fun Program.programB() {
extend {
drawer.fill = ColorRGBa.TRANSPARENT
drawer.stroke = ColorRGBa.CYAN
drawer.strokeWeight = 50.0
drawer.circle(mouse.position, 200.0)
}
}
fun main() = application {
configure {
width = 720
height = 360
}
program {
val proxyBoxes = mutableListOf<ViewBox>()
proxyBoxes.add(viewBox(drawer.bounds).apply { programA() })
proxyBoxes.add(viewBox(drawer.bounds).apply { programB() })
var currentBox = 1
extend {
proxyBoxes[currentBox].draw()
}
mouse.buttonDown.listen {
currentBox = (currentBox + 1) % proxyBoxes.size
}
}
}

View File

@@ -1,30 +1,46 @@
import org.openrndr.application
import org.openrndr.extra.camera.Camera2D
import org.openrndr.extra.viewbox.viewBox
import org.openrndr.math.transforms.transform
import org.openrndr.shape.Rectangle
fun main() {
application {
configure {
width = 800
height = 800
}
program {
val vbx = viewBox(Rectangle(0.0, 0.0, 200.0, 200.0)) {
extend(Camera2D())
extend {
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
/**
* Demonstrates how to create a viewBox with an interactive 2D camera and
* display it multiple times.
*
* Instead of calling the viewBox's `.draw()` method multiple times,
* we call its `.update()` method once, then draw its `.result`
* repeatedly, in a grid of 4 columns and 4 rows.
*
* The camera's initial rotation and scaling are specified as a transformation matrix.
* To control the camera use the mouse wheel and buttons on the top-left view.
*/
fun main() = application {
configure {
width = 720
height = 720
}
program {
val vbx = viewBox(Rectangle(0.0, 0.0, 200.0, 200.0)) {
extend(Camera2D()) {
// Set the initial view for the camera
view = transform {
rotate(30.0)
scale(2.0)
}
}
extend {
vbx.update()
for (j in 0 until 4) {
for (i in 0 until 4) {
drawer.image(vbx.result, j * 200.0, i * 200.0)
}
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
}
}
extend {
vbx.update()
for (j in 0 until 4) {
for (i in 0 until 4) {
drawer.image(vbx.result, j * 200.0, i * 200.0)
}
}
}
}
}
}

View File

@@ -1,4 +1,3 @@
import org.openrndr.application
import org.openrndr.draw.BufferMultisample
import org.openrndr.draw.DrawPrimitive
@@ -11,56 +10,61 @@ import org.openrndr.extra.meshgenerators.boxMesh
import org.openrndr.extra.viewbox.viewBox
import org.openrndr.shape.Rectangle
fun main() {
application {
configure {
width = 800
height = 800
}
program {
val vbx = viewBox(Rectangle(0.0, 0.0, 200.0, 800.0)) {
extend(Screenshots())
extend(Camera2D()).also {
shouldDraw = { it.hasChanged }
}
extend {
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
}
/**
* Demonstrates how to draw multiple view boxes. The first two feature
* interactive 2D cameras, the third one uses an Orbital 3D camera.
* All three can be controlled with the mouse wheel and buttons.
*
* The `shouldDraw` viewBox variable is used to avoid re-rendering the view
* unnecessarily when the camera has not changed.
*
*/
fun main() = application {
configure {
width = 720
height = 720
}
program {
val vbx = viewBox(Rectangle(0.0, 0.0, 200.0, height * 1.0)) {
extend(Screenshots())
extend(Camera2D()).also {
shouldDraw = { it.hasChanged }
}
val vbx2 = viewBox(Rectangle(200.0, 0.0, 200.0, 800.0)) {
extend(Post()) {
val blur = ApproximateGaussianBlur()
blur.sigma = 10.0
blur.window = 25
post { i, o ->
blur.apply(i, o)
}
}
extend(Camera2D())
extend {
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
drawer.circle(mouse.position, 10.0)
}
}
val vbx3d = viewBox(Rectangle(400.0, 0.0, 400.0, 800.0), multisample = BufferMultisample.SampleCount(8)) {
extend(Orbital()).also {
this.shouldDraw = {
it.hasChanged
}
}
val cube = boxMesh()
extend {
drawer.vertexBuffer(cube, DrawPrimitive.TRIANGLES)
}
}
extend {
vbx.draw()
vbx2.draw()
vbx3d.draw()
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
}
}
val vbx2 = viewBox(Rectangle(200.0, 0.0, 200.0, height * 1.0)) {
extend(Post()) {
val blur = ApproximateGaussianBlur()
blur.sigma = 10.0
blur.window = 25
post { i, o ->
blur.apply(i, o)
}
}
extend(Camera2D())
extend {
drawer.rectangle(20.0, 20.0, 100.0, 100.0)
drawer.circle(mouse.position, 10.0)
}
}
val vbx3d = viewBox(Rectangle(400.0, 0.0, 400.0, 800.0), multisample = BufferMultisample.SampleCount(8)) {
extend(Orbital()).also {
shouldDraw = { it.hasChanged }
}
val cube = boxMesh()
extend {
drawer.vertexBuffer(cube, DrawPrimitive.TRIANGLES)
}
}
extend {
vbx.draw()
vbx2.draw()
vbx3d.draw()
}
}
}
}