#include <stdlib.h>
#include <string.h>
#include <math.h>
+#include <float.h>
+#include <treestore.h>
#include "csgimpl.h"
#include "matrix.h"
#include "geom.h"
static void shade(float *col, struct ray *ray, struct hit *hit);
static void background(float *col, struct ray *ray);
static int find_intersection(struct ray *ray, struct hit *best);
+static csg_object *load_object(struct ts_node *node);
+static float ambient[3];
static struct camera cam;
static csg_object *oblist;
+static csg_object *plights;
int csg_init(void)
{
oblist = 0;
+ plights = 0;
+
+ csg_ambient(0, 0, 0);
csg_view(0, 0, 5, 0, 0, 0);
csg_fov(50);
int csg_load(const char *fname)
{
- return 0; /* TODO */
+ struct ts_node *root = 0, *c;
+
+ if(!(root = ts_load(fname))) {
+ fprintf(stderr, "failed to open %s\n", fname);
+ return -1;
+ }
+ if(strcmp(root->name, "scene") != 0) {
+ fprintf(stderr, "invalid scene file: %s\n", fname);
+ goto err;
+ }
+
+ c = root->child_list;
+ while(c) {
+ csg_object *o = load_object(c);
+ if(!o) goto err;
+ csg_add_object(o);
+ c = c->next;
+ }
+
+ ts_free_tree(root);
+ return 0;
+
+err:
+ if(root) {
+ ts_free_tree(root);
+ }
+ return -1;
}
int csg_save(const char *fname)
{
- return 0; /* TODO */
+ return -1; /* TODO */
}
void csg_add_object(csg_object *o)
{
o->ob.next = oblist;
oblist = o;
+
+ if(o->ob.type == OB_NULL && (o->ob.emr > 0.0f || o->ob.emg > 0.0f || o->ob.emb > 0.0f)) {
+ o->ob.plt_next = plights;
+ plights = o;
+ }
}
int csg_remove_object(csg_object *o)
csg_object *csg_null(float x, float y, float z)
{
- return alloc_object(OB_NULL);
+ csg_object *o;
+
+ if(!(o = alloc_object(OB_NULL))) {
+ return 0;
+ }
+
+ mat4_translation(o->ob.xform, x, y, z);
+ return o;
}
csg_object *csg_sphere(float x, float y, float z, float r)
o->plane.ny = ny;
o->plane.nz = nz;
o->plane.d = x * nx + y * ny + z * nz;
- return 0;
+ return o;
}
csg_object *csg_box(float x, float y, float z, float xsz, float ysz, float zsz)
return o;
}
+void csg_ambient(float r, float g, float b)
+{
+ ambient[0] = r;
+ ambient[1] = g;
+ ambient[2] = b;
+}
void csg_emission(csg_object *o, float r, float g, float b)
{
o->ob.opacity = p;
}
+void csg_metallic(csg_object *o, int m)
+{
+ o->ob.metallic = m;
+}
+
void csg_render_pixel(int x, int y, int width, int height, float aspect, float *color)
{
static void calc_primary_ray(struct ray *ray, int x, int y, int w, int h, float aspect)
{
- float px, py;
-
- px = aspect * ((float)x / (float)w * 2.0f - 1.0f);
- py = 1.0f - (float)y / (float)h * 2.0f;
+ /* TODO */
+ ray->dx = aspect * ((float)x / (float)w * 2.0f - 1.0f);
+ ray->dy = 1.0f - (float)y / (float)h * 2.0f;
+ ray->dz = -1.0f / tan(cam.fov * 0.5f);
- ray->x = px;
- ray->y = py;
+ ray->x = cam.x;
+ ray->y = cam.y;
ray->z = cam.z;
-
- ray->dx = ray->dy = 0.0f;
- ray->dz = -1.0f;
}
static int ray_trace(struct ray *ray, float *col)
return 1;
}
+#define NULLXPOS(o) ((o)->ob.xform[12])
+#define NULLYPOS(o) ((o)->ob.xform[13])
+#define NULLZPOS(o) ((o)->ob.xform[14])
+
static void shade(float *col, struct ray *ray, struct hit *hit)
{
- col[0] = 1.0f;
- col[1] = col[2] = 0.0f;
+ float ndotl, ndoth, len, falloff, spec;
+ csg_object *o, *lt = plights;
+ float dcol[3], scol[3] = {0};
+ float ldir[3], lcol[3], hdir[3];
+ struct ray sray;
+ struct hit tmphit;
+
+ o = hit->o;
+ dcol[0] = ambient[0];
+ dcol[1] = ambient[1];
+ dcol[2] = ambient[2];
+
+ while(lt) {
+ ldir[0] = NULLXPOS(lt) - hit->x;
+ ldir[1] = NULLYPOS(lt) - hit->y;
+ ldir[2] = NULLZPOS(lt) - hit->z;
+
+ sray.x = hit->x;
+ sray.y = hit->y;
+ sray.z = hit->z;
+ sray.dx = ldir[0];
+ sray.dy = ldir[1];
+ sray.dz = ldir[2];
+
+ if(!find_intersection(&sray, &tmphit) || tmphit.t > 1.0f) {
+ if((len = sqrt(ldir[0] * ldir[0] + ldir[1] * ldir[1] + ldir[2] * ldir[2])) != 0.0f) {
+ float s = 1.0f / len;
+ ldir[0] *= s;
+ ldir[1] *= s;
+ ldir[2] *= s;
+ }
+ falloff = 1.0f / (len * len);
+
+ lcol[0] = lt->ob.emr * falloff;
+ lcol[1] = lt->ob.emg * falloff;
+ lcol[2] = lt->ob.emb * falloff;
+
+ if((ndotl = hit->nx * ldir[0] + hit->ny * ldir[1] + hit->nz * ldir[2]) < 0.0f) {
+ ndotl = 0.0f;
+ }
+
+ dcol[0] += o->ob.r * lcol[0] * ndotl;
+ dcol[1] += o->ob.g * lcol[1] * ndotl;
+ dcol[2] += o->ob.b * lcol[2] * ndotl;
+
+ if(o->ob.roughness < 1.0f) {
+ float gloss = 1.0f - o->ob.roughness;
+
+ hdir[0] = ldir[0] - ray->dx;
+ hdir[1] = ldir[1] - ray->dy;
+ hdir[2] = ldir[2] - ray->dz;
+ if((len = sqrt(hdir[0] * hdir[0] + hdir[1] * hdir[1] + hdir[2] * hdir[2])) != 0.0f) {
+ float s = 1.0f / len;
+ hdir[0] *= s;
+ hdir[1] *= s;
+ hdir[2] *= s;
+ }
+
+ if((ndoth = hit->nx * hdir[0] + hit->ny * hdir[1] + hit->nz * hdir[2]) < 0.0f) {
+ ndoth = 0.0f;
+ }
+ spec = gloss * pow(ndoth, 100.0f * gloss);
+
+ if(o->ob.metallic) {
+ lcol[0] *= o->ob.r;
+ lcol[1] *= o->ob.g;
+ lcol[2] *= o->ob.b;
+ }
+ scol[0] += lcol[0] * spec;
+ scol[1] += lcol[1] * spec;
+ scol[2] += lcol[2] * spec;
+ }
+ }
+
+ lt = lt->ob.plt_next;
+ }
+
+ col[0] = dcol[0] + scol[0];
+ col[1] = dcol[1] + scol[1];
+ col[2] = dcol[2] + scol[2];
}
static void background(float *col, struct ray *ray)
static int find_intersection(struct ray *ray, struct hit *best)
{
+ int idx = 0;
csg_object *o;
- struct hit *hit;
+ struct hinterv *hit, *it;
- best->t = 1e-6f;
+ best->t = FLT_MAX;
best->o = 0;
o = oblist;
while(o) {
- if((hit = ray_intersect(ray, o)) && hit->t < best->t) {
- *best = *hit;
+ if((hit = ray_intersect(ray, o))) {
+ it = hit;
+ while(it) {
+ if(it->end[0].t > 1e-6) {
+ idx = 0;
+ break;
+ }
+ if(it->end[1].t > 1e-6) {
+ idx = 1;
+ break;
+ }
+ it = it->next;
+ }
+
+ if(it && it->end[idx].t < best->t) {
+ *best = it->end[idx];
+ }
}
- free_hit(hit);
+ free_hit_list(hit);
o = o->ob.next;
}
return best->o != 0;
}
+
+static csg_object *load_object(struct ts_node *node)
+{
+ float *apos, *arot, *ascale, *acolor;
+ float aroughness;
+ struct ts_node *c;
+ csg_object *o, *olist = 0, *otail = 0;
+ int num_subobj = 0;
+
+ c = node->child_list;
+ while(c) {
+ if((o = load_object(c))) {
+ if(olist) {
+ otail->ob.next = o;
+ otail = o;
+ } else {
+ olist = otail = o;
+ }
+ ++num_subobj;
+ }
+ c = c->next;
+ }
+
+ apos = ts_get_attr_vec(node, "position", 0);
+ arot = ts_get_attr_vec(node, "rotation", 0);
+ ascale = ts_get_attr_vec(node, "scaling", 0);
+ acolor = ts_get_attr_vec(node, "color", 0);
+ aroughness = ts_get_attr_num(node, "roughness", 0);
+
+ if(strcmp(node->name, "null") == 0) {
+ if(num_subobj) {
+ fprintf(stderr, "null can't have sub-objects\n");
+ goto err;
+ }
+ if(!(o = alloc_object(OB_NULL))) {
+ goto err;
+ }
+ return o;
+
+ } else if(strcmp(node->name, "sphere") == 0) {
+ if(num_subobj) {
+ fprintf(stderr, "sphere can't have sub-objects\n");
+ goto err;
+ }
+ if(!(o = alloc_object(OB_SPHERE))) {
+ goto err;
+ }
+ }
+
+err:
+ while(olist) {
+ o = olist;
+ olist = olist->ob.next;
+ csg_free_object(o);
+ }
+ return 0;
+}