Advertisement
Guest User

Untitled

a guest
Aug 15th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. bool bindWithParse( GLuint shader, ToolBox::Utilities::File file )
  2. {
  3.     /**
  4.         Parse the vertex shader file.
  5.         For every 'in' or 'attribute', try to assign a proper location for it.
  6.  
  7.         Contains Vertex, Vert, Location, Loc, Position, or Pos
  8.         Bind to GLSL_ATTR::POSITION
  9.  
  10.         Contains Color, Col
  11.         Bind to GLSL_ATTR::COLOR
  12.  
  13.         Contains Normal, Norm
  14.         Bind to GLSL_ATTR::NORMAL
  15.  
  16.         Contains Texture, Tex
  17.         Bind to GLSL_ATTR::TEXTURE_0 or GLSL_ATTR::TEXTURE_1 or GLSL_ATTR::TEXTURE_2
  18.  
  19.         Everything else get bound to 6 - ( GL_MAX_VERTEX_ATTRIBS - 1 ), based on availability.
  20.     **/
  21.  
  22.     GLuint loc;                                                     // Current location used later.
  23.  
  24.     GLint max_attribs;                                              // Maximum number of attribute locations.
  25.     glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max_attribs );
  26.  
  27.     std::fstream stream;                                            // The input stream
  28.     stream.open( file.m_FullPath.c_str( ), std::ios_base::in );
  29.  
  30.     std::string line;                                               // line is current line, line is attribute name in uppercase.
  31.  
  32.     bool* available = new bool[ max_attribs ];                      // Keeps track of locations are available to bind to
  33.     memset( available, true, max_attribs );                         // Set initially all to available
  34.  
  35.     size_t find;                                                    // Used for various std::string::find calls
  36.  
  37.     if( stream.is_open( ) )
  38.     {
  39.         while( getline( stream, line ) )
  40.         {
  41.  
  42.             // ...
  43.             // ...
  44.             // ...
  45.  
  46.             find = line.find( "NORM" );
  47.  
  48.             if( find != std::string::npos )
  49.             {
  50.                 // Assuming positional attribute
  51.                 if( available[ ToolBox::Math::GLSL_ATTR::NORMAL ] )
  52.                     loc = ToolBox::Math::GLSL_ATTR::NORMAL;
  53.                 else
  54.                 {
  55.                     // Get next available location index that isnt reserved
  56.                     for( loc = ToolBox::Math::GLSL_ATTR::END; loc < max_attribs; loc++ )
  57.                     {
  58.                         if( available[ loc ] )
  59.                             break;
  60.                     }
  61.  
  62.                     // See if we ran out of locations.
  63.                     // We will warn via an error of this situation.
  64.                     if( loc == max_attribs - 1 && available[ loc ] == false )
  65.                         ToolBox::Utilities::Logger::get( )->write( 'e', "Ran out of Vertex Attribute locations when binding attribute '%s' in shader '%s'! Loss of data will occur!",
  66.                                                                     line.c_str( ), file.m_Name.c_str( ) );
  67.                 }
  68.  
  69.                 // Set this location no longer available
  70.                 available[ loc ] = false;
  71.  
  72.                 // bind
  73.                 glBindAttribLocation( shader, loc, line.substr( line.find( ' ' ) + 1 ).c_str( ) );
  74.  
  75.                 continue;
  76.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement