Sign in to follow this  
harshman_chris

Passing a Matrix4 to a Vertex Attribute [Solved]

Recommended Posts

So I am looking for a way to pass a Matrix4 from my program into my shader as an attribute.

 

As for the reason why, I am working on a particle system and every particle requires its own Model matrix so I can do things like per particle rotation.

 


I know you cannot just do this, because you can only used 1,2,3,4 where the 16 is. I do know I could split this into 4 vectors, but then I have to reassemble on the other side so I would rather not do that.

glVertexAttribPointer(3, 16, GL_FLOAT, GL_FALSE, sizeof(ParticleData), (const GLvoid*)36);

 

 

Is there a way to pass this into the shader some way that doesn't involve me adding code to disassemble and reassemble my matrix?

 

Edit:

I should add I have a struct that holds the information for each particle so I am using interleaving to pass data.

 

I also found a code sample that works for non interleaving:

 

for (unsigned int i = 0; i < 4 ; i++) 
{
glEnableVertexAttribArray(WVP_LOCATION + i);
glVertexAttribPointer(WVP_LOCATION + i, 4, GL_FLOAT, GL_FALSE, sizeof(Matrix4f), (const GLvoid*)(sizeof(GLfloat) * i * 4));
glVertexAttribDivisor(WVP_LOCATION + i, 1);
}

 

 

 

As a side note, I am applying my ModelView to a GL_POINT which then gets passed into a Geometry shader, if I apply a rotation to that GL_POINT will it be reflected on the textured triangle strip that comes out the other side.

 

 

Edit:

 

Just my luck, I spend hours searching for an answer, decide to post and find it within 5 minutes after posting...

 

I just need to add the offset of 36 to the code above.

Edited by harshman_chris

Share this post


Link to post
Share on other sites

I have never tried it, but I think you can define an attribute in your shader as usual and give it a matrix type. The attribute location of each column of the matrix are numbered consecutively.

 

In the shader:

in mat4 myMatrix;

 

In the code:

GLint loc = glGetAttribLocation(program, "myMatrix");
 
glVertexAttribPointer(loc+0, ...); // first column
glVertexAttribPointer(loc+1, ...); // second column
glVertexAttribPointer(loc+2, ...); // and so on...
glVertexAttribPointer(loc+3, ...);

 

Never tried it though, so it may be wrong.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this