16bit Z buffer and lores/hires switch
diff --git a/examples/3d-demo/3d-demo.cpp b/examples/3d-demo/3d-demo.cpp index 1037a68..6797468 100644 --- a/examples/3d-demo/3d-demo.cpp +++ b/examples/3d-demo/3d-demo.cpp
@@ -27,7 +27,7 @@ bool draw_lines = false; bool draw_vertices = false; -float* zbuffer; +int16_t* zbuffer; Object *link_object; Camera cam( @@ -41,10 +41,12 @@ constexpr uint32_t fixed_shift = 0; constexpr uint32_t fixed_mul = 1 << fixed_shift; -void init() { - set_screen_mode(lores); +ScreenMode screen_mode = hires; - zbuffer = new float[screen.bounds.w * screen.bounds.h]; +void init() { + set_screen_mode(hires); + + zbuffer = new int16_t[screen.bounds.w * screen.bounds.h]; link_object = Object::load_obj((char*)link_obj); main_texture = Surface::load(main_texture_packed); @@ -114,8 +116,8 @@ return; } -float near = 1.0f; -float far = 50.0f; +int16_t near = 1; +int16_t far = 50; void render(uint32_t time_ms) { // clear the screen buffer @@ -124,7 +126,7 @@ // reset the zbuffer for (uint32_t i = 0; i < screen.bounds.w * screen.bounds.h; i++) { - zbuffer[i] = -1.0f; + zbuffer[i] = -1; } uint32_t ms_start = now(); @@ -270,5 +272,10 @@ if(buttons.pressed & Button::Y) { draw_vertices = !draw_vertices; } + + if(buttons.pressed & Button::MENU) { + screen_mode = screen_mode == hires ? lores : hires; + set_screen_mode(screen_mode); + } }
diff --git a/examples/3d-demo/renderer.cpp b/examples/3d-demo/renderer.cpp index 1a25c6d..ab118d9 100644 --- a/examples/3d-demo/renderer.cpp +++ b/examples/3d-demo/renderer.cpp
@@ -18,7 +18,7 @@ uint32_t pixels_drawn = 0; -void draw_face(Vec3* vertices, Vec3* normals, Vec2* texture_coordinates, Surface* texture, Vec3 light, Pen *color, float* zbuffer, float near, float far) { +void draw_face(Vec3* vertices, Vec3* normals, Vec2* texture_coordinates, Surface* texture, Vec3 light, Pen *color, int16_t* zbuffer, int16_t near, int16_t far) { // convert vertices into Q8 integer points Point p0(vertices[0].x, vertices[0].y); Point p1(vertices[1].x, vertices[1].y); @@ -81,9 +81,9 @@ float beta = w1 * invarea; float gamma = 1.0f - alpha - beta; // ensures sum == 1.0f - float z = vertices[0].z * alpha + vertices[1].z * beta + vertices[2].z * gamma; + int16_t z = (vertices[0].z * alpha + vertices[1].z * beta + vertices[2].z * gamma) * 16384; - if (z > zbuffer[p.x + (p.y * screen.bounds.w)] && z > -1.0f) { + if (z > zbuffer[p.x + (p.y * screen.bounds.w)] && z > -1) { zbuffer[p.x + (p.y * screen.bounds.w)] = z; pixels_drawn++;
diff --git a/examples/3d-demo/renderer.hpp b/examples/3d-demo/renderer.hpp index c5efc61..c82338b 100644 --- a/examples/3d-demo/renderer.hpp +++ b/examples/3d-demo/renderer.hpp
@@ -6,4 +6,4 @@ extern uint32_t pixels_drawn; -void draw_face(Vec3* vertices, Vec3* normals, Vec2* texture_coordinates, Surface* texture, Vec3 light, Pen* color, float *zbuffer, float near, float far); \ No newline at end of file +void draw_face(Vec3* vertices, Vec3* normals, Vec2* texture_coordinates, Surface* texture, Vec3 light, Pen* color, int16_t *zbuffer, int16_t near, int16_t far); \ No newline at end of file