improve z-buffer implementation by removing the oddframe state
This commit is contained in:
parent
cdc427a54b
commit
3324e4f990
32
main.c
32
main.c
|
@ -17,14 +17,7 @@
|
|||
#define H 600
|
||||
#define PI 3.14159265358979323844
|
||||
|
||||
int oddframe = 1;
|
||||
|
||||
typedef struct {
|
||||
int bit;
|
||||
int32_t value;
|
||||
} zval;
|
||||
|
||||
zval zbuffer[W*H] = {0};
|
||||
int32_t zbuffer[W*H] = {INT32_MIN};
|
||||
|
||||
int wireframe = 1;
|
||||
|
||||
|
@ -349,10 +342,9 @@ draw_horizontal_line(
|
|||
|
||||
size_t i = x + y * W;
|
||||
|
||||
if (zbuffer[i].bit != oddframe || zbuffer[i].value < z) {
|
||||
if (zbuffer[i] < z) {
|
||||
canvas[i] = c;
|
||||
zbuffer[i].value = z;
|
||||
zbuffer[i].bit = oddframe;
|
||||
zbuffer[i] = z;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -724,8 +716,8 @@ main(void)
|
|||
|
||||
/* const mesh iso = load_mesh("resources/iso.mod"); */
|
||||
/* mesh iso_copy = copy_mesh(iso); */
|
||||
/* const mesh plane = load_mesh("resources/plane.mod"); */
|
||||
/* mesh plane_copy = copy_mesh(plane); */
|
||||
const mesh plane = load_mesh("resources/plane.mod");
|
||||
mesh plane_copy = copy_mesh(plane);
|
||||
const mesh monkey = load_mesh("resources/monkey.mod");
|
||||
mesh monkey_copy = copy_mesh(monkey);
|
||||
|
||||
|
@ -733,6 +725,9 @@ main(void)
|
|||
|
||||
scalar rot_speed = 0.6;
|
||||
for (;;) {
|
||||
/* clear z-buffer */
|
||||
for (size_t i = 0; i < W*H; i++) zbuffer[i] = INT32_MIN;
|
||||
|
||||
scalar elapsed, start = SDL_GetPerformanceCounter();
|
||||
|
||||
mat3x3 rotation = mat3x3_mul(
|
||||
|
@ -747,7 +742,11 @@ main(void)
|
|||
/* iso_copy.vertices[i] = vec3_mat3x3_mul(iso.vertices[i], rotation); */
|
||||
/* } */
|
||||
|
||||
/* draw_mesh(state.canvas, WHITE, cam, plane_copy); */
|
||||
for (size_t i = 0; i < monkey.vertex_num; i++) {
|
||||
monkey_copy.vertices[i].y = monkey.vertices[i].y + sin(t*0.01) * 2;
|
||||
}
|
||||
|
||||
draw_mesh(state.canvas, WHITE, cam, plane_copy);
|
||||
/* draw_mesh(state.canvas, WHITE, cam, iso_copy); */
|
||||
draw_mesh(state.canvas, WHITE, cam, monkey_copy);
|
||||
|
||||
|
@ -799,14 +798,13 @@ main(void)
|
|||
SDL_Delay(clamp(16.666f - elapsed, 0, 1000));
|
||||
|
||||
t++;
|
||||
oddframe = !oddframe;
|
||||
}
|
||||
|
||||
quit:
|
||||
/* free_mesh(iso); */
|
||||
/* free_mesh(iso_copy); */
|
||||
/* free_mesh(plane); */
|
||||
/* free_mesh(plane_copy); */
|
||||
free_mesh(plane);
|
||||
free_mesh(plane_copy);
|
||||
free_mesh(monkey);
|
||||
free_mesh(monkey_copy);
|
||||
free_sdl(state);
|
||||
|
|
Loading…
Reference in New Issue