]>
Dogcows Code - chaz/rasterize/blob - scene.c
3 * CS5600 University of Utah
5 * mcgarvey@eng.utah.edu
16 static int _model_set_colors(model_t
* m
, FILE* file
);
17 static int _model_add_translate(model_t
* m
, FILE* file
);
18 static int _model_add_rotate(model_t
* m
, FILE* file
);
19 static int _model_add_scale(model_t
* m
, FILE* file
);
20 static int _model_set_material(model_t
* m
, FILE* file
);
21 static int _model_set_texture(model_t
* m
, FILE* file
);
22 static int _scene_set_ambient(scene_t
* s
, FILE* file
);
23 static int _scene_add_light(scene_t
* s
, FILE* file
);
38 scene_t
* scene_alloc(FILE* file
)
41 double eyeX
, eyeY
, eyeZ
, spotX
, spotY
, spotZ
, upX
, upY
, upZ
;
42 double fovy
, aspect
, near
, far
;
44 if (fscanf(file
, "U4 %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
46 &eyeX
, &eyeY
, &eyeZ
, &spotX
, &spotY
, &spotZ
, &upX
, &upY
, &upZ
,
47 &fovy
, &aspect
, &near
, &far
,
48 &aR
, &aG
, &aB
) != 18) {
49 fprintf(stderr
, "Cannot read scene header.\n");
53 scene_t
* s
= (scene_t
*)mem_alloc(sizeof(scene_t
));
57 s
->view
= MAT_LOOKAT(vec_new( (scal_t
)eyeX
, (scal_t
)eyeY
, (scal_t
)eyeZ
),
58 vec_new((scal_t
)spotX
, (scal_t
)spotY
, (scal_t
)spotZ
),
59 vec_new( (scal_t
)upX
, (scal_t
)upY
, (scal_t
)upZ
));
60 s
->eye
= vec_new(eyeX
, eyeY
, eyeZ
);
61 s
->projection
= MAT_PERSPECTIVE((scal_t
)fovy
, (scal_t
)aspect
, (scal_t
)near
, (scal_t
)far
);
63 s
->ambient
= color_new((scal_t
)aR
, (scal_t
)aG
, (scal_t
)aB
, S(1.0));
70 fprintf(stderr, "Unexpected line before group is specified.\n"); \
75 while (fscanf(file
, " %c", &type
) == 1) {
78 if (fgets(filename
, 4096, file
) == NULL
) {
79 fprintf(stderr
, "Cannot read model filename.\n");
81 m
= model_alloc(trim(filename
));
85 list_push2(&s
->models
, m
, DTOR(model_destroy
));
90 if (_model_set_colors(m
, file
) != 0) {
97 if (_model_add_translate(m
, file
) != 0) {
103 if (_model_add_rotate(m
, file
) != 0) {
110 if (_model_add_scale(m
, file
) != 0) {
116 if (_scene_add_light(s
, file
) != 0) {
123 if (_model_set_material(m
, file
) != 0) {
130 if (_model_set_texture(m
, file
) != 0) {
139 fprintf(stderr
, "Unknown identifier: %c\n", type
);
145 list_reverse(&s
->models
);
153 void scene_destroy(scene_t
* s
)
155 list_destroy(&s
->models
);
156 list_destroy(&s
->lights
);
162 * Set the colors of the triangles in the group as defined in a file.
164 static int _model_set_colors(model_t
* m
, FILE* file
)
167 if (fscanf(file
, " %lf %lf %lf %lf", &a
, &r
, &g
, &b
) != 4) {
168 fprintf(stderr
, "Cannot read color values from scene.\n");
172 color_t color
= color_new((colorchan_t
)r
, (colorchan_t
)g
, (colorchan_t
)b
, (colorchan_t
)a
);
173 for (const list_t
* i
= model_geometry(m
); i
; i
= i
->link
) {
174 tri_t
* t
= (tri_t
*)i
->val
;
183 * Concat a translation matrix to the transformation as defined in a file.
185 static int _model_add_translate(model_t
* m
, FILE* file
)
188 if (fscanf(file
, " %lf %lf %lf", &tx
, &ty
, &tz
) != 3) {
189 fprintf(stderr
, "Cannot read translate coordinates from scene.\n");
192 mat_t transform
= MAT_TRANSLATE((scal_t
)tx
, (scal_t
)ty
, (scal_t
)tz
);
193 model_transform(m
, &transform
);
198 * Concat a rotation matrix to the transformation as defined in a file.
200 static int _model_add_rotate(model_t
* m
, FILE* file
)
202 double theta
, ax
, ay
, az
;
203 if (fscanf(file
, " %lf %lf %lf %lf", &theta
, &ax
, &ay
, &az
) != 4) {
204 fprintf(stderr
, "Cannot read rotation angle from scene.\n");
207 mat_t transform
= MAT_ROTATE((scal_t
)theta
, (scal_t
)ax
, (scal_t
)ay
, (scal_t
)az
);
208 model_transform(m
, &transform
);
213 * Concat a scale matrix to the transformation as defined in a file.
215 static int _model_add_scale(model_t
* m
, FILE* file
)
218 if (fscanf(file
, " %lf %lf %lf", &sx
, &sy
, &sz
) != 3) {
219 fprintf(stderr
, "Cannot read scale factors from scene.\n");
222 mat_t transform
= MAT_SCALE((scal_t
)sx
, (scal_t
)sy
, (scal_t
)sz
);
223 model_transform(m
, &transform
);
228 * Set the specular highlight and shininess factor for the group.
230 static int _model_set_material(model_t
* m
, FILE* file
)
232 double sr
, sg
, sb
, shininess
;
233 if (fscanf(file
, " %lf %lf %lf %lf", &sr
, &sg
, &sb
, &shininess
) != 4) {
234 fprintf(stderr
, "Cannot read material information from scene.\n");
237 model_material(m
, color_new((scal_t
)sr
, (scal_t
)sg
, (scal_t
)sb
, S(1.0)), (scal_t
)shininess
);
242 * Set the texture to be used while drawing the current model.
244 static int _model_set_texture(model_t
* m
, FILE* file
)
247 if (fgets(filename
, 4096, file
) == NULL
) {
248 fprintf(stderr
, "Cannot read texture filename from scene.\n");
252 model_texture(m
, (const void*)raster_import(trim(filename
)));
258 * Add a light to the scene.
260 static int _scene_add_light(scene_t
* s
, FILE* file
)
262 double lx
, ly
, lz
, r
, g
, b
;
263 if (fscanf(file
, " %lf %lf %lf %lf %lf %lf",
264 &lx
, &ly
, &lz
, &r
, &g
, &b
) != 6) {
265 fprintf(stderr
, "Cannot read light values from scene.\n");
268 light_t
* l
= light_alloc(
270 color_new((scal_t
)r
, (scal_t
)g
, (scal_t
)b
, S(1.0)),
271 color_new((scal_t
)r
, (scal_t
)g
, (scal_t
)b
, S(1.0))
273 list_push2(&s
->lights
, l
, mem_free
);
278 raster_t
* scene_render(scene_t
* s
)
284 raster_t
* p
= raster_alloc(s
->w
, s
->h
, COLOR_BLACK
);
285 raster_view(p
, &s
->view
);
286 raster_projection(p
, &s
->projection
);
287 raster_eye(p
, s
->eye
);
288 raster_ambient(p
, s
->ambient
);
290 for (list_t
* i
= s
->lights
; i
; i
= i
->link
) {
291 raster_light(p
, *(light_t
*)i
->val
);
295 printf("rendering scene...\n");
298 for (list_t
* gi
= s
->models
; gi
; gi
= gi
->link
) {
299 model_t
* m
= (model_t
*)gi
->val
;
300 raster_draw_model(p
, m
);
304 long dt
= timer_stop();
305 printf("render complete!\ntime\t%.3fms\n", (float)dt
/ 1000.0f
);
This page took 0.052201 seconds and 5 git commands to generate.