QGLWidget - Distortion Occurred

I would like to map sample 6 OptixSDK to QGLWidget.

My application has only 3 QSlider for rotation around the X, Y, Z and QGLWidget axis.

For my understanding, paintGL () is called whenever updateGL () is called by my QSlider or Mouseevents. Then I initialize the rotation matrix and applied this matrix to the PinholeCamera to track the scene with the new converted cameras, right?

When the trace is finished, I get the output buffer and use it to draw pixels using glDrawPixels (), as in GLUTdisplay.cpp, presented in the OptiX framework.

But my problem is that the image is distorted / distorted. For example, I wanted to display the ball, but the ball is extremely flat, but the rotation works great. When I zoom in, it seems that the image scales much more slowly horizontally than vertically.

I am pretty sure / hope it has something to do with the gl ... () functions that are not being used properly. What am I missing? Can someone help me?

For completeness, I write my code paintGL () and 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); 
   }

And here is 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();
}
+4
source share
1 answer

After several hours of debugging, I found out that I forgot to set the camera parameters correctly, he had nothing to do with OpenGL materials.

My U-coordinate, the horizontal axis of the viewing plane was mixed up, but the V, W and eye coordinates were correct.

After I added these lines to initializeGL ()

m_camera->setParameters(initial_camera_data.eye,
                        initial_camera_data.lookat,
                        initial_camera_data.up,
                        initial_camera_data.vfov,
                        initial_camera_data.vfov,
                        PinholeCamera::KeepVertical );

everything was right.

+1
source

Source: https://habr.com/ru/post/1589073/


All Articles