Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Help needed with Math of setting gl_TextureMatrix

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I would like to setup 4 different texture matricies to various texture coordinates. e.g. 1st Matrix I would like to have 0,0; .5,0; .5,.5; 0,.5; as my texture coordinates so I can access the lower left of my texture unit... Thanks for any help BTW I am using GLSL...

Share this post


Link to post
Share on other sites
Advertisement
As you've posted here I'll reply here instead of via email [smile]

OK, as I mentioned in the email, the theory is pretty much the same as when you make a remap matrix for shadow mapping.
In the case of shadow mapping you want to divide by 2 (scale by .5) and then add 0.5 (translate by .5).

For for the first texture coords to access the bottom left of the texture, you supply a matrix which simply scales by 0.5.
To get to the other corners the same priniple applies, scale by 0.5 and then translate it on the correct axis so that its in the right range for that corner.

You should be able to work the numbers out for yourself as its not really that hard [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
As you've posted here I'll reply here instead of via email [smile]

OK, as I mentioned in the email, the theory is pretty much the same as when you make a remap matrix for shadow mapping.
In the case of shadow mapping you want to divide by 2 (scale by .5) and then add 0.5 (translate by .5).

For for the first texture coords to access the bottom left of the texture, you supply a matrix which simply scales by 0.5.
To get to the other corners the same priniple applies, scale by 0.5 and then translate it on the correct axis so that its in the right range for that corner.

You should be able to work the numbers out for yourself as its not really that hard [smile]


What I was thinking on doing is having 4 different Texture matricies so I can just upload the matrix as I want it on the CPU side and just call one of the four matricies I need in GLSL... Or are you saying just modify the texture matrix in the vertex program everytime?

Share this post


Link to post
Share on other sites
Well,it really depends on how flexible you want it to be. Are you going to need to change the values on the fly all that much? or will you always want to pull the same 4 values from the same for corners of the texture? If so, you might as well just put the maths inline in the shader and be done with it.


// assuming coord is a vec2 with your texture coords in
vec4 color1 = texture2D(tex,coord*vec2(0.5,0.5)); // bottom left
vec4 color2 = texture2D(tex,coord*vec2(0.5,0.5)+vec2(0.5,0.0)) // top left
// etc


Ofcourse, if you want to change the range and/or part of the texture you want to pull the image data from a matrix via a uniform would be the best solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
Well,it really depends on how flexible you want it to be. Are you going to need to change the values on the fly all that much? or will you always want to pull the same 4 values from the same for corners of the texture? If so, you might as well just put the maths inline in the shader and be done with it.


// assuming coord is a vec2 with your texture coords in
vec4 color1 = texture2D(tex,coord*vec2(0.5,0.5)); // bottom left
vec4 color2 = texture2D(tex,coord*vec2(0.5,0.5)+vec2(0.5,0.0)) // top left
// etc


Ofcourse, if you want to change the range and/or part of the texture you want to pull the image data from a matrix via a uniform would be the best solution.


Ok here is what I got and it works. I am looking to access 4 corners right now, but might move to 16.


mat4 upperRightTexMatrix = mat4(vec4(.5, .0, .0, .0),
vec4(.0, .5, .0, .0),
vec4(.0, .0, .5, .0),
vec4(.5, .5, .5, .5));
mat4 upperLeftTexMatrix = mat4(vec4(.5, .0, .0, .0),
vec4(.0, .5, .0, .0),
vec4(.0, .0, .5, .0),
vec4(.0, .5, .0, .0));
mat4 lowerLeftTexMatrix = mat4(vec4(.5, .0, .0, .0),
vec4(.0, .5, .0, .0),
vec4(.0, .0, .5, .0),
vec4(.0, .0, .0, .0));
mat4 lowerRightTexMatrix = mat4(vec4(.5, .0, .0, .0),
vec4(.0, .5, .0, .0),
vec4(.0, .0, .5, .0),
vec4(.5, .0, .0, .5));




I don't plan on changing the matricies, but wouldn't it seem faster to compute this on the CPU side and store them in gl_TextureMatrix[1] though gl_TextureMatrix[4]? that way all I have to do in the shader is multiply the matrix by the texture coordinates? :) I am looking from a stand point of which way is faster. I can see if I was to change them alot, putting a if/else in the vertex program and uploading some type to determine the output, but as of now I don't need that. Now here is the kicker how do I tile it?? I tried to multiply it by 16 or insert amount here and that blows up the code...

Share this post


Link to post
Share on other sites
If you dont change the matrix then the faster method is just to inline the maths in the code (like I did) and leave it at that.

Although over all thats the method which requires more work to change, but as you said you dont plan on changing it then I dont see the point in going the matrix route as you are doing more maths than you really need todo.

Share this post


Link to post
Share on other sites
Hey Phantom, I re-read your post and it says if I want the range out of 0-1 I might need to use matricies? I am hoping to not have to do that now that I have my code setup to do the 0-1 range... I have tried the math and can't seem to figure out how to come up with any good solutions. I am trying to tile the texture 16x. Thanks

Share this post


Link to post
Share on other sites
errm... i've no idea where I said that, mainly because I'm sure I didnt :)

when you say 'tile the texture 16 times' do you mean 16 time across the whole object?
Atm I cant think of a way to allow you todo what you want...

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
errm... i've no idea where I said that, mainly because I'm sure I didnt :)

when you say 'tile the texture 16 times' do you mean 16 time across the whole object?
Atm I cant think of a way to allow you todo what you want...


Yeah, I would like to tile the texture I grab out of the larger one using the texture coordinates and tile that one 16x...

I can't either but it seems so simple that it should be doable. I guess I can't understand why I am not allowed to or limited to not being able to do this???

Share this post


Link to post
Share on other sites
the problem is that you are trying to map an arbatry range into a smaller repeating range.

So, some how 0->1 needs to be worked out for each block of floats between each interger (0->1, 1->2, 2->3 etc all need to be initally mapped into 0->1).. there probably is a way, but atm I'll be buggered if I can see it

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!