void Shader::compileShader() { glLinkProgram(m_program); checkShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program"); // glGetProgramiv(program, GL_LINK_STATUS, &success); //if (success == 0) //{ // glGetProgramInfoLog(program, sizeof(error), NULL, ErrorLog); // fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog); // exit(1); //} glValidateProgram(m_program); checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program"); //glGetProgramiv(program, GL_VALIDATE_STATUS, &Success); //if (!success) { // glGetProgramInfoLog(ShaderProgram, sizeof(error), NULL, ErrorLog); // fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog); // exit(1); //} //glUseProgram(program); //gScaleLocation = glGetUniformLocation(ShaderProgram, "gScale"); //assert(gScaleLocation != 0xFFFFFFFF); }
void init() { if (!initialized) { GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); // vertex shader glShaderSource(vertexShaderId, 1, &vertexShader, NULL); glCompileShader(vertexShaderId); checkShaderError(vertexShaderId, "Vertex shader compilation failed."); // fragment shader glShaderSource(fragmentShaderId, 1, &fragmentShader, NULL); glCompileShader(fragmentShaderId); checkShaderError(fragmentShaderId, "Fragment shader compilation failed."); // linking program programId = glCreateProgram(); glAttachShader(programId, vertexShaderId); glAttachShader(programId, fragmentShaderId); glLinkProgram(programId); checkProgramError(programId, "Shader program link failed."); // once linked into a program, we no longer need the shaders. glDeleteShader(vertexShaderId); glDeleteShader(fragmentShaderId); projectionUniformId = glGetUniformLocation(programId, "projection"); modelViewUniformId = glGetUniformLocation(programId, "modelView"); initialized = true; } }
Shader::Shader(const std::string& fileName) { m_program = glCreateProgram(); m_shaders[0] = createShader(loadShader("./res/shaders/" + fileName + ".vs"), GL_VERTEX_SHADER); //Vertex shader m_shaders[1] = createShader(loadShader("./res/shaders/" + fileName + ".fs"), GL_FRAGMENT_SHADER); //Fragment shader for (unsigned int i = 0; i < NUM_SHADERS; i++) glAttachShader(m_program, m_shaders[i]); glBindAttribLocation(m_program, 0, "position"); glBindAttribLocation(m_program, 1, "texCoords"); glBindAttribLocation(m_program, 2, "normal"); glBindAttribLocation(m_program, 3, "tangent"); glLinkProgram(m_program); checkShaderError(m_program, GL_LINK_STATUS, true, "Shader program link failed: "); glValidateProgram(m_program); checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Shader program validation failed: "); m_uniforms[MODEL_U] = glGetUniformLocation(m_program, "model"); m_uniforms[VIEW_U] = glGetUniformLocation(m_program, "view"); m_uniforms[PROJECTION_U] = glGetUniformLocation(m_program, "projection"); m_uniforms[VIEWPOS_U] = glGetUniformLocation(m_program, "viewPos"); m_uniforms[LIGHTPOS_U] = glGetUniformLocation(m_program, "lightPos"); m_uniforms[MAT_DIFFUSE_U] = glGetUniformLocation(m_program, "diffuseMap"); //m_uniforms[MAT_SPECULAR_U] = glGetUniformLocation(m_program, "material.specular"); m_uniforms[MAT_EMISSION_U] = glGetUniformLocation(m_program, "emissionMap"); m_uniforms[MAT_NORMAL_U] = glGetUniformLocation(m_program, "normalMap"); /*m_uniforms[MAT_SHININESS_U] = glGetUniformLocation(m_program, "material.shininess"); m_uniforms[DLIGHT_DIRECTION_U] = glGetUniformLocation(m_program, "dirLight.direction"); m_uniforms[DLIGHT_AMBIENT_U] = glGetUniformLocation(m_program, "dirLight.ambient"); m_uniforms[DLIGHT_DIFFUSE_U] = glGetUniformLocation(m_program, "dirLight.diffuse"); m_uniforms[DLIGHT_SPECULAR_U] = glGetUniformLocation(m_program, "dirLight.specular"); m_uniforms[PLIGHT_POSITION_U] = glGetUniformLocation(m_program, "pointLight.position"); m_uniforms[PLIGHT_AMBIENT_U] = glGetUniformLocation(m_program, "pointLight.ambient"); m_uniforms[PLIGHT_DIFFUSE_U] = glGetUniformLocation(m_program, "pointLight.diffuse"); m_uniforms[PLIGHT_SPECULAR_U] = glGetUniformLocation(m_program, "pointLight.specular"); m_uniforms[PLIGHT_CONSTANT_U] = glGetUniformLocation(m_program, "pointLight.constant"); m_uniforms[PLIGHT_LINEAR_U] = glGetUniformLocation(m_program, "pointLight.position"); m_uniforms[PLIGHT_QUADRATIC_U] = glGetUniformLocation(m_program, "pointLight.quadratic");*/ }
TTShader::TTShader(const std::string &fileName) { initializeOpenGLFunctions(); m_program = glCreateProgram(); m_shaders[0] = createShader(loadShader(fileName + ".vert"), GL_VERTEX_SHADER); m_shaders[1] = createShader(loadShader(fileName + ".fsh"), GL_FRAGMENT_SHADER); for (unsigned int i = 0; i < NUM_SHADERS; i++) { glAttachShader(m_program, m_shaders[i]); } glLinkProgram(m_program); checkShaderError(m_program, GL_LINK_STATUS, true, "Linking failed:"); glValidateProgram(m_program); checkShaderError(m_program, GL_VALIDATE_STATUS, true, "Program is invalid:"); m_uniforms[TT_TRANSFORM_U] = glGetUniformLocation(m_program, "transformation"); }
Shader::Shader(const std::string& fileName) { program = glCreateProgram(); shaders[0] = createShader(loadShader(fileName + ".vs"), GL_VERTEX_SHADER); shaders[1] = createShader(loadShader(fileName + ".fs"), GL_FRAGMENT_SHADER); for(unsigned int i = 0; i < NUM_SHADERS; i++) glAttachShader(program, shaders[i]); glBindAttribLocation(program, 0, "position"); glBindAttribLocation(program, 1, "texCoord"); glBindAttribLocation(program, 2, "normal"); glLinkProgram(program); checkShaderError(program, GL_LINK_STATUS, true, "Program linking failed: "); glValidateProgram(program); checkShaderError(program, GL_VALIDATE_STATUS, true, "Program is invalid: "); uniforms[0] = glGetUniformLocation(program, "MVP"); uniforms[1] = glGetUniformLocation(program, "Normal"); uniforms[2] = glGetUniformLocation(program, "lightDirection"); }
Shader::Shader(const std::string& filename) { program = glCreateProgram(); shaders[vertexShader] = createShader(loadShader(filename + ".vert"), GL_VERTEX_SHADER); shaders[fragmentShader] = createShader(loadShader(filename + ".frag"), GL_FRAGMENT_SHADER); for (unsigned int i = 0; i < NUM_SHADERS; i++) { glAttachShader(program, shaders[i]); } glBindAttribLocation(program, 0, "position"); glBindAttribLocation(program, 1, "texCoord"); glBindAttribLocation(program, 2, "normal"); glLinkProgram(program); checkShaderError(program, GL_LINK_STATUS, true, "Error: Program linking failed: "); glValidateProgram(program); checkShaderError(program, GL_VALIDATE_STATUS, true, "Error: Program is invalid: "); uniforms[TRANFORM_U] = glGetUniformLocation(program, "transform"); }
static GLuint createShader(const std::string& code, GLenum type) { GLuint shader = glCreateShader(type); if (shader == 0) std::cerr << "Shader creation failed." << std::endl; const GLchar* shaderSource[1]; GLint shaderSourceLength[1]; shaderSource[0] = code.c_str(); shaderSourceLength[0] = code.length(); glShaderSource(shader, 1, shaderSource, shaderSourceLength); glCompileShader(shader); checkShaderError(shader, GL_COMPILE_STATUS, false, "Shader compilation failed: "); return shader; }
GLuint TTShader::createShader(const std::string &text, GLenum type) { GLuint shader = glCreateShader(type); if (shader == 0) qDebug() << "Shader creation failed!"; const GLchar* shaderSourceStrings[1]; shaderSourceStrings[0] = text.c_str(); GLint shaderSourceStringLengths[1]; shaderSourceStringLengths[0] = text.length(); glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths); glCompileShader(shader); checkShaderError(shader, GL_COMPILE_STATUS, false, "Shader did not compile:"); return shader; }
GLuint Shader::createShader(const std::string& text, GLenum shaderType) { GLuint shader = glCreateShader(shaderType); if(shader == 0) std::cerr << "Shader creation failed: " << std::endl; const GLchar* shaderSourceStrings[1]; GLint shaderSourceStringLengths[1]; shaderSourceStrings[0] = text.c_str(); shaderSourceStringLengths[0] = text.length(); glShaderSource(shader, 1 ,shaderSourceStrings, shaderSourceStringLengths); glCompileShader(shader); checkShaderError(shader, GL_COMPILE_STATUS, false, "Shader compilation failed: "); return shader; }
const GLuint render::ShaderFactory::getShader(const std::string shaderName, GLenum shaderType) { //Check for shader in flyweight pool if(shaders.find(shaderName) != shaders.end()) { return shaders[shaderName]; } //Shader not in flyweight pool: load and compile shader std::ifstream shaderSourceFile(std::string("./shaders/" + shaderName + ".glsl").c_str()); if(!shaderSourceFile.is_open()) { //TODO To logging std::cerr << "Could not find shader " << shaderName << std::endl; assert(shaderSourceFile.is_open()); } std::stringstream shaderSourceStream; while(shaderSourceFile.good()) { std::string line; std::getline(shaderSourceFile, line); shaderSourceStream << line << std::endl; } //We need to copy the C-string because of the volatile characteristics of std::string.c_str() char* shaderSourceCString = new char[shaderSourceStream.str().size()+1]; std::strcpy(shaderSourceCString, shaderSourceStream.str().c_str()); GLuint shader = glCreateShader(shaderType); glShaderSource(shader, 1, (const GLchar**) &shaderSourceCString, 0); glCompileShader(shader); checkShaderError(shader); delete[] shaderSourceCString; shaders.insert(std::pair<std::string, GLuint>(shaderName, shader)); return shader; }
static GLuint createShader(const std::string& code, GLenum shaderType) { GLuint shader = glCreateShader(shaderType); if (shader == 0) { std::cerr << "Error: Shader creation failed!" << std::endl; } const GLchar* shaderSourceStrings[1]; GLint shaderSourceStringLengths[1]; shaderSourceStrings[0] = code.c_str(); shaderSourceStringLengths[0] = code.length(); glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths); glCompileShader(shader); checkShaderError(shader, GL_COMPILE_STATUS, false, "Error: Shader compilation failed: "); return shader; }
void Shader::createShader(const std::string& shaderFile) { _vertexShaderID = glCreateShader(GL_VERTEX_SHADER); _fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); std::string vertexShaderCode; std::ifstream vertexShaderStream(shaderFile+".vert", std::ios::in); if (vertexShaderStream.is_open()) { std::string line = ""; while (std::getline(vertexShaderStream, line)) vertexShaderCode += "\n" + line; vertexShaderStream.close(); } std::string fragmentShaderCode; std::ifstream fragmentShaderStream(shaderFile + ".frag", std::ios::in); if (fragmentShaderStream.is_open()) { std::string line = ""; while (std::getline(fragmentShaderStream, line)) fragmentShaderCode += "\n" + line; fragmentShaderStream.close(); } compileShader(vertexShaderCode, fragmentShaderCode); checkShaderError(); linkShader(); glDeleteShader(_vertexShaderID); glDeleteShader(_fragmentShaderID); }