void GameObject::loadShader(const string& vsFilename, const string& fsFilename) { GLuint vertexShaderProgram = 0; vertexShaderProgram = loadShaderFromFile(vsFilename, VERTEX_SHADER); checkForCompilerErrors(vertexShaderProgram); GLuint fragmentShaderProgram = 0; fragmentShaderProgram = loadShaderFromFile(fsFilename, FRAGMENT_SHADER); checkForCompilerErrors(fragmentShaderProgram); m_ShaderProgram = glCreateProgram(); glAttachShader(m_ShaderProgram, vertexShaderProgram); glAttachShader(m_ShaderProgram, fragmentShaderProgram); //Link attributes glBindAttribLocation(m_ShaderProgram, 0, "vertexPosition"); glBindAttribLocation(m_ShaderProgram, 1, "vertexColour"); glBindAttribLocation(m_ShaderProgram, 2, "vertexTexCoords"); glBindAttribLocation(m_ShaderProgram, 3, "vertexNormal"); glLinkProgram(m_ShaderProgram); checkForLinkErrors(m_ShaderProgram); //now we can delete the VS & FS Programs glDeleteShader(vertexShaderProgram); glDeleteShader(fragmentShaderProgram); }
bool Shader::link() { if (m_VertexShader == 0 || m_FragmentShader == 0) { cout << "Shaders not set" << endl; return false; } m_ShaderProgram = glCreateProgram(); glAttachShader(m_ShaderProgram, m_VertexShader); glAttachShader(m_ShaderProgram, m_FragmentShader); glLinkProgram(m_ShaderProgram); if (checkForLinkErrors(m_ShaderProgram)) { glDeleteShader(m_VertexShader); glDeleteShader(m_FragmentShader); return false; } glBindAttribLocation(m_ShaderProgram, 0, "vertexPosition"); glBindAttribLocation(m_ShaderProgram, 1, "vertexColour"); glBindAttribLocation(m_ShaderProgram, 2, "vertexTexCoords"); glDeleteShader(m_VertexShader); glDeleteShader(m_FragmentShader); return true; }
void initScene() { string modelPath = ASSET_PATH + MODEL_PATH + "/armoredrecon.fbx"; loadFBXFromFile(modelPath, ¤tMesh); //Generate Vertex Array glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, currentMesh.getNumVerts()*sizeof(Vertex), ¤tMesh.vertices[0], GL_STATIC_DRAW); //create buffer glGenBuffers(1, &EBO); //Make the EBO active glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); //Copy Index data to the EBO glBufferData(GL_ELEMENT_ARRAY_BUFFER, currentMesh.getNumIndices()*sizeof(int), ¤tMesh.indices[0], GL_STATIC_DRAW); //Tell the shader that 0 is the position element glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void**)(sizeof(vec3))); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void**)(sizeof(vec3) + sizeof(vec4))); GLuint vertexShaderProgram = 0; string vsPath = ASSET_PATH + SHADER_PATH + "/simpleVS.glsl"; vertexShaderProgram = loadShaderFromFile(vsPath, VERTEX_SHADER); checkForCompilerErrors(vertexShaderProgram); GLuint fragmentShaderProgram = 0; string fsPath = ASSET_PATH + SHADER_PATH + "/simpleFS.glsl"; fragmentShaderProgram = loadShaderFromFile(fsPath, FRAGMENT_SHADER); checkForCompilerErrors(fragmentShaderProgram); shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShaderProgram); glAttachShader(shaderProgram, fragmentShaderProgram); //Link attributes glBindAttribLocation(shaderProgram, 0, "vertexPosition"); glBindAttribLocation(shaderProgram, 1, "vertexColour"); glBindAttribLocation(shaderProgram, 2, "vertexTexCoords"); glLinkProgram(shaderProgram); checkForLinkErrors(shaderProgram); //now we can delete the VS & FS Programs glDeleteShader(vertexShaderProgram); glDeleteShader(fragmentShaderProgram); }
//creates shaders used in Post Processing - RT void PostProcessing::createShader(string& vertexShaderFilename, string& fragmentShaderFilename) { GLuint vertexShaderProgram = 0; vertexShaderProgram = loadShaderFromFile(vertexShaderFilename, VERTEX_SHADER); GLuint fragmentShaderProgram = 0; fragmentShaderProgram = loadShaderFromFile(fragmentShaderFilename, FRAGMENT_SHADER); postProcessingProgram = glCreateProgram(); glAttachShader(postProcessingProgram, vertexShaderProgram); glAttachShader(postProcessingProgram, fragmentShaderProgram); glLinkProgram(postProcessingProgram); checkForLinkErrors(postProcessingProgram); glDeleteShader(vertexShaderProgram); glDeleteShader(fragmentShaderProgram); glBindAttribLocation(postProcessingProgram, 0, "vertexPosition"); }
void createFramebuffer() { glActiveTexture(GL_TEXTURE0); glGenTextures(1, &FBOTexture); glBindTexture(GL_TEXTURE_2D, FBOTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FRAME_BUFFER_WIDTH, FRAME_BUFFER_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glGenRenderbuffers(1, &FBODepthBuffer); glBindRenderbuffer(GL_RENDERBUFFER, FBODepthBuffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, FRAME_BUFFER_WIDTH, FRAME_BUFFER_HEIGHT); glBindRenderbuffer(GL_RENDERBUFFER, 0); glGenFramebuffers(1, &frameBufferObject); glBindFramebuffer(GL_FRAMEBUFFER, frameBufferObject); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, FBOTexture, 0); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER, FBODepthBuffer); GLenum status; if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) { cout << "Issue with Framebuffers" << endl; } float vertices[] = { -1, -1, 1, -1, -1, 1, 1, 1, }; glGenVertexArrays(1, &fullScreenVAO); glBindVertexArray(fullScreenVAO); glGenBuffers(1, &fullScreenVBO); glBindBuffer(GL_ARRAY_BUFFER, fullScreenVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer( 0, // attribute 2, // number of elements per vertex, here (x,y) GL_FLOAT, // the type of each element GL_FALSE, // take our values as-is 0, // no extra data between each position 0 // offset of first element ); GLuint vertexShaderProgram = 0; string vsPath = ASSET_PATH + SHADER_PATH + "/simplePostProcessVS.glsl"; vertexShaderProgram = loadShaderFromFile(vsPath, VERTEX_SHADER); checkForCompilerErrors(vertexShaderProgram); GLuint fragmentShaderProgram = 0; string fsPath = ASSET_PATH + SHADER_PATH + "/simplePostProcessFS.glsl"; fragmentShaderProgram = loadShaderFromFile(fsPath, FRAGMENT_SHADER); checkForCompilerErrors(fragmentShaderProgram); fullScreenShaderProgram = glCreateProgram(); glAttachShader(fullScreenShaderProgram, vertexShaderProgram); glAttachShader(fullScreenShaderProgram, fragmentShaderProgram); //Link attributes glBindAttribLocation(fullScreenShaderProgram, 0, "vertexPosition"); glLinkProgram(fullScreenShaderProgram); checkForLinkErrors(fullScreenShaderProgram); //now we can delete the VS & FS Programs glDeleteShader(vertexShaderProgram); glDeleteShader(fragmentShaderProgram); }