在OpenGL上为迭代DE解算器编写和读取相同的纹理

时间:2017-04-22 11:38:34

标签: c++ opengl

我正在尝试编写一个流体模拟器,需要迭代求解一些微分方程(Lattice-Boltzmann方法)。我希望它是使用OpenGL的实时图形可视化。我遇到了一个问题。我使用着色器在GPU上执行相关计算。我在时间t将描述系统状态的纹理传递到着色器的内容是什么,着色器执行计算并在时间t + dt返回系统状态,我在四边形上渲染纹理然后传递纹理回到着色器。但是,我发现我无法同时读取和写入相同的纹理。但我相信我已经在GPU上看到过这种计算的实现。他们如何解决这个问题?我想我看到了一些关于OpenGL可以读写相同纹理的不同方法的讨论,但我无法理解它们并使它们适应我的情况。要渲染纹理,我使用:glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, renderedTexture, 0);

这是我的渲染例程:

do{


    //count frames
    frame_counter++;


    // Render to our framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
    glViewport(0,0,windowWidth,windowHeight); // Render on the whole framebuffer, complete from the lower left corner to the upper right

    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Use our shader
    glUseProgram(programID);
    // Bind our texture in Texture Unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderTexture);


    glUniform1i(TextureID, 0);

    printf("Inv Width: %f", (float)1.0/windowWidth);
    //Pass inverse widths (put outside of the cycle in future)
    glUniform1f(invWidthID, (float)1.0/windowWidth);
    glUniform1f(invHeightID, (float)1.0/windowHeight);

    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
    glVertexAttribPointer(
                          0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
                          3,                  // size
                          GL_FLOAT,           // type
                          GL_FALSE,           // normalized?
                          0,                  // stride
                          (void*)0            // array buffer offset
                          );

    // Draw the triangles !
    glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles

    glDisableVertexAttribArray(0);
    // Render to the screen
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    // Render on the whole framebuffer, complete from the lower left corner to the upper right
    glViewport(0,0,windowWidth,windowHeight);

    // Clear the screen
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Use our shader
    glUseProgram(quad_programID);

    // Bind our texture in Texture Unit 0
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, renderedTexture);
    // Set our "renderedTexture" sampler to user Texture Unit 0
    glUniform1i(texID, 0);

    glUniform1f(timeID, (float)(glfwGetTime()*10.0f) );

    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
    glVertexAttribPointer(
                          0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
                          3,                  // size
                          GL_FLOAT,           // type
                          GL_FALSE,           // normalized?
                          0,                  // stride
                          (void*)0            // array buffer offset
                          );

    // Draw the triangles !
    glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles

    glDisableVertexAttribArray(0);

    glReadBuffer(GL_BACK);
    glBindTexture(GL_TEXTURE_2D, sourceTexture);
    glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, windowWidth, windowHeight, 0);


    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();



}

现在发生的事情是,当我渲染到帧缓冲区时,我认为作为输入的纹理是空的,我想。但是当我在屏幕上渲染相同的纹理时,它会成功地呈现我所感受到的。

1 个答案:

答案 0 :(得分:0)

好吧,我想我已经成功了解了一些事情。我可以使用glCopyTexImage2D将屏幕上呈现的内容复制到纹理,而不是渲染到帧缓冲区。但是,现在我有另一个问题:我无法理解glCopyTexImage2D是否可以使用帧缓冲区。它适用于屏幕渲染,但是当我渲染到帧缓冲区时,我无法使其工作。首先不确定这是否可行。对此提出了一个单独的问题: Does glCopyTexImage2D work when rendering offscreen?