Search code examples
qtopenglglew

Resolving OpenGL functions using glew in QT


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);
}

Solution

  • 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.