texture coordinates mess up
Hello. First post.
I am writing a game engine in DX 9. I have had, for a long time, a strange problem where texture coordinates mess up when the camera gets close and views from an angle.
The engine currently uses an effect file to render, but I am fairly sure the problem also existed before this, when I was using the fixed function pipeline.
The problem exists not only when using textures, the lighting (such as which areas are lit the most by a point light) also messes up in the same way, even when textures are not used at all.
The image on the left shows how it is meant to be, the right is what happens when one moves the camera close to the object. The object shown is a box generate by DirectX, but the problem also occurs when using meshes loaded in.
I read somewhere this might have something to do with perspective correction.
Thank you in advance for any suggestions.
EDIT: does bbcode not work here? Anyway, direct link to image:
http://img505.imageshack.us/img505/5202/probleme.jpg
Without more information (like a code sample from your renderer), I'm not sure where anyone can help. The only thing I can suggest is that if the texture coordinates and the lighting is off then that most likely points to something in your coordinate system isnt translating correctly. It might be one of your transformations is incorrect somewhere, but thats a shot in the dark without a code sample.
Hi,
I'm not sure but I think it's;
a-) a texture filtering problem (look at your sampler state's MinFilter, MagFilter, MipFilter parameters).
b-) or a FOV of projection problem. Values greater than 60 degrees of FOV angle may cause some image distortion.
Hope this helps.
I'm not sure but I think it's;
a-) a texture filtering problem (look at your sampler state's MinFilter, MagFilter, MipFilter parameters).
b-) or a FOV of projection problem. Values greater than 60 degrees of FOV angle may cause some image distortion.
Hope this helps.
Ok here is my vertex shader:
Where the matrices are:
structs:
The samplers all look like this:
The matrices are set like this:
objects are then rendered like this:
The projection matrix is set with this:
where float FOV defaults to 45 (which is what I use)
[Edited by - JezzaP on May 29, 2009 1:06:18 AM]
vertexOutput std_VS(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.WorldNormal = normalize(mul(IN.Normal,WorldITXf).xyz); if(useNorm){ OUT.WorldTangent = normalize(mul(IN.Tangent,WorldITXf).xyz); OUT.WorldBinormal = normalize(mul(IN.Binormal,WorldITXf).xyz); } float4 Po = float4(IN.Position.xyz,1); float3 Pw = mul(Po,WorldXf).xyz; if(1==lightType){ OUT.LightVec = lightDir - Pw; }else OUT.LightVec = lightDir; if(flipy){ if(flipx) OUT.UV = float2((1.0-IN.UV.x),(1.0-IN.UV.y)); else OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y)); } else{ if(flipx) OUT.UV = float2((1.0-IN.UV.x),IN.UV.y); else OUT.UV = float2(IN.UV.x,IN.UV.y); } OUT.WorldView = normalize(ViewIXf[3].xyz - Pw); OUT.HPosition = mul(Po,WvpXf); return OUT;}
Where the matrices are:
float4x4 WorldITXf : WorldInverseTranspose;float4x4 WvpXf : WorldViewProjection;float4x4 WorldXf : World;float4x4 Projection : Projection;float4x4 ViewIXf : ViewInverse;float4x4 WorldIXf : WorldInverse;
structs:
/* data from application vertex buffer */struct appdata { float3 Position : POSITION; float4 UV : TEXCOORD0; float4 Normal : NORMAL; float4 Tangent : TANGENT; float4 Binormal : BINORMAL;};/* data passed from vertex shader to pixel shader */struct vertexOutput { float4 HPosition : POSITION; float2 UV : TEXCOORD0; float3 WorldNormal : TEXCOORD2; float3 WorldTangent : TEXCOORD3; float3 WorldBinormal : TEXCOORD4; float3 WorldView : TEXCOORD1; float3 LightVec : TEXCOORD5; float3 Attenuation : TEXCOORD6;};
The samplers all look like this:
sampler ColorSampler = sampler_state { texture = <ColorTexture> ; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = WRAP; AddressV = WRAP; AddressW = WRAP; };
The matrices are set like this:
sys->Device->GetTransform(D3DTS_VIEW, &matView); sys->Device->GetTransform(D3DTS_PROJECTION, &matProjection); D3DXMatrixInverse(&matInverseView, NULL, &matView); matWorld = new D3DXMATRIX[numObjects]; matWorldViewProjection = new D3DXMATRIX[numObjects]; matWorldView = new D3DXMATRIX[numObjects]; matWorldInverseTransponse = new D3DXMATRIX[numObjects]; matWorldInverse = new D3DXMATRIX[numObjects]; phong.Shader->SetMatrix("ViewIXf", &matInverseView); for(int i2=0; i2<numObjects; i2++){ matWorld[i2]=objects[i2].pose; D3DXMatrixMultiply(&matWorldView[i2], &matWorld[i2], &matView); D3DXMatrixMultiply(&matWorldViewProjection[i2], &matWorldView[i2], &matProjection); D3DXMatrixInverse(&matWorldInverse[i2], NULL, &matWorld[i2]); D3DXMatrixTranspose(&matWorldInverseTransponse[i2], &matWorldInverse[i2]); }
objects are then rendered like this:
for(int i2=0; i2<numObjects; i2++){ //set variables for shader phong.Shader->SetMatrix("WorldXf", &matWorld[i2]); phong.Shader->SetMatrix("WorldITXf", &matWorldInverseTransponse[i2]); phong.Shader->SetMatrix("WvpXf", &matWorldViewProjection[i2]); for(DWORD i = 0; i < objects[i2].numMaterials; i++) { if(objects[i2].diffuse!=NULL) phong.Shader->SetTexture("ColorTexture", objects[i2].diffuse); if(objects[i2].specular!=NULL) phong.Shader->SetTexture("SpecTexture", objects[i2].specular); if(objects[i2].normal!=NULL) phong.Shader->SetTexture("NormTexture", objects[i2].normal); phong.Shader->CommitChanges(); objects[i2].DXObject->DrawSubset(i); } }
The projection matrix is set with this:
void PD_RENDERER::SetFOV(float FOV){ D3DXMATRIX matProjection; D3DXMatrixPerspectiveFovLH(&matProjection, pdDegree2Rad(FOV), (FLOAT)ScreenWidth / (FLOAT)ScreenHeight, 0.1f, 500.0f ); matProjection(2,2) = 1.0f; matProjection(3,2) = -1.0f; sys->Device->SetTransform(D3DTS_PROJECTION, &matProjection);}
where float FOV defaults to 45 (which is what I use)
[Edited by - JezzaP on May 29, 2009 1:06:18 AM]
Can anyone see the problem in this code? Or want to see more?
EDIT: How strange. I upgraded to the latest dx version (March, I was using November 08) and it works. I also changed all the shaders to model 2 instead of 3. One of those made it work. Ah well.
[Edited by - JezzaP on May 29, 2009 8:22:02 AM]
EDIT: How strange. I upgraded to the latest dx version (March, I was using November 08) and it works. I also changed all the shaders to model 2 instead of 3. One of those made it work. Ah well.
[Edited by - JezzaP on May 29, 2009 8:22:02 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement