【问题标题】:only the left image is displayed on OpenGL StereoOpenGL Stereo 上仅显示左侧图像
【发布时间】:2013-11-26 03:09:33
【问题描述】:

尝试在 OpenGL 上显示两个图像时遇到问题(使用 PFD_STEREO 启用立体)。当我尝试同时显示 2 张图像时,只显示了一张(最后一张)图像,而另一张图像仅显示黑色(未显示图像)。

代码如下:

WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;

GLuint  leftImage,rightImage;

// register window class
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";

RegisterClass( &wc );

// create main window
hWnd = CreateWindow( "GLSample", "OpenGL Texture Sample", 
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 800, 600,
NULL, NULL, hInstance, NULL );

// enable OpenGL for the window
EnableOpenGL( hWnd, &hDC, &hRC );

// load our texture
leftImage = LoadTexture( "left0000.bmp", TRUE );
rightImage= LoadTexture( "right0000.bmp", TRUE );

// program main loop
while ( !bQuit ) {
    // check for messages
    if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
    {
        // handle or dispatch messages
        if ( msg.message == WM_QUIT )
        {
            bQuit = TRUE;
        }
        else
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
    else
    {
        glDrawBuffer(GL_BACK_LEFT);
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawBuffer(GL_BACK_RIGHT);
        glClear(GL_COLOR_BUFFER_BIT);

        // setup texture mapping
        glEnable( GL_TEXTURE_2D );
        glBindTexture( GL_TEXTURE_2D, leftImage );

        glPushMatrix();

        glBegin( GL_QUADS );
            glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
            glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
            glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
            glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
        glEnd();

        glBindTexture( GL_TEXTURE_2D, rightImage );

        glBegin( GL_QUADS );
            glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
            glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
            glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
            glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
        glEnd();

        glPopMatrix();
        glDisable( GL_TEXTURE_2D );

        SwapBuffers( hDC );
    }
}

// free the texture
FreeTexture( leftImage );
FreeTexture( rightImage );

// shutdown OpenGL
DisableOpenGL( hWnd, hDC, hRC );

// destroy the window explicitly
DestroyWindow( hWnd );

return msg.wParam;

只要我尝试过,我确信代码在这里不能正常工作:

glDrawBuffer(GL_BACK_LEFT);
glClear(GL_COLOR_BUFFER_BIT);

glDrawBuffer(GL_BACK_RIGHT);
glClear(GL_COLOR_BUFFER_BIT);

// setup texture mapping
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, leftImage );

glPushMatrix();

glBegin( GL_QUADS );
    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();

glBindTexture( GL_TEXTURE_2D, rightImage );

    glBegin( GL_QUADS );
        glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
        glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
        glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
        glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();

glPopMatrix();
glDisable( GL_TEXTURE_2D );

SwapBuffers( hDC );

【问题讨论】:

    标签: c++ image visual-c++ opengl textures


    【解决方案1】:

    您是否检查过实际选择的像素格式是否支持立体声?不幸的是,由于政治/营销而非技术原因,来自 NVidia 和 AMD 的消费级 GPU 不支持四缓冲立体声。因此,您返回的像素格式很可能也不支持它。因此,在这种情况下,右眼帧缓冲区将不可用。

    更新

    除此之外:你在哪里设置你想要实际渲染的帧缓冲区?

    它应该看起来像这样:

    glDrawBuffer(GL_BACK);  // <<<< enable both back buffers for clearing
    glClear(GL_COLOR_BUFFER_BIT);
    
    // setup texture mapping
    glEnable( GL_TEXTURE_2D );
    
    glPushMatrix();
    
    glBindTexture( GL_TEXTURE_2D, leftImage );
    glDrawBuffer(GL_BACK_LEFT); // <<<< enable left back buffer to draw left image to
    glBegin( GL_QUADS );
        glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
        glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
        glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
        glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
    
    
    glBindTexture( GL_TEXTURE_2D, rightImage );
    glDrawBuffer(GL_BACK_RIGHT); // <<<< enable left back buffer to draw right image to
    glBegin( GL_QUADS );
        glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
        glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
        glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
        glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
    glEnd();
    
    glPopMatrix();
    glDisable( GL_TEXTURE_2D );
    
    SwapBuffers( hDC );
    

    【讨论】:

    • 我已经尝试过支持的四缓冲显卡(用 Quadro 4000、Quadro 600、Quadro FX 3500 测试)它使用 gali-3d 样本(红蓝)效果很好 www.gali-3d.com /archive/articles/StereoOpenGL/StereoscopeOpenGLTutorial.php。该示例不使用栅格数据,因此我尝试使用栅格数据实现类似的(立体),但结果仅显示一个图像。 (使用上面的代码)。
    • 感谢您的帮助。你的代码工作正常。现在可以显示两个图像(立体)。因此,我们需要在使用glDrawBuffer(GL_BACK_LEFT)和glDrawBuffer(GL_BACK_RIGHT)之前使用glDrawBuffer(GL_BACK)和glClear(GL_COLOR_BUFFER_BIT),然后我们必须开始glBegin(GL_QUADS)来显示它的图像。对吗?
    • @respeto14:实际上你可以调用 glClear 两次,每边一次。您必须了解的一件事是,OpenGL 是一种基于状态的绘图机,它只是将(纹理/阴影)点、线和三角形放入像素帧缓冲区。这不是一些自动场景图。当您调用glDrawBuffer(…) 之后,所有内容都将被绘制到指定的缓冲区。直到您下一次调用 glDrawBuffer(…),它会切换之后所有内容的状态。没有具体的规则,按什么顺序做什么。重要的是,当绘制某些东西时,OpenGL 处于正确的状态。
    • 感谢您的评论。我会更多地了解他们。基本上我是openGL的新手,但我想尝试学习。
    猜你喜欢
    • 2019-06-26
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多