Resolving OpenGL functions using glew in QT
Asked Answered
B

3

8

Its been 17 days I'm struggling to write a simple program in OpenGL using QT, but all tutorials searched on Google are failed. I'v compiled my QT 5.0.1 with -opengl desktop using msvc2010 to make desktop opengl default.

I'v read tutorial by Sean Harmer, but it is depended on QT to resolve opengl functions.

According to some people, it is not possible to use glew in QT, as QT's built in libraries prevent glew from including its headers, but according to Sean Harmer HERE , it is possible (but it is not explained there how).

Even using QGLWidget I'm not able to use OpenGL extensions.

Now all I want is to use all OpenGL functions separate and all windowing functions from QT separate, i.e. no interference in OpenGL by QT.

If anybody explain me the procedure with simple triangle example and using any extension, I'l be so thankful..

I'v used glew library in my project and #included glew.h in very start of my program, but when compiled it gives error: glwidget.obj:-1: error: LNK2001: unresolved external symbol imp__glewGenVertexArrays

My .pro file

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Trial
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp \
    glwidget.cpp

HEADERS  += mainwindow.h \
    glwidget.h

FORMS    += mainwindow.ui

RESOURCES += \
    resources.qrc
win32: LIBS += -L$$PWD/../../../../../OpenGL/glew-1.9.0/lib/ -lglew32s

INCLUDEPATH += $$PWD/../../../../../OpenGL/glew-1.9.0/include
DEPENDPATH += $$PWD/../../../../../OpenGL/glew-1.9.0/include

win32: PRE_TARGETDEPS += $$PWD/../../../../../OpenGL/glew-1.9.0/lib/glew32s.lib

Output by glewInfo : GLEW version 1.9.0 Reporting capabilities of pixelformat 1 Running on a GeForce GT 540M/PCIe/SSE2 from NVIDIA Corporation OpenGL version 4.3.0 is supported

void GLWidget::initializeGL()
{
    GLenum err = glewInit();
    if(err != GLEW_OK)
    {
        printf("%s",glewGetErrorString(err));
    }

    glEnable(GL_DEPTH_TEST);
    qglClearColor(QColor(Qt::red));

    shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    shaderProgram.link();
    shaderProgram.bind();

    const GLubyte *oglVersion = glGetString(GL_VERSION);
    printf( "%s\n",oglVersion );

    GLfloat vertices[3][3] ={{ -0.5, -0.5, -1 },
                { 0,    0.5,    -1},
                    { 0.5,  -0.5,   -1}};
    vertexArray.create();
    vertexArray.setUsagePattern(QOpenGLBuffer::StaticDraw);
    vertexArray.allocate( vertices, 3 * 3 * sizeof( GLfloat ) );
    vertexArray.bind();
    shaderProgram.setAttributeBuffer( "vVertex", GL_FLOAT, 0, 3 );
    shaderProgram.enableAttributeArray( "vVertex" );

    GLuint arr[5];
    glGenBuffers(5,arr);
}
Businessman answered 2/7, 2013 at 11:32 Comment(12)
"According to some people, it is not possible to use glew in QT" Wrong. Most people who use OpenGL in Qt apps configure the extensions with GLEW, and call raw OpenGL functions from inside QGLWidgets. But without specific error messages, the .pro file you have used, etc. we can't help you.Turnover
errors "unresolved external symbol imp__glew" comes when glew32s.lib is added with static marked and they disappear when glew32.lib is added with dynamic marked. Though my program terminates unexpectedly, when ran in debug mode, problem is at glDrawArrays...Businessman
If you are trying to static link on Windows, you need to define the GLEW_STATIC token. Presumably you are also calling glewInit() once you have valid context.Turnover
Yes Iv glewInit() in initialiseGL(). As you said, I added GLEW_STATIC before #including glew.h, but it gives warnings: :-1: warning: LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library glew32s.lib(glew.obj):-1: warning: LNK4099: PDB 'vc100.pdb' was not found with 'glew32s.lib(glew.obj)' or at 'D:\Dexter\Softwares\Programming stuff\QT Projects\Trial-build-Desktop_Qt_5_0_1_MSVC2010_32bit-Debug\debug\vc100.pdb'; linking object as if no debug infoBusinessman
Just to make sure, what version of OpenGL does your GPU support (as returned by glewinfo)? Also, post your initializeGL() method.Turnover
I don't know much (anything) about MSVC but it seems to be telling you to use /NODEFAULTLIB:library glew32s.lib.Turnover
Is it possible to use OpenGL independant of QT, exactly similar to GLUT? If so then I'l try hard towards it to work. Thanks for your support!Businessman
I've never used GLUT so I don't know. If you are static linking to GLEW, I recommend that you build it from source first.Turnover
but it is not explained there how Move all non-qt OpenGL rendering into separate .h/.cpp files and don't include Qt headers from those files. Can't think of any reason why this wouldn't work.Unhandy
@user2535208: "Is it possible to use OpenGL independant of QT" Sure. Use libsdl or freeglut. Or you could initialize OpenGL yourself. You'll lose access to Qt functions, though.Unhandy
@Unhandy I want to use QT windowing system to display OpenGL output and interact with it. Now glew has working properly with QT QGLWidget but problem is segment fault occurs when calling functions like glGenVertexArrays and glBindVertexArray, any suggestions?Businessman
@user2535208: See my code below? Grab it, add code that demonstrates the problem, and post it within your question. Also read this: sscce.orgUnhandy
B
17

