i use D3DXCreateSphere
method to create sphere mesh. and i want to apply an earth texture on it. i calculate the texture coordinate in pixel shader with following code:
sampler ShadeSampler = sampler_state
{
Texture = (ShadeTex);
AddressU = Mirror;
AddressV = Mirror;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};
PS_OUTPUT PSMain(VS_OUTPUT input){
PS_OUTPUT output = (PS_OUTPUT)0;
vector uv;
uv.x = 0.5 + atan2(input.normal.z, input.normal.x) / piMul2;
uv.y = 0.5f - asin(input.normal.y) / pi;
vector b = tex2D(ShadeSampler, uv.xy);
output.diffuse = b * input.diffuse;
return output;
}
i use D3DXCreateTextureFromFileEx
method to create the IDirect3DTexture9
, but when i enable the mipmap capability by using D3DX_DEFAULT
value for the MipLevels
parameter, the render result goes a little wrong that there is always one pixel line at the seam. the picture below shows the problem, but when i disable the mipmap capability by using D3DX_FROM_FILE
value for the MipLevels
parameter, the line disappears. i don't know why this would happen, any advice?
I had this problem before and it is caused, as you found out by the mipmapping. You're remapping the texture coordinates and at this line there is the seam from u = 1 to 0. This big jump is detected by the mipmapping algorithm and a very low mipmap is chosen, which causes the pixels to have a wrong color. The seam have a thickness of 2 pixels, because mipmapping is determined with the gradient function ddx and ddy of a 2x2 pixelblocks. When the seam is exactly in this block the error occurs.
I solved it by disabling the mipmapping for this geometry, but if you need it you could use the method tex2Dgrad
(doc) to repair the gradients at this seam or use tex2Dlod
(doc) in the shader and determine the miplevel by yourself.
To me this seems to be a matter of texture wrapping. Have you tried the various options of the D3DRS_WRAPx render state? For example,
IDirect3DDevice9::SetRenderState(D3DRS_WRAP0, D3DWRAP_U);
来源:https://stackoverflow.com/questions/19723698/sphere-texture-mapping-error