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