void Scene::_parseOrthographicCamera( ){ float position[3], lookAt[3], up[3]; _checkToken( "OrthographicCamera" ); _nextToken( ); _checkToken( "{" ); _nextToken( ); _checkToken( "position" ); for( int i = 0; i < 3; i++ ){ _nextToken( ); position[i] = _parseFloat( ); } Point3 pPosition(position); _nextToken( ); _checkToken( "lookAt" ); for( int i = 0; i < 3; i++ ){ _nextToken( ); lookAt[i] = _parseFloat( ); } Point3 pLookAt(lookAt); _nextToken( ); _checkToken( "up" ); for( int i = 0; i < 3; i++ ){ _nextToken( ); up[i] = _parseFloat( ); } Vec3 vUp(up); _nextToken( ); _checkToken( "}" ); // Fill me in! }
void ConfigLoader::_skipNewLines(std::ifstream &stream) { while (tok == TOKEN_NewLine) { _nextToken(stream); } }
void ScriptLoader::_skipNewLines(std::ifstream &stream) { while (mToken == TOKEN_NewLine) { _nextToken(stream); } }
void Scene::_parseBackground( ){ unsigned char vec[3]; _nextToken( ); _checkToken( "Background" ); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken( "color" ); for( int i = 0; i < 3; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } _nextToken( ); _checkToken( "}" ); // Fill me in! }
void Scene::_parseCamera( ){ _nextToken( ); if( std::strcmp( _currentToken, "OrthographicCamera") == 0 ){ _parseOrthographicCamera( ); }else if( std::strcmp( _currentToken, "PerspectiveCamera") == 0 ){ _parsePerspectiveCamera( ); }else if( std::strcmp( _currentToken, "SimplePerspectiveCamera") == 0 ){ _parseSimplePerspectiveCamera( ); }else{ _checkToken( "OrthographicCamera | PerspectiveCamera | SimplePerspectiveCamera"); } }
int Lexer::lex() { if (_recallToken) { _recallToken = false; } else { _lastToken = _nextToken(); } return _lastToken.type; }
void Scene::_parseMaterials( ){ _nextToken( ); _checkToken( "Materials" ); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken( "numMaterials" ); _nextToken( ); int materialCount = _parseInt( ); for( int i = 0; i < materialCount; i++ ){ _nextToken( ); if( std::strcmp( _currentToken, "PhongMaterial") == 0 ){ _parsePhongMaterial(i); }else if( std::strcmp( _currentToken, "SomeOtherMaterial") == 0 ){ assert(0 == 1); }else if( std::strcmp( _currentToken, "AnotherMaterial") == 0 ){ assert(0 == 1); }else{ _checkToken( "PhongMaterial | tbd"); } } _nextToken( ); _checkToken("}"); }
void Scene::_parseLights( ){ _nextToken( ); _checkToken( "Lights" ); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken( "numLights" ); _nextToken( ); int lightCount = _parseInt( ); for( int i = 0; i < lightCount; i++ ){ _nextToken( ); if( std::strcmp( _currentToken, "PointLight") == 0 ){ _parsePointLight(i); }else if( std::strcmp( _currentToken, "DirectionalLight") == 0 ){ _parseDirectionalLight(i); }else if( std::strcmp( _currentToken, "SomeOtherLight") == 0 ){ assert(0 == 1); }else{ _checkToken("PointLight | DirectionalLight"); } } _nextToken( ); _checkToken("}"); }
void Scene::_parsePhongMaterial(uint id){ float vec[4]; _checkToken( "PhongMaterial"); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken("ambientColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor ambient(vec); _nextToken( ); _checkToken( "diffuseColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor diffuse(vec); _nextToken( ); _checkToken("specularColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor specular(vec); _nextToken( ); _checkToken("shininess"); float shininess = _parseFloat( ); _nextToken( ); float indexOfRefraction = 10; if( std::strcmp(_currentToken, "indexOfRefraction") == 0 ){ indexOfRefraction = _parseFloat( ); } _nextToken( ); _checkToken( "}"); // Fill me in! }
void ConfigLoader::parseScript(std::ifstream &stream) { //Get first token _nextToken(stream); if (tok == TOKEN_EOF) { stream.close(); return; } //Parse the script _parseNodes(stream, 0); stream.close(); }
void Scene::_parseSphere(int i){ float vec[3]; _checkToken( "Sphere"); _nextToken( ); _checkToken( "{" ); _nextToken( ); _checkToken( "center" ); for( int i = 0; i < 3; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } Point3 center(vec[0], vec[1], vec[2]); _nextToken( ); _checkToken( "radius" ); _nextToken( ); float radius = _parseFloat( ); _nextToken( ); _checkToken("}"); // Fill me in! }
void Scene::_parseGroup( ){ _nextToken( ); _checkToken( "Group" ); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken( "numObjects" ); _nextToken( ); int objectCount = _parseInt( ); int i = 0; _nextToken( ); while( i < objectCount ){ if( std::strcmp( _currentToken, "MaterialIndex") == 0 ){ _parseMaterialIndex( ); }else if( std::strcmp( _currentToken, "Group") == 0 ){ assert(0 == 1); _parseGroup( ); i += 1; }else if( std::strcmp( _currentToken, "Transform") == 0 ){ assert(0 == 1); }else if( std::strcmp( _currentToken, "Sphere") == 0 ){ _parseSphere(i); i += 1; }else if( std::strcmp( _currentToken, "Plane") == 0 ){ assert(0 == 1); i += 1; }else if( std::strcmp( _currentToken, "Triangle") == 0 ){ assert(0 == 1); i += 1; }else if( std::strcmp( _currentToken, "TriangleMesh") == 0 ){ assert(0 == 1); i += 1; }else{ _checkToken( "Group | tbd"); } _nextToken( ); } }
void ConfigLoader::_parseNodes(std::ifstream &stream, ConfigNode *parent) { typedef std::pair<std::string, ConfigNode*> ScriptItem; while (true) { switch (tok) { //Node case TOKEN_Text: //Add the new node ConfigNode *newNode; if (parent) { newNode = parent->addChild(tokVal); } else { newNode = new ConfigNode(0, tokVal); } //Get values _nextToken(stream); while (tok == TOKEN_Text) { newNode->addValue(tokVal); _nextToken(stream); } //Add root nodes to scriptList if (!parent){ std::string key; if (newNode->getValues().empty()) { key = newNode->getName() + ' '; } else { key = newNode->getName() + ' ' + newNode->getValues().front(); } m_scriptList.insert(ScriptItem(key, newNode)); } _skipNewLines(stream); //Add any sub-nodes if (tok == TOKEN_OpenBrace) { //Parse nodes _nextToken(stream); _parseNodes(stream, newNode); //Check for matching closing brace if (tok != TOKEN_CloseBrace) { throw std::runtime_error("Parse Error: Expecting closing brace"); } _nextToken(stream); _skipNewLines(stream); } break; //Out of place brace case TOKEN_OpenBrace: throw std::runtime_error("Parse Error: Opening brace out of plane"); break; //Return if end of nodes have been reached case TOKEN_CloseBrace: return; //Return if reached end of file case TOKEN_EOF: return; case TOKEN_NewLine: _nextToken(stream); break; } }; }
void ScriptLoader::_parseNodes(std::ifstream &stream, ScriptNode *parent) { typedef std::pair<std::string, ScriptNode*> ScriptItem; while (true) { switch (mToken) { //Node case TOKEN_Text: { //Add the new node ScriptNode *newNode; if (parent) { newNode = parent->addChild(mTokenValue); } else { newNode = new ScriptNode(0, mTokenValue); } //Get values _nextToken(stream); std::string valueStr; int i=0; while (mToken == TOKEN_Text) { if (i == 0) valueStr += mTokenValue; else valueStr += " " + mTokenValue; _nextToken(stream); ++i; } newNode->setValue(valueStr); //Add root nodes to scriptList if (!parent) { std::string key; if (newNode->getValue() == "") throw std::runtime_error("Root node must have a name (\"" + newNode->getName() + "\")"); key = newNode->getValue(); m_scriptList.insert(ScriptItem(key, newNode)); } _skipNewLines(stream); //Add any sub-nodes if (mToken == TOKEN_OpenBrace) { //Parse nodes _nextToken(stream); _parseNodes(stream, newNode); //Check for matching closing brace if (mToken != TOKEN_CloseBrace) { throw std::runtime_error("Parse Error: Expecting closing brace"); } _nextToken(stream); _skipNewLines(stream); } newNode->mFileName = mCurrentFileName; break; } //Out of place brace case TOKEN_OpenBrace: throw std::runtime_error("Parse Error: Opening brace out of plane"); break; //Return if end of nodes have been reached case TOKEN_CloseBrace: return; //Return if reached end of file case TOKEN_EOF: return; case TOKEN_NewLine: _nextToken(stream); break; } }; }
void Scene::_parseMaterialIndex( ){ _checkToken( "MaterialIndex"); _nextToken( ); _currentMaterialIndex = _parseInt( ); }
/* * nextToken - fetch the next real token from the buffer */ static token nextToken( void ) { int j; char *endptr; currToken = _nextToken(); if( currToken == T_UNKNOWN ) { currToken = T_CONSTANT; if( isdigit( tokenBuff[0] ) ) { constantVal = strtol( tokenBuff, &endptr, 10 ); if( (endptr - tokenBuff) != tokenBuffCnt ) { constantVal = strtol( tokenBuff, &endptr, 16 ); if( (endptr - tokenBuff) != tokenBuffCnt ) { abortExpr( ERR_INVALID_VALUE ); } } } else { if( tokenBuff[0] == '.' ) { strcpy( tokenBuff, GetASetVal( &tokenBuff[1] ) ); constantVal = strtol( tokenBuff, NULL, 0 ); j = tokenBuffCnt - 1; while( j >= 0 ) { if( !isdigit( tokenBuff[j] ) ) { currToken = T_STRING; break; } j--; } } else if( !strcmp( tokenBuff, "config" ) ) { constantVal = EditFlags.Color * 100 + EditFlags.BlackAndWhite * 10 + EditFlags.Monocolor; } else if( !strcmp( tokenBuff, "rdonly" ) ) { constantVal = CFileReadOnly(); } else if( !strcmp( tokenBuff, "lastrc" ) ) { constantVal = (long)LastRC; } else if( !strcmp( tokenBuff, "pagelen" ) ) { constantVal = EditVars.WindMaxHeight; } else if( !strcmp( tokenBuff, "endcolumn" ) ) { constantVal = EditVars.WindMaxWidth; } else if( !strcmp( tokenBuff, "numundos" ) ) { if( UndoStack == NULL ) { constantVal = 0; } else { constantVal = UndoStack->current + 1; } } else if( !strcmp( tokenBuff, "numredos" ) ) { if( UndoUndoStack == NULL ) { constantVal = 0; } else { constantVal = UndoUndoStack->current + 1; } } else if( !strcmp( tokenBuff, "hassel" ) ) { constantVal = SelRgn.selected; } else if( !strcmp( tokenBuff, "hasfile" ) ) { constantVal = (CurrentFile != NULL); } else if( !strncmp( tokenBuff, "emptybuf", 8 ) ) { j = tokenBuff[8]; constantVal = IsEmptySavebuf( j ); } else if( (j = Tokenize( colorTokens, tokenBuff, true )) != TOK_INVALID ) { constantVal = j; #ifdef __WIN__ } else if( (j = Tokenize( ddeTokens, tokenBuff, true )) != TOK_INVALID ) { constantVal = ddeNums[j]; #endif } else { ReadErrorTokens(); j = Tokenize( ErrorTokens, tokenBuff, true ); if( j != TOK_INVALID ) { constantVal = ErrorValues[j]; } else { currToken = T_STRING; } } } } return( currToken ); } /* nextToken */
void Scene::_parsePointLight(uint id){ float vec[4]; _checkToken( "PointLight"); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken("position"); for( int i = 0; i < 3; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } Point3 position(vec); _nextToken( ); _checkToken("ambientColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor ambient(vec); _nextToken( ); _checkToken( "diffuseColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor diffuse(vec); _nextToken( ); _checkToken("specularColor"); for( int i = 0; i < 4; i++ ){ _nextToken( ); vec[i] = _parseFloat( ); } RGBAColor specular(vec); _nextToken( ); _checkToken("constant_attenuation"); _nextToken( ); float constant_attenuation = _parseFloat( ); _nextToken( ); _checkToken("linear_attenuation"); _nextToken( ); float linear_attenuation = _parseFloat( ); _nextToken( ); _checkToken("quadratic_attenuation"); _nextToken( ); float quadratic_attenuation = _parseFloat( ); _nextToken( ); _checkToken( "}"); // Fill me in! }
void Scene::_parseViewPlane( ){ size_t w, h, s; double p; _nextToken( ); _checkToken( "ViewPlane"); _nextToken( ); _checkToken( "{"); _nextToken( ); _checkToken( "width"); _nextToken( ); w = _parseInt( ); _nextToken( ); _checkToken( "height" ); _nextToken( ); h = _parseInt( ); _nextToken( ); _checkToken( "pixelsize" ); _nextToken( ); p = _parseFloat( ); _nextToken( ); _checkToken( "sampleCount" ); _nextToken( ); s = _parseInt( ); _nextToken( ); _checkToken( "}" ); // Fill me in! }