📄 scenecomponents.cpp
字号:
* shape of node, and shape based properties of node * @param str String containing node properties * @param node Node instance to write properties to * @return Indicates wheather parse was successfull */bool TSceneNode::parseScene( string & str ){ unsigned index = 0; if( str.compare( 0, CMD_SCENE.length(), CMD_SCENE ) != 0 ) { cerr << "TSceneNode::parseScene: This is not an importScene command" << endl; return false; } string token, mass; bool bTorso = false; index += 13; if( !Parse::getToken( str, index, token ) || token == ")" ) return false; if( token.compare( 0, 25, "rsg/boxspheres/sphere.rsg" ) == 0 ) { string radius; if( !Parse::getToken( str, index, radius ) || radius == ")" ) { cerr << "TSceneNode::parseScene: Couldn't parse radius" << endl; return false; } setShape( SHAPE_SPHERE ); setRadius( TGenericData::getNumberFromString( radius ) ); } else if( ( token.compare( 0, 22, "rsg/boxspheres/box.rsg" ) == 0 ) || ( token.compare( 0, 35, "rsg/boxspheres/box_with_handler.rsg" ) == 0 ) || ( token.compare( 0, 28, "rsg/agent/soccerbottorso.rsg" ) == 0 ) ) { if( token.compare( 0, 28, "rsg/agent/soccerbottorso.rsg" ) == 0 ) bTorso = true; string length, width, height; if( !Parse::getToken( str, index, length ) || length == ")" ) { cerr << "TSceneNode::parseScene: Couldn't parse length" << endl; return false; } if( !Parse::getToken( str, index, width ) || width == ")" ) { cerr << "TSceneNode::parseScene: Couldn't parse width" << endl; return false; } if( !Parse::getToken( str, index, height ) || height == ")" ) { cerr << "TSceneNode::parseScene: Couldn't parse height" << endl; return false; } setShape( SHAPE_BOX ); setLength( TGenericData::getNumberFromString( length ) ); setWidth( TGenericData::getNumberFromString( width ) ); setHeight( TGenericData::getNumberFromString( height ) ); } else { cerr << "TSceneNode::parseScene: Scene implied is not defined: '" << token << "'" << endl; return false; } if( !Parse::getToken( str, index, mass ) || mass == ")" ) { cerr << "TSceneNode::parseScene: Couldn't parse mass" << endl; return false; } setMass( TGenericData::getNumberFromString( mass ) ); if( bTorso ) setMaterial( MAT_BLUE ); else { TMaterial mat; if( (mat = parseMaterial( str, index )) == MAT_ILLEGAL ) { cerr << "TSceneNode::parseScene: Couldn't parse material" << endl; return false; } setMaterial( mat ); } return true;}/** * This method parses the string given for a box information: length width height * @param str String that contains box information * @return Indicates parse was successfull or not */bool TSceneNode::parseBoxExtents( string & str ){ unsigned index = 0; if( !Parse::gotoFirstParsableModule( str, index ) || str.compare(0, CMD_EXTENT.length(), CMD_EXTENT) != 0 ) return false; string v1, v2, v3; index = 11; if( !Parse::getToken( str, index, v1 ) ) return false; if( !Parse::getToken( str, index, v2 ) ) return false; if( !Parse::getToken( str, index, v3 ) ) return false; m_Length = TGenericData::getNumberFromString( v1 ); m_Width = TGenericData::getNumberFromString( v2 ); m_Height = TGenericData::getNumberFromString( v3 ); return true;}/** * This method parses cylinders parameters: radius and length * @param str String containing the parameters * @return Indicates parsing was successfull */bool TSceneNode::parseCylinderParams( string & str ){ unsigned index = 0; if( !Parse::gotoFirstParsableModule( str, index ) || str.compare(0, CMD_PARAM.length(), CMD_PARAM) != 0 ) return false; string v1, v2; index = 11; if( !Parse::getToken( str, index, v1 ) ) return false; if( !Parse::getToken( str, index, v2 ) ) return false; m_Radius = TGenericData::getNumberFromString( v1 ); m_Length = TGenericData::getNumberFromString( v2 ); return true;}/** * This method skips the box collider node so that other can be parsed * @param str String containing the box collider node * @return true for parsing the node */bool TSceneNode::parseBoxColliderNode( string & str ){ return true;}/** * This method parses a touch perceptor and adds it to the perceptors * @param str Stirng containing the perceptor information * @return */bool TSceneNode::parseTouchPerceptor( string & str ){ return true;}/** * This is a general purpose method to parse the information residing in a string and are in S-Lang form. * @param str String that contains the infomration to be parsed * @param index Current index of string to use * @param pran Number of pranthesis that has been parsed till the begining of this method * @param nodes All the nodes that have been previously parsed * @param joints All the joints that have been previously parsed * @return */bool TSceneNode::parseInformation( string & str, unsigned & index, int & pran, TNodeList & nodes, TJointList & joints ){ unsigned i = index; string strToken; Parse::gotoFirstParsableModule( str, i ); while( i < str.size() && str[i] != ')' ) { if( str.compare( i, 1, "(" ) != 0 ) { cerr << "TSceneNode::parseInformation: Unknown token parsed" << endl; break; } pran++; strToken = "("; while( pran > 1 && i < str.size() ) /// 1: because of the node itself has a pranthesis { i++; if( str[i] == ')' ) pran--; else if( str[i] == '(' ) pran++; strToken += str[i]; } i++; /// Because the last thing added was not moved forward if( strToken.compare( 0, CMD_NAME.length(), CMD_NAME ) == 0 ) { setName( parseName( strToken ) ); if( getName() == "" && bStrict ) break; } else if( strToken.compare( 0, CMD_MATERIAL.length(), CMD_MATERIAL ) == 0 ) { unsigned jindex = CMD_MATERIAL.length(); setMaterial( parseMaterial( strToken, jindex ) ); if( getMaterial() == MAT_ILLEGAL && bStrict ) break; } else if( strToken.compare( 0, CMD_POSITION.length(), CMD_POSITION ) == 0 ) { setPosition( parseNodePosition( strToken ) ); if( getPosition() == UnknownPosition && bStrict ) break; } else if( strToken.compare( 0, CMD_SCENE.length(), CMD_SCENE ) == 0 ) { if( !parseScene( strToken ) && bStrict ) break; } else if( strToken.compare( 0, NODE_BOXCOLLIDER.length(), NODE_BOXCOLLIDER ) == 0 ) { if( !parseBoxColliderNode( strToken ) && bStrict ) break; } else if( (strToken.compare( 4, NODE_HINGE.length(), NODE_HINGE ) == 0) || (strToken.compare( 4, NODE_HINGE2.length(), NODE_HINGE2 ) == 0) || (strToken.compare( 4, NODE_UNIVERSAL.length(), NODE_UNIVERSAL ) == 0) || (strToken.compare( 4, NODE_FIXED.length(), NODE_FIXED ) == 0) ) { TSceneJoint *joint = new TSceneJoint; if( !joint->parseAndLoad( strToken, this, nodes, joints ) ) { delete joint; break; } } else if( strToken.compare( 0, CMD_ROTATATION.length(), CMD_ROTATATION ) == 0 ) { setDirection( parseLocalRotation( strToken ) ); if( getDirection() == UnknownPosition && bStrict ) break; } else if( strToken.compare( 4, NODE_TOUCH.length(), NODE_TOUCH ) == 0 ) { if( !parseTouchPerceptor( strToken ) && bStrict ) break; } else if( strToken.compare( 0, CMD_PARAM.length(), CMD_PARAM ) == 0 ) { if( !parseCylinderParams( strToken ) && bStrict ) break; } else if( strToken.compare( 0, CMD_EXTENT.length(), CMD_EXTENT ) == 0 ) { if( !parseBoxExtents( strToken ) && bStrict ) break; } else if( strToken.compare( 0, 4, "(nd " ) == 0 ) { if( strToken.compare( 4, NODE_TRANSFORM.length(), NODE_TRANSFORM ) == 0 || strToken.compare( 4, NODE_CCYLINDER.length(), NODE_CCYLINDER ) == 0 || strToken.compare( 4, NODE_BOX.length(), NODE_BOX ) == 0 || strToken.compare( 4, NODE_AGENTASPECT.length(), NODE_AGENTASPECT ) == 0 ) { TSceneNode * child = new TSceneNode; if( !child->parseAndLoad( strToken, this, nodes, joints ) ) { delete child; if( bStrict ) break; } else m_Children.push_back( child ); } } else cerr << "TSceneNode::parseInformation: Unknown sub_token was token: '" << strToken << "'" << endl; Parse::gotoFirstParsableModule( str, i ); } Parse::gotoFirstParsableModule( str, i ); if( str[i] != ')' ) return false; return true;}/** * This method saves internal information of node to the specified output source * @param os Output stream to write internal information of node to * @param strGap Gap to print before each element */void TSceneNode::saveInformation( ostream & os, string strGap ) const{ string strmat; if( m_Material == MAT_BLUE ) strmat = "matBlue"; else if( m_Material == MAT_DARKGREY ) strmat = "matDarkGrey"; else if( m_Material == MAT_GREEN ) strmat = "matGreen"; else if( m_Material == MAT_GREY ) strmat = "matGrey"; else if( m_Material == MAT_RED ) strmat = "matRed"; else if( m_Material == MAT_WHITE ) strmat = "matWhite"; else if( m_Material == MAT_YELLOW ) strmat = "matYellow"; else cerr << "show: yeki be man bege materialam chie?!" << endl; if( m_Name != "" ) os << strGap << CMD_NAME << " " << m_Name << ")\n"; if( m_Position != 0 ) os << strGap << CMD_POSITION << " " << m_Position.getX() << " " << m_Position.getY() << " " << m_Position.getZ() << ")\n"; if( m_Direction != 0 ) os << strGap << CMD_ROTATATION << " " << m_Direction.getX() << " " << m_Direction.getY() << " " << m_Direction.getZ() << ")\n"; if( m_NodeName == NODE_TRANSFORM ) { if( m_Shape != SHAPE_ILLEGAL ) { string strScene; if( m_Shape == SHAPE_BOX ) strScene = "rsg/boxspheres/box.rsg"; else if( m_Shape == SHAPE_SPHERE ) strScene = "rsg/boxspheres/sphere.rsg"; os << strGap << CMD_SCENE << " " << strScene << " "; if( m_Shape == SHAPE_BOX ) os << m_Length << " " << m_Width << " " << m_Height; else if( m_Shape == SHAPE_SPHERE ) os << m_Radius; else cerr << "save: shape chie?!" << endl; os << " " << m_Mass << " "; os << strmat << ")\n"; } } else if( m_NodeName == NODE_CCYLINDER ) { os << strGap << CMD_MATERIAL << " " << strmat << ")\n"; os << strGap << CMD_PARAM << " " << m_Radius << " " << m_Length << ")\n"; } else if( m_NodeName == NODE_BOX ) { os << strGap << CMD_MATERIAL << " " << strmat << ")\n"; os << strGap << CMD_EXTENT << " " << m_Length << " " << m_Width << " " << m_Height << ")\n"; } else if( m_NodeName == NODE_SPACE ) { /// Currently nothing is needed } else if( m_NodeName == NODE_AGENTASPECT ) { if( m_Shape != SHAPE_ILLEGAL ) { string strScene; if( m_Shape == SHAPE_BOX ) strScene = "rsg/boxspheres/box.rsg"; else if( m_Shape == SHAPE_SPHERE ) strScene = "rsg/boxspheres/sphere.rsg"; os << strGap << CMD_SCENE << " " << strScene << " "; if( m_Shape == SHAPE_BOX ) os << m_Length << " " << m_Width << " " << m_Height; else if( m_Shape == SHAPE_SPHERE ) os << m_Radius; else cerr << "save: shape chie?!" << endl; os << " " << m_Mass << " "; os << strmat << ")\n"; } } else cerr << "show : yeki be man bege node man chie?! : '" << m_NodeName << "'" << endl;}/** * This method parses AgentAspect infomration form the given string * @param str String that contains AgentAspect infomration * @param parrent Parrent of this node * @param nodes List of nodes to add this node to them * @param joints List of joints to add the joints in this node to them * @return Indicates that parse and loading was successfull */bool TSceneNode::parseAgentAspectNode( string str, TSceneNode * parrent, TNodeList & nodes, TJointList & joints ){ if( str.compare( 4, NODE_AGENTASPECT.length(), NODE_AGENTASPECT ) != 0 ) { cerr << "TSceneNode::parseAgentAspectNode: This is not a AgentAspect" << endl; return false; } setNodeName( NODE_AGENTASPECT ); m_Position.setVecPosition( 0, 0, 0 ); unsigned index = 15; int pran = 1; if( !parseInformation( str, index, pran, nodes, joints ) ) { cerr << "TSceneNode::parseAgentAspectNode: In parsing AgentAspect node" << endl; return false; } bool bReturn = false; if( pran != 1 ) {} else if( !initialized() ) { clearChildren(nodes); unsigned jindex = index; Parse::eatMessage( str, jindex, pran ); cerr << "TSceneNode::parseAgentAspectNode: Not parsed: '" << str.substr( index - 15, jindex - (index - 15) + 1 ) << "'" << endl; } else bReturn = true; return bReturn;}/** * This method parses a space node * @param str String that contains information about the space node * @param parrent Parrent of this node (if any) * @param nodes List of nodes to add this node after being loaded to * @param joints List of joints to pass the joint parser to after loaded joint information * @return */bool TSceneNode::parseSpaceNode( string str, TSceneNode * parrent, TNodeList & nodes, TJointList & joints ){ if( str.compare( 4, NODE_SPACE.length(), NODE_SPACE ) != 0 ) { cerr << "TSceneNode::parseSpaceNode: This is not a space node" << endl; return false; } unsigned first = str.find( ")", 1 ); if( first == str.npos ) { cerr << "TSceneNode::parseSpaceNode: Couldn't get find first pranthesis" << endl; return false; } unsigned last = str.rfind( ")" ); if( last == str.npos ) { cerr << "TSceneNode::parseSpaceNode: Couldn't get find last pranthesis" << endl; return false; } setNodeName( NODE_SPACE ); m_Position.setVecPosition( 0, 0, 0 ); unsigned index = 9; int pran = 1; if( !parseInformation( str, index, pran, nodes, joints ) ) { cerr << "TSceneNode::parseSpaceNode: In parsing space node" << endl; return false; } bool bReturn = false; if( pran != 1 ) {} else if( !initialized() ) { clearChildren(nodes); unsigned jindex = index; Parse::eatMessage( str, jindex, pran ); cerr << "TSceneNode::parseSpaceNode: Not parsed: '" << str.substr( index - 15, jindex - (index - 15) + 1 ) << "'" << endl; } else bReturn = true; return bReturn;}/** * This method parses a cylinder and adds it to the list of nodes * @param str String containing the cylinder information * @return Indicates wheather parse was successfull */bool TSceneNode::parseCylinderNode( string str, TSceneNode * parrent, TNodeList & nodes, TJointList & joints ){ unsigned index = 0; if( str.compare( 4, NODE_CCYLINDER.length(), NODE_CCYLINDER ) != 0 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -