glUseProgram不会更改着色器

时间:2014-06-04 20:13:20

标签: opengl shader framebuffer

我目前正在开发一个带有(尚未实现)后期效果的应用程序。因此,3D场景在缓冲区中呈现,然后,另一个着色器在屏幕上呈现缓冲区。

我的问题是程序在第二遍中使用我的基本着色器与3d场景而不是后效果着色器。你有什么想法,可以做到这一点吗?

我的方法:我有一个有两个主要功能的课程:   - bindBuffer()将渲染Target设置为缓冲区   - renderSSAO()将缓冲区渲染到屏幕上并且有一天会添加环境遮挡效果。 :)

BIND BUFFER FUNCTION

void SSAOShader::bindBuffer() {
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glViewport(0,0,windowWidth,windowHeight);
}

RENDER FUNCTION

void SSAOShader::renderSSAO(GLuint currentShader) {
    // set shader
    glUseProgram(shader);

    //draw on Screen
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0,0,windowWidth,windowHeight);

    // clear screen
    glClearColor(0.4, 0.4, 0.4, 1.0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    // bind  texture, pass to shader
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderTexture);
    glUniform1i(textureID, 0);

    glBindVertexArray(vertexarray);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glBindVertexArray(0);

    glUseProgram(currentShader);
}

渲染循环(函数来自ssaoShader对象)

while (!glfwWindowShouldClose(mainWindow)) {
    ssaoShader->bindBuffer();
    // clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // draw cool stuff

    ssaoShader->renderSSAO(programID);

    // Swap buffers
    glfwSwapBuffers(mainWindow);
    glUseProgram(programID);    
}

基本上,渲染目标设置为我的缓冲区bindBuffer();,然后我用我的基本(phong等)着色器绘制对象,renderSSAO函数将目标更改为我的屏幕。此函数还将着色器更改为我的后效应着色器,并将其更改回之前的状态。 programID是着色器-GLuint。

我知道问题是使用过的着色器,因为所有的结果是我只有一个带有四角形的黑色屏幕。我也可以像往常一样四处走动。

CONSTRUCTOR

SSAOShader::SSAOShader(float windowWidth, float windowHeight, GLuint currentShader) {
    this->windowWidth = windowWidth;
    this->windowHeight = windowHeight;

    //shader
    shader = LoadShaders("Passthrough.vertexshader", "SSAO.fragmentshader");
    glUseProgram(shader);
    textureID = glGetUniformLocation(shader, "renderTexture");
    int status;
    glGetProgramiv(shader, GL_COMPILE_STATUS, &status);
    std::cout << (status == GL_TRUE)    << std::endl;

    GLfloat planeVertices[] = {
        -1, -1,
         1, -1,
        -1,  1,
         1,  1
    };
    // create vertex array
    glGenVertexArrays(1, &vertexarray);
    glBindVertexArray(vertexarray);

    // create "uv" (vertex) buffer
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), planeVertices, GL_STATIC_DRAW);

    // add vertecies to as attributes
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2,  GL_FLOAT, GL_FALSE, 0, (void*)0);
    glBindVertexArray(0);



    // ---- set up framebuffer ----
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    glGenTextures(1, &renderTexture);
    glBindTexture(GL_TEXTURE_2D, renderTexture);
    glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, windowWidth, windowHeight, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glGenRenderbuffers(1, &depthrenderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, windowWidth, windowHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);

    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTexture, 0);
    DrawBuffers[1] = GL_COLOR_ATTACHMENT0;
    glDrawBuffers(1, DrawBuffers);

    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE){
        exit(0); // bad luck
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    //glUseProgram(currentShader);
}

VS2012项目 https://studi.f4.htw-berlin.de/~s0539750/SSAOTest.zip

谁曾经疯狂到看我的代码。 ;)

1 个答案:

答案 0 :(得分:0)

我发现了问题。

glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderTexture, 0);
DrawBuffers[1] = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, DrawBuffers);

构造函数中的这一部分改变了我的GLuint shader;。我不知道原因究竟是什么,但原因是DrawBuffers是一个大小为1的固定数组。显然,访问DrawBuffers[1]并不是当天最好的主意。 ;)

我的猜测是DarBuffers[1]用于我的&#34;着色器&#34; GLuint或问题是glDrawBuffers永远不会真正地将GL_COLOR_ATTACHMENT0附加到我的帧缓冲区。怎么回事?

感谢顺便提供的所有帮助,它帮助了很多。 :)