Skip to content

Commit

Permalink
A little bit of work on culling and rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Feb 2, 2024
1 parent 06c5e9e commit b8adc08
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
15 changes: 6 additions & 9 deletions modules/graphics/src/rendering/culling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ bitflags! {

impl<'a> VisibleObjectSet<'a> {
/// Groups the objects by material sorting key.
pub fn group_by_material(
&self,
required_flags: MaterialFlags,
) -> impl Iterator<Item = (&'a Material, &[VisibleObject<'a>])> {
///
/// The flags parameter can be used to filter the materials that are included
/// in the result. Only materials that have all the specified flags will be
/// included in the result.
pub fn group_by_material(&self, flags: MaterialFlags) -> impl Iterator<Item = (&'a Material, &[VisibleObject<'a>])> {
self
.objects
.chunk_by(|a, b| {
Expand All @@ -66,11 +67,7 @@ impl<'a> VisibleObjectSet<'a> {

a == b
})
.filter(move |it| {
let flags = it[0].material.flags();

flags.contains(required_flags)
})
.filter(move |it| flags.contains(it[0].material.flags()))
.map(|it| (it[0].material, it))
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/graphics/src/rendering/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> RenderFrame<'a> {
self.queue.set_material(material);

for entry in group {
entry.object.render(self)
entry.object.render(self);
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions modules/scene/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use std::fmt::{Debug, Formatter};

use bitflags::bitflags;
use common::{Camera, FastHashMap, FastHashSet, FromRandom, Frustum};
use graphics::{RenderFrame, RenderObject, RenderScene, VisibleObjectSet};

use super::*;

mod rendering;

common::impl_arena_index!(SceneNodeId, "Identifies a node in a scene graph.");

bitflags! {
Expand Down Expand Up @@ -157,6 +156,30 @@ impl<'a, T: Transform> Drop for SceneGraph<'a, T> {
}
}

impl<'a, T: Transform> RenderScene for SceneGraph<'a, T> {
fn cameras(&self) -> Vec<&dyn Camera> {
todo!()
}

fn cull_visible_objects(&self, camera: &dyn Camera) -> VisibleObjectSet<'a> {
let frustum = camera.frustum();
let objects = Vec::new();

// walk the tree and find visible objects
self.root.walk_recursive(|node| {
if !node.is_visible_to(&frustum) {
return false;
}

// TODO: push the object to the list

true
});

VisibleObjectSet { frustum, objects }
}
}

/// A node in a [`SceneGraph`].
///
/// A node is a sub-tree of [`SceneNode`]s that represent a scene in a
Expand Down Expand Up @@ -668,6 +691,14 @@ impl<'a, T: Transform> IntoIterator for &'a SceneNode<'a, T> {
}
}

impl<'a, T: Transform> RenderObject for SceneNode<'a, T> {
fn render(&self, frame: &mut RenderFrame<'_>) {
for component in &self.components {
component.on_draw(frame.renderer);
}
}
}

/// A utility builder for [`SceneNode`]s.
#[must_use]
#[derive(Default)]
Expand Down
39 changes: 0 additions & 39 deletions modules/scene/src/graph/rendering.rs

This file was deleted.

0 comments on commit b8adc08

Please sign in to comment.