Prechádzať zdrojové kódy

world: immediate-mode aspect/view computation

Fixes: #196
Daniele Bartolini 1 rok pred
rodič
commit
1acb6b4ed4
4 zmenil súbory, kde vykonal 23 pridanie a 45 odobranie
  1. 1 0
      docs/changelog.rst
  2. 1 4
      src/device/device.cpp
  3. 19 25
      src/world/world.cpp
  4. 2 16
      src/world/world.h

+ 1 - 0
docs/changelog.rst

@@ -9,6 +9,7 @@ Changelog
 * Fixed some dialogs retaining old state when switching between projects.
 * Fixed the Project Browser not updating when adding/removing files.
 * Snapping is now disabled by default.
+* Fixed the Camera Compass and Gizmos not updating immediately in some circumnstances.
 
 **Runtime**
 

+ 1 - 4
src/device/device.cpp

@@ -721,11 +721,8 @@ void Device::render(World &world, UnitId camera_unit)
 		: _boot_config.aspect_ratio
 		);
 	CameraInstance camera = world.camera_instance(camera_unit);
-	world.camera_set_aspect(camera, aspect_ratio);
-	world.camera_set_viewport_metrics(camera, 0, 0, _width, _height);
-
 	const Matrix4x4 view = world.camera_view_matrix(camera);
-	const Matrix4x4 proj = world.camera_projection_matrix(camera);
+	const Matrix4x4 proj = world.camera_projection_matrix(camera, aspect_ratio);
 
 	const bgfx::Caps *caps = bgfx::getCaps();
 	f32 bx_ortho[16];

+ 19 - 25
src/world/world.cpp

@@ -11,6 +11,7 @@
 #include "core/math/vector4.inl"
 #include "core/memory/temp_allocator.inl"
 #include "core/strings/string_id.inl"
+#include "device/device.h"
 #include "lua/lua_environment.h"
 #include "resource/resource_manager.h"
 #include "resource/unit_resource.h"
@@ -322,7 +323,7 @@ ProjectionType::Enum World::camera_projection_type(CameraInstance camera)
 	return _camera[camera.i].projection_type;
 }
 
