Jump to content


problem with glUniformMatrix4fv


  • You cannot reply to this topic
8 replies to this topic

#1 srinivasareddyp

    New Member

  • Members
  • Pip
  • 9 posts

Posted 04 March 2009 - 08:27 AM

Hi,

I am using glUniformMatrix4fv to pass 3 matrices from my main program to vertex shader and geometry shader. When I pass 3 matrices independently its working fine but when trying to pass as array of matrices its giving invalid operation.
main program..

loc1 = glGetUniformLocation(shader,"gx1");

.....

....

 float mat[48];


        for(int i=0;i<16;i++)

        {

                mat[i]=gx[m][i];

                mat[i+16]=gy[m][i];

                mat[i+32]=gz[m][i];

        }



        glUniformMatrix4fv(loc1,3,0,mat);

in vertex and geometry shaders my declaration is

uniform mat4 gx1[3];


So what am I doing wrong? help me....

#2 kruseborn

    Member

  • Members
  • PipPip
  • 33 posts

Posted 04 March 2009 - 01:21 PM

I dont know exactly what you are doing wrong but try something more ease,

vector<Matrix> vecM;

vecM.push_back(gx);

vecM.push .....

glUniformMatrix4fv(loc1, size(vecM), 0, &vecMat[0])



#3 srinivasareddyp

    New Member

  • Members
  • Pip
  • 9 posts

Posted 12 March 2009 - 03:20 PM

Hi kruseborn,
I have tried what u suggested .Here is the code
//global
#include<vector>
typedef vector<double> Vec;
typedef vector<Vec> Matrix;
func displaypatch()
{
799        vector<Matrix> vecM;
800       vecM.push_back(gx[m]);
801        vecM.push_back(gy[m]);
802        vecM.push_back(gz[m]);

804        glUniformMatrix4fv(loc1, size(vecM), 0, &vecM[0]);
}
but I ended up with the following errors.As I have never used vectors i couldnt rectify them.
spline_patch.cpp: In function ‘void displaypatch(int)’:
spline_patch.cpp:800: error: no matching function for call to ‘std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > >::push_back(float*&)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, _Alloc = std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > >]
there are same errors on 801 & 802 too.
Plz help me

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 12 March 2009 - 03:35 PM

For this to work you need to implement Matrix as a vector<float> with 16 elements in the correct order expected by glUniformMatrix4fv, not vector<vector<float> >. Also note that if you use doubles you need to switch to glUniformMatrix4dv ('d' for double, 'f' for float).
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 srinivasareddyp

    New Member

  • Members
  • Pip
  • 9 posts

Posted 12 March 2009 - 05:02 PM

Hi kruseborn,
The method you suggested,did it work for you?. I dont think it works.Because in glUnoformMatrix4fv the 4th arg should be of type GLfloat* and you were passing kinda float***.
If it worked for you plz let me know.

#6 srinivasareddyp

    New Member

  • Members
  • Pip
  • 9 posts

Posted 12 March 2009 - 05:10 PM

Hi Reedbeta,
How do I know the order that is expected by glUniformMatrix4fv?Is there any standard format?. The method I did initially
loc1 = glGetUniformLocation(shader,"gx1");
.....
....
 float mat[48];

        for(int i=0;i<16;i++)
        {
                mat[i]=gx[m][i];
                mat[i+16]=gy[m][i];
                mat[i+32]=gz[m][i];
        }


        glUniformMatrix4fv(loc1,3,0,mat);

works fine when passing to geometry shader but goes wrong for vertex shader.

#7 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 12 March 2009 - 07:42 PM

The order is the same as for other GL matrix commands like glLoadMatrix - that is, column major order, using the column vector convention.

Also, for questions about the behavior of a function like this, you should consult the docs first.
reedbeta.com - developer blog, OpenGL demos, and other projects

#8 kruseborn

    Member

  • Members
  • PipPip
  • 33 posts

Posted 13 March 2009 - 08:39 AM

I have not tested it, what I wrote was how I pass multiple matrices in DX10, in DX10 there is an entity D3DXMATRIX, then you can just do a vector<D3DXMATRIX> matrices.

I just thought you can do the same thing in opengl, I have not tested it.

#9 srinivasareddyp

    New Member

  • Members
  • Pip
  • 9 posts

Posted 13 March 2009 - 10:58 AM

Hi,
Its woriking with a single dimensional vector with floats.
         vector<float> vec;
        for(int i=0;i<16;i++)
                vec.push_back(gx[m][i]);
        for(int i=0;i<16;i++)
                vec.push_back(gy[m][i]);
        for(int i=0;i<16;i++)
                vec.push_back(gz[m][i]);
        glUniformMatrix4fv(loc4,3,0,&vec[0]);
Thank You all Guys.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users