Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - steps to using QOpenGLWidget to display images

Tags:

qt

opengl

I'm trying to use a QOpenGLWidget to show some images instead of using QLabel. But I'm a bit confused about how to do this.

To make the widget get the job done, I know I need to reimplement the initializeGL() method and paintGL() method.

To get the texture of an image, what I used is SOIL_load_image(). Why is unsigned char* img_data over unsigned char* img_data[3]? I think each pixel of an image has 3 values(RGB).

After getting the texture, I have no idea what I should do and where should I do them in initializeGL() or paintGL(). Can anyone tell the steps?

void MyOpenGLWidget::loadTexture(const char* file_path)
{
    *image = cv::imread(file_path, cv::IMREAD_COLOR);
    width = image->rows;
    height = image->cols;
    int channels = image->channels();
    img_data = SOIL_load_image(file_path, &width, &height, &channels, SOIL_LOAD_RGB);
}
like image 434
SpellTheif Avatar asked Aug 31 '25 04:08

SpellTheif


1 Answers

Why is unsigned char* img_data over unsigned char* img_data[3]

unsigned char* is a pointer to a buffer (of arbitrary length) of data. unsigned char* …[3] is an array of 3 pointers to buffers of data. You have only one buffer, not 3.

For some reason you're using both OpenCV and then SOIL to read the same image two times. Why?

Once you've loaded the image, to display it with OpenGL you have to

  1. Create a texture object (glGenTextures, glBindTexture, glTexImage)
  2. Create some geometry to draw it (usually a quad, or a viewport filling triangle), by filling a vertex buffer object (glGenBuffers, glBindBuffer, glBufferData) and associating the data in the buffer with vertex attributes of a vertex array object (glGenVertexArrays, glBindVertexArray, glEnableVertexArrayAttrib, glVertexAttribPointer)
  3. Create a shader program, consisting of a vertex shader that places the geometry and paramtizes the fragment shader, which actually samples from the texture. (glCreateShader, glShaderSource, glCreateProgram, glLinkProgram)

Then to draw

  1. select the shader program (glUseProgram)
  2. set parameters (glUniform)
  3. draw (glDrawArrays)
like image 101
datenwolf Avatar answered Sep 02 '25 19:09

datenwolf