summaryrefslogtreecommitdiffstats
path: root/dimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-11-25 02:38:41 -0500
committerTavian Barnes <tavianator@gmail.com>2009-11-25 02:38:41 -0500
commit70379f85feb3b294742d880a75fabd4452c5ecf3 (patch)
tree7c969e5b1982cbd8f766f46805581b1140c2a96b /dimension
parent7bbc14551b0513ec94bdac0eff0d64842fe55b2b (diff)
downloaddimension-70379f85feb3b294742d880a75fabd4452c5ecf3.tar.xz
Support scaling and translation transformations.
Diffstat (limited to 'dimension')
-rw-r--r--dimension/grammar.y14
-rw-r--r--dimension/parse.h2
-rw-r--r--dimension/realize.c31
3 files changed, 44 insertions, 3 deletions
diff --git a/dimension/grammar.y b/dimension/grammar.y
index 2b83511..d69a5a0 100644
--- a/dimension/grammar.y
+++ b/dimension/grammar.y
@@ -556,7 +556,7 @@ yyerror(YYLTYPE *locp, dmnsn_array *astree, dmnsn_token_iterator *iterator,
%token DMNSN_T_ROUGHNESS
%token DMNSN_T_SAMPLES
%token DMNSN_T_SAVE_FILE
-%token DMNSN_T_SCALE
+%token DMNSN_T_SCALE "scale"
%token DMNSN_T_SCALLOP_WAVE
%token DMNSN_T_SCATTERING
%token DMNSN_T_SEED
@@ -619,7 +619,7 @@ yyerror(YYLTYPE *locp, dmnsn_array *astree, dmnsn_token_iterator *iterator,
%token DMNSN_T_TORUS
%token DMNSN_T_TRACE
%token DMNSN_T_TRANSFORM
-%token DMNSN_T_TRANSLATE
+%token DMNSN_T_TRANSLATE "translate"
%token DMNSN_T_TRANSMIT "transmit"
%token DMNSN_T_TRIANGLE
%token DMNSN_T_TRIANGLE_WAVE
@@ -744,6 +744,12 @@ SCENE_ITEM: OBJECT
TRANSFORMATION: "rotate" VECTOR {
$$ = dmnsn_new_astnode1(DMNSN_AST_ROTATION, @$, $2);
}
+ | "scale" VECTOR {
+ $$ = dmnsn_new_astnode1(DMNSN_AST_SCALE, @$, $2);
+ }
+ | "translate" VECTOR {
+ $$ = dmnsn_new_astnode1(DMNSN_AST_TRANSLATION, @$, $2);
+ }
/* Objects */
@@ -1374,8 +1380,10 @@ dmnsn_astnode_string(dmnsn_astnode_type astnode_type)
dmnsn_astnode_map(DMNSN_AST_BOX, "box");
dmnsn_astnode_map(DMNSN_AST_VECTOR, "vector");
dmnsn_astnode_map(DMNSN_AST_SPHERE, "sphere");
- dmnsn_astnode_map(DMNSN_AST_OBJECT_MODIFIERS, "object-modifiers");
dmnsn_astnode_map(DMNSN_AST_ROTATION, "rotate");
+ dmnsn_astnode_map(DMNSN_AST_SCALE, "scale");
+ dmnsn_astnode_map(DMNSN_AST_TRANSLATION, "translate");
+ dmnsn_astnode_map(DMNSN_AST_OBJECT_MODIFIERS, "object-modifiers");
default:
fprintf(stderr, "Warning: unrecognised astnode type %d.\n",
diff --git a/dimension/parse.h b/dimension/parse.h
index ea9ec87..dc90e6d 100644
--- a/dimension/parse.h
+++ b/dimension/parse.h
@@ -34,6 +34,8 @@ typedef enum {
DMNSN_AST_BOX,
DMNSN_AST_SPHERE,
DMNSN_AST_ROTATION,
+ DMNSN_AST_SCALE,
+ DMNSN_AST_TRANSLATION,
DMNSN_AST_OBJECT_MODIFIERS,
} dmnsn_astnode_type;
diff --git a/dimension/realize.c b/dimension/realize.c
index 2c5bd81..3b1d327 100644
--- a/dimension/realize.c
+++ b/dimension/realize.c
@@ -87,6 +87,31 @@ dmnsn_realize_rotation(dmnsn_astnode astnode, dmnsn_object *object)
}
static dmnsn_object *
+dmnsn_realize_scale(dmnsn_astnode astnode, dmnsn_object *object)
+{
+ dmnsn_astnode scale_node;
+ dmnsn_array_get(astnode.children, 0, &scale_node);
+ dmnsn_vector scale = dmnsn_realize_vector(scale_node);
+
+ object->trans = dmnsn_matrix_mul(dmnsn_scale_matrix(scale), object->trans);
+ return object;
+}
+
+static dmnsn_object *
+dmnsn_realize_translation(dmnsn_astnode astnode, dmnsn_object *object)
+{
+ dmnsn_astnode trans_node;
+ dmnsn_array_get(astnode.children, 0, &trans_node);
+ dmnsn_vector trans = dmnsn_realize_vector(trans_node);
+
+ object->trans = dmnsn_matrix_mul(
+ dmnsn_translation_matrix(trans),
+ object->trans
+ );
+ return object;
+}
+
+static dmnsn_object *
dmnsn_realize_object_modifiers(dmnsn_astnode astnode, dmnsn_object *object)
{
unsigned int i;
@@ -98,6 +123,12 @@ dmnsn_realize_object_modifiers(dmnsn_astnode astnode, dmnsn_object *object)
case DMNSN_AST_ROTATION:
object = dmnsn_realize_rotation(modifier, object);
break;
+ case DMNSN_AST_SCALE:
+ object = dmnsn_realize_scale(modifier, object);
+ break;
+ case DMNSN_AST_TRANSLATION:
+ object = dmnsn_realize_translation(modifier, object);
+ break;
default:
dmnsn_error(DMNSN_SEVERITY_HIGH, "Invalid object modifier.");