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. OpengGL Shader Program fails
Forum Updated to NodeBB v4.3 + New Features

OpengGL Shader Program fails

Scheduled Pinned Locked Moved Game Development
28 Posts 7 Posters 15.5k 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on last edited by
    #8

    As long as you specify to use the CompatibilityProfile it should work anyway. Maybe the bug is elsewhere is your code? Can you provide a small compilable example please?

    Nokia Certified Qt Specialist
    Interested in hearing about Qt related work

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sasmaster
      wrote on last edited by
      #9

      Here it is .Pretty same as of the guy in the above mentioned thread:
      Window class ,calls the OpenGLWrapper widget:
      @
      Window::Window()
      {
      QGLFormat fmt;
      fmt.setVersion(3,3);
      fmt.setProfile(QGLFormat::CompatibilityProfile);
      fmt.setSampleBuffers(true);
      glWidget=new OpenGLWrapper(fmt);

      .....

      @
      OpenGLWrapper:
      @

      #include<QtGui>

      #include<math.h>
      #include<QGLShaderProgram>
      #include "openglwrapper.h"
      #include "GL/glext.h"
      #include "qtlogo.h"

      #ifndef GL_MULTISAMPLE
      #define GL_MULTISAMPLE 0x809D
      #endif
      bool OpenGLWrapper::USE_SHADERS = 1;
      static GLfloat const triangleVertices[] = {
      60.0f, 10.0f, 0.0f,
      110.0f, 110.0f, 0.0f,
      10.0f, 110.0f, 0.0f
      };
      OpenGLWrapper::OpenGLWrapper(const QGLFormat & format,QWidget *parent):QGLWidget(format,parent),mShaderProgram(this)
      {

      logo=0;
      xRot=0;
      yRot=0;
      zRot=0;
      qtGreen = QColor::fromCmykF(0.10, 0.78, 1.0, 0.0);
      qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
      

      }

      void OpenGLWrapper::initShaderProgram(){

      mShaderProgram.addShaderFromSourceCode(QGLShader::Vertex,
      "#version 330\n"
      "in highp vec4 vertex;\n"
      "in mediump mat4 matrix;\n"
      "void main(void)\n"
      "{\n"
      " gl_Position = matrix * vertex;\n"
      "}");
      mShaderProgram.addShaderFromSourceCode(QGLShader::Fragment,
      "#version 330\n"
      "uniform mediump vec4 color;\n"
      "out mediump vec4 out_color;\n"
      "void main(void)\n"
      "{\n"
      " out_color = color;\n"
      "}");
      bool linked= mShaderProgram.link();
      bool binded= mShaderProgram.bind();
      if(linked&&binded){
      qDebug()<<binded<<"and"<<linked;
      }

      }

      QSize OpenGLWrapper::minimumSizeHint()const{
      return QSize(50,50);
      }
      QSize OpenGLWrapper::sizeHint()const{
      return QSize(400,400);
      }
      static void qNormalizeAngle(int &angle){
      while (angle < 0)
      angle += 360 * 16;
      while (angle > 360 * 16)
      angle -= 360 * 16;
      }

      void OpenGLWrapper::setXRotation(int angle){
      qNormalizeAngle(angle);
      if(angle!=xRot){
      xRot=angle;
      emit xRotationChanged(angle);
      updateGL();
      }
      }
      void OpenGLWrapper::setYRotation(int angle)
      {
      qNormalizeAngle(angle);
      if (angle != yRot) {
      yRot = angle;
      emit yRotationChanged(angle);
      updateGL();
      }
      }

      void OpenGLWrapper::setZRotation(int angle)
      {
      qNormalizeAngle(angle);
      if (angle != zRot) {
      zRot = angle;
      emit zRotationChanged(angle);
      updateGL();
      }

      }
      void OpenGLWrapper::initializeGL()
      {
      qglClearColor(qtPurple.dark());

      logo = new QtLogo(this, 64);
      logo->setColor(qtGreen.lighter());
      
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_CULL_FACE);
      glShadeModel(GL_SMOOTH);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_MULTISAMPLE);
      static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
      glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
      initShaderProgram();
      
      
         printGLString();
      

      }

      void OpenGLWrapper::paintGL()
      {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      QMatrix4x4 pmvMatrix;
      pmvMatrix.ortho(rect());
      int vertexLocation = mShaderProgram.attributeLocation("vertex");
      int matrixLocation = mShaderProgram.attributeLocation("matrix");
      int colorLocation = mShaderProgram.uniformLocation("color");

      mShaderProgram.enableAttributeArray(vertexLocation);
      mShaderProgram.setAttributeArray(vertexLocation, triangleVertices,3);
      mShaderProgram.setUniformValue(matrixLocation, pmvMatrix);
      mShaderProgram.setUniformValue(colorLocation,QColor(0, 255, 0, 255));

      glDrawArrays(GL_TRIANGLES, 0, 3);
      

      mShaderProgram.disableAttributeArray(vertexLocation);

      }
      void OpenGLWrapper::printGLString()
      {
      const char* version = reinterpret_cast<const char *>(glGetString(GL_VERSION));
      qDebug() << "GL_VERSION:" << version;

      }

      void OpenGLWrapper::resizeGL(int width, int height)
      {
      int side = qMin(width, height);
      glViewport((width - side) / 2, (height - side) / 2, side, side);

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      

      #ifdef QT_OPENGL_ES_1
      glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
      #else
      glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
      #endif
      glMatrixMode(GL_MODELVIEW);
      }

      void OpenGLWrapper::mousePressEvent(QMouseEvent *event)
      {
      lastPos = event->pos();
      }

      void OpenGLWrapper::mouseMoveEvent(QMouseEvent *event)
      {
      int dx = event->x() - lastPos.x();
      int dy = event->y() - lastPos.y();

      if (event->buttons() & Qt::LeftButton) {
          setXRotation(xRot + 8 * dy);
          setYRotation(yRot + 8 * dx);
      } else if (event->buttons() & Qt::RightButton) {
          setXRotation(xRot + 8 * dy);
          setZRotation(zRot + 8 * dx);
      }
      lastPos = event->pos();
      

      }

      OpenGLWrapper::~OpenGLWrapper(){

      }

      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        minimoog77
        wrote on last edited by
        #10

        [quote author="sasmaster" date="1315900787"]I am trying to write a modern OpenGL (programmable pipeline) program.Qt OpenGL examples show only the fixed pipeline implementation.The documentation on how to initialize Shader Program is very poor.[/quote]

        No, it's not poor. You should learn how to setup rendering pipeline in OpenGL. This is not Qt fault.

        BTW, did you bind attribute location?

        And usually there is error report from graphic driver why something failed.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sasmaster
          wrote on last edited by
          #11

          The code I am using here to pass attributes into shaders is taken from the "not poor" "docs":http://doc.qt.nokia.com/latest/qglshaderprogram.html of Qt.

          Regarding the attributes location binding ,I think this does this work in Qt :
          @
          int vertexLocation = mShaderProgram.attributeLocation("vertex");
          int matrixLocation = mShaderProgram.attributeLocation("matrix");
          int colorLocation = mShaderProgram.uniformLocation("color");

          @
          Yes this is slightly different from how it is done in "raw" OpenGL .May be you can point me where I am wrong ? Also the Dude from here http://developer.qt.nokia.com/forums/viewthread/4856/ does it the same way.

          I am getting no shader program errors either .Everything compiles and runs OK .But the display is empty.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndrewJacob
            wrote on last edited by
            #12

            What is the reason of getting empty screen in GL ?

            [url=http://www.rtaflooring.com]Bamboo Flooring[/url]

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sasmaster
              wrote on last edited by
              #13

              That is the question of this post :)

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AndrewJacob
                wrote on last edited by
                #14

                Yes it is but what is the code about ?

                [url=http://www.rtaflooring.com]Bamboo Flooring[/url]

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sasmaster
                  wrote on last edited by
                  #15

                  Triangle.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    minimoog77
                    wrote on last edited by
                    #16

                    [quote author="sasmaster" date="1316083047"]
                    I am getting no shader program errors either .Everything compiles and runs OK .But the display is empty.[/quote]

                    Sorry for lateness, I am looking at your code, first question, why did you enable GL_LIGHTING/GL_LIGHT? That's fixed pipeline stuff.

                    Second look, GL_MODELVIEW and GL_PROJECTION matrices, you don't have that also in programmable pipeline. You have to do it yourself in vertex shader. Yes, there is option to set the matrices and access them in shader, but all of the multiplying you have to do it yourself.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sasmaster
                      wrote on last edited by
                      #17

                      Those are remnants from my previous trials.I don't think those influence the modern pipeline as I am using a compatibility mode .So those should be discarded by the OpenGL. Regarding the matrices ,you can see that I load a orthogonal projection matrix into the vertex shader on each render loop. Still nothing works.If anybody could put a full example on how to write a OpenGL 3x in Qt it could be really helpful to many people I believe :)

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        minimoog77
                        wrote on last edited by
                        #18

                        Hey, I have example, http://minimoog.wordpress.com/2010/05/10/transfverteksi/ Oh, I am sorry it's in my native language (if it is for somebody interesting I would translate/write in english) but code is simple for understanding http://dl.dropbox.com/u/6108964/QtGL2.zip

                        BR.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          sasmaster
                          wrote on last edited by
                          #19

                          He He ,very resembles Russian which I know :) .Is it wrapped with Qt? Thanks a lot .I will check it out!

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            minimoog77
                            wrote on last edited by
                            #20

                            Also you can use it for learning basic usage of VBO. :)

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sasmaster
                              wrote on last edited by
                              #21

                              I can see you didn't define the format and it actually works ?Another question ,are those inclosed GLEW headers and sources used in your program?

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                minimoog77
                                wrote on last edited by
                                #22

                                [quote author="sasmaster" date="1316340394"]I can see you didn't define the format and it actually works ?Another question ,are those inclosed GLEW headers and sources used in your program? [/quote]

                                By format you mean 'window framebuffer pixel format'?

                                Yes, it's using GLEW, since there are glGenBuffers, glBindBuffers, glBufferData, glVertexAttribPointer etc.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  sasmaster
                                  wrote on last edited by
                                  #23

                                  But why haven't you used the Qt version of those methods?
                                  By Format I mean the QGLFormat.

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    minimoog77
                                    wrote on last edited by
                                    #24

                                    [quote author="sasmaster" date="1316343899"]But why haven't you used the Qt version of those methods?[/quote]

                                    You mean QGLBuffer. Well at that time there was no QGLBuffer.

                                    [quote]By Format I mean the QGLFormat.[/quote]

                                    It uses default format. I don't see why would I need a fancy format for that code. :)

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      matti-
                                      wrote on last edited by
                                      #25

                                      What's the motivation to use QGLShaderProgram anyway instead of standard OpenGL for portability?

                                      Author of <a href="http://mmark.777-team.org/">MMark13</a>

                                      1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        tomato
                                        wrote on last edited by
                                        #26

                                        An idea (maybe wrong...) : Implementation of openGL is platform (hardware) dependent. So I guess it could permit an universal call through various platforms or OpenGL revision (and avoid use of OGL extension which I used to add on mac OS platform for exemple).
                                        Nervertheless I prefer use pure OpenGL functions call as long as OGL 4.0 (and so OGL ES 2.0) will not be supported anywhere...

                                        お前はもう死んでいる

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          sasmaster
                                          wrote on last edited by
                                          #27

                                          minimoog77 .Getting can't open the shader files errors when running your demo.Do you know what can be the problem?

                                          @
                                          QGLShader: Unable to open file "/vert1.vert"
                                          ""
                                          QGLShader: Unable to open file "simple.frag"

                                          @

                                          1 Reply Last reply
                                          0

                                          • Login

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