Skip to content

Commit

Permalink
Custom bounding box providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Twometer committed Oct 8, 2021
1 parent 71bb31a commit e85ae8d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion neko-demo/assets/shaders/debug.nks
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
out vec4 color;

void main() {
color = vec4(1,1,1,1);
color = vec4(1,1,0,1);
}
#end fragment
31 changes: 25 additions & 6 deletions neko-demo/src/main/kotlin/DemoApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import de.twometer.neko.audio.SoundEngine
import de.twometer.neko.core.AppConfig
import de.twometer.neko.core.NekoApp
import de.twometer.neko.events.KeyPressEvent
import de.twometer.neko.events.RenderForwardEvent
import de.twometer.neko.render.Primitives
import de.twometer.neko.res.*
import de.twometer.neko.scene.AABB
import de.twometer.neko.scene.Color
import de.twometer.neko.scene.component.BoundingBoxProviderComponent
import de.twometer.neko.scene.nodes.*
import de.twometer.neko.util.MathF.toRadians
import imgui.ImGui
import org.greenrobot.eventbus.Subscribe
import org.joml.Matrix4f
import org.joml.Vector3f
import org.lwjgl.glfw.GLFW.*
import org.lwjgl.opengl.GL11.*

class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {
class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo", windowIcon = "icon.png")) {

private lateinit var girl: ModelNode
private lateinit var rin: ModelNode
Expand All @@ -28,10 +34,23 @@ class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {

val testFont = FontCache.get("lucida")

scene.rootNode.attachChild(ModelLoader.load("astronaut.fbx").also {
it.transform.translation.set(-1.7f, 0.25f, 16f)
it.transform.rotation.rotateX(toRadians(90f))
//it.transform.rotation.rotateY(toRadians(-45f))
it.scanTree(ScanFilters.GEOMETRY) { n ->
n.attachComponent(BoundingBoxProviderComponent {
AABB(Vector3f(), Vector3f(1f, 1f, 1f))
})
}

it.playAnimation(it.animations[1])
})

scene.rootNode.attachChild(ModelLoader.load("girl.fbx").also {
it.animations.add(AnimationCache.get("walk.ani"))
it.transform.scale.set(0.0001)
it.transform.translation.set(-1.7f,0.25f,16f)
it.transform.translation.set(-1.7f, 0.25f, 16f)
it.transform.rotation.rotateX(toRadians(90f))
it.transform.rotation.rotateY(toRadians(-5f))
it.playAnimation(it.animations[1])
Expand Down Expand Up @@ -80,7 +99,7 @@ class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {
it.transform.translation.set(2f, 2f, 0f)
})

scene.rootNode.attachChild(ModelLoader.load("skeld.obj"))
//scene.rootNode.attachChild(ModelLoader.load("skeld.obj"))

val lightPositions = RawLoader.loadLines("lights.txt")
for (pos in lightPositions) {
Expand Down Expand Up @@ -137,7 +156,7 @@ class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {
ImGui.text("SCALE:" + this.transform.scale)

if (ImGui.button("RESET")) {
this.transform.reset()
this.transform.scale.set(1f, 1f, 1f)
}
}
ImGui.end()
Expand Down Expand Up @@ -167,7 +186,7 @@ class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {
}
}

/*@Subscribe
@Subscribe
fun renderFwd(e: RenderForwardEvent) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
val shader = ShaderCache.get("debug.nks")
Expand All @@ -183,7 +202,7 @@ class DemoApp : NekoApp(AppConfig(windowTitle = "Neko Engine Demo")) {
}

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
}*/
}
}

fun main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PickEngine(private val scene: Scene) {
val nodes = ArrayList<NodeReference>()
scene.rootNode.scanTree {
if (it is Geometry && it.aabb != null && it.canPick) {
val transformedAABB = it.aabb.transform(it.compositeTransform.matrix)
val transformedAABB = it.aabb!!.transform(it.compositeTransform.matrix)
val distSq = scene.camera.position.distanceSquared(transformedAABB.center)
if (distSq <= maxDistanceSq)
nodes.add(NodeReference(it, transformedAABB, distSq))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.twometer.neko.scene.component

import de.twometer.neko.scene.AABB
import de.twometer.neko.scene.nodes.Node

class BoundingBoxProviderComponent(val provider: (Node) -> AABB) : BaseComponent() {

override fun createInstance(): BaseComponent {
return BoundingBoxProviderComponent(provider)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import de.twometer.neko.scene.AABB
import de.twometer.neko.scene.Bone
import de.twometer.neko.scene.Material
import de.twometer.neko.scene.Mesh
import de.twometer.neko.scene.component.BoundingBoxProviderComponent
import de.twometer.neko.scene.component.SkeletonComponent
import org.lwjgl.opengl.GL30.*
import java.nio.Buffer
import java.nio.FloatBuffer
import java.nio.IntBuffer

class Geometry(name: String, material: Material = Material.Default, val aabb: AABB? = null) :
class Geometry(name: String, material: Material = Material.Default, private val _aabb: AABB? = null) :
RenderableNode(name = name, material = material) {

private var vao: Int = -1
Expand All @@ -24,6 +25,8 @@ class Geometry(name: String, material: Material = Material.Default, val aabb: AA
private var numIndices: Int = -1
private var numVertices: Int = -1
var canPick: Boolean = true
val aabb: AABB?
get() = getComponent<BoundingBoxProviderComponent>()?.provider?.invoke(this) ?: _aabb

companion object {
const val VertexIdx = 0
Expand Down Expand Up @@ -65,7 +68,7 @@ class Geometry(name: String, material: Material = Material.Default, val aabb: AA
}

override fun createInstance(): Node {
val node = Geometry(name = name, material = material, aabb = aabb)
val node = Geometry(name = name, material = material, _aabb = _aabb)
node.initializeFrom(this)
node.vao = vao
node.vertexBuffer = vertexBuffer
Expand Down

0 comments on commit e85ae8d

Please sign in to comment.