[6] | 1 | #include <vrmllib/nodes.h> |
---|
| 2 | |
---|
| 3 | #include <map> |
---|
| 4 | #include <stdexcept> |
---|
| 5 | |
---|
| 6 | #include <vrmllib/types_bits.h> |
---|
| 7 | |
---|
| 8 | #define BEGIN_NODE(type) \ |
---|
| 9 | namespace {\ |
---|
| 10 | node_creator<type> register_##type##_creator(#type); \ |
---|
| 11 | } \ |
---|
| 12 | void type::parse_attribute(const std::string &a_name, \ |
---|
| 13 | std::istream &a_stream, file &a_data) \ |
---|
| 14 | { |
---|
| 15 | |
---|
| 16 | #define ATTR(d_var) \ |
---|
| 17 | if (a_name == #d_var) { \ |
---|
| 18 | using namespace bits; \ |
---|
| 19 | return parse_value(d_var, a_stream, a_data); \ |
---|
| 20 | } |
---|
| 21 | #define VATTR(d_var) \ |
---|
| 22 | if (a_name == #d_var) { \ |
---|
| 23 | using namespace bits; \ |
---|
| 24 | return parse_vector(d_var, a_stream, a_data); \ |
---|
| 25 | } |
---|
| 26 | #define END_NODE \ |
---|
| 27 | base_type::parse_attribute(a_name, a_stream, a_data); \ |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | #define BEGIN_DEFAULTS(type) \ |
---|
| 31 | type::type() : |
---|
| 32 | #define END_DEFAULTS \ |
---|
| 33 | {} |
---|
| 34 | |
---|
| 35 | namespace vrmllib { |
---|
| 36 | namespace bits { |
---|
| 37 | |
---|
| 38 | class node_creator_base { |
---|
| 39 | public: |
---|
| 40 | virtual ~node_creator_base() {} |
---|
| 41 | virtual node *create() = 0; |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | std::map<std::string, node_creator_base *> g_node_creators; |
---|
| 45 | |
---|
| 46 | template<class Type> |
---|
| 47 | class node_creator : public node_creator_base { |
---|
| 48 | public: |
---|
| 49 | node_creator(const char *type) { g_node_creators[type] = this; } |
---|
| 50 | node *create() { return new Type; } |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | } // namespace bits |
---|
| 54 | |
---|
| 55 | using namespace bits; |
---|
| 56 | |
---|
| 57 | node *node::create_node(const std::string &type) |
---|
| 58 | { |
---|
| 59 | node_creator_base *c = g_node_creators[type]; |
---|
| 60 | |
---|
| 61 | if (c) |
---|
| 62 | return c->create(); |
---|
| 63 | else { |
---|
| 64 | std::cerr << "unknown type: " << type << std::endl; |
---|
| 65 | return 0; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | BEGIN_NODE(grouping_node) |
---|
| 70 | VATTR(children) |
---|
| 71 | END_NODE |
---|
| 72 | |
---|
| 73 | BEGIN_DEFAULTS(Transform) |
---|
| 74 | translation(0,0,0), |
---|
| 75 | center(0,0,0), |
---|
| 76 | scale(1,1,1) |
---|
| 77 | END_DEFAULTS |
---|
| 78 | BEGIN_NODE(Transform) |
---|
| 79 | ATTR(translation) |
---|
| 80 | ATTR(rotation) |
---|
| 81 | ATTR(center) |
---|
| 82 | ATTR(scale) |
---|
| 83 | ATTR(scaleOrientation) |
---|
| 84 | END_NODE |
---|
| 85 | |
---|
| 86 | BEGIN_NODE(Group) |
---|
| 87 | END_NODE |
---|
| 88 | |
---|
| 89 | BEGIN_DEFAULTS(Shape) |
---|
| 90 | appearance(0), |
---|
| 91 | geometry(0) |
---|
| 92 | END_DEFAULTS |
---|
| 93 | BEGIN_NODE(Shape) |
---|
| 94 | ATTR(appearance) |
---|
| 95 | ATTR(geometry) |
---|
| 96 | END_NODE |
---|
| 97 | |
---|
| 98 | BEGIN_DEFAULTS(Switch) |
---|
| 99 | whichChoice(0) |
---|
| 100 | END_DEFAULTS |
---|
| 101 | BEGIN_NODE(Switch) |
---|
| 102 | VATTR(choice) |
---|
| 103 | ATTR(whichChoice) |
---|
| 104 | END_NODE |
---|
| 105 | |
---|
| 106 | BEGIN_DEFAULTS(Viewpoint) |
---|
| 107 | fieldOfView(0.785398), |
---|
| 108 | position(0,0,10) |
---|
| 109 | END_DEFAULTS |
---|
| 110 | BEGIN_NODE(Viewpoint) |
---|
| 111 | ATTR(fieldOfView) |
---|
| 112 | ATTR(orientation) |
---|
| 113 | ATTR(position) |
---|
| 114 | ATTR(description) |
---|
| 115 | END_NODE |
---|
| 116 | |
---|
| 117 | BEGIN_DEFAULTS(Appearance) |
---|
| 118 | material(0), |
---|
| 119 | texture(0) |
---|
| 120 | END_DEFAULTS |
---|
| 121 | BEGIN_NODE(Appearance) |
---|
| 122 | ATTR(material) |
---|
| 123 | ATTR(texture) |
---|
| 124 | END_NODE |
---|
| 125 | |
---|
| 126 | BEGIN_NODE(WorldInfo) |
---|
| 127 | ATTR(title) |
---|
| 128 | VATTR(info) |
---|
| 129 | END_NODE |
---|
| 130 | |
---|
| 131 | BEGIN_DEFAULTS(Material) |
---|
| 132 | diffuseColor(204,204,204), |
---|
| 133 | specularColor(0,0,0), |
---|
| 134 | emissiveColor(0,0,0), |
---|
| 135 | ambientIntensity(0.2), |
---|
| 136 | shininess(0.2), |
---|
| 137 | transparency(0) |
---|
| 138 | END_DEFAULTS |
---|
| 139 | BEGIN_NODE(Material) |
---|
| 140 | ATTR(diffuseColor) |
---|
| 141 | ATTR(specularColor) |
---|
| 142 | ATTR(emissiveColor) |
---|
| 143 | ATTR(ambientIntensity) |
---|
| 144 | ATTR(shininess) |
---|
| 145 | ATTR(transparency) |
---|
| 146 | END_NODE |
---|
| 147 | |
---|
| 148 | BEGIN_DEFAULTS(IndexedFaceSet) |
---|
| 149 | solid(true), |
---|
| 150 | convex(true), |
---|
| 151 | ccw(true), |
---|
| 152 | normalPerVertex(true), |
---|
| 153 | colorPerVertex(true), |
---|
| 154 | coord(0), |
---|
| 155 | normal(0), |
---|
| 156 | texCoord(0), |
---|
| 157 | color(0) |
---|
| 158 | END_DEFAULTS |
---|
| 159 | BEGIN_NODE(IndexedFaceSet) |
---|
| 160 | ATTR(solid) |
---|
| 161 | ATTR(convex) |
---|
| 162 | ATTR(ccw) |
---|
| 163 | ATTR(normalPerVertex) |
---|
| 164 | ATTR(colorPerVertex) |
---|
| 165 | ATTR(coord) |
---|
| 166 | ATTR(color) |
---|
| 167 | ATTR(normal) |
---|
| 168 | ATTR(texCoord) |
---|
| 169 | VATTR(coordIndex) |
---|
| 170 | VATTR(texCoordIndex) |
---|
| 171 | VATTR(normalIndex) |
---|
| 172 | VATTR(colorIndex) |
---|
| 173 | END_NODE |
---|
| 174 | |
---|
| 175 | BEGIN_NODE(Coordinate) |
---|
| 176 | VATTR(point) |
---|
| 177 | END_NODE |
---|
| 178 | |
---|
| 179 | BEGIN_NODE(Color) |
---|
| 180 | VATTR(color) |
---|
| 181 | END_NODE |
---|
| 182 | |
---|
| 183 | BEGIN_NODE(Box) |
---|
| 184 | ATTR(size) |
---|
| 185 | END_NODE |
---|
| 186 | |
---|
| 187 | BEGIN_NODE(Sphere) |
---|
| 188 | ATTR(radius) |
---|
| 189 | END_NODE |
---|
| 190 | |
---|
| 191 | BEGIN_NODE(TextureCoordinate) |
---|
| 192 | VATTR(point) |
---|
| 193 | END_NODE |
---|
| 194 | |
---|
| 195 | BEGIN_NODE(Normal) |
---|
| 196 | VATTR(vector) |
---|
| 197 | END_NODE |
---|
| 198 | |
---|
| 199 | BEGIN_DEFAULTS(ImageTexture) |
---|
| 200 | repeatS(true), repeatT(true) |
---|
| 201 | END_DEFAULTS |
---|
| 202 | BEGIN_NODE(ImageTexture) |
---|
| 203 | VATTR(url) |
---|
| 204 | ATTR(repeatS) |
---|
| 205 | ATTR(repeatT) |
---|
| 206 | END_NODE |
---|
| 207 | |
---|
| 208 | } // namespace vrmllib |
---|