3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
22 int left
, right
, bottom
, top
;
26 mat_t modelviewprojection
;
29 const model_t
* current
;
45 raster_t
* raster_alloc(int width
, int height
, color_t fill
)
47 assert(0 < width
&& 0 < height
&& "zero-dimension raster not allowed");
48 size_t size
= width
* height
;
50 raster_t
* p
= (raster_t
*)mem_alloc(sizeof(raster_t
));
51 p
->pixels
= (color_t
*)mem_alloc(sizeof(color_t
) * size
);
54 raster_clear(p
, fill
);
55 raster_viewport(p
, 0, 0, width
, height
);
56 p
->model
= p
->view
= p
->projection
= MAT_IDENTITY
;
61 p
->ambient
= color_new(S(0.2), S(0.2), S(0.2), S(1.0));
63 p
->specular
= COLOR_WHITE
;
64 p
->shininess
= S(1.0);
67 p
->zbuf
= (scal_t
*)mem_alloc(sizeof(scal_t
) * size
);
68 for (size_t i
= 0; i
< size
; ++i
) {
75 void raster_destroy(raster_t
* p
)
80 list_destroy(&p
->lights
);
86 void raster_printstats(raster_t
* p
)
89 unsigned drawn
= p
->total
- p
->clipped
- p
->culled
;
90 float percent
= 100.0f
* (float)drawn
/ (float)p
->total
;
93 "drawn\t%u (%6.2f%%)\n"
94 "total\t%u\n", p
->culled
, p
->clipped
, drawn
, percent
, p
->total
);
99 color_t
raster_color(const raster_t
* p
, vec_t pt
)
101 int u
= (int)((scal_t
)p
->w
* pt
.x
);
102 int v
= (int)((scal_t
)p
->h
* pt
.y
);
103 return p
->pixels
[p
->w
* (p
->h
- v
- 1) + u
];
107 void raster_clear(raster_t
* p
, color_t fill
)
109 size_t size
= p
->w
* p
->h
;
110 for (int i
= 0; i
< size
; ++i
) {
121 void raster_viewport(raster_t
* p
, int x
, int y
, int width
, int height
)
124 p
->right
= x
+ width
;
127 p
->viewport
= MAT_VIEWPORT(x
, y
, width
, height
);
130 void raster_model(raster_t
* p
, const mat_t
* transform
)
132 p
->model
= *transform
;
136 void raster_view(raster_t
* p
, const mat_t
* transform
)
138 p
->view
= *transform
;
142 void raster_projection(raster_t
* p
, const mat_t
* transform
)
144 p
->projection
= *transform
;
149 void raster_eye(raster_t
* p
, vec_t eye
)
156 void raster_ambient(raster_t
* p
, color_t ambient
)
159 p
->ambient
= ambient
;
163 void raster_light(raster_t
* p
, light_t light
)
166 light_t
* l
= light_copy(light
);
167 list_push2(&p
->lights
, l
, mem_free
);
171 void raster_material(raster_t
* p
, color_t specular
, scal_t shininess
)
174 p
->specular
= specular
;
175 p
->shininess
= shininess
;
180 #define _DO_OR_DIE(X) if ((X) <= 0) goto fail
182 int raster_export_ppm(const raster_t
* p
, const char* filename
)
184 FILE* file
= fopen(filename
, "w");
186 fail
: fprintf(stderr
, "Cannot write to %s: %s\n", filename
, strerror(errno
));
190 _DO_OR_DIE(fprintf(file
, "P3\n%u %u\n255\n", p
->w
, p
->h
));
191 for (int y
= (int)p
->h
- 1; y
>= 0; --y
) {
192 for (int x
= 0; x
< p
->w
; ++x
) {
194 color_split(p
->pixels
[y
* p
->w
+ x
], &r
, &g
, &b
, NULL
);
195 _DO_OR_DIE(fprintf(file
, "%hhu %hhu %hhu\n", r
, g
, b
));
203 int raster_export_bmp(const raster_t
* p
, const char* filename
)
206 * This function was adapted from sample code provided with the assignment
209 FILE* file
= fopen(filename
, "wb");
211 fail
: fprintf(stderr
, "Cannot write to %s: %s\n", filename
, strerror(errno
));
215 uint16_t magicNumber
= 0x4D42;
216 uint16_t reserved0
= 0;//0x4D41;
217 uint16_t reserved1
= 0;//0x5454;
218 uint32_t dataOffset
= 54;
219 uint32_t infoHeaderSize
= 40;
220 uint32_t width
= p
->w
;
221 uint32_t height
= p
->h
;
222 uint16_t colorPlanes
= 1;
223 uint16_t bitsPerPixel
= 32;
224 uint32_t compression
= 0;
225 uint32_t dataSize
= width
* height
* bitsPerPixel
/ 8;
226 uint32_t horizontalResolution
= 2835;
227 uint32_t verticalResolution
= 2835;
228 uint32_t paletteColorCount
= 0;
229 uint32_t importantPaletteColorCount
= 0;
230 uint32_t fileSize
= 54 + dataSize
;
233 * Check the return values to avoid loud warnings.
235 _DO_OR_DIE(fwrite(&magicNumber
, sizeof(magicNumber
), 1, file
));
236 _DO_OR_DIE(fwrite(&fileSize
, sizeof(fileSize
), 1, file
));
237 _DO_OR_DIE(fwrite(&reserved0
, sizeof(reserved0
), 1, file
));
238 _DO_OR_DIE(fwrite(&reserved1
, sizeof(reserved1
), 1, file
));
239 _DO_OR_DIE(fwrite(&dataOffset
, sizeof(dataOffset
), 1, file
));
240 _DO_OR_DIE(fwrite(&infoHeaderSize
, sizeof(infoHeaderSize
), 1, file
));
241 _DO_OR_DIE(fwrite(&width
, sizeof(width
), 1, file
));
242 _DO_OR_DIE(fwrite(&height
, sizeof(height
), 1, file
));
243 _DO_OR_DIE(fwrite(&colorPlanes
, sizeof(colorPlanes
), 1, file
));
244 _DO_OR_DIE(fwrite(&bitsPerPixel
, sizeof(bitsPerPixel
), 1, file
));
245 _DO_OR_DIE(fwrite(&compression
, sizeof(compression
), 1, file
));
246 _DO_OR_DIE(fwrite(&dataSize
, sizeof(dataSize
), 1, file
));
247 _DO_OR_DIE(fwrite(&horizontalResolution
, sizeof(horizontalResolution
), 1, file
));
248 _DO_OR_DIE(fwrite(&verticalResolution
, sizeof(verticalResolution
), 1, file
));
249 _DO_OR_DIE(fwrite(&paletteColorCount
, sizeof(paletteColorCount
), 1, file
));
250 _DO_OR_DIE(fwrite(&importantPaletteColorCount
, sizeof(importantPaletteColorCount
), 1, file
));
252 size_t size
= width
* height
;
253 for (int i
= 0; i
< size
; ++i
)
255 rgbachan_t a
, r
, g
, b
;
256 color_split(p
->pixels
[i
], &r
, &g
, &b
, &a
);
257 uint32_t argb
= PACK(argb
, 3, a
);
258 argb
= PACK(argb
, 2, r
);
259 argb
= PACK(argb
, 1, g
);
260 argb
= PACK(argb
, 0, b
);
261 _DO_OR_DIE(fwrite(&argb
, sizeof(argb
), 1, file
));
269 raster_t
* raster_import(const char* filename
)
272 char* ext
= strrchr(filename
, '.');
278 if (strcmp(ext
, "bmp") == 0) {
279 return raster_import_bmp(filename
);
281 if (strcmp(ext
, "ppm") == 0) {
282 return raster_import_ppm(filename
);
286 fprintf(stderr
, "Unknown file type: %s", filename
);
290 raster_t
* raster_import_ppm(const char* filename
)
292 FILE* file
= fopen(filename
, "r");
294 fprintf(stderr
, "Cannot read from %s: %s\n", filename
, strerror(errno
));
299 if (fscanf(file
, "P3 %d %d 255 ", &w
, &h
) != 2) {
300 fprintf(stderr
, "Cannot read header from %s: %s\n", filename
, strerror(errno
));
304 raster_t
* p
= raster_alloc(w
, h
, COLOR_WHITE
);
306 for (int y
= h
- 1; y
>= 0; --y
) {
307 for (int x
= 0; x
< w
; ++x
) {
309 if (fscanf(file
, "%hhu %hhu %hhu ", &r
, &g
, &b
) != 3) {
310 fprintf(stderr
, "Failed reading color values from %s: %s\n", filename
, strerror(errno
));
313 rgba_t rgba
= PACK(rgba
, 3, r
);
314 rgba
= PACK(rgba
, 2, g
);
315 rgba
= PACK(rgba
, 1, b
);
316 rgba
= PACK(rgba
, 0, 255);
317 p
->pixels
[y
* w
+ x
] = color_from_rgba(rgba
);
327 fprintf(stderr
, "Unexpected file format in %s: %s\n", filename
, strerror(errno
));
331 raster_t
* raster_import_bmp(const char* filename
)
333 FILE* file
= fopen(filename
, "rb");
335 fprintf(stderr
, "Cannot read from %s: %s\n", filename
, strerror(errno
));
339 uint16_t magicNumber
;
340 uint16_t reserved0
;//0x4D41;
341 uint16_t reserved1
;//0x5454;
343 uint32_t infoHeaderSize
;
346 uint16_t colorPlanes
;
347 uint16_t bitsPerPixel
;
348 uint32_t compression
;
350 uint32_t horizontalResolution
;
351 uint32_t verticalResolution
;
352 uint32_t paletteColorCount
;
353 uint32_t importantPaletteColorCount
;
358 _DO_OR_DIE(fread(&magicNumber
, sizeof(magicNumber
), 1, file
));
359 _DO_OR_DIE(fread(&fileSize
, sizeof(fileSize
), 1, file
));
360 _DO_OR_DIE(fread(&reserved0
, sizeof(reserved0
), 1, file
));
361 _DO_OR_DIE(fread(&reserved1
, sizeof(reserved1
), 1, file
));
362 _DO_OR_DIE(fread(&dataOffset
, sizeof(dataOffset
), 1, file
));
363 _DO_OR_DIE(fread(&infoHeaderSize
, sizeof(infoHeaderSize
), 1, file
));
364 _DO_OR_DIE(fread(&width
, sizeof(width
), 1, file
));
365 _DO_OR_DIE(fread(&height
, sizeof(height
), 1, file
));
366 _DO_OR_DIE(fread(&colorPlanes
, sizeof(colorPlanes
), 1, file
));
367 _DO_OR_DIE(fread(&bitsPerPixel
, sizeof(bitsPerPixel
), 1, file
));
368 _DO_OR_DIE(fread(&compression
, sizeof(compression
), 1, file
));
369 _DO_OR_DIE(fread(&dataSize
, sizeof(dataSize
), 1, file
));
370 _DO_OR_DIE(fread(&horizontalResolution
, sizeof(horizontalResolution
), 1, file
));
371 _DO_OR_DIE(fread(&verticalResolution
, sizeof(verticalResolution
), 1, file
));
372 _DO_OR_DIE(fread(&paletteColorCount
, sizeof(paletteColorCount
), 1, file
));
373 _DO_OR_DIE(fread(&importantPaletteColorCount
, sizeof(importantPaletteColorCount
), 1, file
));
375 p
= raster_alloc((int)width
, (int)height
, COLOR_WHITE
);
377 size_t size
= width
* height
;
378 for (int i
= 0; i
< size
; ++i
)
381 _DO_OR_DIE(fread(&argb
, sizeof(argb
), 1, file
));
382 rgba_t rgba
= PACK(rgba
, 3, UNPACK(argb
, 2));
383 rgba
= PACK(rgba
, 2, UNPACK(argb
, 1));
384 rgba
= PACK(rgba
, 1, UNPACK(argb
, 0));
385 rgba
= PACK(rgba
, 0, UNPACK(argb
, 3));
386 p
->pixels
[i
] = color_from_rgba(rgba
);
397 fprintf(stderr
, "Unexpected file format in %s: %s\n", filename
, strerror(errno
));
404 void raster_draw_model(raster_t
* p
, const model_t
* model
)
407 #define PROGRESS_FMT "\033[80D\033[2K %s\t %9d / %d"
411 model_transformation(model
, &p
->model
);
413 raster_material(p
, model_specular(model
), model_shininess(model
));
415 IF_RENDER_PROGRESS(tri
= 0);
416 for (const list_t
* ti
= model_geometry(model
); ti
; ti
= ti
->link
) {
418 if (++tri
% 100 == 0) {
419 printf(PROGRESS_FMT
, model_name(model
), tri
, model_size(model
));
423 raster_draw_tri(p
, (tri_t
*)ti
->val
);
426 printf(PROGRESS_FMT
"\n", model_name(model
), tri
, model_size(model
));
432 * See if the triangle is at all visible in the viewport. Also, minimize the
433 * rectangle around the area that includes the triangle.
436 bool _try_clip(tri_t t
, int* left
, int* right
, int* bottom
, int* top
)
439 aabb_t box
= tri_aabb(t
);
440 if (box
.min
.z
< S(-1.0) || S(1.0) < box
.max
.z
) {
443 *left
= imax((int)scal_floor(box
.min
.x
), *left
);
444 *right
= imin((int)scal_ceil(box
.max
.x
), *right
);
445 if (*right
<= *left
) {
448 *bottom
= imax((int)scal_floor(box
.min
.y
), *bottom
);
449 *top
= imin((int)scal_ceil(box
.max
.y
), *top
);
450 if (*top
<= *bottom
) {
458 * See whether or not we need to draw based on the orientation of the
462 bool _try_cull_backface(tri_t t
)
465 vec_t n
= tri_normal(t
);
474 * Determine what color is associated with the given vertex.
477 color_t
_do_phong_lighting(raster_t
* p
, vert_t vert
)
480 vert
.c
= color_mult(vert
.c
, model_tcolor(p
->current
, vert
.t
));
483 color_t color
= COLOR_BLACK
;
485 for (list_t
* i
= p
->lights
; i
; i
= i
->link
) {
486 light_t light
= *(light_t
*)i
->val
;
488 vec_t lpos
= light
.v
;
491 vec_t l
= vec_normalize(vec_sub(lpos
, mpos
));
492 vec_t r
= vec_normalize(vec_sub(vec_scale(n
, S(2.0) * vec_dot(n
, l
)), l
));
493 vec_t v
= vec_normalize(vec_sub(vpos
, mpos
));
495 scal_t kd
= scal_max(vec_dot(l
, n
), S(0.0));
496 color_t Id
= color_scale2(light
.d
, vert
.c
, kd
);
497 scal_t ks
= scal_pow(scal_max(vec_dot(r
, v
), S(0.0)), p
->shininess
);
498 color_t Is
= color_scale2(light
.s
, p
->specular
, ks
);
500 color
= color_add2(color
, Id
, Is
);
502 color_t Ia
= color_mult(p
->ambient
, vert
.c
);
503 return color_clamp(color_add(color
, Ia
));
509 void raster_draw_tri(raster_t
* p
, const tri_t
* triangle
)
511 IF_RASTER_STATS(++p
->total
);
514 // need to recalculate the model-view-projection matrix if any one of its
515 // composing matrices have been changed
517 p
->modelviewprojection
= mat_mult(p
->view
, p
->model
);
518 p
->modelviewprojection
= mat_mult(p
->projection
, p
->modelviewprojection
);
522 t
= tri_transform(t
, p
->modelviewprojection
);
524 // save w-values for texture mapping perspective correction
531 if (!_try_cull_backface(t
)) {
532 IF_RASTER_STATS(++p
->culled
);
536 t
= tri_transform(t
, p
->viewport
);
539 int right
= p
->right
;
540 int bottom
= p
->bottom
;
543 if (!_try_clip(t
, &left
, &right
, &bottom
, &top
)) {
544 IF_RASTER_STATS(++p
->clipped
);
549 tri_t tl
= tri_transform(*triangle
, p
->model
);
556 vert_t tv
= vert_new(tri_midpoint(tl
));
557 tv
.n
= vec_normalize(tri_normal(tl
));
558 tv
.c
= tri_color(tl
);
559 color_t color
= _do_phong_lighting(p
, tv
);
560 #elif LIGHTING == 2 && SMOOTH_COLOR
561 color_t color1
= _do_phong_lighting(p
, tl
.a
);
562 color_t color2
= _do_phong_lighting(p
, tl
.b
);
563 color_t color3
= _do_phong_lighting(p
, tl
.c
);
564 #elif LIGHTING == 2 && !SMOOTH_COLOR
565 color_t c
= tri_color(t
);
566 tl
.a
.c
= tl
.b
.c
= tl
.c
.c
= c
;
567 color_t color1
= _do_phong_lighting(p
, tl
.a
);
568 color_t color2
= _do_phong_lighting(p
, tl
.b
);
569 color_t color3
= _do_phong_lighting(p
, tl
.c
);
570 #elif !LIGHTING && SMOOTH_COLOR
571 color_t color1
= t
.a
.c
;
572 color_t color2
= t
.b
.c
;
573 color_t color3
= t
.c
.c
;
575 color_t color
= tri_color(t
);
578 for (int y
= bottom
; y
< top
; ++y
) {
579 for (int x
= left
; x
< right
; ++x
) {
580 vec_t v
= vec_new((scal_t
)x
, (scal_t
)y
, S(0.0));
582 if (tri_barycentric(t
, b
, v
)) {
585 scal_t
* n
= p
->zbuf
+ y
* p
->w
+ x
;
586 if (S(-1.0) < v
.z
&& v
.z
< *n
) {
588 color_t
* c
= p
->pixels
+ y
* p
->w
+ x
;
591 #if LIGHTING == 2 || (!LIGHTING && SMOOTH_COLOR)
592 newC
= color_interp2(color1
, color2
, color3
, b
);
593 #elif LIGHTING == 3 && SMOOTH_COLOR
594 newC
= _do_phong_lighting(p
, tri_interp(tl
, b
));
595 #elif LIGHTING == 3 && !SMOOTH_COLOR
596 vert_t d
= vert_new(tri_point(t
, b
));
598 d
.n
= tri_normal2(t
, b
);
599 newC
= _do_phong_lighting(p
, d
);
605 *c
= color_blend(*c
, newC
);