X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Frasterize;a=blobdiff_plain;f=raster.c;h=766e6ea7ba4ce6cb22ad0b687e5eccd8bc377c0e;hp=aca2c5a860315c0cc70f44c8cab3069fb1ce0441;hb=34efcdbb29b75754fef5066c5999671bc2d2fb12;hpb=aca1bceb5ab7dd87e910f0b8c5dba784ab6e8290 diff --git a/raster.c b/raster.c index aca2c5a..766e6ea 100644 --- a/raster.c +++ b/raster.c @@ -61,7 +61,7 @@ raster_t* raster_alloc(int width, int height, color_t fill) p->specular = COLOR_WHITE; p->shininess = S(1.0); #endif - /*= light_new(COLOR_WHITE, vec_new(S(-2.0), S(4.0), S(0.0)));*/ + p->zbuf = (scal_t*)mem_alloc(sizeof(scal_t) * size); for (size_t i = 0; i < size; ++i) { p->zbuf[i] = S(1.0); @@ -304,7 +304,7 @@ bool _try_cull_backface(tri_t t) * Determine what color is associated with the given vertex. */ INLINE_MAYBE -color_t _get_vertex_color(raster_t* p, vert_t vert) +color_t _do_phong_lighting(raster_t* p, vert_t vert) { #if LIGHTING color_t color = COLOR_BLACK; @@ -315,7 +315,7 @@ color_t _get_vertex_color(raster_t* p, vert_t vert) vec_t vpos = p->eye; vec_t n = vert.n; vec_t l = vec_normalize(vec_sub(lpos, mpos)); - vec_t r = vec_sub(vec_scale(n, S(2.0) * vec_dot(n, l)), l); + vec_t r = vec_normalize(vec_sub(vec_scale(n, S(2.0) * vec_dot(n, l)), l)); vec_t v = vec_normalize(vec_sub(vpos, mpos)); scal_t kd = scal_max(vec_dot(l, n), S(0.0)); @@ -364,17 +364,34 @@ void raster_draw_tri(raster_t* p, const tri_t* triangle) return; } - tri_t temp = tri_transform(*triangle, p->model); -#if LIGHTING && (!PRE_NORMALS || (!SMOOTH_COLOR && PRE_NORMALS >= 2)) - temp.a.n = temp.b.n = temp.c.n = vec_normalize(tri_normal(temp)); +#if LIGHTING >= 1 + tri_t tl = tri_transform(*triangle, p->model); +#if !PRE_NORMALS + tl.a.n = tl.b.n = tl.c.n = vec_normalize(tri_normal(tl)); #endif -#if !SMOOTH_COLOR - temp.a.c = tri_color(temp); #endif - color_t color1 = _get_vertex_color(p, temp.a); -#if SMOOTH_COLOR - color_t color2 = _get_vertex_color(p, temp.b); - color_t color3 = _get_vertex_color(p, temp.c); + +#if LIGHTING == 1 + vert_t tv = vert_new(tri_midpoint(tl)); + tv.n = vec_normalize(tri_normal(tl)); + tv.c = tri_color(tl); + color_t color = _do_phong_lighting(p, tv); +#elif LIGHTING == 2 && SMOOTH_COLOR + color_t color1 = _do_phong_lighting(p, tl.a); + color_t color2 = _do_phong_lighting(p, tl.b); + color_t color3 = _do_phong_lighting(p, tl.c); +#elif LIGHTING == 2 && !SMOOTH_COLOR + color_t c = tri_color(t); + tl.a.c = tl.b.c = tl.c.c = c; + color_t color1 = _do_phong_lighting(p, tl.a); + color_t color2 = _do_phong_lighting(p, tl.b); + color_t color3 = _do_phong_lighting(p, tl.c); +#elif !LIGHTING && SMOOTH_COLOR + color_t color1 = t.a.c; + color_t color2 = t.b.c; + color_t color3 = t.c.c; +#else + color_t color = tri_color(t); #endif for (int y = bottom; y < top; ++y) { @@ -388,11 +405,20 @@ void raster_draw_tri(raster_t* p, const tri_t* triangle) if (S(-1.0) < v.z && v.z < *n) { #endif color_t* c = p->pixels + y * p->w + x; -#if SMOOTH_COLOR + +#if LIGHTING == 2 || (!LIGHTING && SMOOTH_COLOR) *c = color_interp2(color1, color2, color3, b); +#elif LIGHTING == 3 && SMOOTH_COLOR + *c = _do_phong_lighting(p, tri_interp(tl, b)); +#elif LIGHTING == 3 && !SMOOTH_COLOR + vert_t d = vert_new(tri_point(t, b)); + d.c = tri_color(t); + d.n = tri_normal2(t, b); + *c = _do_phong_lighting(p, d); #else - *c = color1; + *c = color; #endif + #if DEPTH_TEST *n = v.z; }