溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于Qt的OpenGL可編程管線學(xué)習(xí)(8)- 探照燈

發(fā)布時間:2020-06-18 21:34:48 來源:網(wǎng)絡(luò) 閱讀:502 作者:Douzhq 欄目:編程語言

關(guān)于探照燈的效果如下圖所示:

基于Qt的OpenGL可編程管線學(xué)習(xí)(8)- 探照燈

基于Qt的OpenGL可編程管線學(xué)習(xí)(8)- 探照燈


探照燈需要傳入光源的位置,光源的方向以及夾角的大?。▕A角為光源覆蓋的夾角的一半)

計算思路:

用光源到點的距離與光源的方向的單位向量做點乘,得到夾角的cos,用計算的夾角cos與

傳入的角度的cos做比較,確定光線照射的范圍。邊緣不部分計算的cos做基底,然后給定一個冪,

就可以做到漸變的效果;探照燈的計算時也要算上衰減


Shader中的相關(guān)代碼如下:

vec3 light = M_LightPos.xyz;
float distanceLight = 0.0;      // 距離光源的距離
float attenuation = 1.0;         // 衰減系數(shù)

// 衰減因子
float constantFactor = 0.9;           // 常亮衰減常數(shù)
float linerFactor = 0.0;                 //  線性衰減系數(shù)
float expFactor = 0.0;                  //  平方衰減系數(shù)
// 點光源
if (M_LightPos.w != 0.0)
{
    light = M_LightPos.xyz - M_WordPos;
    distanceLight = length(light);
    attenuation = 1.0 / (constantFactor + linerFactor * distanceLight + expFactor * distanceLight * distanceLight);
}
vec3 LightNormal = normalize(light);       // 指向光源的單位向量
vec3 NormalNormal = normalize(M_normal);      //  法線的單位向量

light = M_LightPos.xyz - M_WordPos;
vec3 spotLightPointDirection = normalize(-light);
vec3 spotLightDirection = normalize(M_SpotLightDirection.xyz);
float spotDegreeCos = dot(spotLightPointDirection, spotLightDirection);
float spotCutoffCos = cos(M_SpotLightCutoff * 3.14 / 180.0);
float diffuseIntensity = 0.0;
if (M_SpotLightDirection.w > 0 && M_SpotLightCutoff > 0)
{
    if (spotDegreeCos > spotCutoffCos)
    {
        diffuseIntensity = pow(max(0.0, spotDegreeCos), M_SpotLightDirection.w) * 2;
    }
}
else
{
    diffuseIntensity = max(0.0, dot(NormalNormal, LightNormal));
}
vec4 diffuseColor = M_DiffuseLightColor * M_DiffuseMaterial * 
                    diffuseIntensity * attenuation;


CPU中的設(shè)置

// 光源位置
float nLightPos[4] = {0.0f, 0.0f, -2.0f, 1.0f};
OpenGLCore->glUniform4fv(m_LightPos, 1, nLightPos);
// 探照燈方向及角度
float nSpotLightDirection[4] = {0.0f, 0.0f, -1.0f, 128.0f};
OpenGLCore->glUniform4fv(m_SpotLightDirection, 1, nSpotLightDirection);
float nSpotLightDegree = 30.0f;
OpenGLCore->glUniform1f(m_SpotLightCutoff, nSpotLightDegree);


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI