add viewing_system

This is inspirted by the book "3D Computer Graphics" by Alan Watt.
This commit is contained in:
Zack Buhman 2024-01-06 18:43:57 +08:00
parent c9b57abf81
commit 6836790205
9 changed files with 6253 additions and 2 deletions

View File

@ -17,11 +17,10 @@
#include "holly/texture_memory_alloc.hpp"
#include "memorymap.hpp"
#include "geometry/geometry.hpp"
#include "geometry/cube.hpp"
#include "math/vec4.hpp"
using vec4 = vec<4, float>;
constexpr float half_degree = 0.01745329f / 2;
vec3 rotate(const vec3& vertex, float theta)

View File

@ -163,6 +163,18 @@ HEART_OBJ = \
example/heart.elf: LDSCRIPT = $(LIB)/alt.lds
example/heart.elf: $(START_OBJ) $(HEART_OBJ)
VIEWING_SYSTEM_OBJ = \
example/viewing_system.o \
vga.o \
holly/core.o \
holly/region_array.o \
holly/background.o \
holly/ta_fifo_polygon_converter.o \
$(LIBGCC)
example/viewing_system.elf: LDSCRIPT = $(LIB)/alt.lds
example/viewing_system.elf: $(START_OBJ) $(VIEWING_SYSTEM_OBJ)
MACAW_CUBE_OBJ = \
example/macaw_cube.o \
vga.o \

202
example/viewing_system.cpp Normal file
View File

@ -0,0 +1,202 @@
#include <cstdint>
#include "align.hpp"
#include "vga.hpp"
#include "holly/texture_memory_alloc.hpp"
#include "holly/holly.hpp"
#include "holly/core.hpp"
#include "holly/core_bits.hpp"
#include "holly/ta_fifo_polygon_converter.hpp"
#include "holly/ta_parameter.hpp"
#include "holly/ta_global_parameter.hpp"
#include "holly/ta_vertex_parameter.hpp"
#include "holly/ta_bits.hpp"
#include "holly/region_array.hpp"
#include "holly/background.hpp"
#include "holly/isp_tsp.hpp"
#include "memorymap.hpp"
#include "geometry/geometry.hpp"
#include "geometry/suzanne2.hpp"
#include "viewing_system/view_space.hpp"
#include "viewing_system/screen_space.hpp"
uint32_t _ta_parameter_buf[((32 * 8192) + 32) / 4];
struct viewer {
vec3 position;
vec3 orientation;
};
constexpr mat4x4 world_transform = { 1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 3.f,
0.f, 0.f, 0.f, 1.f };
void ta_upload(ta_parameter_writer& parameter,
const position__color * vertices,
const face_vtn * faces,
const uint32_t num_faces,
const mat4x4 screen_transform
)
{
const uint32_t parameter_control_word = para_control::para_type::polygon_or_modifier_volume
| para_control::list_type::opaque
| obj_control::col_type::floating_color
| obj_control::gouraud;
const uint32_t isp_tsp_instruction_word = isp_tsp_instruction_word::depth_compare_mode::greater
| isp_tsp_instruction_word::culling_mode::no_culling;
const uint32_t tsp_instruction_word = tsp_instruction_word::src_alpha_instr::one
| tsp_instruction_word::dst_alpha_instr::zero
| tsp_instruction_word::fog_control::no_fog;
for (uint32_t face_ix = 0; face_ix < num_faces; face_ix++) {
parameter.append<ta_global_parameter::polygon_type_0>() =
ta_global_parameter::polygon_type_0(parameter_control_word,
isp_tsp_instruction_word,
tsp_instruction_word,
0, // texture_control_word
0, // data_size_for_sort_dma
0 // next_address_for_sort_dma
);
auto& face = faces[face_ix];
constexpr uint32_t strip_length = 3;
mat4x4 transform = screen_transform * world_transform;
for (uint32_t i = 0; i < strip_length; i++) {
const uint32_t vertex_ix = face[i].vertex;
auto& position = vertices[vertex_ix].position;
auto& color = vertices[vertex_ix].color;
vec4 vertex = { position.x,
position.y,
position.z,
1.0f };
// in three-dimensional screen space
vec4 v = transform * vertex;
float x = v.x / v.w;
float y = v.y / v.w;
float z = v.w / v.z;
x = x * 240.f + 320.f;
y = y * 240.f + 240.f;
// perspective divide
bool end_of_strip = i == strip_length - 1;
parameter.append<ta_vertex_parameter::polygon_type_1>() =
ta_vertex_parameter::polygon_type_1(polygon_vertex_parameter_control_word(end_of_strip),
x,
y,
z,
1.0f, // alpha
color.r, // red
color.g, // green
color.b // blue
);
}
}
}
void init_texture_memory(const struct opb_size& opb_size)
{
auto mem = reinterpret_cast<volatile texture_memory_alloc *>(texture_memory32);
background_parameter(mem->background, 0xff220000);
holly.VO_BORDER_COL = 0x00220000;
region_array2(mem->region_array,
(offsetof (struct texture_memory_alloc, object_list)),
640 / 32, // width
480 / 32, // height
opb_size
);
}
void main()
{
vga();
// The address of `ta_parameter_buf` must be a multiple of 32 bytes.
// This is mandatory for ch2-dma to the ta fifo polygon converter.
uint32_t * ta_parameter_buf = align_32byte(_ta_parameter_buf);
constexpr uint32_t ta_alloc = ta_alloc_ctrl::pt_opb::no_list
| ta_alloc_ctrl::tm_opb::no_list
| ta_alloc_ctrl::t_opb::no_list
| ta_alloc_ctrl::om_opb::no_list
| ta_alloc_ctrl::o_opb::_16x4byte;
constexpr struct opb_size opb_size = { .opaque = 16 * 4
, .opaque_modifier = 0
, .translucent = 0
, .translucent_modifier = 0
, .punch_through = 0
};
holly.SOFTRESET = softreset::pipeline_soft_reset
| softreset::ta_soft_reset;
holly.SOFTRESET = 0;
core_init();
init_texture_memory(opb_size);
viewer viewer {
.position = {0.f, -3.f, 0.f},
.orientation = {0.f, -1.f, 0.f}, // approximate "up" orientation
};
vec3 plane_normal = view_space::viewing_direction(pi / 2.f, // azimuth
pi / 4.f // colatitude
);
vec3 up_vector = view_space::project_vector_to_plane(plane_normal,
viewer.orientation
);
const mat4x4 view_space_transform = view_space::transformation_matrix(viewer.position,
plane_normal,
up_vector);
const mat4x4 perspective_transform = screen_space::transformation_matrix(1.f, // the z-coordinate of the view window
100.f, // the z-coordinate of the far clip plane
1.f // the dimension of the square view window
);
const mat4x4 screen_transform = perspective_transform * view_space_transform;
uint32_t frame_ix = 0;
constexpr uint32_t num_frames = 1;
while (true) {
ta_polygon_converter_init(opb_size.total(),
ta_alloc,
640 / 32,
480 / 32);
auto parameter = ta_parameter_writer(ta_parameter_buf);
ta_upload(parameter,
suzanne::vertices,
suzanne::faces,
suzanne::num_faces,
screen_transform
);
// end of opaque list
parameter.append<ta_global_parameter::end_of_list>() = ta_global_parameter::end_of_list(para_control::para_type::end_of_list);
ta_polygon_converter_transfer(ta_parameter_buf, parameter.offset);
ta_wait_opaque_list();
core_start_render(frame_ix, num_frames);
v_sync_in();
core_wait_end_of_render_video(frame_ix, num_frames);
frame_ix += 1;
}
}

View File

@ -5,11 +5,14 @@
#include "math/vec2.hpp"
#include "math/vec3.hpp"
#include "math/vec4.hpp"
#include "math/mat4x4.hpp"
using vec2 = vec<2, float>;
using vec3 = vec<3, float>;
using vec4 = vec<4, float>;
using mat4x4 = mat<4, 4, float>;
struct vertex__texture__normal {
uint16_t vertex;
uint16_t texture;

2992
geometry/suzanne2.hpp Normal file

File diff suppressed because it is too large Load Diff

2976
geometry/suzanne2.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -139,6 +139,14 @@ inline constexpr T dot(vec<3, T> const& v1, vec<3, T> const& v2)
return tmp.x + tmp.y + tmp.z;
}
template <typename T>
inline constexpr vec<3, T> cross(vec<3, T> const& v1, vec<3, T> const& v2)
{
return vec<3, T>(v1.y * v2.z - v2.y * v1.z,
v1.z * v2.x - v2.z * v1.x,
v1.x * v2.y - v2.x * v1.y);
}
template <typename T>
inline constexpr vec<3, T> functor1(T (&func) (T const& x), vec<3, T> const& v)
{

View File

@ -0,0 +1,15 @@
#include "geometry/geometry.hpp"
namespace screen_space {
constexpr mat4x4 transformation_matrix(const float d, // the z-coordinate of the view window and the near clip plane
const float f, // the z-coordnate of the far clip plane
const float h // the dimension of the square view window
)
{
return { d/h, 0.f, 0.f , 0.f ,
0.f, d/h, 0.f , 0.f ,
0.f, 0.f, f/(f-d), -d*f/(f-d),
0.f, 0.f, 1.f , 0.f };
}
}

View File

@ -0,0 +1,44 @@
#include "geometry/geometry.hpp"
namespace view_space {
constexpr vec3 viewing_direction(const float azimuth,
const float colatitude
)
{
const float x = sin(colatitude) * cos(azimuth);
const float y = sin(colatitude) * sin(azimuth);
const float z = cos(colatitude);
return {x, y, z};
}
constexpr vec3 project_vector_to_plane(const vec3& n, // N: plane normal
const vec3& v_ // V': approximate "up" orientation
)
{
return v_ - dot(v_, n) * n;
}
constexpr mat4x4 transformation_matrix(const vec3& c, // C: in world space, the position of the viewer
const vec3& n, // N: in world space, the viewing direction
const vec3& v_ // V': approximate "up" orientation
)
{
const vec3 v = project_vector_to_plane(n, v_);
const vec3 u = cross(n, v);
const mat4x4 t = { 1.f, 0.f, 0.f, -c.x,
0.f, 1.f, 0.f, -c.y,
0.f, 0.f, 1.f, -c.z,
0.f, 0.f, 0.f, 1.f };
const mat4x4 r = { u.x, u.y, u.z, 0.f,
v.x, v.y, v.z, 0.f,
n.x, n.y, n.z, 0.f,
0.f, 0.f, 0.f, 1.f };
return r * t;
}
}