首页 > 解决方案 > OpenGL:纹理渲染卡住

问题描述

当我运行drawPlane时:

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

一切都很好。图像被渲染,还有我的三角形。

但是,在更改为

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

纹理不再更新,只有我的三角形......

我该如何解决?


void drawCursorHit() {
    float cx = (hit_coord.x / (float)wWnd) * 2 - 1;
    float cy = ((hit_coord.y / (float)hWnd) * 2 - 1)*-1.f;

    float lx = 0.02f;
    float ly = lx * 16. / 9.;

    glLineWidth(1.5);
    glColor3f(0.25f, 0.55f, 0.85f);
    glBegin(GL_LINES);
    glVertex3f(cx - lx, cy, 0.0);
    glVertex3f(cx + lx, cy, 0.0);
    glVertex3f(cx, cy - ly, 0.0);
    glVertex3f(cx, cy + ly, 0.0);
    glEnd();
}

void drawPlane() {

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

    glColor3f(0.25f, 0.55f, 0.85f);
    glBegin(GL_TRIANGLES);

    for (int i = 0; i < continuous_plane.triangles.size(); i++)
    {

        sl::float3 vertices_1 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).x);
        sl::float3 vertices_2 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).y);
        sl::float3 vertices_3 = continuous_plane.vertices.at(continuous_plane.triangles.at(i).z);

        int pixel_x_1 = static_cast<int>(camera_cx + (vertices_1.x * camera_fx) / vertices_1.z);
        int pixel_y_1 = static_cast<int>(camera_cy + (vertices_1.y * camera_fy) / vertices_1.z);
        int pixel_x_2 = static_cast<int>(camera_cx + (vertices_2.x * camera_fx) / vertices_2.z);
        int pixel_y_2 = static_cast<int>(camera_cy + (vertices_2.y * camera_fy) / vertices_2.z);
        int pixel_x_3 = static_cast<int>(camera_cx + (vertices_3.x * camera_fx) / vertices_3.z);
        int pixel_y_3 = static_cast<int>(camera_cy + (vertices_3.y * camera_fy) / vertices_3.z);

        float x_gl_1 = (pixel_x_1 / (float)wWnd) * 2 - 1;
        float y_gl_1 = ((pixel_y_1 / (float)hWnd) * 2 - 1)*-1.f;
        float x_gl_2 = (pixel_x_2 / (float)wWnd) * 2 - 1;
        float y_gl_2 = ((pixel_y_2 / (float)hWnd) * 2 - 1)*-1.f;
        float x_gl_3 = (pixel_x_3 / (float)wWnd) * 2 - 1;
        float y_gl_3 = ((pixel_y_3 / (float)hWnd) * 2 - 1)*-1.f;


        glVertex3f(x_gl_1, y_gl_1, 0.0);
        glVertex3f(x_gl_2, y_gl_2, 0.0);
        glVertex3f(x_gl_3, y_gl_3, 0.0);
    }

    glEnd();
}

/**
OpenGL draw function
Render Image and wireframe mesh into a texture using the FrameBuffer
**/
void drawGL() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glActiveTexture(GL_TEXTURE0);

    glViewport(0, 0, wWnd, hWnd);

    // Render image and wireframe mesh into a texture using frame buffer
    // Bind the frame buffer and specify the viewport (full screen)
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    // Render the ZED view (Left) in the framebuffer
    glUseProgram(shader_image->getProgramId());
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, imageTex);
    glUniform1i(texID, 0);
    //invert y axis and color for this image (since its reverted from cuda array)
    glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "revert"), 1);
    glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "rgbflip"), 1);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, quad_vb);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableVertexAttribArray(0);
    glUseProgram(0);

    // Unbind the framebuffer since the texture is now updated
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    // Render the texture to the screen
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glUseProgram(shader_image->getProgramId());
    glBindTexture(GL_TEXTURE_2D, renderedTexture);
    glUniform1i(texID, 0);
    glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "revert"), 0);
    glUniform1i(glGetUniformLocation(shader_image->getProgramId(), "rgbflip"), 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, quad_vb);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glDisableVertexAttribArray(0);
    glUseProgram(0);
    glDisable(GL_TEXTURE_2D);

    if (user_hit_the_screen > 0)
    {
        drawPlane();
        drawCursorHit();
    }

    // Swap buffers
    glutSwapBuffers();

}

标签: c++openglimage-processing3dglut

解决方案


您必须在函数中glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);的第一个之前设置:glDrawArraysdrawGL

void drawGL() {

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    .....
    glDrawArrays(GL_TRIANGLES, 0, 6);
    ....

    if (user_hit_the_screen > 0)
    {
        drawPlane();
        drawCursorHit();
    }
    glutSwapBuffers();
}

要理解这个问题,你必须知道 OpenGL 是一个状态机。每个设置的状态都会被保留,直到它再次被改变。
此外,主循环功能drawGL被连续执行。

注意,当glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)设置为 时drawPlane,多边形光栅化模式变为GL_LINE
当函数离开时所有处于活动状态的状态在开始时仍然处于活动状态,下次执行该函数时。这意味着,在下一帧中,光栅化模式GL_LINE在开始。
那是你没想到的,也不是你想要的。您必须确保在函数开始时正确设置所有状态。


推荐阅读