]>
Dogcows Code - chaz/rasterize/blob - scene.c
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
20 * A group of triangles and a transformation.
27 typedef struct _group _group_t
;
30 * Allocate a group by reading raw triangle coordinates from a file.
32 static _group_t
* _group_alloc(const char* filename
)
34 FILE* file
= fopen(filename
, "r");
36 fprintf(stderr
, "Cannot read %s: %s\n", filename
, strerror(errno
));
40 _group_t
* g
= (_group_t
*)mem_alloc(sizeof(_group_t
));
42 g
->modelview
= MAT_IDENTITY
;
44 double x1
, y1
, z1
, x2
, y2
, z2
, x3
, y3
, z3
;
45 while (fscanf(file
, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
46 &x1
, &y1
, &z1
, &x2
, &y2
, &z2
, &x3
, &y3
, &z3
) == 9) {
48 vert_new2((scal_t
)x1
, (scal_t
)y1
, (scal_t
)z1
, COLOR_WHITE
),
49 vert_new2((scal_t
)x2
, (scal_t
)y2
, (scal_t
)z2
, COLOR_WHITE
),
50 vert_new2((scal_t
)x3
, (scal_t
)y3
, (scal_t
)z3
, COLOR_WHITE
)
52 list_push2(&g
->triangles
, t
, mem_free
);
54 list_reverse(&g
->triangles
);
58 if (g
->triangles
== NULL
) {
59 fprintf(stderr
, "No triangles coordinates read from %s\n", filename
);
70 static void _group_destroy(_group_t
* g
)
72 list_destroy(g
->triangles
);
78 * Set the colors of the triangles in the group as defined in a file.
80 static int _group_set_colors(_group_t
* g
, FILE* file
)
82 double r1
, g1
, b1
, r2
, g2
, b2
, r3
, g3
, b3
;
83 if (fscanf(file
, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
84 &r1
, &g1
, &b1
, &r2
, &g2
, &b2
, &r3
, &g3
, &b3
) != 9) {
85 fprintf(stderr
, "Cannot read color values from scene file.\n");
89 for (list_t
* i
= g
->triangles
; i
; i
= i
->link
) {
90 tri_t
* t
= (tri_t
*)i
->val
;
91 t
->a
.c
= color_new((colorchan_t
)r1
, (colorchan_t
)g1
, (colorchan_t
)b1
, S(1.0));
92 t
->b
.c
= color_new((colorchan_t
)r2
, (colorchan_t
)g2
, (colorchan_t
)b2
, S(1.0));
93 t
->c
.c
= color_new((colorchan_t
)r3
, (colorchan_t
)g3
, (colorchan_t
)b3
, S(1.0));
99 * Concat a translation matrix to the transformation as defined in a file.
101 static int _group_add_translate(_group_t
* g
, FILE* file
)
104 if (fscanf(file
, " %lf %lf", &tx
, &ty
) != 2) {
105 fprintf(stderr
, "Cannot read translate coordinates from scene file.\n");
109 g
->modelview
= mat_mult(g
->modelview
, MAT_TRANSLATE((scal_t
)tx
, (scal_t
)ty
, S(1.0)));
114 * Concat a rotation matrix to the transformation as defined in a file.
116 static int _group_add_rotate(_group_t
* g
, FILE* file
)
119 if (fscanf(file
, " %lf", &theta
) != 1) {
120 fprintf(stderr
, "Cannot read rotation angle from scene file.\n");
124 g
->modelview
= mat_mult(g
->modelview
, MAT_ROTATE_Z((scal_t
)theta
));
129 * Concat a scale matrix to the transformation as defined in a file.
131 static int _group_add_scale(_group_t
* g
, FILE* file
)
134 if (fscanf(file
, " %lf %lf", &sx
, &sy
) != 2) {
135 fprintf(stderr
, "Cannot read scale factors from scene file.\n");
139 g
->modelview
= mat_mult(g
->modelview
, MAT_SCALE((scal_t
)sx
, (scal_t
)sy
, S(1.0)));
152 scene_t
* scene_alloc(const char* filename
)
154 scene_t
* s
= (scene_t
*)mem_alloc(sizeof(scene_t
));
157 FILE* file
= fopen(filename
, "r");
160 double minX
, minY
, maxX
, maxY
;
161 if (fscanf(file
, "U2 %d %d %lf %lf %lf %lf",
162 &w
, &h
, &minX
, &minY
, &maxX
, &maxY
) != 6) {
163 fprintf(stderr
, "Cannot read scene file header.\n");
169 s
->projection
= MAT_ORTHO((scal_t
)minX
, (scal_t
)maxX
, (scal_t
)minY
, (scal_t
)maxY
);
171 char grp_filename
[4096];
176 fprintf(stderr, "Unexpected line before group is loaded.\n"); \
181 while (fscanf(file
, " %c", &type
) == 1) {
184 if (fgets(grp_filename
, 4096, file
) == NULL
) {
185 fprintf(stderr
, "Cannot read raw triangle filename.\n");
188 g
= _group_alloc(grp_filename
);
192 list_push2(&s
->groups
, g
, DTOR(_group_destroy
));
197 if (_group_set_colors(g
, file
) != 0) {
204 if (_group_add_translate(g
, file
) != 0) {
210 if (_group_add_rotate(g
, file
) != 0) {
217 if (_group_add_scale(g
, file
) != 0) {
223 fprintf(stderr
, "Unknown identifier: %c\n", type
);
226 list_reverse(&s
->groups
);
238 void scene_destroy(scene_t
* s
)
240 list_destroy(s
->groups
);
245 pixmap_t
* scene_render(scene_t
* s
)
247 pixmap_t
* pix
= pixmap_alloc(s
->w
, s
->h
, COLOR_BLACK
);
248 pixmap_projection(pix
, &s
->projection
);
250 for (list_t
* gi
= s
->groups
; gi
; gi
= gi
->link
) {
251 _group_t
* g
= (_group_t
*)gi
->val
;
252 pixmap_modelview(pix
, &g
->modelview
);
253 for (list_t
* ti
= g
->triangles
; ti
; ti
= ti
->link
) {
254 pixmap_draw_tri(pix
, (tri_t
*)ti
->val
);
This page took 0.049232 seconds and 4 git commands to generate.