Jump to content

  • Log In with Google      Sign In   
  • Create Account


Passing a Matrix4 to a Vertex Attribute [Solved]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 harshman_chris   Members   -  Reputation: 184

Like
0Likes
Like

Posted 05 April 2013 - 07:57 AM

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, 05 April 2013 - 08:07 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7928

Like
0Likes
Like

Posted 05 April 2013 - 08:13 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS