示例#1
0
	void Shader::handleShaderCompilerError( GLuint shaderID, const char* fileName, const std::string& shaderBuffer )
	{
		GLint length = 0;
		GLchar* log;
		glGetShaderiv( shaderID, GL_INFO_LOG_LENGTH, &length );
		log = new char[ length + 1 ];
		glGetShaderInfoLog( shaderID, length, &length, log );
		
		std::string sLog( log );
		std::vector< std::string > errorTokens;
		stringTokenizer( sLog, errorTokens, "\n" );

		std::string popupOutputMessage;
		for( size_t i = 0; i < errorTokens.size() && errorTokens[i].size() > 0; ++i )
		{

			size_t lineIndexStart = errorTokens[i].find( "(" );
			size_t lineIndexEnd = errorTokens[i].find( ")" );

			std::string lineNumber = 
				errorTokens[i].substr( lineIndexStart + 1, lineIndexEnd - lineIndexStart - 1 );

			std::string errorMessage( "Error on line: " + lineNumber );
		
			std::vector< std::string > lineTokens;
			stringTokenizer( shaderBuffer, lineTokens, "\n" );

			int lineNum = atoi( lineNumber.c_str() );
			errorMessage += "\n>";
			if( lineNum != 0 )
			{
				errorMessage += lineTokens[ lineNum - 1 ] + "\n";
			}
			else
			{
				errorMessage += lineTokens[ 0 ] + "\n";
			}

			errorMessage += errorTokens[i];
		
			std::string consoleOutput( "./" );
			consoleOutput += fileName;
			consoleOutput += "(" + lineNumber + "): " + errorTokens[i] + "\n";

			MonkyException::printToCompilerOutputConsole( consoleOutput.c_str() );
			popupOutputMessage += errorMessage + "\n\n";
		}
		MonkyException::fatalErrorMessageBox( "Shader Error", popupOutputMessage.c_str() );
	}
	//------------------------------------------------
	RandRangeFloat::RandRangeFloat( const std::string& str )
	{
		float min = 0;
		float max = 0;
			
		std::vector< std::string > tokens;
		stringTokenizer( str, tokens, "~" );
		if( tokens.size() == 1 )
		{
			float count = 0;
			stringToType( tokens[0], &count );
			min = count;
			max = count;
		}
		else if( tokens.size() == 2 )
		{
			if( !stringToType( tokens[0], &min ) )
			{
				min = 0;
			}
			if( !stringToType( tokens[1], &max ) )
			{
				max = 0;
			}
		}
		assertion( min <= max, "Min value: %f is not less than max value: %f", min, max );
		m_min = min;
		m_max = max;
	}
示例#3
0
//----------------------------------------------------------------------
void App::onFileDrop( const std::string& filePath )
{
    std::vector< std::string > tokens;
    stringTokenizer( filePath, tokens, "\\" );
    std::string parseFilePath;
    if( tokens.size() >= 2 );
    {
        parseFilePath = ".\\" + tokens[ tokens.size() - 2 ] + "\\" + tokens[ tokens.size() - 1 ];
    }

    if( parseFilePath != "" )
    {
        Material* mat = m_materialGenerator->GenerateMaterial( parseFilePath );
        if( mat != nullptr )
        {
            m_currentMatBeingTestedName = mat->getMaterialName();
            m_currentMatBeingTestedPath = parseFilePath;
            m_icosahedron->setMaterial( m_currentMatBeingTestedName );
            m_icosahedron->addLight( m_light, 0 );

            m_dudLight = new Light();
            for( int i = 1; i < MAX_NUM_LIGHTS; ++i )
            {
                m_icosahedron->addLight( m_dudLight, i );
            }
        }
    }
}
示例#4
0
	std::string P3DImporter::changeFileExt( const std::string& texFile, const std::string& newExt )
	{
		std::vector< std::string > tokens;
		stringTokenizer( texFile, tokens, "." );
		assertion( tokens.size() == 2, "Invalid texture file name: %s", texFile.c_str() );

		return tokens[0] + newExt;
	}
