Skip to content

Commit

Permalink
Calculate and return global transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Feb 7, 2024
1 parent 6e60508 commit 4f6c6d5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import nz.net.ultraq.redhorizon.engine.graphics.imgui.ChangeEvent
import nz.net.ultraq.redhorizon.engine.graphics.imgui.ControlsOverlayRenderPass
import nz.net.ultraq.redhorizon.engine.graphics.imgui.ImGuiLayer
import nz.net.ultraq.redhorizon.engine.input.InputEventStream
import nz.net.ultraq.redhorizon.engine.scenegraph.Node
import nz.net.ultraq.redhorizon.engine.scenegraph.Scene

import org.joml.FrustumIntersection
Expand Down Expand Up @@ -211,9 +212,9 @@ class RenderPipeline implements AutoCloseable {
// Cull the list of renderable items to those just visible in the scene
averageNanos('objectCulling', 1f, logger) { ->
visibleElements.clear()
def frustumIntersection = new FrustumIntersection(camera.projection * camera.view)
scene.accept { element ->
if (element instanceof GraphicsElement && frustumIntersection.testPlaneXY(element.bounds)) {
var frustumIntersection = new FrustumIntersection(camera.projection * camera.view)
scene.accept { Node element ->
if (element instanceof GraphicsElement && frustumIntersection.testPlaneXY(element.globalBounds)) {
visibleElements << element
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class Node<T extends Node> implements SceneEvents, Scriptable<T>, Visitable {
final Rectanglef bounds = new Rectanglef()

protected Node parent
protected final List<Node> children = []
protected List<Node> children = []

private final Rectanglef globalBounds = new Rectanglef()
private final Matrix4f globalTransform = new Matrix4f()
private final Vector3f globalScale = new Vector3f()
private final Vector3f globalTranslate = new Vector3f()

@Override
void accept(SceneVisitor visitor) {
Expand All @@ -56,6 +61,40 @@ class Node<T extends Node> implements SceneEvents, Scriptable<T>, Visitable {
return this
}

/**
* Return the world-space bounds of this node. ie: the local bounds, then
* taking into account local and all parent/ancestor transforms along the path
* to this node.
*
* @return
*/
// TODO: Surely this is inefficient having to calculate this each time? 🤔
Rectanglef getGlobalBounds() {

getGlobalTransform()
var scale = globalTransform.getScale(globalScale)
var translate = globalTransform.getTranslation(globalTranslate)
return globalBounds.set(bounds)
.scale(scale.x, scale.y)
.translate(translate.x, translate.y)
}

/**
* Get the world-space transform of this node. ie: the local transform, then
* modified by all of the ancestor transforms along the path to this node.
* The result is stored in the private {@code globalTransform} property, and
* returned.
*
* @return
*/
// TODO: Surely this is inefficient having to calculate this each time? 🤔
private Matrix4f getGlobalTransform() {

return parent != null ?
globalTransform.mul(parent.globalTransform) :
globalTransform.set(transform)
}

/**
* An alias for {@link #addChild(Node)}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,13 @@ import nz.net.ultraq.redhorizon.filetypes.ImageFile

import org.joml.primitives.Rectanglef

import groovy.transform.TupleConstructor

/**
* A simple 2D sprite node. Contains a texture and coordinate data for what
* parts of that texture to render (ie: the texture represents a larger sprite
* sheet / texture atlas, so we need to know what part of that to render).
*
* @author Emanuel Rabina
*/
@TupleConstructor(includes = ['imageFile'])
class Sprite extends Node<Sprite> implements GraphicsElement {

final ImageFile imageFile
Expand All @@ -50,6 +47,12 @@ class Sprite extends Node<Sprite> implements GraphicsElement {
private Material material
private Rectanglef region

Sprite(ImageFile imageFile) {

bounds.set(0, 0, imageFile.width, imageFile.height)
this.imageFile = imageFile
}

@Override
void delete(GraphicsRenderer renderer) {
}
Expand Down

0 comments on commit 4f6c6d5

Please sign in to comment.