void ae3d::Shader::Load( const char* vertexSource, const char* fragmentSource ) { GLuint vertexShader = CompileShader( vertexSource, GL_VERTEX_SHADER ); GLuint fragmentShader = CompileShader( fragmentSource, GL_FRAGMENT_SHADER ); GLuint program = GfxDevice::CreateProgramId(); if (GfxDevice::HasExtension( "KHR_debug" )) { glObjectLabel( GL_PROGRAM, id, (GLsizei)std::string( "shader" ).size(), "shader" ); } glAttachShader( program, vertexShader ); glAttachShader( program, fragmentShader ); glLinkProgram( program ); GLint wasLinked; glGetProgramiv( program, GL_LINK_STATUS, &wasLinked ); if (!wasLinked) { ae3d::System::Print("Shader linking failed.\n"); PrintInfoLog( program, InfoLogType::Program ); return; } id = program; uniformLocations = GetUniformLocations( program ); }
bool ShaderProgram::Link() { glLinkProgram( id ); GLint success; glGetProgramiv( id, GL_LINK_STATUS, &success ); bool isValid = ( success == GL_TRUE ); if( !isValid ) { PrintInfoLog(); return false; } // Get active attributes for attribute map creation glGetProgramiv( id, GL_ACTIVE_ATTRIBUTES, &numAttributes ); attrArray = new ShaderAttribute * [ numAttributes ]; // Get active uniforms for parameter map creation glGetProgramiv( id, GL_ACTIVE_UNIFORMS, &numParams ); uniformsArray = new Uniform * [ numParams ]; RetrieveAttributes(); RetrieveParameters(); // Bind attributes for( int i=0; i<numAttributes; i++ ) glBindAttribLocation( id, attrArray[i]->location, attrArray[i]->name.c_str() ); // Detach after linking for( int i=0; i<shadersArray.size(); i++ ) { DetachShader( shadersArray[i] ); } return true; }