# Trying todo water reflection, with render texture and plane

This topic is 4815 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

Greetings, ive been "lurking" alot on gamedev.net forums, but always managed to find anwsers "on my own", however not this time :( Im trying to setup water reflection via a render to texture and plane. I have no problem with the render to texture, however im having problems with the projection, i believe i must not understand at all.. this and this is what im getting, quite odd to me. In addition there dosent to be alot on D3DTTFF_PROJECTED. Ive searched these forums, however it confused me more since most threads were saying diferent things. So here i am, with a few questions : 1) Since im using D3DTTFF_PROJECTED, should my vertex fvf be ---- D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 ---- OR ---- D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE3(0) ---- Im aware it will use 3d tex cord todo the proj, but im passing 2d ones, so im confused. 2) Texture matrix are 3x3 correct? 3) If yes @ 3, prior to using D3DTTFF_PROJECTED and my texture matrix, should i "_3x = _4x" the projection matrix, or even others? 4) Ive seen D3DTSS_TCI_CAMERASPACEPOSITION | 1 in examples, what does the 1 stand for? 5) Whould it be posible to use only a texture matrix and D3DTTFF_PROJECTED without using D3DTSS_TCI_CAMERASPACEPOSITION? 6) If yes @ 6, would the proper mat be : mod * invView * proj, all with 3x3 matrix? 7) It is normal i must clamp or im getting somewhat-titled results? 8) Lasty any clue whats happening in the screenshots? I tryed alot of "combo" these last screenshots were with this :
		m_matWater = D3DXMATRIXA16(1.12f, 0.0f,  0.0f, 0.0f,
0.0f,  1.12f, 0.0f, 0.0f,
0.5f,  0.5f,  1.0f, 0.0f,
0.0f,  0.0f,  0.0f, 1.0f );//last row not used right?

//...some alpha state, i tested without thats not the prob.

m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU,	D3DTADDRESS_CLAMP);
m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV,	D3DTADDRESS_CLAMP);

if (FAILED(m_pd3dDevice->SetTransform(D3DTS_TEXTURE0, &m_matWater)))	{ //...

m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 | D3DTTFF_PROJECTED);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);

//..set vb/ib/texture/draw


*shy* i dont really know why i use this m_matWater matrix i got some example from a thread, and came up with 1.12f with trial and error (to get rid of some others issues i had) however its clear its not correct, because a offset appears (i "cheated" for the screenshots above, placing myself where it is "ok") Im pretty stuck with this issue, so any tips would help. Thanks.

#### Share this post

##### Share on other sites
I'm not sure what's going on in the screenshots. If you could somehow narrow down where the artifacts are coming from, or what they are dependant on, it would help.

#### Share this post

##### Share on other sites
I don't know much about projected textures or if this applies... one common mistake when rendering reflections is forgetting to reverse the winding order.

#### Share this post

##### Share on other sites
I haven't done much with projected coords, but I'll try to answer.

1) If you're using texture coordinate generation (CAMERASPACENORMAL), you don't need TEX1, or need to set the size of the coords. TEX1 says you have 1 set of UVs. TEXCOORDSIZE3(0) says UV set 0 is 3 floats long. For that your vertex data would have to be x,y,z,nx,ny,nz,u,v,w. Since you're generating your UVs, you can just use position and normal. If you're not lighting, just use position.

2) If your texture transform is COUNT2 the matrix is 3x3. If your texture transform is COUNT3 the matrix is 4x4.

3) You'll want COUNT3 | PROJECTED and use standard transform matrices. ie: With COUNT2 you use _31 and _32 as translation. With COUNT3 that's what _41, _42, and _43 are for, like every other D3D matrix.

4) As the SDK says, when using any texture generation flags, "With the exception of D3DTSS_TCI_PASSTHRU, which resolves to zero, if any of the following values is included with the index being set, the system uses the index strictly to determine texture wrapping mode. These flags are most useful when performing environment mapping." So, you're saying I want the coord generated, but I want them to wrap as if they were UV set 1.

5) Yes, but you'd need to use TEX1 and TEXCOORDSIZE(3), and put u,v,w in your vertices. It take less space to just autogenerate them. COUNT3 just means you're supplying a 3D coordinate. PROJECTED just means divide the coords by W. Taking position, transforming, then dividing by W is exactly what the graphic card does take 3D data and put in on your 2D window. You're just instructing D3D that you'd like to do this for a texture coordinate.

6) I'm not sure what the proper matrix should be... like I said, I haven't done much projected texture work.

7) You can clamp, or border (and set border to transparent). Yes, this is expected. Imagine you take a camera, take a picture. Now turn your camera a bit, and try to use your previous image as a reflection. Anything that was outside your camera's view when you took the picture will just wrap around.

7a) You might need to scale and bias the resulting coordinates... Valid set of coordinates might be -1..1. So after projection you want to *0.5 and +0.5. This is the standard matrix used for spherical env mapping, which turns the normals from -1..1 to 0..1

8) No idea.

#### Share this post

##### Share on other sites

This topic is 4815 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.

## 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

• ### Forum Statistics

• Total Topics
628638
• Total Posts
2983970

• 10
• 18
• 20
• 13
• 9