Skip to content

Commit

Permalink
frustrum is managed by gfx now
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Dec 29, 2023
1 parent 1de9a90 commit 6427ac1
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 70 deletions.
22 changes: 10 additions & 12 deletions engine/src/drawables/terrain.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::{
bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext,
IndexType, PipelineBuilder, RenderParams, Texture, TextureBuilder, Uniform, TL,
};
use geom::{vec2, vec3, Camera, InfiniteFrustrum, Intersect3, Matrix4, Vec2, AABB3};
use std::sync::Arc;

use wgpu::{
BindGroupDescriptor, BindGroupLayoutDescriptor, BufferUsages, CommandEncoder,
CommandEncoderDescriptor, Extent3d, FilterMode, ImageCopyTexture, ImageDataLayout, IndexFormat,
Origin3d, RenderPass, RenderPipeline, RenderPipelineDescriptor, TextureFormat, TextureView,
VertexAttribute, VertexBufferLayout,
};

use geom::{vec2, vec3, Camera, Intersect3, Matrix4, Vec2, AABB3};

use crate::{
bg_layout_litmesh, pbuffer::PBuffer, CompiledModule, Drawable, FrameContext, GfxContext,
IndexType, PipelineBuilder, RenderParams, Texture, TextureBuilder, Uniform, TL,
};

const LOD: usize = 5;
const LOD_MIN_DIST_LOG2: f32 = 9.0; // 2^9 = 512, meaning until 1048m away, we use the highest lod
const MAX_HEIGHT: f32 = 2008.0;
Expand Down Expand Up @@ -203,12 +206,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
);
}