示例#5
0
	P3DImporter::P3DImporter( const std::string& file )
		:	m_version( 0 )
	{
		std::vector< std::string > tokens;
		stringTokenizer( file, tokens, "." );
		assertion( tokens[ tokens.size() - 1 ] == "p3d", "Invalid file format.\n%s", file.c_str() );
		
		m_fh = fopen( file.c_str(), "rb" );
		assertion( m_fh != nullptr, "File could not be opened.\n%s", file.c_str() );

		fread( &m_version, sizeof( int ), 1, m_fh );

		std::vector< std::string > pathTokens;
		stringTokenizer( file, pathTokens, "\\" );
		std::string fileName = pathTokens[ pathTokens.size() - 1 ];
		std::vector< std::string > fileNameAndPath;
		stringTokenizer( fileName, fileNameAndPath );
		m_fileName = fileNameAndPath[ fileNameAndPath.size() - 1 ];		
	}
示例#6
0
/*!
Tries to fill the image object with the data read from
the given input stream. Returns true on success.
*/
bool NRRDImageFileType::read(ImagePtr &image, std::istream &in, const std::string &mimetype)
{
    bool     retCode = false;
    bool     isSigned = false, littleEndian = true, rawEncoding = true;
    int      width = 1, height = 1, depth = 1, dimension = 3, channel = 1;
    HeadElem dataType = INVALID_HE;
    unsigned i, n, m;
    UChar8   *data = 0;
    std::string line;
    std::vector < std::string > tokenVec;
    bool inHead;
    osg::Image::PixelFormat pixelFormat;
    osg::Image::Type imageType;

    if (in.good()) {
      for (inHead = true; inHead;) {
        std::getline(in,line);
        tokenVec.clear();
        if ((n = stringTokenizer (line, tokenVec, ": \n"))) {
          switch (mapElem(tokenVec[0])){
          case TYPE_HE:
            switch (n) {
            case 2:
              dataType = mapElem(tokenVec[1]);
              break;
            case 3:
              isSigned = (mapElem(tokenVec[1]) == SIGNED_HE);
              dataType = mapElem(tokenVec[2]);
              break;
            default:
              FFATAL (( "Invalid %s/%d in NRRD header\n",
                        tokenVec[0].c_str(), n ));
              break;
            }
            break;
          case DIMENSION_HE:
            switch (n) {
            case 2:
              dimension = atoi(tokenVec[1].c_str());
              break;
            default:
              FFATAL (( "Invalid %s/%d in NRRD header\n",
                        tokenVec[0].c_str(), n ));
              break;
            }
            break;
          case SIZES_HE:
            switch (n) {
            case 5:
              channel = atoi(tokenVec[1].c_str());
              width   = atoi(tokenVec[2].c_str());
              height  = atoi(tokenVec[3].c_str());
              depth   = atoi(tokenVec[4].c_str());              
              break;              
            case 4:
              width  = atoi(tokenVec[1].c_str());
              height = atoi(tokenVec[2].c_str());
              depth  = atoi(tokenVec[3].c_str());              
              break;
            case 3:
              width  = atoi(tokenVec[1].c_str());
              height = atoi(tokenVec[2].c_str());
              depth  = 1;
              break;
            case 2:
              width  = atoi(tokenVec[1].c_str());
              height = 1;
              depth  = 1;
              break;
            default:
              FFATAL (( "Invalid %s/%d in NRRD header\n",
                        tokenVec[0].c_str(), n ));
              break;
            }
	    if ((n <= 4) && (width <= 4)) 
            {
               channel = width;
               width = height;
               height = depth;
               depth = 1;
            }
            break;
          case ENDIAN_HE:
            switch (n) {
            case 2:
              littleEndian = (mapElem(tokenVec[1]) == LITTLE_HE);
              break;
            default:
              FFATAL (( "Invalid %s/%d in NRRD header\n",
                        tokenVec[0].c_str(), n ));
              break;
            }
            break;
          case ENCODING_HE:
            switch (n) {
            case 2:
              rawEncoding = (mapElem(tokenVec[1]) == RAW_HE);
              break;
            default:
              FFATAL (( "Invalid %s/%d in NRRD header\n",
                        tokenVec[0].c_str(), n ));
              break;              
            }
            break;
          default:
            break;
          }
        }
        else 
          inHead = false;
      }
      
      switch (dataType) {
      case SHORT_HE:
        imageType = osg::Image::OSG_UINT16_IMAGEDATA;
        break;
      default:
        imageType = osg::Image::OSG_UINT8_IMAGEDATA;
        break;
      }

      switch (channel) {
      case 4:
        pixelFormat = osg::Image::OSG_RGBA_PF;
        break;
      case 3:
        pixelFormat = osg::Image::OSG_RGB_PF;
        break;
      case 2:
        pixelFormat = osg::Image::OSG_LA_PF;
        break;
      default:
        pixelFormat = osg::Image::OSG_L_PF;
        break;
      }

      if (isSigned) {
        FFATAL (( "Read signed data not supported\n" ));
      }

      FINFO(("NRRDImageFileType::read: got %d D %s endian %s data.\n",
                dimension, littleEndian?"little":"big",
                rawEncoding?"raw":"cooked"));
      
      image->set ( pixelFormat, width, height, depth,
                   1, 1, 0.0, 0, imageType );

      n    = image->getSize();
      data = image->editData();

      in.read (reinterpret_cast<char*>(data), n);

      retCode = true;
    }

    return retCode;
}
	//----------------------------------------------------------------------
	StatementData VariableNodeProcessor::ProcessAsRoot( XMLParser& parser, XMLNode* node )
	{
		std::string variableName;
		std::string variableValue;
		std::string variableType;

		if( parser.validateXMLAttributes( node, "name,type", "" ) )
		{		
			variableName = parser.getXMLAttributeAsString( node, "name", "" );
			variableType = parser.getXMLAttributeAsString( node, "type", "" );
			std::string variableData = parser.getXMLElementPCDataAsString( node );
			if( variableType.compare( "Texture2D" ) == 0 )
			{
				m_shaderGenerator->AddLogMessage( "Cannot declare textures as variables", color::RED );
				m_shaderGenerator->EnableCompilerErrorFlag();
			}
			else
			{

				XMLNode* child = node->FirstChildElement();
				if( child != nullptr )
				{
					for( ; child != nullptr; child = child->NextSiblingElement() )
					{
						std::string name = child->Name();
						if( name.compare( "Input" ) == 0 )
						{
							StatementData statementData;
							statementData = ProcessInputNode( parser, child );
							if( statementData.outputType != VT_COUNT )
							{
								if( statementData.outputType == VT_TEXTURE_SAMPLE_2D )
								{
									m_shaderGenerator->AddLogMessage( "Cannot declare a variable of type Texture2D", color::RED );
								}
								else
								{
									variableValue = statementData.statement;
								}
							}
							else
							{
								variableValue = "0";
								variableType = "Real";
							}						
						}
						else
						{
							StatementNodeProcessor* processor = m_shaderGenerator->GetStatementNodeProcessor( name );
							if( processor != nullptr )
							{
								variableValue = processor->ProcessAsChild( parser, child ).statement;
							}
							else
							{
								//Invalid node processor
								m_shaderGenerator->AddLogMessage( "Invalid child node: %s for variable: %s", color::RED, name.c_str(), variableName.c_str() );
								m_shaderGenerator->EnableCompilerErrorFlag();
							}
						}
					}
				}
				else
				{
					std::vector< std::string > variableDataList;
					stringTokenizer( variableData, variableDataList, "," );
					if( ValidListOfVariables( variableDataList ) )
						variableValue = GetVariableConstructionFromType( GetVariableTypeFromString( variableType ), StripDollarSignsFromCommaSeperatedVariables( variableData ) );
					else
					{
						for( unsigned int i = 0; i < variableDataList.size(); ++i )
						{
							if( variableDataList[i][0] == '$' )
								variableDataList[i] = variableDataList[i].substr( 1, variableDataList[i].npos );
							else if( !ValidRealNumber( variableDataList[i] ) )
							{
								m_shaderGenerator->AddLogMessage( "Syntax error in variable declaration in variable name usage: %s. Missing \'$\'?", color::RED, variableData.c_str() );
								m_shaderGenerator->EnableCompilerErrorFlag();
							}
						}
						if( !m_shaderGenerator->WasCompilerError() && ValidListOfVariables( variableDataList ) )
						{
							variableValue = StripDollarSignsFromListOfVariables( variableDataList );
						}
						else
						{
							m_shaderGenerator->AddLogMessage( "Invalid data entered for variable declaration: Variable name: %s Data: %s", color::RED, variableName.c_str(), variableData.c_str() );
							m_shaderGenerator->EnableCompilerErrorFlag();
						}
					}
				}
			}
		}

		if( variableName.size() > 0 )
			m_shaderGenerator->AddVariable( variableType, variableName, variableValue, ( m_name.compare( "Constant" ) == 0 ) );
		return StatementData( "", VT_COUNT );
	}