Skip to content

Commit

Permalink
Trying to make Scenery more "kotlinesque", starting by refactoring th…
Browse files Browse the repository at this point in the history
…e first "Basic" test:

- added some fake constructors call for initializing the object directly after the instantiation
- added an `inline reified` counterpart for `Image.fromResource`
- added an `Image::toTexture` function
- added the operator overloading `plusAssign` for adding element(s) to the scene
- minors
  • Loading branch information
elect86 committed Jun 14, 2023
1 parent c47e15f commit a74b7a8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/graphics/scenery/Box.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.joml.Vector3f
import java.lang.IllegalArgumentException
import kotlin.jvm.JvmOverloads

inline fun Box(sizes: Vector3f = Vector3f(1.0f, 1.0f, 1.0f), insideNormals: Boolean = false, block: Box.() -> Unit): Box = Box(sizes, insideNormals).apply(block)

/**
* Constructs a Box [Node] with the dimensions given in [sizes]
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/graphics/scenery/DetachedHeadCamera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlin.math.PI
import kotlin.math.atan
import kotlin.reflect.KProperty

inline fun DetachedHeadCamera(block: DetachedHeadCamera.() -> Unit): DetachedHeadCamera = DetachedHeadCamera().apply(block)

/**
* Detached Head Camera is a Camera subclass that tracks the head orientation
* in addition to general orientation - useful for HMDs
Expand Down
24 changes: 21 additions & 3 deletions src/main/kotlin/graphics/scenery/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.locks.ReentrantLock
import java.util.function.Consumer
import kotlin.collections.ArrayList
import kotlin.collections.Collection

interface Node : Networkable {
var name: String
Expand Down Expand Up @@ -137,6 +138,23 @@ interface Node : Networkable {
/** Unique ID of the Node */
fun getUuid(): UUID

/**
* Attaches a child node to this node.
*
* @param[child] The child to attach to this node.
*/
operator fun plusAssign(child: Node) = addChild(child)

/**
* Attaches the given children nodes to this node.
*
* @param[children] The children to attach to this node.
*/
operator fun plusAssign(children: Collection<Node>) {
for (child in children)
addChild(child)
}

/**
* Attaches a child node to this node.
*
Expand Down Expand Up @@ -184,12 +202,12 @@ interface Node : Networkable {
*/
fun generateBoundingBox(): OrientedBoundingBox? {
val geometry = geometryOrNull()
if(geometry == null) {
return if(geometry == null) {
logger.warn("$name: Assuming 3rd party BB generation")
return boundingBox
boundingBox
} else {
boundingBox = geometry.generateBoundingBox(children)
return boundingBox
boundingBox
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/graphics/scenery/PointLight.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import graphics.scenery.utils.extensions.xyz
import org.joml.Vector3f
import org.joml.Vector4f

inline fun PointLight(radius: Float = 5.0f, block: PointLight.() -> Unit): PointLight = PointLight(radius).apply(block)

/**
* Point light class.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/graphics/scenery/utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package graphics.scenery

inline operator fun <M : Mesh> M.invoke(init: M.() -> Unit): M {
apply(init)
return this
}

23 changes: 22 additions & 1 deletion src/main/kotlin/graphics/scenery/utils/Image.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package graphics.scenery.utils

import graphics.scenery.textures.Texture
import graphics.scenery.volumes.Colormap
import net.imglib2.type.numeric.integer.UnsignedByteType
import org.joml.Vector3i
import org.lwjgl.system.MemoryUtil
import java.awt.Color
import java.awt.color.ColorSpace
Expand All @@ -9,7 +12,6 @@ import java.awt.image.*
import java.io.BufferedInputStream
import java.io.IOException
import java.io.InputStream
import java.lang.RuntimeException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.*
Expand All @@ -23,6 +25,17 @@ import javax.imageio.ImageIO
*/
open class Image(val contents: ByteBuffer, val width: Int, val height: Int, val depth: Int = 1) {

fun toTexture(
repeatUVW: Triple<Texture.RepeatMode, Texture.RepeatMode, Texture.RepeatMode> = Texture.RepeatMode.Repeat.all(),
borderColor: Texture.BorderColor = Texture.BorderColor.OpaqueBlack,
normalized: Boolean = true,
mipmap: Boolean = true,
minFilter: Texture.FilteringMode = Texture.FilteringMode.Linear,
maxFilter: Texture.FilteringMode = Texture.FilteringMode.Linear,
usage: HashSet<Texture.UsageType> = hashSetOf(Texture.UsageType.Texture)
): Texture {
return Texture(Vector3i(width, height, depth),4, UnsignedByteType(), contents, repeatUVW, borderColor, normalized, mipmap, usageType = usage, minFilter = minFilter, maxFilter = maxFilter)
}
companion object {
protected val logger by lazyLogger()

Expand Down Expand Up @@ -98,6 +111,14 @@ open class Image(val contents: ByteBuffer, val width: Int, val height: Int, val
return Image(imageData, bi.width, bi.height)
}

/**
* Creates an Image from a resource given in [path], with [baseClass] as basis for the search path.
* [path] is expected to end in an extension (e.g., ".png"), such that the file type can be determined.
*/
inline fun <reified K> fromResource(path: String): Image {
return fromStream(K::class.java.getResourceAsStream(path), path.substringAfterLast(".").lowercase())
}

/**
* Creates an Image from a resource given in [path], with [baseClass] as basis for the search path.
* [path] is expected to end in an extension (e.g., ".png"), such that the file type can be determined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,56 @@ class ArcballExample : SceneryBase("ArcballExample") {
override fun init() {
renderer = hub.add(Renderer.createRenderer(hub, applicationName, scene, 1024, 1024))

val cam: Camera = DetachedHeadCamera()
with(cam) {
val cam: Camera = DetachedHeadCamera {
spatial {
position = Vector3f(0.0f, 0.0f, 2.5f)
}
perspectiveCamera(70.0f, windowWidth, windowHeight)

targeted = true
target = Vector3f(0.0f, 0.0f, 0.0f)

scene.addChild(this)
}
scene += cam

val camlight = PointLight(3.0f)
camlight.intensity = 5.0f
cam.addChild(camlight)

val box = Box(Vector3f(1.0f, 1.0f, 1.0f))
val camlight = PointLight(3.0f) {
intensity = 5.0f
}
cam += camlight

with(box) {
val box = Box(Vector3f(1.0f, 1.0f, 1.0f)) {
spatial {
position = Vector3f(0.0f, 0.0f, 0.0f)
}
material {
ambient = Vector3f(1.0f, 0.0f, 0.0f)
diffuse = Vector3f(0.0f, 1.0f, 0.0f)
textures["diffuse"] = Texture.fromImage(Image.fromResource("textures/helix.png", TexturedCubeExample::class.java))
textures["diffuse"] = Image.fromResource<TexturedCubeExample>("textures/helix.png").toTexture()
specular = Vector3f(1.0f, 1.0f, 1.0f)
}

scene.addChild(this)
}
scene += box

val lights = (0..2).map {
PointLight(radius = 15.0f)
}.map { light ->
light.spatial {
position = Random.random3DVectorFromRange(-3.0f, 3.0f)
val lights = List(3) {
PointLight(radius = 15.0f) {
spatial {
position = Random.random3DVectorFromRange(-3.0f, 3.0f)
}
emissionColor = Random.random3DVectorFromRange(0.2f, 0.8f)
intensity = Random.randomFromRange(0.1f, 0.8f)
}
light.emissionColor = Random.random3DVectorFromRange(0.2f, 0.8f)
light.intensity = Random.randomFromRange(0.1f, 0.8f)
light
}

val floor = Box(Vector3f(500.0f, 0.05f, 500.0f))
floor.spatial {
position = Vector3f(0.0f, -1.0f, 0.0f)
}
floor.material {
diffuse = Vector3f(1.0f, 1.0f, 1.0f)
val floor = Box(Vector3f(500.0f, 0.05f, 500.0f)) {
spatial {
position = Vector3f(0.0f, -1.0f, 0.0f)
}
material {
diffuse = Vector3f(1.0f, 1.0f, 1.0f)
}
}
scene.addChild(floor)
scene += floor

lights.forEach(scene::addChild)
scene += lights
}

override fun inputSetup() {
Expand Down

0 comments on commit a74b7a8

Please sign in to comment.