pub fn draw_terrain(
&mut self,
cam: &Camera,
frustrum: &InfiniteFrustrum,
fctx: &mut FrameContext<'_>,
) {
pub fn draw_terrain(&mut self, cam: &Camera, fctx: &mut FrameContext<'_>) {
profiling::scope!("terrain::draw_terrain");
let eye = cam.eye();

Expand All @@ -224,7 +222,7 @@ impl<const CSIZE: usize, const CRESOLUTION: usize> TerrainRender<CSIZE, CRESOLUT
let chunk_corner = vec2(x as f32, y as f32) * CSIZE as f32;
let chunk_center = chunk_corner + Vec2::splat(CSIZE as f32 * 0.5);

if !frustrum.intersects(&AABB3::new(
if !fctx.gfx.frustrum.intersects(&AABB3::new(
chunk_corner.z(MIN_HEIGHT),
chunk_corner.z0() + vec3(CSIZE as f32, CSIZE as f32, MAX_HEIGHT + 16.0),
)) {
Expand Down
2 changes: 1 addition & 1 deletion engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct GfxContext {
pub tick: u64,
pub(crate) pipelines: RefCell<Pipelines>,
pub(crate) projection: Uniform<Matrix4>,
pub(crate) frustrum: InfiniteFrustrum,
pub frustrum: InfiniteFrustrum,
pub(crate) sun_projection: [Uniform<Matrix4>; N_CASCADES],
pub render_params: Uniform<RenderParams>,
pub(crate) texture_cache_paths: FastMap<PathBuf, Arc<Texture>>,
Expand Down
7 changes: 4 additions & 3 deletions engine_demo/src/helmet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::DemoElement;
use engine::meshload::load_mesh;
use engine::{Context, FrameContext, InstancedMesh, InstancedMeshBuilder, MeshInstance};
use geom::{vec3, Camera, InfiniteFrustrum, LinearColor, Vec3};
use geom::{vec3, Camera, LinearColor, Vec3};

use crate::DemoElement;

pub struct Helmet {
mesh: Option<InstancedMesh>,
Expand Down Expand Up @@ -31,7 +32,7 @@ impl DemoElement for Helmet {

fn update(&mut self, _ctx: &mut Context, _cam: &Camera) {}

fn render(&mut self, fc: &mut FrameContext, _cam: &Camera, _frustrum: &InfiniteFrustrum) {
fn render(&mut self, fc: &mut FrameContext, _cam: &Camera) {
fc.draw(self.mesh.clone());
}
}
26 changes: 4 additions & 22 deletions engine_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait DemoElement {
where
Self: Sized;
fn update(&mut self, ctx: &mut Context, cam: &Camera);
fn render(&mut self, fc: &mut FrameContext, cam: &Camera, frustrum: &InfiniteFrustrum);
fn render(&mut self, fc: &mut FrameContext, cam: &Camera);
fn render_gui(&mut self, _ui: &mut egui::Ui) {}
}

Expand All @@ -31,10 +31,6 @@ struct State {

camera: Camera,
camera_speed: f32,
frustrum: InfiniteFrustrum,
last_cam: Camera,

freeze_cam: bool,

delta: f32,
play_queue: Vec<&'static str>,
Expand Down Expand Up @@ -84,9 +80,6 @@ impl engine::framework::State for State {
delta: 0.0,
play_queue: vec![],
camera_speed: 100.0,
frustrum: InfiniteFrustrum::new([Plane::X; 5]),
last_cam: camera,
freeze_cam: false,
ms_hist: History::new(128),
gfx_settings,
sun_angle: Degrees(0.0),
Expand Down Expand Up @@ -115,10 +108,8 @@ impl engine::framework::State for State {
params.sun_col = 4.0
* sun.z.max(0.0).sqrt().sqrt()
* LinearColor::new(1.0, 0.95 + sun.z * 0.05, 0.95 + sun.z * 0.05, 1.0);
if !self.freeze_cam {
params.cam_pos = self.camera.eye();
params.cam_dir = self.camera.dir();
}
params.cam_pos = self.camera.eye();
params.cam_dir = self.camera.dir();
params.sun = sun;
params.viewport = Vec2::new(gfx.size.0 as f32, gfx.size.1 as f32);
self.camera.dist = 300.0;
Expand Down Expand Up @@ -146,19 +137,11 @@ impl engine::framework::State for State {
}

fn render(&mut self, fc: &mut FrameContext) {
if !self.freeze_cam {
self.frustrum = InfiniteFrustrum::from_reversez_invviewproj(
self.camera.eye(),
fc.gfx.render_params.value().inv_proj,
);
self.last_cam = self.camera;
}

for (de, enabled) in &mut self.demo_elements {
if !*enabled {
continue;
}
de.render(fc, &self.last_cam, &self.frustrum);
de.render(fc, &self.camera);
}
}

Expand Down Expand Up @@ -193,7 +176,6 @@ impl engine::framework::State for State {
));

ui.add(egui::Slider::new(&mut self.camera_speed, 1.0..=100.0).text("Camera speed"));
ui.checkbox(&mut self.freeze_cam, "Freeze camera");

ui.checkbox(&mut self.gfx_settings.fullscreen, "Fullscreen");
ui.checkbox(&mut self.gfx_settings.vsync, "VSync");
Expand Down
7 changes: 4 additions & 3 deletions engine_demo/src/spheres.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::DemoElement;
use engine::meshload::load_mesh;
use engine::{
Context, FrameContext, InstancedMesh, InstancedMeshBuilder, Material, MeshInstance,
MetallicRoughness,
};
use geom::{vec3, Camera, InfiniteFrustrum, LinearColor, Vec3};
use geom::{vec3, Camera, LinearColor, Vec3};

use crate::DemoElement;

pub struct Spheres {
meshes: Vec<InstancedMesh>,
Expand Down Expand Up @@ -56,7 +57,7 @@ impl DemoElement for Spheres {

fn update(&mut self, _ctx: &mut Context, _cam: &Camera) {}

fn render(&mut self, fc: &mut FrameContext, _cam: &Camera, _frustrum: &InfiniteFrustrum) {
fn render(&mut self, fc: &mut FrameContext, _cam: &Camera) {
fc.draw(self.meshes.clone());
}
}
9 changes: 5 additions & 4 deletions engine_demo/src/terrain.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::DemoElement;
use engine::meshload::load_mesh;
use engine::terrain::TerrainRender as EngineTerrainRender;
use engine::{Context, FrameContext, InstancedMeshBuilder, MeshInstance};
use geom::{vec2, Camera, Heightmap, HeightmapChunk, InfiniteFrustrum, LinearColor, Vec3};
use geom::{vec2, Camera, Heightmap, HeightmapChunk, LinearColor, Vec3};

use crate::DemoElement;

const CSIZE: usize = 512;
const CRESO: usize = 16;
Expand Down Expand Up @@ -95,8 +96,8 @@ impl DemoElement for Terrain {
}
}

fn render(&mut self, fc: &mut FrameContext, cam: &Camera, frustrum: &InfiniteFrustrum) {
self.terrain.draw_terrain(cam, frustrum, fc);
fn render(&mut self, fc: &mut FrameContext, cam: &Camera) {
self.terrain.draw_terrain(cam, fc);

self.hitmesh.instances.clear();
if let Some(pos) = self.last_hitpos {
Expand Down
3 changes: 1 addition & 2 deletions native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ impl engine::framework::State for State {
&sim.map(),
time.seconds,
&camera.camera,
&camera.frustrum,
MapRenderOptions {
show_arrows: self.uiw.read::<Tool>().show_arrows(),
show_lots: self.uiw.read::<Tool>().show_lots(),
Expand Down Expand Up @@ -261,7 +260,7 @@ impl State {
.build_sun_shadowmap_matrix(
sun,
params.shadow_mapping_resolution as f32,
&camera.frustrum,
&ctx.gfx.frustrum,
)
.try_into()
.unwrap();
Expand Down
7 changes: 3 additions & 4 deletions native_app/src/rendering/map_rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,18 @@ impl MapRenderer {
map: &Map,
time: u32,
cam: &Camera,
frustrum: &InfiniteFrustrum,
options: MapRenderOptions,
draw: &mut ImmediateDraw,
ctx: &mut FrameContext<'_>,
) {
profiling::scope!("render map renderer");
self.terrain.draw(cam, frustrum, ctx);
self.terrain.draw(cam, ctx);

self.trees.draw(map, cam, frustrum, ctx);
self.trees.draw(map, cam, ctx);

self.meshb.latest_mesh(map, options, ctx);

Self::signals_render(map, time, cam, frustrum, draw);
Self::signals_render(map, time, cam, &ctx.gfx.frustrum, draw);

ctx.draw(self.water.clone());
}
Expand Down
6 changes: 3 additions & 3 deletions native_app/src/rendering/map_rendering/terrain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use engine::terrain::TerrainRender as EngineTerrainRender;
use engine::{Context, FrameContext, GfxContext};
use geom::{Camera, InfiniteFrustrum};
use geom::Camera;
use simulation::map::{Map, MapSubscriber, UpdateType};
use simulation::Simulation;

Expand All @@ -24,8 +24,8 @@ impl TerrainRender {
}
}

pub fn draw(&mut self, cam: &Camera, frustrum: &InfiniteFrustrum, fctx: &mut FrameContext<'_>) {
self.terrain.draw_terrain(cam, frustrum, fctx);
pub fn draw(&mut self, cam: &Camera, fctx: &mut FrameContext<'_>) {
self.terrain.draw_terrain(cam, fctx);
}

pub fn update(&mut self, ctx: &mut Context, map: &Map) {
Expand Down
18 changes: 5 additions & 13 deletions native_app/src/rendering/map_rendering/trees.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use std::ops::Mul;

use common::FastMap;
use engine::meshload::load_mesh;
use engine::wgpu::RenderPass;
use engine::{
Drawable, FrameContext, GfxContext, InstancedMesh, InstancedMeshBuilder, MeshInstance,
};
use geom::{
vec3, vec4, Camera, HeightmapChunk, InfiniteFrustrum, Intersect3, LinearColor, Matrix4, Vec3,
AABB3,
};
use geom::{vec3, vec4, Camera, HeightmapChunk, Intersect3, LinearColor, Matrix4, Vec3, AABB3};
use simulation::config;
use simulation::map::{Map, MapSubscriber, SubscriberChunkID, UpdateType};
use std::ops::Mul;

pub struct TreesRender {
tree_builder: InstancedMeshBuilder<false>,
Expand Down Expand Up @@ -56,13 +54,7 @@ impl TreesRender {
}
}

pub fn draw(
&mut self,
map: &Map,
cam: &Camera,
frustrum: &InfiniteFrustrum,
ctx: &mut FrameContext<'_>,
) {
pub fn draw(&mut self, map: &Map, cam: &Camera, ctx: &mut FrameContext<'_>) {
self.build(map, ctx);

if config().disable_trees {
Expand Down Expand Up @@ -113,7 +105,7 @@ impl TreesRender {
.map(HeightmapChunk::max_height)
.fold(0.0, f32::max);

if !frustrum.intersects(&AABB3::new_size(
if !ctx.gfx.frustrum.intersects(&AABB3::new_size(
cid.corner().z(-40.0),
vec3(
5.0 + SubscriberChunkID::SIZE_F32,
Expand Down
4 changes: 1 addition & 3 deletions native_app/src/rendering/orbit_camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use common::saveload::Encoder;
use engine::{Context, Tesselator};
use geom::{Camera, InfiniteFrustrum, Plane, Radians, Vec2, Vec3, AABB};
use geom::{Camera, Plane, Radians, Vec2, Vec3, AABB};
use simulation::map::pathfinding_crate::num_traits::Pow;

use crate::gui::windows::settings::Settings;
Expand All @@ -18,7 +18,6 @@ pub struct OrbitCamera {
pub targetyaw: Radians,
pub targetpitch: Radians,
pub targetdist: f32,
pub frustrum: InfiniteFrustrum,
}

impl OrbitCamera {
Expand Down Expand Up @@ -82,7 +81,6 @@ impl OrbitCamera {
targetyaw: camera.yaw,
targetpitch: camera.pitch,
targetdist: camera.dist,
frustrum: InfiniteFrustrum::new([Plane::new(Vec3::ZERO, 0.0); 5]),
}
}

Expand Down

0 comments on commit 6427ac1

Please sign in to comment.