diff --git a/main/Material.cc b/main/Material.cc index 3f608ff..f280558 100644 --- a/main/Material.cc +++ b/main/Material.cc @@ -7,5 +7,6 @@ Material::Material() { m_diffuse_color = Color::white; m_specular_color = Color::white; - m_shininess = 50; + m_shininess = 50.0; + m_reflectance = 0.0; } diff --git a/main/Material.h b/main/Material.h index 5bef0db..e1b9ae5 100644 --- a/main/Material.h +++ b/main/Material.h @@ -33,11 +33,15 @@ class Material void setShininess(double shininess) { m_shininess = shininess; } double getShininess() const { return m_shininess; } + void setReflectance(double reflectance) { m_reflectance = reflectance; } + double getReflectance() const { return m_reflectance; } + protected: Color m_ambient_color; Color m_diffuse_color; Color m_specular_color; double m_shininess; + double m_reflectance; }; #endif diff --git a/parser/parser.lex b/parser/parser.lex index 7be38e9..78d138e 100644 --- a/parser/parser.lex +++ b/parser/parser.lex @@ -32,9 +32,10 @@ -?[0-9]+ return DEC_NUMBER; -?[0-9]*\.[0-9]+ return REAL_NUMBER; +ambient return AMBIENT; box return BOX; camera return CAMERA; -color return COLOR; +diffuse return DIFFUSE; height return HEIGHT; intersect return INTERSECT; look_at return LOOKAT; @@ -47,6 +48,7 @@ reflectance return REFLECTANCE; scene return SCENE; shininess return SHININESS; size return SIZE; +specular return SPECULAR; sphere return SPHERE; subtract return SUBTRACT; union return UNION; diff --git a/parser/parser.yy b/parser/parser.yy index f303456..825da1b 100644 --- a/parser/parser.yy +++ b/parser/parser.yy @@ -33,11 +33,12 @@ class Node Node(); int type; refptr the_double; - refptr the_Material; refptr the_Node; refptr the_tail; + refptr the_Material; refptr the_Shape; refptr the_Vector; + refptr the_Color; }; enum Node_Type @@ -78,9 +79,10 @@ enum Node_Type %token DEC_NUMBER; %token REAL_NUMBER; +%token AMBIENT; %token BOX; %token CAMERA; -%token COLOR; +%token DIFFUSE; %token HEIGHT; %token INTERSECT; %token LOOKAT; @@ -93,6 +95,7 @@ enum Node_Type %token SCENE; %token SHININESS; %token SIZE; +%token SPECULAR; %token SPHERE; %token SUBTRACT; %token UNION; @@ -103,10 +106,24 @@ enum Node_Type %% scene: SCENE LCURLY scene_items RCURLY { + refptr node = $3; + while ( ! node.isNull() ) + { + /* process this scene item */ + refptr thisNode = node->the_Node; + node = node->the_tail; + } } ; -boolean_items: shape shape boolean_items_more +boolean_items: shape shape boolean_items_more { + $$ = new Node(); + $$->the_Node = $1; + $$->the_tail = new Node(); + $$->the_tail->the_Node = $2; + $$->the_tail->the_tail = new Node(); + $$->the_tail->the_tail->the_Node = $3; + } ; boolean_items_more: /* empty */ @@ -155,17 +172,57 @@ camera_item: POSITION vector { intersect: INTERSECT LCURLY boolean_items RCURLY ; -material: MATERIAL LCURLY material_items RCURLY +material: MATERIAL LCURLY material_items RCURLY { + $$ = new Node(); + refptr material = new Material(); + refptr node = $3; + while ( ! node.isNull() ) + { + switch (node->type) + { + case AMBIENT: + material->setAmbientColor(*node->the_Color); + break; + case DIFFUSE: + material->setDiffuseColor(* node->the_Color); + break; + case SPECULAR: + material->setSpecularColor(* node->the_Color); + break; + case SHININESS: + material->setShininess(* node->the_double); + break; + case REFLECTANCE: + material->setReflectance(* node->the_double); + break; + } + node = node->the_Node; + } + } ; material_items: /* empty */ - | material_item material_items + | material_item material_items { + $$ = new Node(); + $$->the_Node = $1; + $$->the_tail = $2; + } ; -material_item: COLOR vector { +material_item: AMBIENT vector { $$ = new Node(); - $$->type = COLOR; - $$->the_Vector = $2->the_Vector; + $$->type = AMBIENT; + $$->the_Color = new Color(* $2->the_Vector); + } + | DIFFUSE vector { + $$ = new Node(); + $$->type = DIFFUSE; + $$->the_Color = new Color(* $2->the_Vector); + } + | SPECULAR vector { + $$ = new Node(); + $$->type = SPECULAR; + $$->the_Color = new Color(* $2->the_Vector); } | REFLECTANCE number { $$ = new Node(); diff --git a/util/Color.cc b/util/Color.cc index fc99dc3..8b5d328 100644 --- a/util/Color.cc +++ b/util/Color.cc @@ -19,6 +19,13 @@ Color::Color(double r, double g, double b) this->b = b; } +Color::Color(const Vector & v) +{ + r = v[0]; + g = v[1]; + b = v[2]; +} + Color Color::operator*(const Color & other) const { Color result; diff --git a/util/Color.h b/util/Color.h index 1dacbf7..aa0c3a6 100644 --- a/util/Color.h +++ b/util/Color.h @@ -3,6 +3,7 @@ #define COLOR_H COLOR_H #include +#include "util/Vector.h" class Color { @@ -11,6 +12,7 @@ class Color Color(); Color(double r, double g, double b); + Color(const Vector & v); Color operator*(const Color & other) const; Color operator*(double scale) const;