QGLWidget - 发生失真

时间:2015-05-20 19:29:10

标签: qt opengl nvidia optix

我想在QGLWidget中显示OptixSDK的sample6。

我的应用程序只有3个QSlider,用于围绕X,Y,Z轴和QGLWidget旋转。

根据我的理解,只要我的QSlider或Mouseevents调用 updateGL(),就会调用 paintGL()。然后我初始化一个旋转矩阵并将此矩阵应用于PinholeCamera,以便用新变换的cameracoordinates追踪场景,对吧?

当跟踪完成时,我得到outputbuffer并使用它用 glDrawPixels()绘制像素,就像在OptiX框架中给出的GLUTdisplay.cpp一样。

但我的问题是图像偏斜/扭曲。例如,我想要显示一个球,但球是极端的,但旋转工作正常。 当我缩小时,图像似乎在水平方向上比在垂直方向上缩放得慢得多。

我几乎可以肯定/希望它必须对未正确使用的 gl ...()函数执行某些操作。我错过了什么?有人可以帮助我吗?

为了完整性,我发布了我的 paintGL() updateGL()代码。

void MyGLWidget::initializeGL()
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
m_scene = new MeshViewer();
m_scene->setMesh( (std::string( sutilSamplesDir() ) + "/ball.obj").c_str());                                  
int buffer_width, buffer_height;
    // Set up scene
    SampleScene::InitialCameraData initial_camera_data;
    m_scene->setUseVBOBuffer( false );
    m_scene->initScene( initial_camera_data );
    int m_initial_window_width = 400;
    int m_initial_window_height = 400;

    if( m_initial_window_width > 0 && m_initial_window_height > 0)
        m_scene->resize( m_initial_window_width, m_initial_window_height );

   // Initialize camera according to scene params
    m_camera = new PinholeCamera( initial_camera_data.eye,
                                  initial_camera_data.lookat,
                                  initial_camera_data.up,
                                  -1.0f, // hfov is ignored when using keep vertical
                                  initial_camera_data.vfov,
                                  PinholeCamera::KeepVertical );

    Buffer buffer = m_scene->getOutputBuffer();
    RTsize buffer_width_rts, buffer_height_rts;
    buffer->getSize( buffer_width_rts, buffer_height_rts );

    buffer_width  = static_cast<int>(buffer_width_rts);
    buffer_height = static_cast<int>(buffer_height_rts);

    float3 eye, U, V, W;
    m_camera->getEyeUVW( eye, U, V, W );
    SampleScene::RayGenCameraData camera_data( eye, U, V, W );

    // Initial compilation
    m_scene->getContext()->compile();
    // Accel build
    m_scene->trace( camera_data );
    m_scene->getContext()->launch( 0, 0 );

    // Initialize state
    glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(0, 1, 0, 1, -1, 1 );
    glMatrixMode(GL_MODELVIEW); glLoadIdentity();  glViewport(0, 0, buffer_width, buffer_height); 
   }

这是paintGL()

void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
    float3 eye, U, V, W;
    m_camera->getEyeUVW( eye, U, V, W );
    SampleScene::RayGenCameraData camera_data( eye, U, V, W );

    {
        nvtx::ScopedRange r( "trace" );
        m_scene->trace( camera_data );
    }
    // Draw the resulting image
    Buffer buffer = m_scene->getOutputBuffer();
    RTsize buffer_width_rts, buffer_height_rts;
    buffer->getSize( buffer_width_rts, buffer_height_rts );
    int buffer_width  = static_cast<int>(buffer_width_rts);
    int buffer_height = static_cast<int>(buffer_height_rts);
    RTformat buffer_format = buffer.get()->getFormat();

    GLvoid* imageData = buffer->map();
    assert( imageData );
    switch (buffer_format) {
    /*... set gl_data_type and gl_format ...*/
    }

    RTsize elementSize = buffer->getElementSize();
    int align = 1;
    if      ((elementSize % 8) == 0) align = 8;
    else if ((elementSize % 4) == 0) align = 4;
    else if ((elementSize % 2) == 0) align = 2;
    glPixelStorei(GL_UNPACK_ALIGNMENT, align);

    gldata = QGLWidget::convertToGLFormat(image_data);

    NVTX_RangePushA("glDrawPixels");
    glDrawPixels( static_cast<GLsizei>( buffer_width ), static_cast<GLsizei>( buffer_height ),gl_format, gl_data_type, imageData);
    // glDraw
    NVTX_RangePop();
    buffer->unmap();
}

1 个答案:

答案 0 :(得分:2)

经过几个小时的调试,我发现我忘了设置Camera-parameters,它没有任何东西可以用于OpenGL的东西。

我的U坐标,视平面的水平轴搞砸了,但是V,W和眼睛的坐标是正确的。

initializeGL()

中添加这些行之后
abc_bbc,2100
一切都是对的。