InternalTree::InternalTree(const std::string & line, const bool terminal): m_isTerminal(terminal) { size_t found = line.find_first_of("[] "); if (found == line.npos) { m_value = line; } else { AddSubTree(line, 0); } }
size_t InternalTree::AddSubTree(const std::string & line, size_t pos) { char token = 0; size_t len = 0; bool has_value = false; while (token != ']' && pos != std::string::npos) { size_t oldpos = pos; pos = line.find_first_of("[] ", pos); if (pos == std::string::npos) break; token = line[pos]; len = pos-oldpos; if (token == '[') { if (has_value) { m_children.push_back(boost::make_shared<InternalTree>(line, oldpos, len, true)); pos = m_children.back()->AddSubTree(line, pos+1); } else { if (len > 0) { m_value.CreateFromString(Output, StaticData::Instance().options()->output.factor_order, StringPiece(line).substr(oldpos, len), false); has_value = true; } pos = AddSubTree(line, pos+1); } } else if (token == ' ' || token == ']') { if (len > 0 && !has_value) { m_value.CreateFromString(Output, StaticData::Instance().options()->output.factor_order, StringPiece(line).substr(oldpos, len), true); has_value = true; } else if (len > 0) { m_children.push_back(boost::make_shared<InternalTree>(line, oldpos, len, false)); } if (token == ' ') { pos++; } } } if (pos == std::string::npos) { return line.size(); } return std::min(line.size(),pos+1); }
size_t InternalTree::AddSubTree(const std::string & line, size_t pos) { std::string value; char token = 0; while (token != ']' && pos != std::string::npos) { size_t oldpos = pos; pos = line.find_first_of("[] ", pos); if (pos == std::string::npos) break; token = line[pos]; value = line.substr(oldpos,pos-oldpos); if (token == '[') { if (m_value.size() > 0) { m_children.push_back(boost::make_shared<InternalTree>(value,false)); pos = m_children.back()->AddSubTree(line, pos+1); } else { if (value.size() > 0) { m_value = value; } pos = AddSubTree(line, pos+1); } } else if (token == ' ' || token == ']') { if (value.size() > 0 && !(m_value.size() > 0)) { m_value = value; } else if (value.size() > 0) { m_isTerminal = false; m_children.push_back(boost::make_shared<InternalTree>(value,true)); } if (token == ' ') { pos++; } } if (m_children.size() > 0) { m_isTerminal = false; } } if (pos == std::string::npos) { return line.size(); } return std::min(line.size(),pos+1); }
LinkTree* LinkTree::Load(const pXmlTree tree, int *currIndexPtr){ Clear(); int currIndex = 0; if(currIndexPtr!=NULL){ currIndex = *currIndexPtr; } if(tree!=NULL){ if(tree->GetName()=="Link"){ mData->Load(tree); mData->SetIndex(currIndex); currIndex++; } pXmlTree tmp = tree->Find("Children"); if(tmp!=NULL){ pXmlTreeList tlist = tmp->GetSubTrees(); for(unsigned int i=0;i<tlist->size();i++){ if(tlist->at(i)->GetName()=="Link"){ //cout << tlist->at(i)->GetData()<<endl; pLinkTree linkTree = (new LinkTree())->Load(tlist->at(i),&currIndex); if(linkTree->mData->mJoint!=NULL){ linkTree->mData->mJoint->SetParentLink(mData); mData->AddChildJoint(linkTree->mData->mJoint); } AddSubTree(linkTree); } } } } if(currIndexPtr!=NULL){ *currIndexPtr = currIndex; } return this; }
/** * Construct the scene */ void ConstructScene() { // -------------------- Geometry -------------------- // movingSquare = new UnitSquareFlatSurface(200,32.2f*.814f,true,Vector2(-.01f,0),.03f); SceneNode* house = ConstructHouse(70,90); // Construct a unit cylinder surface WasherSurface* cylinder = new WasherSurface(.9f, 1.0f, 18); //Wheel WasherSurface* washer = new WasherSurface(10,20,14); std::vector<LightNode*> lights; lights.push_back(new LightNode(GL_LIGHT3)); lights.push_back(new LightNode(GL_LIGHT4)); wheel = new WheelNode(10,20,washer,ConstructBox(9,19.5f,1),8,4,lights); waterfall = new ParticleNode(25); //-------------------- Materials ------------------------- // // Wood PresentationNode* wood = new PresentationNode; wood->SetMaterialAmbientAndDiffuse(Color4(0.5f, 0.5f, 0.5f)); wood->SetMaterialSpecular(Color4(0.3f, 0.3f, 0.3f)); wood->SetMaterialShininess(32.0f); wood->setTexture(LoadTextureBMP("images/wood.bin",true,128)); // Stone PresentationNode* stone = new PresentationNode; stone->SetMaterialAmbient(Color4(0.19225f, 0.19225f, 0.19225f)); stone->SetMaterialDiffuse(Color4(0.50754f, 0.50754f, 0.50754f)); stone->SetMaterialSpecular(Color4(0.508273f, 0.508273f, 0.508273f)); stone->SetMaterialShininess(10.2f); stone->setTexture(LoadTextureBMP("images/stone.bin",true,128)); PresentationNode* steel = new PresentationNode; steel->SetMaterialAmbient(Color4(0.19225f, 0.19225f, 0.19225f)); steel->SetMaterialDiffuse(Color4(0.7f, 0.7f, 0.7f)); steel->SetMaterialSpecular(Color4(0.7f, 0.7f, 0.7f)); steel->SetMaterialShininess(90.2f); // Water PresentationNode* water = new PresentationNode; water->SetMaterialAmbient(Color4(0.01f, 0.01f, 0.1f)); water->SetMaterialDiffuse(Color4(0.2f, 0.2f, 0.6f)); water->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f)); water->SetMaterialShininess(10); water->setTexture(LoadTextureBMP("images/water.bin",true,128)); PresentationNode* blue = new PresentationNode; blue->SetMaterialAmbient(Color4(0.01f, 0.01f, 0.1f)); blue->SetMaterialDiffuse(Color4(0.2f, 0.2f, 0.6f)); blue->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f)); blue->SetMaterialShininess(10); // ------------------ Transformations ------------------- // TransformNode* wheelTransform = new TransformNode; wheelTransform->Translate(-80.0f, 35.0f, 40.0f); wheelTransform->Rotate(90.0f, 1.0f, 0.0f, 0.0f); TransformNode* houseTransform = new TransformNode; houseTransform->Translate(-100,35,0); TransformNode* house2Transform = new TransformNode; house2Transform->Rotate(180,0,0,1); house2Transform->Translate(-100,-35,0); TransformNode* wallTransform = new TransformNode; wallTransform->Translate(0,35,0); TransformNode* waterfallTransform = new TransformNode; waterfallTransform->Translate(-128.0f,35,15.0f); TransformNode* pipeTransform = new TransformNode; pipeTransform->Rotate(-90,0,1,0); pipeTransform->Translate(76.5f,35,125); pipeTransform->Scale(5,5,20); // -------------------- Lighting --------------------------/ // Light 0 - point light source in back right corner LightNode* light0 = new LightNode(GL_LIGHT0); light0->SetDiffuse(Color4(0.5f, 0.5f, 0.5f, 1.0f)); light0->SetSpecular(Color4(0.5f, 0.5f, 0.5f, 1.0f)); light0->SetPosition(HPoint3(90.0f, 90.0f, 30.f, 1.0f)); light0->Enable(); // Light1 - directional light from the ceiling LightNode* light1 = new LightNode(GL_LIGHT1); light1->SetDiffuse(Color4(0.7f, 0.7f, 0.7f, 1.0f )); light1->SetSpecular(Color4(0.7f, 0.7f, 0.7f, 1.0f)); light1->SetPosition(HPoint3(0.0f, 0.0f, 1.0f, 0.0f)); light1->Enable(); // Light2 - spotlight - we will place at the camera location // shining along -VPN LightNode* light2 = new LightNode(GL_LIGHT2); light2->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f )); light2->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); light2->SetPosition(HPoint3(0.0f, 0.0f, 0.0f, 1.0f)); light2->SetSpotlight(Vector3(0.0f, 0.0f, -1.0f), 32.0f, 30.0f); light2->Enable(); lights[0]->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[0]->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[0]->SetPosition(HPoint3(-100.0f,35.0f,90.0f,1.0f)); lights[0]->SetSpotlightDirection(Vector3(0,0,-1)); lights[0]->SetSpotlight(Vector3(0,0,-1),1,90); lights[0]->Disable(); lights[1]->SetDiffuse(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[1]->SetSpecular(Color4(0.8f, 0.8f, 0.8f, 1.0f)); lights[1]->SetPosition(HPoint3(100.0f,35.0f,90.0f,1.0f)); lights[1]->SetSpotlightDirection(Vector3(0,0,-1)); lights[1]->SetSpotlight(Vector3(0,0,-1),1,90); lights[1]->Disable(); // --------------------------- Camera ----------------------- // MyCamera = new CameraNode; MyCamera->SetPosition(Point3(100.0f, -100.0f, 50.0f)); MyCamera->SetLookAtPt(Point3(0.0f, 0.0f, 50.0f)); MyCamera->SetViewUp(Vector3(0.0, 0.0, 1.0)); MyCamera->SetPerspective(50.0, 1.0, 1.0, 2400); // --------------------- Scene construction ----------------- // // Construct the scene root node SceneRoot = new SceneNode; // Create a scene node to hold all scene objects (other than camera // and lights) SceneNode* myScene = new SceneNode; // Add the spotlight as the first child of the root node. Since this is // accessed before the camera it will position the light relative to the // camera SceneRoot->AddChild(light2); // Set the camera SceneRoot->AddChild(MyCamera); MyCamera->AddChild(light0); MyCamera->AddChild(light1); MyCamera->AddChild(lights[0]); MyCamera->AddChild(lights[1]); MyCamera->AddChild(myScene); // Construct the room (walls, floor, ceiling) SceneNode* skybox = ConstructRoom(); wallTransform->AddChild(ConstructWall(stone,water,movingSquare)); myScene->AddChild(wallTransform); // Construct the wheel AddSubTree(myScene,wood,wheelTransform,wheel); // Place 2 Houses wood->AddChild(houseTransform); houseTransform->AddChild(house); wood->AddChild(house2Transform); house2Transform->AddChild(house); myScene->AddChild(skybox); AddSubTree(myScene,steel,pipeTransform,cylinder); AddSubTree(myScene,blue,waterfallTransform,waterfall); }
/** * Construct room as a child of the specified node * @param parent Parent node * @param unitSquare Geometry node to use */ void ConstructRoom(SceneNode* parent, UnitSquareSurface* unitSquare) { // Contruct transform nodes for the walls. Perform rotations so the // walls face inwards TransformNode* floorTransform = new TransformNode; floorTransform->Scale(200.0f, 200.0f, 1.0f); // Back wall is rotated +90 degrees about x: (y -> z) TransformNode* backWallTransform = new TransformNode; backWallTransform->Translate(0.0f, 100.0f, 40.0f); backWallTransform->Rotate(90.0f, 1.0f, 0.0f, 0.0f); backWallTransform->Scale(200.0f, 80.0f, 1.0f); // Front wall is rotated -90 degrees about x: (z -> y) TransformNode* frontWallTransform = new TransformNode; frontWallTransform->Translate(0.0f, -100.0f, 40.0f); frontWallTransform->Rotate(-90.0f, 1.0f, 0.0f, 0.0f); frontWallTransform->Scale(200.0f, 80.0f, 1.0f); // Left wall is rotated 90 degrees about y: (z -> x) TransformNode* leftWallTransform = new TransformNode; leftWallTransform->Translate(-100.0f, 0.0f, 40.0f); leftWallTransform->Rotate(90.0f, 0.0f, 1.0f, 0.0f); leftWallTransform->Scale(80.0f, 200.0f, 1.0f); // Right wall is rotated -90 about y: (z -> -x) TransformNode* rightWallTransform = new TransformNode; rightWallTransform->Translate(100.0f, 0.0f, 40.0f); rightWallTransform->Rotate(-90.0f, 0.0f, 1.0f, 0.0f); rightWallTransform->Scale(80.0f, 200.0f, 1.0f); // Ceiling is rotated 180 about x so it faces inwards TransformNode* ceilingTransform = new TransformNode; ceilingTransform->Translate(0.0f, 0.0f, 80.0f); ceilingTransform->Rotate(180.0f, 1.0f, 0.0f, 0.0f); ceilingTransform->Scale(200.0f, 200.0f, 1.0f); // Floor should be tan, mostly dull PresentationNode* floorMaterial = new PresentationNode; floorMaterial->SetMaterialAmbientAndDiffuse(Color4(0.3f, 0.45f, 0.1f)); floorMaterial->SetMaterialSpecular(Color4(0.1f, 0.1f, 0.1f)); floorMaterial->SetMaterialShininess(2.0f); // Make the walls reddish, slightly shiny PresentationNode* wallMaterial = new PresentationNode; wallMaterial->SetMaterialAmbientAndDiffuse(Color4(0.7f, 0.55f, 0.55f)); wallMaterial->SetMaterialSpecular(Color4(0.5f, 0.5f, 0.5f)); wallMaterial->SetMaterialShininess(16.0f); // Ceiling should be white, moderately shiny PresentationNode* ceilingMaterial = new PresentationNode; ceilingMaterial->SetMaterialAmbientAndDiffuse(Color4(1.0f, 1.0f, 1.0f)); ceilingMaterial->SetMaterialSpecular(Color4(0.9f, 0.9f, 0.9f)); ceilingMaterial->SetMaterialShininess(64.0f); // Add floor and ceiling to the parent. Use convenience method to add material, // then presentation, then geometry. AddSubTree(parent, floorMaterial, floorTransform, unitSquare); AddSubTree(parent, ceilingMaterial, ceilingTransform, unitSquare); // Walls. We can group these all under a single presentation node. parent->AddChild(wallMaterial); wallMaterial->AddChild(backWallTransform); backWallTransform->AddChild(unitSquare); wallMaterial->AddChild(leftWallTransform); leftWallTransform->AddChild(unitSquare); wallMaterial->AddChild(rightWallTransform); rightWallTransform->AddChild(unitSquare); wallMaterial->AddChild(frontWallTransform); frontWallTransform->AddChild(unitSquare); }