bool GLSLProgram::compileAndLinkShaders(const char* vertexShaderFile, const char* fragmentShaderFile) { const char* vertexSrc = readShaderFromFile(vertexShaderFile); const char* fragSrc = readShaderFromFile(fragmentShaderFile); int isCompiledVS, isCompiledFS, maxLength, isLinked; GLint shader = glCreateProgram(); int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, (const GLchar**) &vertexSrc, 0); glCompileShader(vertexShader); glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiledVS); if (!isCompiledVS) { glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength); char* vertexInfoLog = new char[maxLength]; glGetShaderInfoLog(vertexShader, maxLength, &maxLength, vertexInfoLog); printf("vertex shader: compile error: "); printf("%s", vertexInfoLog); delete[] vertexInfoLog; return false; } // compile the fragment shader int fragShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragShader, 1, (const GLchar**) &fragSrc, 0); glCompileShader(fragShader); glGetShaderiv(fragShader, GL_COMPILE_STATUS, &isCompiledFS); if (!isCompiledFS) { glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &maxLength); char* fragInfoLog = new char[maxLength]; glGetShaderInfoLog(fragShader, maxLength, &maxLength, fragInfoLog); printf("fragment shader: compile error: "); printf("%s", fragInfoLog); delete[] fragInfoLog; return false; } // attach and link the shaders to the program glAttachShader(shader, vertexShader); glAttachShader(shader, fragShader); glLinkProgram(shader); glGetProgramiv(shader, GL_LINK_STATUS, (int*) &isLinked); if (!isLinked) { glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &maxLength); char* infoLog = new char[maxLength]; glGetProgramInfoLog(shader, maxLength, &maxLength, infoLog); fprintf(stdout, "shader: link error: "); printf("%s", infoLog); delete[] infoLog; return false; } mHandle = shader; return true; }
void Shader::loadShaders(std::string vertexFile, std::string fragmentFile) { // Create full relative pathes std::string vertexPath = SHADER_PATH + vertexFile; std::string fragmentPath = SHADER_PATH + fragmentFile; // If already initialized, delete old program if(programInitialized) { glDeleteShader(vertexHandle); glDeleteShader(fragmentHandle); glDeleteProgram(programHandle); } // Load data for vertex shader vertexHandle = glCreateShader(GL_VERTEX_SHADER); readShaderFromFile(vertexPath, vertexHandle); // Load data for fragment shader fragmentHandle = glCreateShader(GL_FRAGMENT_SHADER); readShaderFromFile(fragmentPath, fragmentHandle); // Bind shaders programHandle = glCreateProgram(); glAttachShader(programHandle, vertexHandle); glAttachShader(programHandle, fragmentHandle); glLinkProgram(programHandle); // Set program as initialized programInitialized = GL_TRUE; // Reset counter textureSlotCounter = 0; }
void initGL() { // The image is displayed in the window by using two simple primitives(triangles) // that we map to a texture. The texture will contain the colors we get from our raytracing. glewInit(); // Create an array for holding the vertex positions of our two triangles float positions[] = { 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f }; //Create a buffer for holding our triangle vertex positions GLuint positionBuffer; glGenBuffers(1, &positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); //Create a vertex array object glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(0); //TODO: Init vertex/fragment shaders GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); std::string vertShaderString = readShaderFromFile("simple.vert"); std::string fragShaderString = readShaderFromFile("simple.frag"); const char *vertShaderChars = vertShaderString.c_str(); const char *fragShaderChars = fragShaderString.c_str(); glShaderSource(vertexShader, 1, &vertShaderChars, NULL); glShaderSource(fragmentShader, 1, &fragShaderChars, NULL); compileShader(vertexShader); compileShader(fragmentShader); defShaderProgram = glCreateProgram(); glAttachShader(defShaderProgram, vertexShader); glAttachShader(defShaderProgram, fragmentShader); // Might be counterintuitive to delete the shaders here but they live on in our shaderprogram // so basically we just free up some unused memory. glDeleteShader(vertexShader); glDeleteShader(fragmentShader); glBindAttribLocation(defShaderProgram, 0, "position"); glLinkProgram(defShaderProgram); GLint programIsLinked; glGetProgramiv(defShaderProgram, GL_LINK_STATUS, &programIsLinked); if (!programIsLinked) { std::cout << "Error when linking default shader program\n"; } //TODO: Init texture to hold tracing results(image) //TODO: }