Sign in to follow this  
mwadelin

Selecting a part of texture without editing UVs

Recommended Posts

Hi everyone.

I'm trying to render part of texture onto a surface, for example say I have a texture 200 x 600 and I only want the top half of the texture rendered to a surface.
The texture will have text on it and the idea is that you can scroll the text up and down. Heres the tricky part, I'm trying to do it without editing the UVs of the vertices and using the standard D3D pipeline.
Is this possible or will I need to either edit the UVs or use a custom shader that takes parameters to tell it how much of the texture to use?

Any help much appreciated.

Thanks.

Share this post


Link to post
Share on other sites
You can translate (move), rotate, and scale uv coords just like you can objects. So, if you have the texture coords of (using directx) (0, 0) top left, (1, 0) top right, (0, 1) bottom left, and (1, 1) as the bottom right. Then, to get the top half of the image without modifying the actual UVs you can multiply all of the UV coords by (1, .5). Do the math, you should get (0, 0), (1, 0), (0, .5), and (1, .5). This would only select the top half of the image. You can move too by adding onto it. If you want to only view the center of the image, but at half the size, you can mutliple everything by (.5, .5). Hopefully, you will get the idea. Try out simple at first . . .

Share this post


Link to post
Share on other sites
With IDirect3DDevice9::SetTransform you can set texture coordinate transforms.

You also need to adjust the texture stage state transform flags so that the transforms are actually applied.

Do note that if you have a 2D texture, the part of the transform that gets used is the top left 3x3 portion of the matrix, not the full 4x4. The D3DX matrix functions, therefore, may not function as expected when transforming 2D texture coordinates. It is relatively easy to construct your own matrices, though.

All that said, with a vertex shader this would be a lot simpler.

Share this post


Link to post
Share on other sites
[quote name='Nik02' timestamp='1307272017' post='4819706']
With IDirect3DDevice9::SetTransform you can set texture coordinate transforms.

You also need to adjust the texture stage state transform flags so that the transforms are actually applied.

Do note that if you have a 2D texture, the part of the transform that gets used is the top left 3x3 portion of the matrix, not the full 4x4. The D3DX matrix functions, therefore, may not function as expected when transforming 2D texture coordinates. It is relatively easy to construct your own matrices, though.

All that said, with a vertex shader this would be a lot simpler.
[/quote]

Cheers for the help guys. After much fiddling I've decided that Nik you're indeed right, alot easier doing a shader so thats what I've done, problem solved! That said I would have though the API would provide a 3x3 matrix struct.

Share this post


Link to post
Share on other sites
Matrix sizes other than 4x4 are special cases in the D3D world. The D3DX helper functions reflect this fact.

It is worth noting that the DX APIs must also work in raw C (in addition to C++); hence, it is not so easy to make more "generic" public functions without sacrificing maintenance ease and usage simplicity.

Vertex shaders are a good tool to learn, since newer versions of D3D (10 and beyond) do not support SetTransform and related fixed-function vertex processing at all.

Share this post


Link to post
Share on other sites
[quote name='Nik02' timestamp='1307438511' post='4820442']
Matrix sizes other than 4x4 are special cases in the D3D world.[/quote]

I gather you mean Special as in 'A Royal Pain In The Behind'? To this day I still look back in horror to coaxing and coercing the fixed function pipeline :rolleyes:

[size="1"]Sorry about this utterly useless post, but I just had to respond to Nik while I'm here. Nice to see you're still around =)[/size]

Share this post


Link to post
Share on other sites
I got very used to fixed function stuff during the D3D6-D3D9 era, but I rarely mess about with that nowadays (D3D11 rules!).

[size="1"]Rim, I was buried with work for a while, but I try to become active in gdnet again. I missed you guys :cool:
[/size]

Share this post


Link to post
Share on other sites
I already knew how to do HLSL and using effects, I was just being lazy [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img] But now I've converted the app to D3D10 anyway so I had to use the shaders.

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