#include #include #include #include #include #include"shader.h" #include #include using namespace std; using namespace glm; namespace vb01{ Shader::Shader(string vertShaderPath, string fragShaderPath, string geoShaderPath){ geometry=(geoShaderPath!=""); ifstream vertShaderFile,geoShaderFile,fragShaderFile; vertShaderFile.open(vertShaderPath); geoShaderFile.open(geoShaderPath); fragShaderFile.open(fragShaderPath); stringstream vertShaderStream,geoShaderStream,fragShaderStream; vertShaderStream<length(); i++) if(shaderString[0][i] == '\n'){ if(numPassedLines==line-1) lineStart=i+1; if(numPassedLines==line){ lineEnd=i; break; } numPassedLines++; } *shaderString=shaderString->substr(0,lineStart)+insertion+shaderString->substr(lineEnd,string::npos); loadShaders(); } void Shader::loadShaders(){ const char *vertShaderSource=vString.c_str(); const char *geoShaderSource=gString.c_str(); const char *fragShaderSource=fString.c_str(); unsigned int vert,geo,frag; vert=glCreateShader(GL_VERTEX_SHADER); glShaderSource(vert,1,&vertShaderSource,NULL); glCompileShader(vert); checkCompileErrors(vert,VERTEX_ERROR); if(geometry){ geo=glCreateShader(GL_GEOMETRY_SHADER); glShaderSource(geo,1,&geoShaderSource,NULL); glCompileShader(geo); checkCompileErrors(geo,GEOMETRY_ERROR); } frag=glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(frag,1,&fragShaderSource,NULL); glCompileShader(frag); checkCompileErrors(frag,FRAGMENT_ERROR); id=glCreateProgram(); glAttachShader(id,vert); if(geometry) glAttachShader(id,geo); glAttachShader(id,frag); glLinkProgram(id); checkCompileErrors(id,PROGRAM_ERROR); glDeleteShader(vert); if(geometry) glDeleteShader(geo); glDeleteShader(frag); } void Shader::checkCompileErrors(unsigned int shader, ErrorType type){ int success; char infoLog[1024]; switch(type){ case PROGRAM_ERROR: glGetProgramiv(shader,GL_LINK_STATUS,&success); if(!success){ glGetProgramInfoLog(shader,1024,NULL,infoLog); cout<<"ERROR::PROGRAM_LINK_ERROR: "<