bool bindWithParse( GLuint shader, ToolBox::Utilities::File file )
{
/**
Parse the vertex shader file.
For every 'in' or 'attribute', try to assign a proper location for it.
Contains Vertex, Vert, Location, Loc, Position, or Pos
Bind to GLSL_ATTR::POSITION
Contains Color, Col
Bind to GLSL_ATTR::COLOR
Contains Normal, Norm
Bind to GLSL_ATTR::NORMAL
Contains Texture, Tex
Bind to GLSL_ATTR::TEXTURE_0 or GLSL_ATTR::TEXTURE_1 or GLSL_ATTR::TEXTURE_2
Everything else get bound to 6 - ( GL_MAX_VERTEX_ATTRIBS - 1 ), based on availability.
**/
GLuint loc; // Current location used later.
GLint max_attribs; // Maximum number of attribute locations.
glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max_attribs );
std::fstream stream; // The input stream
stream.open( file.m_FullPath.c_str( ), std::ios_base::in );
std::string line; // line is current line, line is attribute name in uppercase.
bool* available = new bool[ max_attribs ]; // Keeps track of locations are available to bind to
memset( available, true, max_attribs ); // Set initially all to available
size_t find; // Used for various std::string::find calls
if( stream.is_open( ) )
{
while( getline( stream, line ) )
{
// ...
// ...
// ...
find = line.find( "NORM" );
if( find != std::string::npos )
{
// Assuming positional attribute
if( available[ ToolBox::Math::GLSL_ATTR::NORMAL ] )
loc = ToolBox::Math::GLSL_ATTR::NORMAL;
else
{
// Get next available location index that isnt reserved
for( loc = ToolBox::Math::GLSL_ATTR::END; loc < max_attribs; loc++ )
{
if( available[ loc ] )
break;
}
// See if we ran out of locations.
// We will warn via an error of this situation.
if( loc == max_attribs - 1 && available[ loc ] == false )
ToolBox::Utilities::Logger::get( )->write( 'e', "Ran out of Vertex Attribute locations when binding attribute '%s' in shader '%s'! Loss of data will occur!",
line.c_str( ), file.m_Name.c_str( ) );
}
// Set this location no longer available
available[ loc ] = false;
// bind
glBindAttribLocation( shader, loc, line.substr( line.find( ' ' ) + 1 ).c_str( ) );
continue;
}