]>
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 _scene_set_ambient(scene_t
* s
, FILE* file
);
22 static int _scene_add_light(scene_t
* s
, FILE* file
);
37 scene_t
* scene_alloc(FILE* file
)
40 double eyeX
, eyeY
, eyeZ
, spotX
, spotY
, spotZ
, upX
, upY
, upZ
;
41 double fovy
, aspect
, near
, far
;
42 if (fscanf(file
, "U3 %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
44 &eyeX
, &eyeY
, &eyeZ
, &spotX
, &spotY
, &spotZ
, &upX
, &upY
, &upZ
,
45 &fovy
, &aspect
, &near
, &far
) != 15) {
46 fprintf(stderr
, "Cannot read scene header.\n");
50 scene_t
* s
= (scene_t
*)mem_alloc(sizeof(scene_t
));
54 s
->view
= MAT_LOOKAT(vec_new( (scal_t
)eyeX
, (scal_t
)eyeY
, (scal_t
)eyeZ
),
55 vec_new((scal_t
)spotX
, (scal_t
)spotY
, (scal_t
)spotZ
),
56 vec_new( (scal_t
)upX
, (scal_t
)upY
, (scal_t
)upZ
));
57 s
->eye
= vec_new(eyeX
, eyeY
, eyeZ
);
58 s
->projection
= MAT_PERSPECTIVE((scal_t
)fovy
, (scal_t
)aspect
, (scal_t
)near
, (scal_t
)far
);
60 s
->ambient
= color_new(S(0.05), S(0.05), S(0.05), S(1.0));
67 fprintf(stderr, "Unexpected line before group is specified.\n"); \
72 while (fscanf(file
, " %c", &type
) == 1) {
75 if (fgets(filename
, 4096, file
) == NULL
) {
76 fprintf(stderr
, "Cannot read model filename.\n");
78 m
= model_alloc(trim(filename
));
82 list_push2(&s
->models
, m
, DTOR(model_destroy
));
87 if (_model_set_colors(m
, file
) != 0) {
94 if (_model_add_translate(m
, file
) != 0) {
100 if (_model_add_rotate(m
, file
) != 0) {
107 if (_model_add_scale(m
, file
) != 0) {
113 if (_scene_set_ambient(s
, file
) != 0) {
119 if (_scene_add_light(s
, file
) != 0) {
126 if (_model_set_material(m
, file
) != 0) {
135 fprintf(stderr
, "Unknown identifier: %c\n", type
);
148 void scene_destroy(scene_t
* s
)
150 list_destroy(&s
->models
);
151 list_destroy(&s
->lights
);
157 * Set the colors of the triangles in the group as defined in a file.
159 static int _model_set_colors(model_t
* m
, FILE* file
)
161 double r1
, g1
, b1
, r2
, g2
, b2
, r3
, g3
, b3
;
162 if (fscanf(file
, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
163 &r1
, &g1
, &b1
, &r2
, &g2
, &b2
, &r3
, &g3
, &b3
) != 9) {
164 fprintf(stderr
, "Cannot read color values from scene.\n");
168 for (const list_t
* i
= model_geometry(m
); i
; i
= i
->link
) {
169 tri_t
* t
= (tri_t
*)i
->val
;
170 t
->a
.c
= color_new((colorchan_t
)r1
, (colorchan_t
)g1
, (colorchan_t
)b1
, S(1.0));
171 t
->b
.c
= color_new((colorchan_t
)r2
, (colorchan_t
)g2
, (colorchan_t
)b2
, S(1.0));
172 t
->c
.c
= color_new((colorchan_t
)r3
, (colorchan_t
)g3
, (colorchan_t
)b3
, S(1.0));
178 * Concat a translation matrix to the transformation as defined in a file.
180 static int _model_add_translate(model_t
* m
, FILE* file
)
183 if (fscanf(file
, " %lf %lf %lf", &tx
, &ty
, &tz
) != 3) {
184 fprintf(stderr
, "Cannot read translate coordinates from scene.\n");
187 mat_t transform
= MAT_TRANSLATE((scal_t
)tx
, (scal_t
)ty
, (scal_t
)tz
);
188 model_transform(m
, &transform
);
193 * Concat a rotation matrix to the transformation as defined in a file.
195 static int _model_add_rotate(model_t
* m
, FILE* file
)
197 double theta
, ax
, ay
, az
;
198 if (fscanf(file
, " %lf %lf %lf %lf", &theta
, &ax
, &ay
, &az
) != 4) {
199 fprintf(stderr
, "Cannot read rotation angle from scene.\n");
202 mat_t transform
= MAT_ROTATE((scal_t
)theta
, (scal_t
)ax
, (scal_t
)ay
, (scal_t
)az
);
203 model_transform(m
, &transform
);
208 * Concat a scale matrix to the transformation as defined in a file.
210 static int _model_add_scale(model_t
* m
, FILE* file
)
213 if (fscanf(file
, " %lf %lf %lf", &sx
, &sy
, &sz
) != 3) {
214 fprintf(stderr
, "Cannot read scale factors from scene.\n");
217 mat_t transform
= MAT_SCALE((scal_t
)sx
, (scal_t
)sy
, (scal_t
)sz
);
218 model_transform(m
, &transform
);
223 * Set the specular highlight and shininess factor for the group.
225 static int _model_set_material(model_t
* m
, FILE* file
)
227 double sr
, sg
, sb
, shininess
;
228 if (fscanf(file
, " %lf %lf %lf %lf", &sr
, &sg
, &sb
, &shininess
) != 4) {
229 fprintf(stderr
, "Cannot read material information from scene.\n");
232 model_material(m
, color_new((scal_t
)sr
, (scal_t
)sg
, (scal_t
)sb
, S(1.0)), (scal_t
)shininess
);
237 * Set the ambient light properties of a scene.
239 static int _scene_set_ambient(scene_t
* s
, FILE* file
)
242 if (fscanf(file
, " %lf %lf %lf", &r
, &g
, &b
) != 3) {
243 fprintf(stderr
, "Cannot read ambient light from scene.\n");
246 s
->ambient
= color_new((scal_t
)r
, (scal_t
)g
, (scal_t
)b
, S(1.0));
251 * Add a light to the scene.
253 static int _scene_add_light(scene_t
* s
, FILE* file
)
255 double lx
, ly
, lz
, dr
, dg
, db
, sr
, sg
, sb
;
256 if (fscanf(file
, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
257 &lx
, &ly
, &lz
, &dr
, &dg
, &db
, &sr
, &sg
, &sb
) != 9) {
258 fprintf(stderr
, "Cannot read light values from scene.\n");
261 light_t
* l
= light_alloc(
263 color_new(dr
, dg
, db
, S(1.0)),
264 color_new(sr
, sg
, sb
, S(1.0))
266 list_push2(&s
->lights
, l
, mem_free
);
271 raster_t
* scene_render(scene_t
* s
)
277 raster_t
* p
= raster_alloc(s
->w
, s
->h
, COLOR_BLACK
);
278 raster_view(p
, &s
->view
);
279 raster_projection(p
, &s
->projection
);
280 raster_eye(p
, s
->eye
);
281 raster_ambient(p
, s
->ambient
);
283 for (list_t
* i
= s
->lights
; i
; i
= i
->link
) {
284 raster_light(p
, *(light_t
*)i
->val
);
288 printf("rendering scene...\n");
291 for (list_t
* gi
= s
->models
; gi
; gi
= gi
->link
) {
292 model_t
* m
= (model_t
*)gi
->val
;
293 raster_draw_model(p
, m
);
297 long dt
= timer_stop();
298 printf("render complete!\ntime\t%.3fms\n", (float)dt
/ 1000.0f
);
This page took 0.04965 seconds and 4 git commands to generate.