Use the glDrawBuffer and glReadBuffer functions in Qt

0

I would like to use the glDrawBuffer and glReadBuffer functions in Qt using Qt Creator. I tried to do this:

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

This code would not compile, it gave me linking errors like this one:

main.obj:-1: error: LNK2019: unresolved external symbol __imp__glDrawBuffer@4 referenced in function "public: void __thiscall Simple3DWidget::OpenGLWindow::render(void)" (?render@OpenGLWindow@Simple3DWidget@@QAEXXZ)

This error basically means that the functions were declared in an included header file but not correctly linked.

After searching for this issue on the web, I found out that I have to use the QOpenGLFunctions_3_1 class. I tried both these codes:

QOpenGLFunctions_3_1 m_openGL31Functions;
m_openGL31Functions.glDrawBuffer(GL_NONE);
m_openGL31Functions.glReadBuffer(GL_NONE);

and:

QOpenGLFunctions_3_1().glDrawBuffer(GL_NONE);
QOpenGLFunctions_3_1().glReadBuffer(GL_NONE);

Both these codes compile correctly, but when I run the program, in both cases, the program crashed and gave me the following error:

Access violation reading location 0x00000090

The strange thing is that I usually get this error when I assign the wrong value to a pointer, but none of these codes contain pointers (or maybe the functions contain pointers which generate this error, in which case there could be a bug in Qt's QOpenGLFunctions_3_1 class?).

What is the correct way of using the glDrawBuffer and glReadBuffer functions in Qt?

c++
qt
opengl
asked on Stack Overflow Nov 19, 2016 by Donald Duck • edited Nov 19, 2016 by Donald Duck

1 Answer

1

This error basically means that the functions were declared in an included header file but not correctly linked.

Correct. You have to link against OpenGL. opengl32.lib on Windows, libGL.so on *nix/Linux, -framework OpenGL on MacOS.

but none of these codes contain pointers (or maybe the functions contain pointers which generate this error, in which case there could be a bug in Qt's QOpenGLFunctions_3_1 class?).

Yes, they do. The OpenGL interface for anything that goes beyond a certain version is loaded at runtime into function pointers. This is what the QOpenGLFunctions class does. Before properly loading these pointers, they are invalid – in fact, depending on the OpenGL version your're using some may be valid and others not; that's why Qt puts the OpenGL version into the class name.

Answer the following questions:

  • Does the program create a OpenGL context to use?
  • Is the QOpenGLFunctions_… instance initialized with the context being made active on the calling thread?
  • Did initialization of the QOpenGLFunctions_… instance succeed?

If you'd answered any of these questions with "no" or "I don't know", that's where your problem is.

answered on Stack Overflow Nov 19, 2016 by datenwolf • edited Nov 19, 2016 by datenwolf

User contributions licensed under CC BY-SA 3.0