您好,登錄后才能下訂單哦!
霧的效果,這里只對(duì)牛頭的模型進(jìn)行了霧化,如下圖所示
距離模型越近,越清晰
1、線性霧
fragment shader中實(shí)現(xiàn)效果,
參數(shù)為眼睛到平面的距離,需要傳入霧的起始位置和終止位置
float calcFogLinear(float distance) { float fogAlpha = (distance - M_FogStart) / (M_FogEnd - M_FogStart); fogAlpha = 1.0 - clamp(fogAlpha, 0.0, 1.0); return fogAlpha; }
2、EXP霧
fragment shader中實(shí)現(xiàn),需要傳入霧的濃度
float calcFogExp(float distance) { float fogAlpha = exp(-(distance * M_FogDensity)); fogAlpha = clamp(fogAlpha, 0.0, 1.0); return fogAlpha; }
3、EXPX霧
fragment shader中實(shí)現(xiàn),需要出入霧的濃度和梯度
float calcFogExpX(float distance) { float fogAlpha = exp(-pow(distance * M_FogDensity, M_FogGradiant)); fogAlpha = clamp(fogAlpha, 0.0, 1.0); return fogAlpha; }
眼睛的位置計(jì)算
M_EyesPos = V * wordPos;
fragment shader
// 計(jì)算霧的強(qiáng)度 // 線性計(jì)算 //float fogAlpha = calcFogLinear(abs(M_EyesPos.z / M_EyesPos.w)); // EXP方式計(jì)算 //float fogAlpha = calcFogExp(abs(M_EyesPos.z / M_EyesPos.w)); // EXPX方式計(jì)算 float fogAlpha = calcFogExpX(abs(M_EyesPos.z / M_EyesPos.w)); vec4 lightColor = texture2D(U_MainTexture, M_coord) * ambientColor + texture2D(U_MainTexture, M_coord) * diffuseColor;// + specularColor; gl_FragColor = mix(M_FogColor, lightColor, fogAlpha);
CPU端霧的設(shè)置
// 設(shè)置霧 float nFogStart = 1.0f; float nFogEnd = 500.0f; float nFogColor[] = {41.0f / 255.0f, 71.0f / 255.0f, 121.0f / 255.0f, 1.0f}; float nFogDensity = 0.008f; // 霧的強(qiáng)度 float nFogGradiant = 2.0f; // 霧的衰減梯度 OpenGLCore->glUniform1f(m_Mode2->getLocationHandle("M_FogStart"), nFogStart); OpenGLCore->glUniform1f(m_Mode2->getLocationHandle("M_FogEnd"), nFogEnd); OpenGLCore->glUniform4fv(m_Mode2->getLocationHandle("M_FogColor"), 1, nFogColor); OpenGLCore->glUniform1f(m_Mode2->getLocationHandle("M_FogDensity"), nFogDensity); OpenGLCore->glUniform1f(m_Mode2->getLocationHandle("M_FogGradiant"), nFogGradiant);
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。