簡體   English   中英

在天穹上使用點光源的奇怪照明效果

[英]Weird lighting effect using a point light on a skydome

我的照明設備有問題。 當光線越過頂點或直線時,照明會呈現出怪異的外觀。 (應該是圓的。)我一直在嘗試各種事情,但我不知道為什么會這樣。

看起來很正常的怪異

我創建一個天穹並將其歸一化

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

您無需考慮面孔即可在頂點獲得法線。 對,它只是一個半球,所以法線只是從球心(我猜是原點)到頂點的向量,但是您應該將其規格化為1的長度。因此,頂點的法線就是其坐標已標准化。

傳統的OpenGL固定功能管線照明僅在頂點處進行計算,並在其間進行插值。 根據網格的拓撲,您可能會因為OpenGL在頂點之間進行插值而導致柵格化拓撲不連續。

解決方案:使用在片段着色器中實現的每個片段照明。

順便說一句:為什么首先要對天穹應用照明?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM