From: John Tsiombikas Date: Wed, 28 Mar 2018 06:29:24 +0000 (+0300) Subject: added scene loading X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=csgray;a=commitdiff_plain;h=33b3cb144e8762bbb67424fd68374f92d85cdc3d added scene loading --- diff --git a/RUN b/RUN index 7cf8029..ab8273a 100755 --- a/RUN +++ b/RUN @@ -1,4 +1,4 @@ #!/bin/sh rm -f output.ppm -./csgray $* && display output.ppm +./csgray scene $* && display output.ppm diff --git a/scene b/scene new file mode 100644 index 0000000..6cf673d --- /dev/null +++ b/scene @@ -0,0 +1,42 @@ +# vi:set ts=4 sts=4 sw=4 ai: +csgray_scene { + viewer { + position = [0, 0, 5] + target = [0, 0, 0] + fov = 50 + } + + subtract { + subtract { + sphere { + position = [0, 0, 0] + radius = 1 + color = [1, 0.1, 0.05] + roughness = 0.3 + } + sphere { + position = [0.3, 0.7, 0.7] + radius = 0.7 + color = [0.2, 0.3, 1] + roughness = 0.3 + } + } + sphere { + position = [-0.9, -0.1, 0.7] + radius = 0.5 + color = [1, 0.9, 0.2] + roughness = 0.3 + } + } + + plane { + position = [0, -1, 0] + normal = [0, 1, 0] + color = [0.4, 0.7, 0.4] + } + + null { + position = [-4, 10, 10] + emission = [80, 80, 80] + } +} diff --git a/src/csgray.c b/src/csgray.c index 7fc2b46..150f5fb 100644 --- a/src/csgray.c +++ b/src/csgray.c @@ -61,21 +61,32 @@ void csg_fov(float fov) int csg_load(const char *fname) { struct ts_node *root = 0, *c; + csg_object *o; if(!(root = ts_load(fname))) { fprintf(stderr, "failed to open %s\n", fname); return -1; } - if(strcmp(root->name, "scene") != 0) { + if(strcmp(root->name, "csgray_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); + if(strcmp(c->name, "viewer") == 0) { + static float def_pos[] = {0, 0, 5}; + static float def_targ[] = {0, 0, 0}; + + float *p = ts_get_attr_vec(c, "position", def_pos); + float *t = ts_get_attr_vec(c, "target", def_targ); + + csg_view(p[0], p[1], p[2], t[0], t[1], t[2]); + csg_fov(ts_get_attr_num(c, "fov", 50.0f)); + + } else if((o = load_object(c))) { + csg_add_object(o); + } c = c->next; } @@ -306,6 +317,35 @@ void csg_metallic(csg_object *o, int m) o->ob.metallic = m; } +void csg_reset_xform(csg_object *o) +{ + mat4_identity(o->ob.xform); + mat4_identity(o->ob.inv_xform); +} + +void csg_translate(csg_object *o, float x, float y, float z) +{ + mat4_translate(o->ob.xform, x, y, z); + mat4_pre_translate(o->ob.inv_xform, -x, -y, -z); +} + +void csg_rotate(csg_object *o, float angle, float x, float y, float z) +{ + mat4_rotate(o->ob.xform, angle, x, y, z); + mat4_pre_rotate(o->ob.inv_xform, -angle, x, y, z); +} + +void csg_scale(csg_object *o, float x, float y, float z) +{ + mat4_scale(o->ob.xform, x, y, z); + mat4_pre_scale(o->ob.inv_xform, 1.0f / x, 1.0f / y, 1.0f / z); +} + +void csg_lookat(csg_object *o, float x, float y, float z, float tx, float ty, float tz, float ux, float uy, float uz) +{ + mat4_lookat(o->ob.xform, x, y, z, tx, ty, tz, ux, uy, uz); + mat4_inv_lookat(o->ob.inv_xform, x, y, z, tx, ty, tz, ux, uy, uz); +} void csg_render_pixel(int x, int y, int width, int height, float aspect, float *color) { @@ -484,53 +524,115 @@ static int find_intersection(struct ray *ray, struct hit *best) static csg_object *load_object(struct ts_node *node) { - float *apos, *arot, *ascale, *acolor; - float aroughness; + float *avec; struct ts_node *c; - csg_object *o, *olist = 0, *otail = 0; - int num_subobj = 0; + csg_object *sub, *o = 0, *olist = 0, *otail = 0; + int num_subobj = 0, is_csgop = 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; + if(strcmp(node->name, "null") == 0) { + if(!(o = csg_null(0, 0, 0))) { + goto err; } - 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); + } else if(strcmp(node->name, "sphere") == 0) { + float rad = ts_get_attr_num(node, "radius", 1.0f); + if(!(o = csg_sphere(0, 0, 0, rad))) { + goto err; + } - if(strcmp(node->name, "null") == 0) { - if(num_subobj) { - fprintf(stderr, "null can't have sub-objects\n"); + } else if(strcmp(node->name, "plane") == 0) { + static float def_norm[] = {0, 1, 0}; + float *norm = ts_get_attr_vec(node, "normal", def_norm); + if(!(o = csg_plane(0, 0, 0, norm[0], norm[1], norm[2]))) { goto err; } - if(!(o = alloc_object(OB_NULL))) { + + } else if(strcmp(node->name, "box") == 0) { + static float def_sz[] = {1, 1, 1}; + float *sz = ts_get_attr_vec(node, "size", def_sz); + if(!(o = csg_box(0, 0, 0, sz[0], sz[1], sz[2]))) { goto err; } - return o; - } else if(strcmp(node->name, "sphere") == 0) { - if(num_subobj) { - fprintf(stderr, "sphere can't have sub-objects\n"); + } else if(strcmp(node->name, "union") == 0) { + if(!(o = csg_union(0, 0))) { + goto err; + } + is_csgop = 1; + + } else if(strcmp(node->name, "intersect") == 0) { + if(!(o = csg_intersection(0, 0))) { goto err; } - if(!(o = alloc_object(OB_SPHERE))) { + is_csgop = 1; + + } else if(strcmp(node->name, "subtract") == 0) { + if(!(o = csg_subtraction(0, 0))) { goto err; } + is_csgop = 1; + + } else { + goto err; } + if(is_csgop) { + c = node->child_list; + while(c) { + if((sub = load_object(c))) { + if(olist) { + otail->ob.next = sub; + otail = sub; + } else { + olist = otail = sub; + } + ++num_subobj; + } + c = c->next; + } + + if(num_subobj != 2) { + goto err; + } + o->un.a = olist; + o->un.b = olist->ob.next; + olist->ob.next = 0; + } + + if((avec = ts_get_attr_vec(node, "position", 0))) { + csg_translate(o, avec[0], avec[1], avec[2]); + } + if((avec = ts_get_attr_vec(node, "rotaxis", 0))) { + csg_rotate(o, ts_get_attr_num(node, "rotangle", 0.0f), avec[0], avec[1], avec[2]); + } + if((avec = ts_get_attr_vec(node, "scaling", 0))) { + csg_scale(o, avec[0], avec[1], avec[2]); + } + if((avec = ts_get_attr_vec(node, "target", 0))) { + /* don't move this before position */ + float def_up[] = {0, 1, 0}; + float *up = ts_get_attr_vec(node, "up", def_up); + float x = o->ob.xform[12]; + float y = o->ob.xform[13]; + float z = o->ob.xform[14]; + csg_lookat(o, x, y, z, avec[0], avec[1], avec[2], up[0], up[1], up[2]); + } + + if((avec = ts_get_attr_vec(node, "color", 0))) { + csg_color(o, avec[0], avec[1], avec[2]); + } + if((avec = ts_get_attr_vec(node, "emission", 0))) { + csg_emission(o, avec[0], avec[1], avec[2]); + } + + csg_roughness(o, ts_get_attr_num(node, "roughness", o->ob.roughness)); + csg_opacity(o, ts_get_attr_num(node, "opacity", o->ob.opacity)); + csg_metallic(o, ts_get_attr_int(node, "metallic", o->ob.metallic)); + + return o; + err: + csg_free_object(o); while(olist) { o = olist; olist = olist->ob.next; diff --git a/src/csgray.h b/src/csgray.h index 5e6fc31..5d0cf26 100644 --- a/src/csgray.h +++ b/src/csgray.h @@ -34,6 +34,12 @@ void csg_roughness(csg_object *o, float r); void csg_opacity(csg_object *o, float p); void csg_metallic(csg_object *o, int m); +void csg_reset_xform(csg_object *o); +void csg_translate(csg_object *o, float x, float y, float z); +void csg_rotate(csg_object *o, float angle, float x, float y, float z); +void csg_scale(csg_object *o, float x, float y, float z); +void csg_lookat(csg_object *o, float x, float y, float z, float tx, float ty, float tz, float ux, float uy, float uz); + void csg_render_pixel(int x, int y, int width, int height, float aspect, float *color); void csg_render_image(float *pixels, int width, int height); diff --git a/src/main.c b/src/main.c index 93c7442..6e69844 100644 --- a/src/main.c +++ b/src/main.c @@ -16,6 +16,7 @@ static int parse_opt(int argc, char **argv); static int width = DFL_WIDTH, height = DFL_HEIGHT; static float inv_gamma = 1.0f / DFL_GAMMA; static const char *out_fname = DFL_OUTFILE; +static const char *in_fname; int main(int argc, char **argv) { @@ -30,11 +31,16 @@ int main(int argc, char **argv) return 1; } + if(csg_load(in_fname) == -1) { + return 1; + } + if(!(pixels = malloc(width * height * 3 * sizeof *pixels))) { perror("failed to allocate framebuffer"); return 1; } + /* csg_view(0, 0, 5, 0, 0, 0); oa = csg_sphere(0, 0, 0, 1); @@ -62,6 +68,7 @@ int main(int argc, char **argv) obj = csg_null(-4, 10, 10); csg_emission(obj, 80, 80, 80); csg_add_object(obj); + */ csg_render_image(pixels, width, height); save_image(out_fname, pixels, width, height); @@ -149,9 +156,18 @@ static int parse_opt(int argc, char **argv) return -1; } } else { - fprintf(stderr, "unexpected argument: %s\n", argv[i]); - return -1; + if(in_fname) { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + return -1; + } + in_fname = argv[i]; } } + + if(!in_fname) { + fprintf(stderr, "you need to pass a scene file to read\n"); + return -1; + } + return 0; }