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 /* mingw32 does not like %hhu conversion type */
310 if (fscanf(file
, "%hu %hu %hu ", &r
, &g
, &b
) != 3) {
311 fprintf(stderr
, "Failed reading color values from %s: %s\n", filename
, strerror(errno
));
314 rgba_t rgba
= PACK(rgba
, 3, (uint8_t)r
);
315 rgba
= PACK(rgba
, 2, (uint8_t)g
);
316 rgba
= PACK(rgba
, 1, (uint8_t)b
);
317 rgba
= PACK(rgba
, 0, 255);
318 p
->pixels
[y
* w
+ x
] = color_from_rgba(rgba
);
328 fprintf(stderr
, "Unexpected file format in %s: %s\n", filename
, strerror(errno
));
332 raster_t
* raster_import_bmp(const char* filename
)
334 FILE* file
= fopen(filename
, "rb");
336 fprintf(stderr
, "Cannot read from %s: %s\n", filename
, strerror(errno
));
340 uint16_t magicNumber
;
341 uint16_t reserved0
;//0x4D41;
342 uint16_t reserved1
;//0x5454;
344 uint32_t infoHeaderSize
;
347 uint16_t colorPlanes
;
348 uint16_t bitsPerPixel
;
349 uint32_t compression
;
351 uint32_t horizontalResolution
;
352 uint32_t verticalResolution
;
353 uint32_t paletteColorCount
;
354 uint32_t importantPaletteColorCount
;
359 _DO_OR_DIE(fread(&magicNumber
, sizeof(magicNumber
), 1, file
));
360 _DO_OR_DIE(fread(&fileSize
, sizeof(fileSize
), 1, file
));
361 _DO_OR_DIE(fread(&reserved0
, sizeof(reserved0
), 1, file
));
362 _DO_OR_DIE(fread(&reserved1
, sizeof(reserved1
), 1, file
));
363 _DO_OR_DIE(fread(&dataOffset
, sizeof(dataOffset
), 1, file
));
364 _DO_OR_DIE(fread(&infoHeaderSize
, sizeof(infoHeaderSize
), 1, file
));
365 _DO_OR_DIE(fread(&width
, sizeof(width
), 1, file
));
366 _DO_OR_DIE(fread(&height
, sizeof(height
), 1, file
));
367 _DO_OR_DIE(fread(&colorPlanes
, sizeof(colorPlanes
), 1, file
));
368 _DO_OR_DIE(fread(&bitsPerPixel
, sizeof(bitsPerPixel
), 1, file
));
369 _DO_OR_DIE(fread(&compression
, sizeof(compression
), 1, file
));
370 _DO_OR_DIE(fread(&dataSize
, sizeof(dataSize
), 1, file
));
371 _DO_OR_DIE(fread(&horizontalResolution
, sizeof(horizontalResolution
), 1, file
));
372 _DO_OR_DIE(fread(&verticalResolution
, sizeof(verticalResolution
), 1, file
));
373 _DO_OR_DIE(fread(&paletteColorCount
, sizeof(paletteColorCount
), 1, file
));
374 _DO_OR_DIE(fread(&importantPaletteColorCount
, sizeof(importantPaletteColorCount
), 1, file
));
376 p
= raster_alloc((int)width
, (int)height
, COLOR_WHITE
);
378 size_t size
= width
* height
;
379 for (int i
= 0; i
< size
; ++i
)
382 _DO_OR_DIE(fread(&argb
, sizeof(argb
), 1, file
));
383 rgba_t rgba
= PACK(rgba
, 3, UNPACK(argb
, 2));
384 rgba
= PACK(rgba
, 2, UNPACK(argb
, 1));
385 rgba
= PACK(rgba
, 1, UNPACK(argb
, 0));
386 rgba
= PACK(rgba
, 0, UNPACK(argb
, 3));
387 p
->pixels
[i
] = color_from_rgba(rgba
);
398 fprintf(stderr
, "Unexpected file format in %s: %s\n", filename
, strerror(errno
));
405 void raster_draw_model(raster_t
* p
, const model_t
* model
)
408 #define PROGRESS_FMT "\033[80D\033[2K %s\t %9d / %d"
412 model_transformation(model
, &p
->model
);
414 raster_material(p
, model_specular(model
), model_shininess(model
));
416 IF_RENDER_PROGRESS(tri
= 0);
417 for (const list_t
* ti
= model_geometry(model
); ti
; ti
= ti
->link
) {
419 if (++tri
% 100 == 0) {
420 printf(PROGRESS_FMT
, model_name(model
), tri
, model_size(model
));
424 raster_draw_tri(p
, (tri_t
*)ti
->val
);
427 printf(PROGRESS_FMT
"\n", model_name(model
), tri
, model_size(model
));
433 * See if the triangle is at all visible in the viewport. Also, minimize the
434 * rectangle around the area that includes the triangle.
437 bool _try_clip(tri_t t
, int* left
, int* right
, int* bottom
, int* top
)
440 aabb_t box
= tri_aabb(t
);
441 if (box
.min
.z
< S(-1.0) || S(1.0) < box
.max
.z
) {
444 *left
= imax((int)scal_floor(box
.min
.x
), *left
);
445 *right
= imin((int)scal_ceil(box
.max
.x
), *right
);
446 if (*right
<= *left
) {
449 *bottom
= imax((int)scal_floor(box
.min
.y
), *bottom
);
450 *top
= imin((int)scal_ceil(box
.max
.y
), *top
);
451 if (*top
<= *bottom
) {
459 * See whether or not we need to draw based on the orientation of the
463 bool _try_cull_backface(tri_t t
)
466 vec_t n
= tri_normal(t
);
475 * Determine what color is associated with the given vertex.
478 color_t
_do_phong_lighting(raster_t
* p
, vert_t vert
)
481 vert
.c
= color_mult(vert
.c
, model_tcolor(p
->current
, vert
.t
));
484 color_t color
= COLOR_BLACK
;
486 for (list_t
* i
= p
->lights
; i
; i
= i
->link
) {
487 light_t light
= *(light_t
*)i
->val
;
489 vec_t lpos
= light
.v
;
492 vec_t l
= vec_normalize(vec_sub(lpos
, mpos
));
493 vec_t r
= vec_normalize(vec_sub(vec_scale(n
, S(2.0) * vec_dot(n
, l
)), l
));
494 vec_t v
= vec_normalize(vec_sub(vpos
, mpos
));
496 scal_t kd
= scal_max(vec_dot(l
, n
), S(0.0));
497 color_t Id
= color_scale2(light
.d
, vert
.c
, kd
);
498 scal_t ks
= scal_pow(scal_max(vec_dot(r
, v
), S(0.0)), p
->shininess
);
499 color_t Is
= color_scale2(light
.s
, p
->specular
, ks
);
501 color
= color_add2(color
, Id
, Is
);
503 color_t Ia
= color_mult(p
->ambient
, vert
.c
);
504 return color_clamp(color_add(color
, Ia
));
510 void raster_draw_tri(raster_t
* p
, const tri_t
* triangle
)
512 IF_RASTER_STATS(++p
->total
);
515 // need to recalculate the model-view-projection matrix if any one of its
516 // composing matrices have been changed
518 p
->modelviewprojection
= mat_mult(p
->view
, p
->model
);
519 p
->modelviewprojection
= mat_mult(p
->projection
, p
->modelviewprojection
);
523 t
= tri_transform(t
, p
->modelviewprojection
);
525 // save w-values for texture mapping perspective correction
532 if (!_try_cull_backface(t
)) {
533 IF_RASTER_STATS(++p
->culled
);
537 t
= tri_transform(t
, p
->viewport
);
540 int right
= p
->right
;
541 int bottom
= p
->bottom
;
544 if (!_try_clip(t
, &left
, &right
, &bottom
, &top
)) {
545 IF_RASTER_STATS(++p
->clipped
);
550 tri_t tl
= tri_transform(*triangle
, p
->model
);
557 vert_t tv
= vert_new(tri_midpoint(tl
));
558 tv
.n
= vec_normalize(tri_normal(tl
));
559 tv
.c
= tri_color(tl
);
560 color_t color
= _do_phong_lighting(p
, tv
);
561 #elif LIGHTING == 2 && SMOOTH_COLOR
562 color_t color1
= _do_phong_lighting(p
, tl
.a
);
563 color_t color2
= _do_phong_lighting(p
, tl
.b
);
564 color_t color3
= _do_phong_lighting(p
, tl
.c
);
565 #elif LIGHTING == 2 && !SMOOTH_COLOR
566 color_t c
= tri_color(t
);
567 tl
.a
.c
= tl
.b
.c
= tl
.c
.c
= c
;
568 color_t color1
= _do_phong_lighting(p
, tl
.a
);
569 color_t color2
= _do_phong_lighting(p
, tl
.b
);
570 color_t color3
= _do_phong_lighting(p
, tl
.c
);
571 #elif !LIGHTING && SMOOTH_COLOR
572 color_t color1
= t
.a
.c
;
573 color_t color2
= t
.b
.c
;
574 color_t color3
= t
.c
.c
;
576 color_t color
= tri_color(t
);
579 for (int y
= bottom
; y
< top
; ++y
) {
580 for (int x
= left
; x
< right
; ++x
) {
581 vec_t v
= vec_new((scal_t
)x
, (scal_t
)y
, S(0.0));
583 if (tri_barycentric(t
, b
, v
)) {
586 scal_t
* n
= p
->zbuf
+ y
* p
->w
+ x
;
587 if (S(-1.0) < v
.z
&& v
.z
< *n
) {
589 color_t
* c
= p
->pixels
+ y
* p
->w
+ x
;
592 #if LIGHTING == 2 || (!LIGHTING && SMOOTH_COLOR)
593 newC
= color_interp2(color1
, color2
, color3
, b
);
594 #elif LIGHTING == 3 && SMOOTH_COLOR
595 newC
= _do_phong_lighting(p
, tri_interp(tl
, b
));
596 #elif LIGHTING == 3 && !SMOOTH_COLOR
597 vert_t d
= vert_new(tri_point(t
, b
));
599 d
.n
= tri_normal2(t
, b
);
600 newC
= _do_phong_lighting(p
, d
);
606 *c
= color_blend(*c
, newC
);