c++ - 在天穹上使用点光源的奇怪照明效果

标签 c++ opengl directx shader lighting

我的灯光有问题。当光线越过顶点或线时,光线会呈现出这种怪异的外观。 (它应该是圆形的。)我一直在尝试各种方法,但我找不到为什么会这样。

Weird looking normals

我创建了一个天穹并标准化了它的位置

double deltaLatitude = XM_PI / segments;
double deltaLongitude = XM_PI * 2.0 / segments;
int index = 0;
D3DXCOLOR bottomColor = D3DXCOLOR(0.529f, 0.807f, 0.821f, 1.0f);
D3DXCOLOR topColor = D3DXCOLOR(0.179f, 0.557f, 1.0f, 1.0f);

for (int i = 1; i < segments; i++)
{
    double r0 = sin(i * deltaLatitude);
    double y0 = cos(i * deltaLatitude);

    for (int j = 0; j < segments; j++)
    {
        double x0 = r0 * sin(j * deltaLongitude);
        double z0 = r0 * cos(j * deltaLongitude);

        D3DXVECTOR3 v = D3DXVECTOR3(x0, y0, z0);//create normal based on possition (-1 <> 1)
        D3DXVec3Normalize(&v, &v);//normalize
        vertPos.push_back(Vertex(x0, y0, z0, lerp(bottomColor, topColor, y0), v));
    }
}

vertPos.push_back(Vertex(0, 1, 0, lerp(bottomColor, topColor, 1), D3DXVECTOR3(0.0f, 1.0f, 0.0f)));
vertPos.push_back(Vertex(0, -1, 0, lerp(bottomColor, topColor, -1), D3DXVECTOR3(0.0f, -1.0f, 0.0f)));

for (int i = 0; i < segments - 2; i++)
{
    for (int j = 0; j < segments; j++)
    {
        indices.push_back(segments * i + j);
        indices.push_back(segments * i + (j + 1) % segments);
        indices.push_back(segments * (i + 1) + (j + 1) % segments);
        indices.push_back(segments * i + j);
        indices.push_back(segments * (i + 1) + (j + 1) % segments);
        indices.push_back(segments * (i + 1) + j);
    }
}

// create the faces of the top of the dome
for (int i = 0; i < segments; i++)
{
    indices.push_back(segments * (segments - 1));
    indices.push_back((i + 1) % segments);
    indices.push_back(i);
}

// create the faces of the bottom of the dome
for (int i = 0; i < segments; i++)
{
    indices.push_back(segments * (segments - 1) + 1);
    indices.push_back(segments * (segments - 2) + i);
    indices.push_back(segments * (segments - 2) + (i + 1) % segments);
}

顶点着色器:

VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD, float4 inColor : COLOR, float3 normal : NORMAL)
{
    VS_OUTPUT output;
    output.Pos = mul(inPos, WVP);

    output.worldPos = mul(inPos, World);
    output.normal = mul(normal, World);

    output.Color = inColor;

    return output;
}

这是像素着色器:

float4 PS(VS_OUTPUT input) : SV_TARGET
{
    input.normal = normalize(input.normal);

    float4 diffuse = input.Color;
    float skyColor = light.spos.a;
    float4 sunColor = float4(500.0f, 100.0f, 100.0f, 1.0f);

    float3 finalColor = float3(0.0f, 0.0f, 0.0f);
    float3 finalAmbient = diffuse * skyColor;

    float sunRange = 1000.0f;

    float3 lightToPixelVec = light.spos - input.worldPos;

    float d = length(lightToPixelVec);

    if( d > sunRange ){
        return float4(finalAmbient, diffuse.a);
    }

    //Turn lightToPixelVec into a unit length vector describing
    //the pixels direction from the lights position
    lightToPixelVec /= d;

    //Calculate how much light the pixel gets by the angle
    //in which the light strikes the pixels surface
    float howMuchLight = dot(lightToPixelVec, input.normal);

    //If light is striking the front side of the pixel
    if( howMuchLight > 0.0f )
    {   
        //Add light to the finalColor of the pixel
        finalColor += howMuchLight * finalAmbient * sunColor;

        //Calculate Light's Falloff factor
        finalColor /= 10.0f + (0.01 * d) + (0.01 * (d*d));
    }   
    finalColor = saturate(finalColor + finalAmbient);

    return float4(finalColor, diffuse.a);
}

最佳答案

您无需考虑面即可获得顶点处的法线。它只是一个半球,对吧,所以法线只是从球心(我猜是原点)到顶点的 vector ,但是你应该将它归一化为 1 的长度。所以顶点的法线很简单它的坐标标准化。

关于c++ - 在天穹上使用点光源的奇怪照明效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26194049/

相关文章:

c++ - 仅当使用 opengl 3.2 上下文或更高版本时,glVertexAttribPointer 和 glDrawArrays 的 GL_ERROR == 1282

c++ - 将枚举定义为带有 or 的标志

c++ - 线程 : Fine with 10, 崩溃 10000

opengl - 每片段光照坐标系

C++ 编译器抛出 GLUT 错误

image-processing - 外发光作为 HLSL 着色器

c++ - DirectX 11 CreateSwapChain() 失败并出现错误 DXGI_ERROR_INVALID_CALL

c++ - 在 D3D12 上获取显示器的刷新率

c++ - 错误 LNK2019 : unresolved external symbol

java - LWJGL OpenGL Sprite 表