bool Shader::CompileSourceFile(const string& aFileName) { //Delete previous shader if the class already contained one. if(mShaderId != 0) { glDeleteShader(mShaderId); } //Open and read the shader file. fstream ShaderFile; ShaderFile.open(aFileName.c_str()); string ShaderCode( (std::istreambuf_iterator<char>(ShaderFile)), std::istreambuf_iterator<char>() ); ShaderFile.close(); //Create the shader object if(mType == Shader_Vertex) { mShaderId = glCreateShader(GL_VERTEX_SHADER); } else if(mType == Shader_Fragment) { mShaderId = glCreateShader(GL_FRAGMENT_SHADER); } //Associate the shader's source code to the shader object. const char* ShaderChars = ShaderCode.c_str(); glShaderSource(mShaderId,1,&ShaderChars,NULL); //Compile the shader glCompileShader(mShaderId); mShaderId = mShaderId; return IsCompiled(); }
Shader::Shader(string& vertexPath, string& fragPath) : Code(), program(new GLuint( -1), deleteGLProgram()) { Code.push_back(ShaderCode( this, VERTEX, vertexPath)); Code.push_back(ShaderCode( this, FRAGMENT, fragPath)); }