Shader::Shader( const std::string &vertFilename, const std::string &fragFilename ) { AddVertexShader( vertFilename ); AddFragmentShader( fragFilename ); LinkProgram(); }
BasicShader::BasicShader() : Shader() { AddVertexShader(ResourceLoader::LoadShader("resources/shaders/basicVertex.vs")); AddFragmentShader(ResourceLoader::LoadShader("resources/shaders/basicFragment.fs")); CompileShader(); AddUniform("transform"); AddUniform("color"); }
// ShaderData ShaderData::ShaderData(const string& _fileName) { fileName = _fileName; program = glCreateProgram(); if (program == 0) { Debug::LogError("Shader program error. File '" + fileName + " Error: Cannot create the shader program"); return; } if (s_supportedOpenGLLevel == 0) { int majorVersion; int minorVersion; glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); glGetIntegerv(GL_MINOR_VERSION, &minorVersion); s_supportedOpenGLLevel = majorVersion * 100 + minorVersion * 10; if (s_supportedOpenGLLevel >= 330) { std::ostringstream convert; convert << s_supportedOpenGLLevel; s_glslVersion = convert.str(); } else if (s_supportedOpenGLLevel >= 320) { s_glslVersion = "150"; } else if (s_supportedOpenGLLevel >= 310) { s_glslVersion = "140"; } else if (s_supportedOpenGLLevel >= 300) { s_glslVersion = "130"; } else if (s_supportedOpenGLLevel >= 210) { s_glslVersion = "120"; } else if (s_supportedOpenGLLevel >= 200) { s_glslVersion = "110"; } else { fprintf(stderr, "Error: OpenGL Version %d.%d does not support shaders.\n", majorVersion, minorVersion); exit(1); } } string shaderText = LoadShader(fileName + ".glsl"); string vertexShaderText = "#version " + s_glslVersion + "\n#define VS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText; string fragmentShaderText = "#version " + s_glslVersion + "\n#define FS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText; AddVertexShader(vertexShaderText); AddFragmentShader(fragmentShaderText); string attributeKeyword = "uniform"; AddAllAttributes(vertexShaderText, attributeKeyword); CompileShader(); AddShaderUniforms(shaderText); }
//-------------------------------------------------------------------------------- // Constructors/Destructors //-------------------------------------------------------------------------------- ShaderData::ShaderData(const std::string& fileName) { std::string actualFileName = fileName; #if PROFILING_DISABLE_SHADING != 0 actualFileName = "nullShader"; #endif m_program = glCreateProgram(); if (m_program == 0) { fprintf(stderr, "Error creating shader program\n"); exit(1); } if(s_supportedOpenGLLevel == 0) { int majorVersion; int minorVersion; glGetIntegerv(GL_MAJOR_VERSION, &majorVersion); glGetIntegerv(GL_MINOR_VERSION, &minorVersion); s_supportedOpenGLLevel = majorVersion * 100 + minorVersion * 10; if(s_supportedOpenGLLevel >= 330) { std::ostringstream convert; convert << s_supportedOpenGLLevel; s_glslVersion = convert.str(); } else if(s_supportedOpenGLLevel >= 320) { s_glslVersion = "150"; } else if(s_supportedOpenGLLevel >= 310) { s_glslVersion = "140"; } else if(s_supportedOpenGLLevel >= 300) { s_glslVersion = "130"; } else if(s_supportedOpenGLLevel >= 210) { s_glslVersion = "120"; } else if(s_supportedOpenGLLevel >= 200) { s_glslVersion = "110"; } else { fprintf(stderr, "Error: OpenGL Version %d.%d does not support shaders.\n", majorVersion, minorVersion); exit(1); } } std::string shaderText = LoadShader(actualFileName + ".glsl"); std::string vertexShaderText = "#version " + s_glslVersion + "\n#define VS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText; std::string fragmentShaderText = "#version " + s_glslVersion + "\n#define FS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText; AddVertexShader(vertexShaderText); AddFragmentShader(fragmentShaderText); std::string attributeKeyword = "attribute"; AddAllAttributes(vertexShaderText, attributeKeyword); CompileShader(); AddShaderUniforms(shaderText); }
void Shader::AddVertexShaderFromFile(const std::string& text) { AddVertexShader(LoadShader(text)); }