merged branch scene-file-parsing into trunk
git-svn-id: svn://anubis/fart/trunk@343 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
commit
700e47901b
9 files changed
+933
-281
No files matched your search
+72
-92
@@ -9,6 +9,7 @@
|
||||
#include "parser/parser.h"
|
||||
#include "parser/nodes.h"
|
||||
#include "util/Polygon.h"
|
||||
#include "util/Scope.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -16,64 +17,16 @@ typedef vector< refptr<Node> >::const_iterator Node_Iterator;
|
||||
|
||||
void Scene::load(const char * filename)
|
||||
{
|
||||
refptr<Node> node = parse(filename);
|
||||
processNode(node);
|
||||
}
|
||||
refptr<Scope> scope = new Scope();
|
||||
refptr<Node> node = parse(filename, scope);
|
||||
if ( ! node.isNull() )
|
||||
{
|
||||
/* evaluate any scripting nodes in the node tree */
|
||||
refptr<Node> processed_scene = node->evaluate();
|
||||
node->evaluateChildren(processed_scene);
|
||||
|
||||
void Scene::processNode(refptr<Node> node)
|
||||
{
|
||||
if (node.isNull())
|
||||
return;
|
||||
|
||||
if ( typeid(*node) == typeid(SceneNode) )
|
||||
{
|
||||
processScene(node);
|
||||
}
|
||||
else if ( node->isShape() )
|
||||
{
|
||||
refptr<Shape> shape = processShape(node);
|
||||
if ( ! shape.isNull() )
|
||||
m_shapes.push_back(shape);
|
||||
}
|
||||
else if ( typeid(*node) == typeid(LightNode) )
|
||||
{
|
||||
refptr<Light> light = processLight(node);
|
||||
if ( ! light.isNull() )
|
||||
m_lights.push_back(light);
|
||||
}
|
||||
else if ( typeid(*node) == typeid(OptionsNode) )
|
||||
{
|
||||
processOptions(node);
|
||||
}
|
||||
else if (node->isTransformBlock())
|
||||
{
|
||||
vector< refptr<Shape> > shapes = processTransformBlock(node);
|
||||
for (int i = 0, sz = shapes.size(); i < sz; i++)
|
||||
{
|
||||
m_shapes.push_back(shapes[i]);
|
||||
}
|
||||
}
|
||||
else if ( typeid(*node) == typeid(MaterialDefinitionNode) )
|
||||
{
|
||||
processMaterialDefinition(node);
|
||||
}
|
||||
else if ( typeid(*node) == typeid(ShapeDefinitionNode) )
|
||||
{
|
||||
processShapeDefinition(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << __FILE__ << ": " << __LINE__
|
||||
<< ": error: unrecognized node!" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::processChildren(refptr<Node> node)
|
||||
{
|
||||
std::vector< refptr<Node> > & children = node->getChildren();
|
||||
for (int i = 0, sz = children.size(); i < sz; i++)
|
||||
{
|
||||
processNode(children[i]);
|
||||
/* now we have a new node tree under 'dummy' with no scripting nodes */
|
||||
processScene(processed_scene);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,18 +51,34 @@ void Scene::processScene(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
/* then any other scene-specific items */
|
||||
for (Node_Iterator it = node->getChildren().begin();
|
||||
it != node->getChildren().end();
|
||||
it++)
|
||||
{
|
||||
if ( typeid(**it) != typeid(CameraNode) )
|
||||
if ( typeid(**it) == typeid(LightNode) )
|
||||
{
|
||||
processNode(*it);
|
||||
refptr<Light> light = processLight(*it);
|
||||
if ( ! light.isNull() )
|
||||
m_lights.push_back(light);
|
||||
}
|
||||
else if ( typeid(**it) == typeid(OptionsNode) )
|
||||
{
|
||||
processOptions(*it);
|
||||
}
|
||||
}
|
||||
|
||||
/* then any general items */
|
||||
vector<ShapeRef> shapes = processGeneralItems(node);
|
||||
for (vector<ShapeRef>::iterator it = shapes.begin();
|
||||
it != shapes.end();
|
||||
it++)
|
||||
{
|
||||
m_shapes.push_back(*it);
|
||||
}
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processShape(refptr<Node> node)
|
||||
ShapeRef Scene::processShape(refptr<Node> node)
|
||||
{
|
||||
if ( typeid(*node) == typeid(BoxNode) )
|
||||
{
|
||||
@@ -147,7 +116,7 @@ refptr<Shape> Scene::processShape(refptr<Node> node)
|
||||
exit(3);
|
||||
}
|
||||
|
||||
return refptr<Shape>(NULL);
|
||||
return ShapeRef(NULL);
|
||||
}
|
||||
|
||||
void Scene::processCamera(refptr<Node> node)
|
||||
@@ -220,7 +189,7 @@ void Scene::processOptions(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
vector< refptr<Shape> > Scene::processTransformBlock(refptr<Node> node)
|
||||
vector<ShapeRef> Scene::processTransformBlock(refptr<Node> node)
|
||||
{
|
||||
if ( typeid(*node) == typeid(TranslateBlockNode) )
|
||||
{
|
||||
@@ -238,8 +207,22 @@ vector< refptr<Shape> > Scene::processTransformBlock(refptr<Node> node)
|
||||
m_transforms.push(m_transforms.top());
|
||||
m_transforms.top().scale(node->getVector());
|
||||
}
|
||||
else
|
||||
{
|
||||
cerr << "Unknown transformation block node type!" << endl;
|
||||
exit(4);
|
||||
}
|
||||
|
||||
vector< refptr<Shape> > shapes;
|
||||
vector<ShapeRef> shapes = processGeneralItems(node);
|
||||
|
||||
m_transforms.pop();
|
||||
|
||||
return shapes;
|
||||
}
|
||||
|
||||
vector<ShapeRef> Scene::processGeneralItems(refptr<Node> node)
|
||||
{
|
||||
vector<ShapeRef> shapes, incoming;
|
||||
|
||||
for (Node_Iterator it = node->getChildren().begin();
|
||||
it != node->getChildren().end();
|
||||
@@ -247,30 +230,27 @@ vector< refptr<Shape> > Scene::processTransformBlock(refptr<Node> node)
|
||||
{
|
||||
if ((*it)->isTransformBlock())
|
||||
{
|
||||
vector< refptr<Shape> > in = processTransformBlock(*it);
|
||||
for (int i = 0, sz = in.size(); i < sz; i++)
|
||||
{
|
||||
shapes.push_back(in[i]);
|
||||
}
|
||||
incoming = processTransformBlock(*it);
|
||||
}
|
||||
else if ( (*it)->isShape() )
|
||||
else if ( typeid(**it) == typeid(MaterialDefinitionNode) )
|
||||
{
|
||||
shapes.push_back(processShape(*it));
|
||||
processMaterialDefinition(*it);
|
||||
}
|
||||
else if ( typeid(**it) == typeid(ShapeDefinitionNode) )
|
||||
{
|
||||
processShapeDefinition(*it);
|
||||
}
|
||||
else
|
||||
else if ( (*it)->isShape() )
|
||||
{
|
||||
cerr << "Unknown transform block item: " << typeid(**it).name()
|
||||
<< endl;
|
||||
exit(3);
|
||||
shapes.push_back(processShape(*it));
|
||||
}
|
||||
while (incoming.size() > 0)
|
||||
{
|
||||
shapes.push_back(incoming[0]);
|
||||
incoming.erase(incoming.begin());
|
||||
}
|
||||
}
|
||||
|
||||
m_transforms.pop();
|
||||
|
||||
return shapes;
|
||||
}
|
||||
|
||||
@@ -346,7 +326,7 @@ refptr<Material> Scene::processMaterial(refptr<Node> node)
|
||||
return material;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processBox(refptr<Node> node)
|
||||
ShapeRef Scene::processBox(refptr<Node> node)
|
||||
{
|
||||
refptr<Vector> size = new Vector(1, 1, 1);
|
||||
refptr<Material> material;
|
||||
@@ -367,7 +347,7 @@ refptr<Shape> Scene::processBox(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
refptr<Shape> box = new Box(size);
|
||||
ShapeRef box = new Box(size);
|
||||
if ( ! material.isNull() )
|
||||
box->setMaterial(material);
|
||||
box->setTransform(m_transforms.top());
|
||||
@@ -378,7 +358,7 @@ refptr<Shape> Scene::processBox(refptr<Node> node)
|
||||
return box;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processCyl(refptr<Node> node)
|
||||
ShapeRef Scene::processCyl(refptr<Node> node)
|
||||
{
|
||||
double radius1 = 1.0;
|
||||
double radius2 = 1.0;
|
||||
@@ -404,7 +384,7 @@ refptr<Shape> Scene::processCyl(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
refptr<Shape> cyl = new Cyl(radius1, radius2, height);
|
||||
ShapeRef cyl = new Cyl(radius1, radius2, height);
|
||||
if ( ! material.isNull() )
|
||||
cyl->setMaterial(material);
|
||||
cyl->setTransform(m_transforms.top());
|
||||
@@ -457,7 +437,7 @@ refptr<Light> Scene::processLight(refptr<Node> node)
|
||||
return light;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processPlane(refptr<Node> node)
|
||||
ShapeRef Scene::processPlane(refptr<Node> node)
|
||||
{
|
||||
Vector normal(0, 0, 1);
|
||||
double dist = 0;
|
||||
@@ -480,7 +460,7 @@ refptr<Shape> Scene::processPlane(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
refptr<Shape> plane = new Plane(normal[0],
|
||||
ShapeRef plane = new Plane(normal[0],
|
||||
normal[1],
|
||||
normal[2],
|
||||
dist);
|
||||
@@ -494,7 +474,7 @@ refptr<Shape> Scene::processPlane(refptr<Node> node)
|
||||
return plane;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processSphere(refptr<Node> node)
|
||||
ShapeRef Scene::processSphere(refptr<Node> node)
|
||||
{
|
||||
double radius = 1.0;
|
||||
refptr<Material> material;
|
||||
@@ -515,7 +495,7 @@ refptr<Shape> Scene::processSphere(refptr<Node> node)
|
||||
}
|
||||
}
|
||||
|
||||
refptr<Shape> sphere = new Sphere(radius);
|
||||
ShapeRef sphere = new Sphere(radius);
|
||||
if ( ! material.isNull() )
|
||||
sphere->setMaterial(material);
|
||||
sphere->setTransform(m_transforms.top());
|
||||
@@ -526,9 +506,9 @@ refptr<Shape> Scene::processSphere(refptr<Node> node)
|
||||
return sphere;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processBool(refptr<Node> node)
|
||||
ShapeRef Scene::processBool(refptr<Node> node)
|
||||
{
|
||||
vector< refptr<Shape> > shapes;
|
||||
vector<ShapeRef> shapes;
|
||||
refptr<Material> material;
|
||||
|
||||
bool restore_transform = processTransforms(node);
|
||||
@@ -540,7 +520,7 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
|
||||
{
|
||||
if ( (*it)->isShape() )
|
||||
{
|
||||
refptr<Shape> shape = processShape(*it);
|
||||
ShapeRef shape = processShape(*it);
|
||||
if ( ! shape.isNull() )
|
||||
shapes.push_back(shape);
|
||||
}
|
||||
@@ -550,7 +530,7 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
|
||||
}
|
||||
else if ( (*it)->isTransformBlock() )
|
||||
{
|
||||
vector< refptr<Shape> > in = processTransformBlock(*it);
|
||||
vector<ShapeRef> in = processTransformBlock(*it);
|
||||
for (int i = 0, sz = in.size(); i < sz; i++)
|
||||
{
|
||||
shapes.push_back(in[i]);
|
||||
@@ -569,7 +549,7 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
|
||||
exit(3);
|
||||
}
|
||||
|
||||
refptr<Shape> shape;
|
||||
ShapeRef shape;
|
||||
if ( typeid(*node) == typeid(IntersectNode) )
|
||||
shape = new Intersect(shapes);
|
||||
else if ( typeid(*node) == typeid(UnionNode) )
|
||||
@@ -594,7 +574,7 @@ refptr<Shape> Scene::processBool(refptr<Node> node)
|
||||
return shape;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processExtrude(refptr<Node> node)
|
||||
ShapeRef Scene::processExtrude(refptr<Node> node)
|
||||
{
|
||||
refptr<Material> material;
|
||||
Extrude * extrude = new Extrude();
|
||||
@@ -706,7 +686,7 @@ refptr<Polygon> Scene::processNGon(refptr<Node> node)
|
||||
return p;
|
||||
}
|
||||
|
||||
refptr<Shape> Scene::processShapeRef(refptr<Node> node)
|
||||
ShapeRef Scene::processShapeRef(refptr<Node> node)
|
||||
{
|
||||
if (m_shape_definitions.find(node->getString())
|
||||
== m_shape_definitions.end())
|
||||
@@ -717,7 +697,7 @@ refptr<Shape> Scene::processShapeRef(refptr<Node> node)
|
||||
}
|
||||
|
||||
refptr<Material> material;
|
||||
refptr<Shape> shape = m_shape_definitions[node->getString()]->clone();
|
||||
ShapeRef shape = m_shape_definitions[node->getString()]->clone();
|
||||
|
||||
bool restore_transform = processTransforms(node);
|
||||
|
||||
|
||||
+13
-13
@@ -58,25 +58,25 @@ class Scene
|
||||
|
||||
/* In Scene-load.cc */
|
||||
void load(const char * filename);
|
||||
void processNode(refptr<Node> node);
|
||||
void processChildren(refptr<Node> node);
|
||||
void processScene(refptr<Node> node);
|
||||
refptr<Material> processMaterial(refptr<Node> node);
|
||||
refptr<Shape> processBox(refptr<Node> node);
|
||||
refptr<Shape> processCyl(refptr<Node> node);
|
||||
ShapeRef processBox(refptr<Node> node);
|
||||
ShapeRef processCyl(refptr<Node> node);
|
||||
refptr<Light> processLight(refptr<Node> node);
|
||||
refptr<Shape> processPlane(refptr<Node> node);
|
||||
refptr<Shape> processSphere(refptr<Node> node);
|
||||
refptr<Shape> processShape(refptr<Node> node);
|
||||
refptr<Shape> processBool(refptr<Node> node);
|
||||
refptr<Shape> processExtrude(refptr<Node> node);
|
||||
refptr<Shape> processShapeRef(refptr<Node> node);
|
||||
ShapeRef processPlane(refptr<Node> node);
|
||||
ShapeRef processSphere(refptr<Node> node);
|
||||
ShapeRef processShape(refptr<Node> node);
|
||||
ShapeRef processBool(refptr<Node> node);
|
||||
ShapeRef processExtrude(refptr<Node> node);
|
||||
ShapeRef processShapeRef(refptr<Node> node);
|
||||
refptr<Polygon> processPolygon(refptr<Node> node);
|
||||
refptr<Polygon> processNGon(refptr<Node> node);
|
||||
bool processTransforms(refptr<Node> node);
|
||||
void processCamera(refptr<Node> node);
|
||||
void processOptions(refptr<Node> node);
|
||||
std::vector< refptr<Shape> > processTransformBlock(refptr<Node> node);
|
||||
std::vector<ShapeRef> processTransformBlock(refptr<Node> node);
|
||||
std::vector<ShapeRef> processGeneralItems(refptr<Node> node);
|
||||
std::vector<ShapeRef> processForNode(refptr<Node> node);
|
||||
void processMaterialDefinition(refptr<Node> node);
|
||||
void processShapeDefinition(refptr<Node> node);
|
||||
|
||||
@@ -91,8 +91,8 @@ class Scene
|
||||
int m_ambient_occlusion_level;
|
||||
|
||||
/* private data */
|
||||
std::vector< refptr<Shape> > m_shapes;
|
||||
std::map< std::string, refptr<Shape> > m_shape_definitions;
|
||||
std::vector<ShapeRef> m_shapes;
|
||||
std::map<std::string, ShapeRef> m_shape_definitions;
|
||||
std::vector< refptr<Light> > m_lights;
|
||||
std::stack<Transform> m_transforms;
|
||||
double m_view_plane_dist;
|
||||
|
||||
Reference in new issue
Block a user