从相同纹理的撰写和读取OpenGL上的迭代de Solver

Writing and reading from the same texture for an iterative DE solver on OpenGL

本文关键字:OpenGL 迭代 Solver de 读取 纹理      更新时间:2023-10-16

我正在尝试编写一个流体模拟器,该模拟器需要在迭代中求解一些微分方程(lattice-boltzmann方法(。我希望它是使用OpenGL的实时图形可视化。我遇到了一个问题。我使用着色器对GPU执行相关计算。在时间t上描述系统状态的纹理是什么我是什么?回到着色器。但是,我发现我不能同时读写并写入相同的纹理。但是我敢肯定,我已经看到了在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();

}

现在发生的事情是,当我渲染到框架缓冲器时,我认为输入是空的。但是,当我在屏幕上呈现相同的纹理时,它会成功地呈现我的内容。

好吧,我想我已经设法弄清楚了一些东西。我可以使用glCopyTexImage2D将屏幕上的任何内容复制到纹理上,而不是渲染框架。但是,我还有另一个问题:我不明白glCopyTexImage2D是否可以与框架缓冲区一起使用。它可以与屏幕上的渲染配合使用,但是当我渲染到Framebuffer时,我无法使其工作。首先不知道这是否可以。对此提出了一个单独的问题:glcopyteximage2d在渲染屏幕外是否有效?