Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using two QGLShaderProgram for objects in a same 3D scene [SOLVED]
QtWS25 Last Chance

Using two QGLShaderProgram for objects in a same 3D scene [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
29 Posts 4 Posters 9.4k Views
  • 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 11 Apr 2011, 15:15 last edited by
    #10

    Before reconfiguring Qt, try doing:

    @make confclean@

    I'll keep an eye out for your example.

    Nokia Certified Qt Specialist
    Interested in hearing about Qt related work

    1 Reply Last reply
    0
    • B Offline
      B Offline
      billouparis
      wrote on 11 Apr 2011, 15:16 last edited by
      #11

      Thanks ZapB

      1 Reply Last reply
      0
      • B Offline
        B Offline
        billouparis
        wrote on 11 Apr 2011, 23:17 last edited by
        #12

        Hey ZapB,
        My first use-case was actually wrong, I spent a lot of time on this, and the use cases are as follow:
        @
        paintGL()
        {

        program1->bind();
        drawObject1();

        program2->bind()
        drawObject2();

        drawObject3();

        }

        then object 1 is drawn using program 1, and object 2 and object 3 are drawn using program 2. which is fine!
        But now I have this other use case causing the issue:

        paintGL()
        {

        program1->bind();
        drawObject1();

        program2->bind()
        drawObject2();

        program1->bind()
        drawObject3();

        }
        @
        then object 1 and object 3 are drawn using program1, whereas object 2 is not drawn at all.

        It looks like the fact to return to another program (program1) before the end of paintGL function would sort of invalidate the drawing made by program2. Maybe I should add some method to force the rendering in program2, before swapping back to program1?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          billouparis
          wrote on 11 Apr 2011, 23:44 last edited by
          #13

          I tried to add some glFinish, and/or glEnd before swapping programs, but none of them helped in anyway to get my object 2 drawn.... Yes, I am Desperate!

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on 12 Apr 2011, 10:36 last edited by
            #14

            Have you got anywehere with preparing the simple example yet?

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • B Offline
              B Offline
              billouparis
              wrote on 12 Apr 2011, 10:48 last edited by
              #15

              Yes i have a very simple source code to send for you this afternoon.
              Bill

              1 Reply Last reply
              0
              • B Offline
                B Offline
                billouparis
                wrote on 12 Apr 2011, 11:46 last edited by
                #16

                Hello ZapB how do I send yo my source code? it's a zip file. is it possible to send it to you by mail or something?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  billouparis
                  wrote on 12 Apr 2011, 11:58 last edited by
                  #17

                  error

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    billouparis
                    wrote on 12 Apr 2011, 12:00 last edited by
                    #18

                    In the meantime you can download it from here:
                    http://www.mediafire.com/?bdejk5y93gr3wqv

                    In the draw function, you will see there are four different UseCases, you can use the "define" preprocessor directive to activate each one, and see the problems occuring,

                    Thank you,
                    Bill

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      billouparis
                      wrote on 13 Apr 2011, 08:57 last edited by
                      #19

                      Hello ZapB, did you have time to test my source code?
                      Thank you,
                      Bill

                      1 Reply Last reply
                      0
                      • Z Offline
                        Z Offline
                        ZapB
                        wrote on 13 Apr 2011, 09:37 last edited by
                        #20

                        I managed to get it built yesterday but when I ran it I got some error complaining about one of your shaders not defining some variable as a varying. I can't recall which it was at the moment. I am really busy today so I'll try to take a proper look tomorrow.

                        Nokia Certified Qt Specialist
                        Interested in hearing about Qt related work

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          billouparis
                          wrote on 13 Apr 2011, 09:56 last edited by
                          #21

                          In the myWidget.cpp file, you can choose three different options declared as defines.
                          #define PC_BILL , #define PC_WORK, #define TARGET_IMX
                          maybe you can switch between the two firsts (either PC_BILL, or PC_WORK), and see if your vertex compiles alright then.
                          I have this workshop forecast on monday, it could be nice if I can solve the issue before if you are able to help me ;)

                          Thank you,
                          Bill

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            billouparis
                            wrote on 6 May 2011, 15:13 last edited by
                            #22

                            Hello, anybody?

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              matrixx
                              wrote on 6 May 2011, 16:30 last edited by
                              #23

                              Hey, I got your program compiled using BILL definition. The output of the program:
                              myWidget::myWidget parent WinID = 0x80650
                              BILL : InitializeGL
                              Vertex Shader compiled OK ""
                              Fragment Shader compiled OK ""
                              Program 3 linked OK ""
                              Vertex Shader compiled OK ""
                              Fragment Shader compiled OK ""
                              Program 6 linked OK ""
                              BILL : ResizeGL
                              BILL : myWidget instance : myWidget(0x9ba6f18)
                              model draw
                              Error 1282 myWidget.cpp 436
                              model draw
                              model draw
                              model draw
                              BILL : myWidget ~myWidget
                              BILL : myWidget instance : myWidget(0x9ba6f18)

                              As visual, I saw three identical green triangles.

                              I dunno if this helps any, I need to dive into shader programs and source code to deeply analyze what the shaders are supposed to do, and are they doing what they are suppose to :)

                              edit: forgot to say that this output was from USE_CASE1. Running now the rest of them.

                              edit2: Ran the rest of the use cases and got indeed some missing triangles, now digging into the code :)

                              Nokia Certified Qt Specialist

                              1 Reply Last reply
                              0
                              • Z Offline
                                Z Offline
                                ZapB
                                wrote on 6 May 2011, 17:22 last edited by
                                #24

                                Sorry for the delay on this. I'll try to take another look this weekend too.

                                Nokia Certified Qt Specialist
                                Interested in hearing about Qt related work

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  matrixx
                                  wrote on 6 May 2011, 18:55 last edited by
                                  #25

                                  Found the problem:

                                  When you set uniform values like this:
                                  @
                                  g_hProjMatrixLoc = qGLShaderProgramTextures->uniformLocation("g_matProj");
                                  qGLShaderProgramTextures->setUniformValue(g_hProjMatrixLoc, matProj3);
                                  @
                                  you have to have your shaderprogram binded to current context with
                                  @
                                  qGLShaderProgramTextures->bind()
                                  @
                                  before modifying uniforms.

                                  Also a tip:
                                  the two clauses above can be replaced with one:
                                  @
                                  qGLShaderProgramTextures->setUniformValue("g_matProj", matProj3);
                                  @
                                  where it uses uniform name directly.

                                  Of course if you want to save your uniform location for later use, then the tip is useless :)

                                  edited to format code blocks.

                                  Nokia Certified Qt Specialist

                                  1 Reply Last reply
                                  0
                                  • Z Offline
                                    Z Offline
                                    ZapB
                                    wrote on 6 May 2011, 19:46 last edited by
                                    #26

                                    matrixx: Well spotted and it applies to the other matrices too.

                                    The fix is to change your paintGL() function to this:

                                    @
                                    void myWidget::paintGL()
                                    {
                                    /prepare projection matrix, only once!/
                                    static bool loop = 1;
                                    static GLfloat matProj3[4][4];
                                    if (loop == 1)
                                    {
                                    memset (matProj3, 0, 16*sizeof(GLfloat));
                                    matProj3[0][0] = 2;
                                    matProj3[1][1] = matProj3[0][0] * g_fAspectRatio;
                                    matProj3[2][2] = -1.0f;
                                    matProj3[2][3] = -1.0f;
                                    matProj3[3][2] = -1.0f;
                                    loop = 0;
                                    }

                                    // Clear the colorbuffer and depth-buffer
                                    QColor bgColor(10,15,255,255);
                                    qglClearColor( bgColor );
                                    DEBUGBILL
                                    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                                    DEBUGBILL
                                    
                                    qGLShaderProgramTextures->bind();
                                    g_hProjMatrixLoc = qGLShaderProgramTextures->uniformLocation("g_matProj");
                                    qGLShaderProgramTextures->setUniformValue(g_hProjMatrixLoc, matProj3);
                                    
                                    qGLShaderProgramScreen->bind();
                                    g_hProjMatrixLoc = qGLShaderProgramScreen->uniformLocation("g_matProj");
                                    qGLShaderProgramScreen->setUniformValue(g_hProjMatrixLoc, matProj3);
                                    
                                    QMatrix4x4 tempRot(+cosf( fAngle ),0,-sinf( fAngle ),0,
                                                       0,1,0,0,
                                                       +sinf( fAngle ) ,0,+cosf( fAngle ),0,
                                                       0,0,0,1.0);
                                    
                                    GLuint g_hSamplerLoc = 0;
                                    qGLShaderProgramTextures->bind();
                                    g_hSamplerLoc = qGLShaderProgramTextures->uniformLocation("g_matModelView");
                                    qGLShaderProgramTextures->setUniformValue(g_hSamplerLoc, tempRot);
                                    
                                    qGLShaderProgramScreen->bind();
                                    g_hSamplerLoc = qGLShaderProgramScreen->uniformLocation("g_matModelView");
                                    qGLShaderProgramScreen->setUniformValue(g_hSamplerLoc, tempRot);
                                    
                                    drawTest();
                                    

                                    }
                                    @

                                    The reason it worked for the different shaders depending which one was drawn first is beause your were not properly restoring which shader was bound each time around the rendering loop. Now they are explicitly set.

                                    As an aside this type of thing is much easier to spot if you use a consistent coding style. I can spot at least two different coding styles in this file.

                                    Nokia Certified Qt Specialist
                                    Interested in hearing about Qt related work

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      billouparis
                                      wrote on 7 May 2011, 07:58 last edited by
                                      #27

                                      matrixx and ZapB YOU ARE GENIOUS! Thank you so much for this, I really didn't know this, but of course the more I progress in openGL the more this error is made obvious.
                                      THANK YOU!!!

                                      1 Reply Last reply
                                      0
                                      • B Offline
                                        B Offline
                                        billouparis
                                        wrote on 7 May 2011, 07:58 last edited by
                                        #28

                                        we can lock this thread now of course!

                                        1 Reply Last reply
                                        0
                                        • D Offline
                                          D Offline
                                          DenisKormalev
                                          wrote on 7 May 2011, 08:02 last edited by
                                          #29

                                          Thread closing is not neccessary (because there can be other people who will have similar problem later). But marking as [solved] will be good, don't forget to do it.

                                          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