3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
22 int left
, right
, bottom
, top
;
26 mat_t modelviewprojection
;
44 raster_t
* raster_alloc(int width
, int height
, color_t fill
)
46 assert(0 < width
&& 0 < height
&& "zero-dimension raster not allowed");
47 size_t size
= width
* height
;
49 raster_t
* p
= (raster_t
*)mem_alloc(sizeof(raster_t
));
50 p
->pixels
= (color_t
*)mem_alloc(sizeof(color_t
) * size
);
53 raster_clear(p
, fill
);
54 raster_viewport(p
, 0, 0, width
, height
);
55 p
->model
= p
->view
= p
->projection
= MAT_IDENTITY
;
59 p
->ambient
= color_new(S(0.2), S(0.2), S(0.2), S(1.0));
61 p
->specular
= COLOR_WHITE
;
62 p
->shininess
= S(1.0);
64 /*= light_new(COLOR_WHITE, vec_new(S(-2.0), S(4.0), S(0.0)));*/
65 p
->zbuf
= (scal_t
*)mem_alloc(sizeof(scal_t
) * size
);
66 for (size_t i
= 0; i
< size
; ++i
) {
73 void raster_destroy(raster_t
* p
)
78 list_destroy(&p
->lights
);
84 void raster_printstats(raster_t
* p
)
87 unsigned drawn
= p
->total
- p
->clipped
- p
->culled
;
88 float percent
= 100.0f
* (float)drawn
/ (float)p
->total
;
91 "drawn\t%u (%6.2f%%)\n"
92 "total\t%u\n", p
->culled
, p
->clipped
, drawn
, percent
, p
->total
);
97 void raster_clear(raster_t
* p
, color_t fill
)
99 size_t size
= p
->w
* p
->h
;
100 for (int i
= 0; i
< size
; ++i
) {
111 void raster_viewport(raster_t
* p
, int x
, int y
, int width
, int height
)
114 p
->right
= x
+ width
;
117 p
->viewport
= MAT_VIEWPORT(x
, y
, width
, height
);
120 void raster_model(raster_t
* p
, const mat_t
* transform
)
122 p
->model
= *transform
;
126 void raster_view(raster_t
* p
, const mat_t
* transform
)
128 p
->view
= *transform
;
132 void raster_projection(raster_t
* p
, const mat_t
* transform
)
134 p
->projection
= *transform
;
139 void raster_eye(raster_t
* p
, vec_t eye
)
146 void raster_ambient(raster_t
* p
, color_t ambient
)
149 p
->ambient
= ambient
;
153 void raster_light(raster_t
* p
, light_t light
)
156 light_t
* l
= light_copy(light
);
157 list_push2(&p
->lights
, l
, mem_free
);
161 void raster_material(raster_t
* p
, color_t specular
, scal_t shininess
)
164 p
->specular
= specular
;
165 p
->shininess
= shininess
;
170 #define _CHECK_WRITE(X) if ((X) <= 0) goto fail
172 int raster_export_ppm(const raster_t
* p
, const char* filename
)
174 FILE* file
= fopen(filename
, "w");
176 fail
: fprintf(stderr
, "Cannot write to %s: %s\n", filename
, strerror(errno
));
180 _CHECK_WRITE(fprintf(file
, "P3\n%u %u\n255\n", p
->w
, p
->h
));
181 for (int y
= (int)p
->h
- 1; y
>= 0; --y
) {
182 for (int x
= 0; x
< p
->w
; ++x
) {
184 color_split(p
->pixels
[y
* p
->w
+ x
], &r
, &g
, &b
, NULL
);
185 _CHECK_WRITE(fprintf(file
, "%hhu %hhu %hhu\n", r
, g
, b
));
193 int raster_export_bmp(const raster_t
* p
, const char* filename
)
196 * This function was adapted from sample code provided with the assignment
199 FILE* file
= fopen(filename
, "wb");
201 fail
: fprintf(stderr
, "Cannot write to %s: %s\n", filename
, strerror(errno
));
205 uint16_t magicNumber
= 0x4D42;
206 uint16_t reserved0
= 0;//0x4D41;
207 uint16_t reserved1
= 0;//0x5454;
208 uint32_t dataOffset
= 54;
209 uint32_t infoHeaderSize
= 40;
210 uint32_t width
= p
->w
;
211 uint32_t height
= p
->h
;
212 uint16_t colorPlanes
= 1;
213 uint16_t bitsPerPixel
= 32;
214 uint32_t compression
= 0;
215 uint32_t dataSize
= width
* height
* bitsPerPixel
/ 8;
216 uint32_t horizontalResolution
= 2835;
217 uint32_t verticalResolution
= 2835;
218 uint32_t paletteColorCount
= 0;
219 uint32_t importantPaletteColorCount
= 0;
220 uint32_t fileSize
= 54 + dataSize
;
223 * Check the return values to avoid loud warnings.
225 _CHECK_WRITE(fwrite(&magicNumber
, sizeof(magicNumber
), 1, file
));
226 _CHECK_WRITE(fwrite(&fileSize
, sizeof(fileSize
), 1, file
));
227 _CHECK_WRITE(fwrite(&reserved0
, sizeof(reserved0
), 1, file
));
228 _CHECK_WRITE(fwrite(&reserved1
, sizeof(reserved1
), 1, file
));
229 _CHECK_WRITE(fwrite(&dataOffset
, sizeof(dataOffset
), 1, file
));
230 _CHECK_WRITE(fwrite(&infoHeaderSize
, sizeof(infoHeaderSize
), 1, file
));
231 _CHECK_WRITE(fwrite(&width
, sizeof(width
), 1, file
));
232 _CHECK_WRITE(fwrite(&height
, sizeof(height
), 1, file
));
233 _CHECK_WRITE(fwrite(&colorPlanes
, sizeof(colorPlanes
), 1, file
));
234 _CHECK_WRITE(fwrite(&bitsPerPixel
, sizeof(bitsPerPixel
), 1, file
));
235 _CHECK_WRITE(fwrite(&compression
, sizeof(compression
), 1, file
));
236 _CHECK_WRITE(fwrite(&dataSize
, sizeof(dataSize
), 1, file
));
237 _CHECK_WRITE(fwrite(&horizontalResolution
, sizeof(horizontalResolution
), 1, file
));
238 _CHECK_WRITE(fwrite(&verticalResolution
, sizeof(verticalResolution
), 1, file
));
239 _CHECK_WRITE(fwrite(&paletteColorCount
, sizeof(paletteColorCount
), 1, file
));
240 _CHECK_WRITE(fwrite(&importantPaletteColorCount
, sizeof(importantPaletteColorCount
), 1, file
));
242 size_t size
= width
* height
;
243 for (int i
= 0; i
< size
; ++i
)
245 rgbachan_t a
, r
, g
, b
;
246 color_split(p
->pixels
[i
], &r
, &g
, &b
, &a
);
247 uint32_t argb
= PACK(argb
, 3, a
);
248 argb
= PACK(argb
, 2, r
);
249 argb
= PACK(argb
, 1, g
);
250 argb
= PACK(argb
, 0, b
);
251 _CHECK_WRITE(fwrite(&argb
, sizeof(argb
), 1, file
));
262 * See if the triangle is at all visible in the viewport. Also, minimize the
263 * rectangle around the area that includes the triangle.
266 bool _try_clip(tri_t t
, int* left
, int* right
, int* bottom
, int* top
)
269 aabb_t box
= tri_aabb(t
);
270 if (box
.min
.z
< S(-1.0) || S(1.0) < box
.max
.z
) {
273 *left
= imax((int)scal_floor(box
.min
.x
), *left
);
274 *right
= imin((int)scal_ceil(box
.max
.x
), *right
);
275 if (*right
<= *left
) {
278 *bottom
= imax((int)scal_floor(box
.min
.y
), *bottom
);
279 *top
= imin((int)scal_ceil(box
.max
.y
), *top
);
280 if (*top
<= *bottom
) {
288 * See whether or not we need to draw based on the orientation of the
292 bool _try_cull_backface(tri_t t
)
295 vec_t n
= tri_normal(t
);
304 * Determine what color is associated with the given vertex.
307 color_t
_get_vertex_color(raster_t
* p
, vert_t vert
)
310 color_t color
= COLOR_BLACK
;
311 for (list_t
* i
= p
->lights
; i
; i
= i
->link
) {
312 light_t light
= *(light_t
*)i
->val
;
314 vec_t lpos
= light
.v
;
317 vec_t l
= vec_normalize(vec_sub(lpos
, mpos
));
318 vec_t r
= vec_sub(vec_scale(n
, S(2.0) * vec_dot(n
, l
)), l
);
319 vec_t v
= vec_normalize(vec_sub(vpos
, mpos
));
321 scal_t kd
= scal_max(vec_dot(l
, n
), S(0.0));
322 color_t Id
= color_scale2(light
.d
, vert
.c
, kd
);
323 scal_t ks
= scal_pow(scal_max(vec_dot(r
, v
), S(0.0)), p
->shininess
);
324 color_t Is
= color_scale2(light
.s
, p
->specular
, ks
);
326 color
= color_add2(color
, Id
, Is
);
328 color_t Ia
= color_mult(p
->ambient
, vert
.c
);
329 return color_clamp(color_add(color
, Ia
));
335 void raster_draw_tri(raster_t
* p
, const tri_t
* triangle
)
337 IF_RASTER_STATS(++p
->total
);
340 // need to recalculate the model-view-projection matrix if any one of its
341 // composing matrices have been changed
343 p
->modelviewprojection
= mat_mult(p
->view
, p
->model
);
344 p
->modelviewprojection
= mat_mult(p
->projection
, p
->modelviewprojection
);
347 t
= tri_transform(t
, p
->modelviewprojection
);
350 if (!_try_cull_backface(t
)) {
351 IF_RASTER_STATS(++p
->culled
);
355 t
= tri_transform(t
, p
->viewport
);
358 int right
= p
->right
;
359 int bottom
= p
->bottom
;
362 if (!_try_clip(t
, &left
, &right
, &bottom
, &top
)) {
363 IF_RASTER_STATS(++p
->clipped
);
367 tri_t temp
= tri_transform(*triangle
, p
->model
);
368 #if LIGHTING && (!PRE_NORMALS || (!SMOOTH_COLOR && PRE_NORMALS >= 2))
369 temp
.a
.n
= temp
.b
.n
= temp
.c
.n
= vec_normalize(tri_normal(temp
));
372 temp
.a
.c
= tri_color(temp
);
374 color_t color1
= _get_vertex_color(p
, temp
.a
);
376 color_t color2
= _get_vertex_color(p
, temp
.b
);
377 color_t color3
= _get_vertex_color(p
, temp
.c
);
380 for (int y
= bottom
; y
< top
; ++y
) {
381 for (int x
= left
; x
< right
; ++x
) {
382 vec_t v
= vec_new((scal_t
)x
, (scal_t
)y
, S(0.0));
384 if (tri_barycentric(t
, b
, v
)) {
387 scal_t
* n
= p
->zbuf
+ y
* p
->w
+ x
;
388 if (S(-1.0) < v
.z
&& v
.z
< *n
) {
390 color_t
* c
= p
->pixels
+ y
* p
->w
+ x
;
392 *c
= color_interp2(color1
, color2
, color3
, b
);