All problems solved now.

First problem was : QT 5.0.1 does not support opengl by default. You have to compile it with -opengl desktop. Here is tutorial on how to compile QT with vs2010. Or you can download new opengl build from QT.

Second problem in adding glew libraries to project. If you add glew32s.lib (static) then you have to add "#define GLEW_STATIC" before "#include " It is advised to use glew32.lib (dynamic).

Third problem was segment fault on some opengl functions even if your GPU supports version 4.3. GLEW obtains information on the supported extensions from the graphics driver. Experimental or pre-release drivers, however, might not report every available extension through the standard mechanism, in which case GLEW will report it unsupported. To circumvent this situation, the glewExperimental global switch can be turned on by setting it to GL_TRUE before calling glewInit(), which ensures that all extensions with valid entry points will be exposed.

Took 18 days to solve these problems, glad now can start actual programming.

Businessman answered 3/7, 2013 at 6:12 Comment(0)
U
4

Following code compiles just FINE on MSVC2008 express, qt 4.8.1 and glew version 1.7.0

main.cpp:

#include <GL/glew.h>
#include <QApplication>
#include <QGLWidget>

int main(int argc, char** argv){
    glewInit();//<<-- glew function
    QApplication app(argc, argv);
    QGLWidget widget;

    GLuint arr;
    glGenVertexArrays(1, &arr);//<--no linker error (added just for testing, most likely won't gen arrays)

    widget.show();
    return app.exec();
}

gltest.pro:

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += . d:/c++/libs/include
LIBS += -L"d:/c++/libs/lib" -lglew32

CONFIG += console
QT += opengl

# Input
SOURCES += main.cpp

I assume that with Qt 5 situation won't be any different.

Feel free to override QGLWidget's paintGL and draw whatever you want using extensions. I don't have a code snippet nearby for that.

Suggestions:
1. Use precompiled binaries.
2. It might make sense to use dynamically-linked version of glew.
3. Notice header inclusion order.

According to some people

People say many things, you don't need to believe all of them.

Unhandy answered 2/7, 2013 at 13:58 Comment(0)
C
0

I would like to share my latest finding about how to make Qt and glew work together on the (currently) latest Windows environments (Qt 5.15.1 + QMake + MSVC 2019 + glew 2.1.0). Hope someone else an avoid spending countless hours on this like me. Here are what I did:

  1. Copy glew32.dll and glew32.lib to somewhere in your project directory. Make sure to the bitness of glew matches with the bitness of the executable you told Qt to build. I did not use the static version of the library because I failed to make that work.
  2. In your .pro (QMake) file, write something like this:
GLEW_INCLUDE_PATH = "$$PWD/ext/glew/include" # put glew headers here
GLEW_LIB_PATH = "$$PWD/ext/glew/x64"         # put glew32.dll and glew32.lib here

LIBS += -L$$GLEW_LIB_PATH -lglew32 -lglu32 -lopengl32

DEPENDPATH += .
INCLUDEPATH += $$GLEW_INCLUDE_PATH

The only difference I made compared to the previous answers is that I added -lglu32 and -lopengl32 to the link arguments. It seems that they are mandatory on Windows, othewise MSVC will complain about some link errors with OpenGL API functions.

  1. Additionally, try using QOpenGLWidget instead of QGLWidget. QGLWidget caused some weird OpenGL "Invalid Operation" errors when my program is exiting and trying to destroy OpenGL handles such as buffer and texture handles in destructors. glIntercept showed me that it is because wgl context was destroyed before the handles get released. Switching to QOpenGLWidget solved this problem completely.

Hope you can enjoy using glew with Qt now!

Crossroads answered 19/11, 2020 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.