Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGLWidget - distortion occured

I would like to display sample6 of the OptixSDK in a QGLWidget.

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

For my understanding, paintGL() gets called whenever updateGL() is called by my QSlider or Mouseevents. Then I initialize a rotation matrix and apply this matrix to the PinholeCamera in order to trace the scene with new transformed cameracoordinates, right?

When tracing is finished i get the outputbuffer and use it draw the pixels with glDrawPixels(), just like in GLUTdisplay.cpp given in the OptiX framework.

But my issue is that the image is skewed/distorted. For example I wanted to display a ball, but the ball is extremley flatened, but the rotation works fine. When I am zooming out, it seems that the Image scales much slower horizontally than vertically.

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

For the completeness i post my paintGL() and updateGL() code.

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();
}
like image 899
Daniel R. Avatar asked Oct 31 '22 03:10

Daniel R.


1 Answers

After hours of debugging, I found out that I forgot to set the Camera-parameters right, it had nothing to go to with the OpenGL stuff.

My U-coordinate, the horizontal axis of view plane was messed up, but the V,W and eye coordinates were right.

After I added these lines in 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.

like image 153
Daniel R. Avatar answered Dec 01 '22 16:12

Daniel R.