-Matrix4x4 World::camera_projection_matrix(CameraInstance camera)
+Matrix4x4 World::camera_projection_matrix(CameraInstance camera, f32 aspect_ratio)
 {
 	Camera &cam = _camera[camera.i];
 
@@ -331,8 +332,8 @@ Matrix4x4 World::camera_projection_matrix(CameraInstance camera)
 	switch (cam.projection_type) {
 	case ProjectionType::ORTHOGRAPHIC:
 		bx::mtxOrtho(bx_proj
-			, -cam.half_size * cam.aspect
-			, cam.half_size * cam.aspect
+			, -cam.half_size * aspect_ratio
+			, cam.half_size * aspect_ratio
 			, -cam.half_size
 			, cam.half_size
 			, cam.near_range
@@ -345,7 +346,7 @@ Matrix4x4 World::camera_projection_matrix(CameraInstance camera)
 	case ProjectionType::PERSPECTIVE:
 		bx::mtxProj(bx_proj
 			, fdeg(cam.fov)
-			, cam.aspect
+			, aspect_ratio
 			, cam.near_range
 			, cam.far_range
 			, caps->homogeneousDepth
@@ -378,11 +379,6 @@ void World::camera_set_fov(CameraInstance camera, f32 fov)
 	_camera[camera.i].fov = fov;
 }
 
-void World::camera_set_aspect(CameraInstance camera, f32 aspect)
-{
-	_camera[camera.i].aspect = aspect;
-}
-
 f32 World::camera_near_clip_distance(CameraInstance camera)
 {
 	return _camera[camera.i].near_range;
@@ -408,22 +404,17 @@ void World::camera_set_orthographic_size(CameraInstance camera, f32 half_size)
 	_camera[camera.i].half_size = half_size;
 }
 
-void World::camera_set_viewport_metrics(CameraInstance camera, u16 x, u16 y, u16 width, u16 height)
-{
-	_camera[camera.i].view_x = x;
-	_camera[camera.i].view_y = y;
-	_camera[camera.i].view_width = width;
-	_camera[camera.i].view_height = height;
-}
-
 Vector3 World::camera_screen_to_world(CameraInstance camera, const Vector3 &pos)
 {
-	const f32 w = _camera[camera.i].view_width;
-	const f32 h = _camera[camera.i].view_height;
+	u16 view_width;
+	u16 view_height;
+	device()->resolution(view_width, view_height);
+	const f32 w = (f32)view_width;
+	const f32 h = (f32)view_height;
 
 	TransformInstance ti = _scene_graph->instance(_camera[camera.i].unit);
 
-	Matrix4x4 projection = camera_projection_matrix(camera);
+	Matrix4x4 projection = camera_projection_matrix(camera, w/h);
 	Matrix4x4 world_inv = _scene_graph->world_pose(ti);
 	invert(world_inv);
 	Matrix4x4 mvp = world_inv * projection;
@@ -445,14 +436,17 @@ Vector3 World::camera_screen_to_world(CameraInstance camera, const Vector3 &pos)
 
 Vector3 World::camera_world_to_screen(CameraInstance camera, const Vector3 &pos)
 {
-	const f32 x = _camera[camera.i].view_x;
-	const f32 y = _camera[camera.i].view_y;
-	const f32 w = _camera[camera.i].view_width;
-	const f32 h = _camera[camera.i].view_height;
+	u16 view_width;
+	u16 view_height;
+	device()->resolution(view_width, view_height);
+	const f32 x = 0.0f;
+	const f32 y = 0.0f;
+	const f32 w = (f32)view_width;
+	const f32 h = (f32)view_height;
 
 	TransformInstance ti = _scene_graph->instance(_camera[camera.i].unit);
 
-	Matrix4x4 projection = camera_projection_matrix(camera);
+	Matrix4x4 projection = camera_projection_matrix(camera, w/h);
 	Matrix4x4 world_inv = _scene_graph->world_pose(ti);
 	invert(world_inv);
 

+ 2 - 16
src/world/world.h

@@ -30,17 +30,9 @@ struct World
 
 		Frustum frustum;
 		f32 fov;
-		f32 aspect;
 		f32 near_range;
 		f32 far_range;
-
-		// Orthographic projection only
-		f32 half_size;
-
-		u16 view_x;
-		u16 view_y;
-		u16 view_width;
-		u16 view_height;
+		f32 half_size; // Orthographic projection only.
 	};
 
 	u32 _marker;
@@ -117,7 +109,7 @@ struct World
 	ProjectionType::Enum camera_projection_type(CameraInstance camera);
 
 	/// Returns the projection matrix of the @a camera.
-	Matrix4x4 camera_projection_matrix(CameraInstance camera);
+	Matrix4x4 camera_projection_matrix(CameraInstance camera, f32 aspect_ratio);
 
 	/// Returns the view matrix of the @a camera.
 	Matrix4x4 camera_view_matrix(CameraInstance camera);
@@ -128,9 +120,6 @@ struct World
 	/// Sets the field-of-view of the @a camera in degrees.
 	void camera_set_fov(CameraInstance camera, f32 fov);
 
-	/// Sets the aspect ratio of the @a camera. (Perspective projection only.)
-	void camera_set_aspect(CameraInstance camera, f32 aspect);
-
 	/// Returns the near clip distance of the @a camera.
 	f32 camera_near_clip_distance(CameraInstance camera);
 
@@ -147,9 +136,6 @@ struct World
 	/// The horizontal size is proportional to the viewport's aspect ratio.
 	void camera_set_orthographic_size(CameraInstance camera, f32 half_size);
 
-	/// Sets the coordinates for the @a camera viewport in pixels.
-	void camera_set_viewport_metrics(CameraInstance camera, u16 x, u16 y, u16 width, u16 height);
-
 	/// Returns @a pos from screen-space to world-space coordinates.
 	Vector3 camera_screen_to_world(CameraInstance camera, const Vector3 &pos);