Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Rendering an OpenGL ES 2.0 triangle using QGLWidget in Qt5.3
Forum Updated to NodeBB v4.3 + New Features

Rendering an OpenGL ES 2.0 triangle using QGLWidget in Qt5.3

Scheduled Pinned Locked Moved Game Development
8 Posts 6 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    codefy
    wrote on 12 Aug 2014, 11:16 last edited by
    #1

    Hi all.
    Actually I am new to both OpenGL ES and Qt.So please don't mind if I go wrong anywhere.
    I am trying to render a simple triangle with interpolated vertices.When I compile it shows following errors.

    @error: 'initializeGLFunctions' was not declared in this scope
    error: 'glGenBuffers' was not declared in this scope
    error: 'program' was not declared in this scope
    positionID = program.attributeLocation ("s_vPosition");
    error: 'glVertexAttribPointer' was not declared in this scope
    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
    error: 'glEnableVertexAttribArray' was not declared in this scope
    error: 'glBindBuffer' was not declared in this scope
    error: 'glBufferData' was not declared in this scope@

    In one of the Forums I found that its because of exclusion of GLEW(not sure why it is used).So I went ahead and added #include<GL/glew.h>. But it made the thing worst,I got 121 errors.

    This is my code written in Qt Creator. The code below is Geometry.cpp which inherits QGLWidget as Base Class.

    @#include "geometry.h"
    //#include<GL/glew.h>
    #include <QVector3D>
    #include <QtOpenGL/QGLFunctions>
    #include <QGLShaderProgram>
    #include <QtOpenGL/qgl.h>

    Geometry::Geometry(QWidget *parent) :
    QGLWidget(parent)
    {
    }

    void Geometry::initializeGL()
    {
    initializeGLFunctions();
    glGenBuffers(1, &vbo);
    positionID = program.attributeLocation ("s_vPosition");// return the program id which is stored in positionID
    colorID= program.attributeLocation("s_vColor");
    initTriangleGeometry();
    // glewInit();
    }

    void Geometry::resizeGL(int w, int h)
    {
    glViewport(0, 0, w, h);
    }

    void Geometry::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT);

    glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3*3*sizeof(GLfloat)));
    
    glEnableVertexAttribArray(positionID);
    glEnableVertexAttribArray(colorID);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    

    }

    void Geometry::initShaders()
    {
    // Compile vertex shader
    if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.glsl"))
    close();

    // Compile fragment shader
    if (!program.addShaderFromSourceFile&#40;QGLShader::Fragment, ":/fragmen--tShader.glsl"&#41;)
        close();
    
    // Link shader pipeline
    if (!program.link())
        close();
    
    // Bind shader pipeline for use
    if (!program.bind())
        close();
    

    }

    void Geometry::initTriangleGeometry()
    {
    GLfloat vertices[]={-0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.0f, 0.5f, 0.0f};

    GLfloat colors[]={1.0f, 0.0f, 0.0f, 1.0f,
                       0.0f, 1.0f, 0.0f, 1.0f,
                       0.0f, 0.0f, 1.0f, 1.0f};
    
    
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, 7*3*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, 3*3*sizeof(GLfloat), vertices);
    glBufferSubData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat),3*4*sizeof(GLfloat), colors);
    

    }
    @

    Main.cpp

    @#include "mainwindow.h"
    #include <QApplication>
    #include <geometry.h>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    a.setApplicationName("Triangle");
    Geometry widget;
    widget.showMaximized();

    return a.exec&#40;&#41;;
    

    }@

    Vertex Shader:

    @attribute vec3 s_vPosition;
    attribute vec4 s_vColor;
    varying vec4 color;

    void main () {
    color = s_vColor;
    gl_Position = vec4(s_vPosition, 1.0);
    }@

    Fragmnet Shader:
    @varying vec4 color;

    void main () {
    gl_FragColor = color;
    }@

    Please Help.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Ferobles92
      wrote on 12 Aug 2014, 15:51 last edited by
      #2

      codefy,
      the following link is to a youtube video of someone doing exactly what you want
      https://www.youtube.com/watch?v=1nzHSkY4K18

      however, I encourage you to look into QWindow. As I understand, the Qt project is trying to move away from the QGLWidget. They want to use QWindow only. Use createWindowContainer() on your MainWindow class to create a drawable OpenGL surface, the QWindow.

      Hope this works.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 12 Aug 2014, 21:01 last edited by
        #3

        Hi,

        No they are not, QGLWidget will have a replacement called QOpenGLWidget.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • C Offline
          C Offline
          codefy
          wrote on 13 Aug 2014, 05:59 last edited by
          #4

          Thank you both of you for your reply.

          Ferobles,
          Actually I have referred this video before it is to render a triangle using fixed rendering pipeline but I want to do it using programmable pipeline.What about the Error any idea?

          SGaist,
          What does that mean can you please explain it precisely?

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on 13 Aug 2014, 06:04 last edited by
            #5

            A new, better class will be introduced in Qt 5.4 (alpha version will be available this week, or early next week): QOpenGLWidget. You can read some discussion about it here: "link":http://comments.gmane.org/gmane.comp.lib.qt.devel/17852.

            (Z(:^

            1 Reply Last reply
            0
            • A Offline
              A Offline
              agocs
              wrote on 13 Aug 2014, 07:29 last edited by
              #6

              You are most likely not deriving from QGLFunctions, hence the absence of initializeGLFunctions and the GL2 stuff. The Geometry class should derive both from QGLWidget and QGLFunctions:
              class Geometry : public QGLWidget, protected QGLFunctions { ... };

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jiangcaiyang
                wrote on 14 Aug 2014, 01:44 last edited by
                #7

                According to Qt's roadmap, Qt 5.4 will have all QGL* classes and functions deprecated. Try using QOpenGL* classes instead.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  codefy
                  wrote on 14 Aug 2014, 11:52 last edited by
                  #8

                  Thanks all for commenting here.

                  agocs,
                  Why we need multiple inheritance.?How will it solve my problems?

                  sierdzio,
                  That really was an useful information.

                  jiangcaiyang,
                  I tried it but no luck. Seems like Qt5.3.1 does not supports it.Not sure.

                  1 Reply Last reply
                  0

                  1/8

                  12 Aug 2014, 11:16

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved