I'm trying to add a way to create scenes for my raytracer without having to recompile it every time. So far i've gotten the irrXML parser up and working. My xml file declares all the scene properties I need and i can get them into my raytracer, but my problem is when I want to add something to my scene I don't know how to declare a new variable name from my strings. For instance, when i'm hard coding my scene right into my raytracer I do something like this ...

Vector origin (0, 0, 0);
double radius = 1.0;
Color gray (0.5, 0.5, 0.5);
Sphere my_sphere (origin, radius, gray);

now I have an xml file with statements like ..

// scene.xml
<vector name="origin" x="0" y="0" z="0"/>
<color name="gray" r="0.5" g="0.5" b="0.5"/>
<sphere name="my_sphere" position="origin" radius="1" color="gray"/>

the way irrXML works, you can get the data into your program like this ...

string vectorName, colorName, sphereName, position, color;
float x, y, z, r, g, b, radius;

// parse the scene file
IrrXMLReader* xml = createIrrXMLReader("scene.xml");

// parse the file until end reached
while(xml && xml->read()) 
{
    switch(xml->getNodeType()) 
    {
        case EXN_TEXT:
        break;

        case EXN_ELEMENT:
	{
            if (!strcmp("vector", xml->getNodeName())) {
                vectorName = xml->getAttributeValue("name");
                x = xml->getAttributeValueAsFloat("x");
                y = xml->getAttributeValueAsFloat("y");
                z = xml->getAttributeValueAsFloat("z");
            }
            if (!strcmp("color", xml->getNodeName())) {
                colorName = xml->getAttributeValue("name");
                r = xml->getAttributeValueAsFloat("r");
                g = xml->getAttributeValueAsFloat("g");
                b = xml->getAttributeValueAsFloat("b");
            }
            if (!strcmp("sphere", xml->getNodeName())) {
                sphereName = xml->getAttributeValue("name");
                position = xml->getAttributeValue("position");
                radius = xml->getAttributeValueAsFloat("radius");
                color = xml->getAttributeValue("color");
            }

            break;
        }
    }
}

so at this point, the vectorName variable contains the string "origin", but how do I declare a new Vector with that string as the variable? Same goes for vectorName and sphereName .. Any help would be appreciated. Thanks.

I don't use this library or anything, but what it sounds like you want to do is dynamically create some equivalent to

vector< someType > origin;

so that you can later refer to it like:

origin.at(n).doSomething();

I don't think you can do this. The way that I might do it is to create a std::map object like:

std::map< string, vector< someType > > bigMap;

that way you can add things to the map, and refer to them using the string that you get as the key. So:

std::string keyString("origin");
bigMap[keyString].at(n).doSomething();

You then add as many vectors that you can retrieve using strings as you want.

Hope that's roughly what you're looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.