本文介绍了球体贴图错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 D3DXCreateSphere 方法创建球体网格。我想在上面涂上地球纹理。我使用以下代码在像素着色器中计算纹理坐标:

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;
}

我使用 D3DXCreateTextureFromFileEx 创建 IDirect3DTexture9 的方法,但是当我通过对 D3DX_DEFAULT 值来启用mipmap功能时> MipLevels 参数,则渲染结果会出现一些错误,因为接缝处始终有一个像素行。下图显示了问题,但是当我通过对 MipLevels 参数使用 D3DX_FROM_FILE 值禁用mipmap功能时,线消失了。我不知道为什么会发生这种情况,有什么建议吗?

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?

推荐答案

我以前遇到过这个问题,它是由mipmapping发现的。您正在重新映射纹理坐标,并且在此行处有从u = 1到0的接缝。mipmapping算法检测到此较大的跳变,并且选择了非常低的mipmap,这会导致像素颜色错误。接缝的厚度为2像素,因为mipmapping是由2x2像素块的梯度函数ddx和ddy确定的。当接缝恰好在此块中时,就会发生错误。

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.

我通过禁用此几何图形的mipmapping解决了该问题,但是如果需要,可以使用方法 tex2Dgrad ()以修复此接缝处的渐变,或使用 tex2Dlod (),然后自行确定Miplevel。

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.

这篇关于球体贴图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 17:17