30 struct object *clist, *ctail;
31 struct object *parent;
57 static struct camera cam;
58 static csg_object *root;
62 if(!(root = csg_null(0, 0, 0))) {
66 csg_view(0, 0, 5, 0, 0, 0);
72 void csg_destroy(void)
74 csg_free_object(root);
78 void csg_view(float x, float y, float z, float tx, float ty, float tz)
88 void csg_fov(float fov)
90 cam.fov = M_PI * fov / 180.0f;
94 int csg_load(const char *fname)
99 int csg_save(const char *fname)
104 #define OBPTR(o) ((struct object*)(o))
106 void csg_add_object(csg_object *parent, csg_object *child)
112 if(parent->ob.clist) {
113 parent->ob.ctail->next = OBPTR(child);
114 parent->ob.ctail = OBPTR(child);
116 parent->ob.clist = parent->ob.ctail = OBPTR(child);
118 child->ob.parent = OBPTR(parent);
121 void csg_remove_object(csg_object *parent, csg_object *child)
129 c = (csg_object*)parent->ob.clist;
131 if(c->ob.next == OBPTR(child)) {
132 c->ob.next = child->ob.next;
134 child->ob.parent = 0;
137 c = (csg_object*)c->ob.next;
141 void csg_free_object(csg_object *o)
143 csg_object *c = (csg_object*)o->ob.clist;
146 c = (csg_object*)c->ob.next;
147 csg_free_object(tmp);
152 static union csg_object *alloc_object(void)
156 if(!(o = calloc(sizeof *o, 1))) {
159 o->ob.type = OB_NULL;
160 mat4_identity(o->ob.xform);
162 csg_emission(o, 0, 0, 0);
163 csg_color(o, 1, 1, 1);
170 csg_object *csg_null(float x, float y, float z)
172 return alloc_object();
175 csg_object *csg_sphere(float x, float y, float z, float r)
179 if(!(o = alloc_object())) {
184 mat4_translation(o->ob.xform, x, y, z);
188 csg_object *csg_cylinder(float x0, float y0, float z0, float x1, float y1, float z1, float r)
193 csg_object *csg_plane(float x, float y, float z, float nx, float ny, float nz)
198 csg_object *csg_box(float x, float y, float z, float xsz, float ysz, float zsz)
204 csg_object *csg_union(csg_object *a, csg_object *b)
209 csg_object *csg_intersection(csg_object *a, csg_object *b)
214 csg_object *csg_subtraction(csg_object *a, csg_object *b)
220 void csg_emission(csg_object *o, float r, float g, float b)
224 void csg_color(csg_object *o, float r, float g, float b)
228 void csg_roughness(csg_object *o, float r)
232 void csg_opacity(csg_object *o, float p)
237 void csg_render_pixel(int x, int y, float *color)
241 void csg_render_image(float *pixels, int width, int height)