added matrix code
[csgray] / src / csgray.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include "csgray.h"
6 #include "matrix.h"
7
8 enum {
9         OB_NULL,
10         OB_SPHERE,
11         OB_CYLINDER,
12         OB_PLANE,
13         OB_BOX,
14         OB_UNION,
15         OB_INTERSECTION,
16         OB_SUBTRACTION
17 };
18
19 struct object {
20         int type;
21
22         float r, g, b;
23         float emr, emg, emb;
24         float roughness;
25         float opacity;
26
27         float xform[16];
28
29         struct object *next;
30         struct object *clist, *ctail;
31         struct object *parent;
32 };
33
34 struct sphere {
35         struct object ob;
36         float rad;
37 };
38
39 struct plane {
40         struct object ob;
41         float nx, ny, nz;
42         float d;
43 };
44
45 union csg_object {
46         struct object ob;
47         struct sphere sph;
48         struct plane plane;
49 };
50
51 struct camera {
52         float x, y, z;
53         float tx, ty, tz;
54         float fov;
55 };
56
57 static struct camera cam;
58 static csg_object *root;
59
60 int csg_init(void)
61 {
62         if(!(root = csg_null(0, 0, 0))) {
63                 return -1;
64         }
65
66         csg_view(0, 0, 5, 0, 0, 0);
67         csg_fov(50);
68
69         return 0;
70 }
71
72 void csg_destroy(void)
73 {
74         csg_free_object(root);
75         root = 0;
76 }
77
78 void csg_view(float x, float y, float z, float tx, float ty, float tz)
79 {
80         cam.x = x;
81         cam.y = y;
82         cam.z = z;
83         cam.tx = tx;
84         cam.ty = ty;
85         cam.tz = tz;
86 }
87
88 void csg_fov(float fov)
89 {
90         cam.fov = M_PI * fov / 180.0f;
91 }
92
93
94 int csg_load(const char *fname)
95 {
96         return 0;       /* TODO */
97 }
98
99 int csg_save(const char *fname)
100 {
101         return 0;       /* TODO */
102 }
103
104 #define OBPTR(o) ((struct object*)(o))
105
106 void csg_add_object(csg_object *parent, csg_object *child)
107 {
108         if(!parent) {
109                 parent = root;
110         }
111
112         if(parent->ob.clist) {
113                 parent->ob.ctail->next = OBPTR(child);
114                 parent->ob.ctail = OBPTR(child);
115         } else {
116                 parent->ob.clist = parent->ob.ctail = OBPTR(child);
117         }
118         child->ob.parent = OBPTR(parent);
119 }
120
121 void csg_remove_object(csg_object *parent, csg_object *child)
122 {
123         csg_object *c;
124
125         if(!parent) {
126                 parent = root;
127         }
128
129         c = (csg_object*)parent->ob.clist;
130         while(c->ob.next) {
131                 if(c->ob.next == OBPTR(child)) {
132                         c->ob.next = child->ob.next;
133                         child->ob.next = 0;
134                         child->ob.parent = 0;
135                         return;
136                 }
137                 c = (csg_object*)c->ob.next;
138         }
139 }
140
141 void csg_free_object(csg_object *o)
142 {
143         csg_object *c = (csg_object*)o->ob.clist;
144         while(c) {
145                 csg_object *tmp = c;
146                 c = (csg_object*)c->ob.next;
147                 csg_free_object(tmp);
148         }
149         free(o);
150 }
151
152 static union csg_object *alloc_object(void)
153 {
154         csg_object *o;
155
156         if(!(o = calloc(sizeof *o, 1))) {
157                 return 0;
158         }
159         o->ob.type = OB_NULL;
160         mat4_identity(o->ob.xform);
161
162         csg_emission(o, 0, 0, 0);
163         csg_color(o, 1, 1, 1);
164         csg_roughness(o, 1);
165         csg_opacity(o, 1);
166
167         return o;
168 }
169
170 csg_object *csg_null(float x, float y, float z)
171 {
172         return alloc_object();
173 }
174
175 csg_object *csg_sphere(float x, float y, float z, float r)
176 {
177         csg_object *o;
178
179         if(!(o = alloc_object())) {
180                 return 0;
181         }
182
183         o->sph.rad = r;
184         mat4_translation(o->ob.xform, x, y, z);
185         return o;
186 }
187
188 csg_object *csg_cylinder(float x0, float y0, float z0, float x1, float y1, float z1, float r)
189 {
190         return 0;
191 }
192
193 csg_object *csg_plane(float x, float y, float z, float nx, float ny, float nz)
194 {
195         return 0;
196 }
197
198 csg_object *csg_box(float x, float y, float z, float xsz, float ysz, float zsz)
199 {
200         return 0;
201 }
202
203
204 csg_object *csg_union(csg_object *a, csg_object *b)
205 {
206         return 0;
207 }
208
209 csg_object *csg_intersection(csg_object *a, csg_object *b)
210 {
211         return 0;
212 }
213
214 csg_object *csg_subtraction(csg_object *a, csg_object *b)
215 {
216         return 0;
217 }
218
219
220 void csg_emission(csg_object *o, float r, float g, float b)
221 {
222 }
223
224 void csg_color(csg_object *o, float r, float g, float b)
225 {
226 }
227
228 void csg_roughness(csg_object *o, float r)
229 {
230 }
231
232 void csg_opacity(csg_object *o, float p)
233 {
234 }
235
236
237 void csg_render_pixel(int x, int y, float *color)
238 {
239 }
240
241 void csg_render_image(float *pixels, int width, int height)
242 {
243 }