]> Dogcows Code - chaz/rasterize/blob - scene.cc
begin work on ray tracer project
[chaz/rasterize] / scene.cc
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #include <cerrno>
9
10 #include "light.hh"
11 #include "list.hh"
12 #include "vec.hh"
13
14 #include "scene.hh"
15
16
17 static int _scene_add_light(scene_t* s, FILE* file);
18 static int _scene_add_sphere(scene_t* s, FILE* file);
19 static int _scene_add_plane(scene_t* s, FILE* file);
20
21
22 struct scene
23 {
24 int w, h;
25 vec_t eye;
26 vec_t spot;
27 vec_t up;
28 list_t* lights;
29 list_t* spheres;
30 list_t* planes;
31 color_t ambient;
32 };
33
34
35 scene_t* scene_alloc(FILE* file)
36 {
37 int w, h;
38 double eyeX, eyeY, eyeZ, spotX, spotY, spotZ, upX, upY, upZ;
39 double fovy, aspect;
40 double aR, aG, aB;
41 if (fscanf(file, "U5 %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
42 &w, &h,
43 &eyeX, &eyeY, &eyeZ, &spotX, &spotY, &spotZ, &upX, &upY, &upZ,
44 &fovy, &aspect,
45 &aR, &aG, &aB) != 16) {
46 fprintf(stderr, "Cannot read scene header.\n");
47 return NULL;
48 }
49
50 scene_t* s = (scene_t*)mem_alloc(sizeof(scene_t));
51 s->w = w;
52 s->h = h;
53 s->eye = vec_new(eyeX, eyeY, eyeZ);
54 s->spot = vec_new(spotX, spotY, spotZ);
55 s->up = vec_new(upX, upY, upZ);
56 // what to do with fovy and aspect...
57 s->lights = NULL;
58 s->spheres = NULL;
59 s->planes = NULL;
60 s->ambient = color_new((scal_t)aR, (scal_t)aG, (scal_t)aB, S(1.0));
61
62 char type;
63 while (fscanf(file, " %c", &type) == 1) {
64 switch (type) {
65 case 'l':
66 if (_scene_add_light(s, file) != 0) {
67 goto fail;
68 }
69 break;
70
71 case 's':
72 if (_scene_add_sphere(s, file) != 0) {
73 goto fail;
74 }
75 break;
76
77 case 'p':
78 if (_scene_add_plane(s, file) != 0) {
79 goto fail;
80 }
81 break;
82
83 case 'X':
84 goto done;
85
86 default:
87 fprintf(stderr, "Unknown identifier: %c\n", type);
88 }
89 }
90 #undef _ASSERT_G
91
92 done:
93 if (s->spheres) list_reverse(&s->spheres);
94 if (s->planes) list_reverse(&s->planes);
95 return s;
96
97 fail:
98 scene_destroy(s);
99 return NULL;
100 }
101
102 void scene_destroy(scene_t* s)
103 {
104 if (s->lights) list_destroy(&s->lights);
105 if (s->spheres) list_destroy(&s->spheres);
106 if (s->planes) list_destroy(&s->planes);
107 mem_free(s);
108 }
109
110
111 /*
112 * Add a light to the scene.
113 */
114 static int _scene_add_light(scene_t* s, FILE* file)
115 {
116 double lx, ly, lz, r, g, b;
117 if (fscanf(file, " %lf %lf %lf %lf %lf %lf",
118 &lx, &ly, &lz, &r, &g, &b) != 6) {
119 fprintf(stderr, "Cannot read light values from scene.\n");
120 return -1;
121 }
122 light_t* l = light_alloc(
123 vec_new(lx, ly, lz),
124 color_new((scal_t)r, (scal_t)g, (scal_t)b, S(1.0)),
125 color_new((scal_t)r, (scal_t)g, (scal_t)b, S(1.0))
126 );
127 list_push2(&s->lights, l, mem_free);
128 return 0;
129 }
130
131 static int _scene_add_sphere(scene_t* s, FILE* file)
132 {
133 double x, y, z, radius, r, g, b;
134 if (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf",
135 &x, &y, &z, &radius, &r, &g, &b) != 7) {
136 fprintf(stderr, "Cannot read sphere values from scene.\n");
137 return -1;
138 }
139 /*list_push2(&s->spheres, s, mem_free);*/
140 return 0;
141 }
142
143 static int _scene_add_plane(scene_t* s, FILE* file)
144 {
145 double x, y, z, nx, ny, nz, r, g, b;
146 if (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
147 &x, &y, &z, &nx, &ny, &nz, &r, &g, &b) != 9) {
148 fprintf(stderr, "Cannot read plane values from scene.\n");
149 return -1;
150 }
151 /*list_push2(&s->planes, s, mem_free);*/
152 return 0;
153 }
154
155
156 raster_t* scene_render(scene_t* s)
157 {
158 #if VERBOSITY >= 3
159 timer_start();
160 #endif
161
162 raster_t* p = raster_alloc(s->w, s->h, COLOR_BLACK);
163
164 /*for (list_t* i = s->lights; i; i = i->link) {*/
165 /*raster_light(p, *(light_t*)i->val);*/
166 /*}*/
167
168 #if VERBOSITY >= 3
169 printf("rendering scene...\n");
170 #endif
171
172 // RENDER IT
173
174 #if VERBOSITY >= 3
175 long dt = timer_stop();
176 printf("render complete!\ntime\t%.3fms\n", (float)dt / 1000.0f);
177 #endif
178
179 return p;
180 }
181
This page took 0.04231 seconds and 4 git commands to generate.