diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-10-30 11:10:23 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-10-30 11:10:23 -0400 |
commit | 58e698772d6b48817fdff3a2d36f445c70e87ee3 (patch) | |
tree | 244a81cbd7db0ad7ca00a7edc363ca4bc14523ca /dimension | |
parent | 75406058a079e34932f383461644aab1f200f4ec (diff) | |
download | dimension-58e698772d6b48817fdff3a2d36f445c70e87ee3.tar.xz |
Refactor parsing and realization a bit.
Diffstat (limited to 'dimension')
-rw-r--r-- | dimension/parse.c | 12 | ||||
-rw-r--r-- | dimension/realize.c | 61 |
2 files changed, 45 insertions, 28 deletions
diff --git a/dimension/parse.c b/dimension/parse.c index c868d95..55ad78e 100644 --- a/dimension/parse.c +++ b/dimension/parse.c @@ -205,10 +205,11 @@ dmnsn_parse(const dmnsn_array *tokens) setlocale(LC_CTYPE, "C"); setlocale(LC_NUMERIC, "C"); - for (i = 0; i < dmnsn_array_size(tokens); ++i) { + while (i < dmnsn_array_size(tokens)) { dmnsn_array_get(tokens, i, &token); - if (token.type == DMNSN_T_BOX) { + switch (token.type) { + case DMNSN_T_BOX: if (dmnsn_parse_box(tokens, &i, astree) != 0) { dmnsn_diagnostic(token.filename, token.line, token.col, "Invalid box", @@ -216,10 +217,11 @@ dmnsn_parse(const dmnsn_array *tokens) dmnsn_token_name(token.type)); goto bailout; } - } else { + break; + + default: dmnsn_diagnostic(token.filename, token.line, token.col, - "Expected '%s', found '%s'", - dmnsn_token_name(DMNSN_T_BOX), + "Unexpected token '%s'", dmnsn_token_name(token.type)); goto bailout; } diff --git a/dimension/realize.c b/dimension/realize.c index 5df2648..286bae7 100644 --- a/dimension/realize.c +++ b/dimension/realize.c @@ -44,6 +44,35 @@ dmnsn_realize_vector(dmnsn_astnode astnode) return dmnsn_vector_construct(*x, *y, *z); } +dmnsn_object * +dmnsn_realize_box(dmnsn_astnode astnode) +{ + if (astnode.type != DMNSN_AST_BOX) { + dmnsn_error(DMNSN_SEVERITY_HIGH, "Expected a box."); + } + + dmnsn_astnode corner1, corner2; + dmnsn_array_get(astnode.children, 0, &corner1); + dmnsn_array_get(astnode.children, 1, &corner2); + + dmnsn_vector x1 = dmnsn_realize_vector(corner1), + x2 = dmnsn_realize_vector(corner2); + + dmnsn_object *box = dmnsn_new_cube(); + box->trans = dmnsn_scale_matrix(dmnsn_vector_construct((x2.x - x1.x)/2.0, + (x2.y - x1.y)/2.0, + (x2.z - x1.z)/2.0)); + box->trans = dmnsn_matrix_mul( + dmnsn_translation_matrix(dmnsn_vector_construct((x2.x + x1.x)/2.0, + (x2.y + x1.y)/2.0, + (x2.z + x1.z)/2.0)), + box->trans + ); + box->trans = dmnsn_matrix_inverse(box->trans); + + return box; +} + dmnsn_scene * dmnsn_realize(const dmnsn_array *astree) { @@ -75,7 +104,7 @@ dmnsn_realize(const dmnsn_array *astree) trans ); trans = dmnsn_matrix_mul( - dmnsn_rotation_matrix(dmnsn_vector_construct(0.0, 1.0, 0.0)), + dmnsn_rotation_matrix(dmnsn_vector_construct(0.8, 0.7, 0.0)), trans ); @@ -97,30 +126,16 @@ dmnsn_realize(const dmnsn_array *astree) for (i = 0; i < dmnsn_array_size(astree); ++i) { dmnsn_array_get(astree, i, &astnode); - if (astnode.type != DMNSN_AST_BOX) { + dmnsn_object *object; + switch (astnode.type) { + case DMNSN_AST_BOX: + object = dmnsn_realize_box(astnode); + dmnsn_array_push(scene->objects, &object); + break; + + default: dmnsn_error(DMNSN_SEVERITY_HIGH, "We only handle boxes right now."); } - - dmnsn_astnode corner1, corner2; - dmnsn_array_get(astnode.children, 0, &corner1); - dmnsn_array_get(astnode.children, 1, &corner2); - - dmnsn_vector x1 = dmnsn_realize_vector(corner1), - x2 = dmnsn_realize_vector(corner2); - - dmnsn_object *box = dmnsn_new_cube(); - box->trans = dmnsn_scale_matrix(dmnsn_vector_construct((x2.x - x1.x)/2.0, - (x2.y - x1.y)/2.0, - (x2.z - x1.z)/2.0)); - box->trans = dmnsn_matrix_mul( - dmnsn_translation_matrix(dmnsn_vector_construct((x2.x + x1.x)/2.0, - (x2.y + x1.y)/2.0, - (x2.z + x1.z)/2.0)), - box->trans - ); - box->trans = dmnsn_matrix_inverse(box->trans); - - dmnsn_array_push(scene->objects, &box); } return scene; |