您好,登錄后才能下訂單哦!
在上一篇文章中,我們介紹了簡單的Shading,同時提出了一個光照模型,模擬了一個點光源,但是,關于光的故事還沒有結束...
今天要學習的是方向光源(Directional Light),聚光燈,per pixel shading,halfway vector。
關于光源的原理及數學描述,請參考:光線追蹤(RayTracing)算法理論與實踐(三)光照
方向光源就兩個參數,方向和強度。
還是簡單的 ambient + diffuse + spec 光照模型。先看shader的代碼。
basic.vert
#version 400 layout (location = 0) in vec3 VertexPosition; layout (location = 1) in vec3 VertexNormal; out vec3 LightIntensity; struct LightInfo{ vec4 Direction; vec3 Intensity; }; struct MaterialInfo{ vec3 Ka; vec3 Kd; vec3 Ks; float Shininess; }; uniform LightInfo Light; uniform MaterialInfo Material; uniform mat4 ModelViewMatrix; uniform mat3 NormalMatrix; uniform mat4 ProjectionMatrix; uniform mat4 MVP; void getEyeSpace(out vec3 norm, out vec4 position) { norm = normalize(NormalMatrix * VertexNormal); position = ModelViewMatrix * vec4(VertexPosition, 1.0); } vec3 ads(vec4 position, vec3 norm) { vec3 s; if(Light.Direction.w == 0.0) s = normalize(vec3(Light.Direction)); else s = normalize(vec3(Light.Direction - position)); vec3 v = normalize(vec3(-position)); vec3 r = reflect(-s, norm); return Light.Intensity * (Material.Ka + Material.Kd*max(dot(s,norm), 0.0) + Material.Ks * pow(max(dot(r,v),0.0), Material.Shininess)); } void main() { vec3 eyeNorm; vec4 eyePosition; getEyeSpace(eyeNorm, eyePosition); LightIntensity = ads(eyePosition, eyeNorm); gl_Position = MVP * vec4( VertexPosition, 1.0); }
接下來的ads用來計算光照模型下頂點的顏色,分別計算三個分量,然后相加。
basic.frag
#version 400 in vec3 LightIntensity; void main(void) { gl_FragColor = vec4(LightIntensity, 1.0); //gl_FragColor = vec4(1.0,1.0,0.1, 1.0); }這個就是將根據頂點shader傳來的顏色對片段進行賦值。
void CGL::setUniform() { mat4 projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); mat4 mv = view * model; prog.setUniform("Material.Kd", 0.9f, 0.5f, 0.3f); prog.setUniform("Material.Ka", 0.9f, 0.5f, 0.3f); prog.setUniform("Material.Ks", 0.8f, 0.8f, 0.8f); prog.setUniform("Material.Shininess", 100.0f); prog.setUniform("Light.Direction", vec4(1.0f, 0.0f, 0.0f, 0.0f)); prog.setUniform("Light.Intensity", 1.0f, 1.0f, 1.0f); prog.setUniform("ModelViewMatrix", mv); prog.setUniform("NormalMatrix",mat3( vec3(mv[0]), vec3(mv[1]), vec3(mv[2]) )); prog.setUniform("MVP", projection * mv); }
在前面的光照模型中,用于計算specular分量的公式如下:
其中 r 是反射光線的方向向量,v是往視口方向的向量,其中 r 的計算:
這個計算過程會非常耗時,我們可以用一個trick來改善一下。
定義一個 h (halfway vector)向量:
下圖是 h 和其他向量的位置關系。
specular分量的計算就可以轉化成:
相比于計算 r ,h 的計算相對比較簡單,而 h 和 n 之間的夾角與 v 和 r 之間的夾角大小幾乎相同!那就意味著我們可以用 h.n 來代替 r.v 從而可以帶利用 halfway vector 來獲得性能上的一些提升。雖然效果上會有那么小小的不同。
后面的燈光的計算都會用到這個優化。這里的采用一個最簡單的聚光燈模型:
燈光的屬性有:位置,強度,方向,衰減(exponent),裁剪角度。
實現起來也比較簡單,在投射角內的物體,渲染方式和點光源的計算一樣,投射角之外的頂點,著色的時候只有ambient。
還是采用我們比較熟悉的per vertex shading 方式。在vert中定義聚光燈:
//baisc.vert #version 400 layout (location = 0) in vec3 VertexPosition; layout (location = 1) in vec3 VertexNormal; out vec3 LightIntensity; struct SpotLightInfo{ vec4 position; vec3 direction; vec3 intensity; float exponent; float cutoff; }; struct MaterialInfo{ vec3 Ka; vec3 Kd; vec3 Ks; float Shininess; }; uniform SpotLightInfo Spot; uniform MaterialInfo Material; uniform mat4 ModelViewMatrix; uniform mat3 NormalMatrix; uniform mat4 ProjectionMatrix; uniform mat4 MVP; void getEyeSpace(out vec3 norm, out vec4 position) { norm = normalize(NormalMatrix * VertexNormal); position = ModelViewMatrix * vec4(VertexPosition, 1.0); } vec3 adsSpotLight(vec4 position, vec3 norm) { vec3 s = normalize(vec3(Spot.position - position)); float angle = acos(dot(-s, normalize(Spot.direction))); float cutoff = radians(clamp(Spot.cutoff, 0.0, 90.0)); vec3 ambient = Spot.intensity * Material.Ka; if(angle < cutoff){ float spotFactor = pow(dot(-s, normalize(Spot.direction)), Spot.exponent); vec3 v = normalize(vec3(-position)); vec3 h = normalize(v + s); return ambient + spotFactor * Spot.intensity * (Material.Kd * max(dot(s, norm),0.0) + Material.Ks * pow(max(dot(h,norm), 0.0),Material.Shininess)); } else { return ambient; } } void main() { vec3 eyeNorm; vec4 eyePosition; getEyeSpace(eyeNorm, eyePosition); LightIntensity = adsSpotLight(eyePosition, eyeNorm); gl_Position = MVP * vec4( VertexPosition, 1.0); }
genType clamp( genType x, genType minVal, genType maxVal);
獲取三個數中第二大的數。
genType radians(genType degrees);
將角度轉換成弧度。
adsSpotLight是主要的函數,先計算頂點和光源方向之間的夾角,判斷頂點是否在照射的區域,然后分別求得最終的顏色。
片段shader還是那樣:
#version 400 in vec3 LightIntensity; void main(void) { gl_FragColor = vec4(LightIntensity, 1.0); }
void CGL::setUniform() { //model = glm::rotate(this->model, 10.0f, vec3(0.0f,1.0f,0.0f)); mat4 projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); mat4 mv = view * model; mat3 normalMatrix = mat3( vec3(view[0]), vec3(view[1]), vec3(view[2]) ); prog.setUniform("Material.Kd", 0.9f, 0.5f, 0.3f); prog.setUniform("Material.Ka", 0.9f, 0.5f, 0.3f); prog.setUniform("Material.Ks", 0.8f, 0.8f, 0.8f); prog.setUniform("Material.Shininess", 100.0f); vec4 lightPos = vec4(1.0f, 5.0f, 20.0f, 1.0f); // prog.setUniform("Spot.position", lightPos); prog.setUniform("Spot.position", view * lightPos); prog.setUniform("Spot.direction", normalMatrix * vec3(-10.0,0.0,-40.0) ); //prog.setUniform("Spot.direction", vec3(10.9f,10.9f,10.9f) ); prog.setUniform("Spot.intensity", vec3(0.9f,0.9f,0.9f) ); prog.setUniform("Spot.exponent", 30.0f ); prog.setUniform("Spot.cutoff", 15.0f ); prog.setUniform("ModelViewMatrix", mv); prog.setUniform("NormalMatrix",normalMatrix); prog.setUniform("MVP", projection * mv); }
最終效果如下:
相對與前面將主要計算工作放在頂點shader中的 per vertex shading ,per pixel shading 指的是將計算放到片段shader中,這樣可以帶來更加真實可感的渲染效果。
basic2.vert
#version 400 layout (location = 0) in vec3 VertexPosition; layout (location = 1) in vec3 VertexNormal; out vec4 Position; out vec3 Normal; uniform mat4 ModelViewMatrix; uniform mat3 NormalMatrix; uniform mat4 ProjectionMatrix; uniform mat4 MVP; void getEyeSpace(out vec3 norm, out vec4 position) { norm = normalize(NormalMatrix * VertexNormal); position = ModelViewMatrix * vec4(VertexPosition, 1.0); } void main() { getEyeSpace(Normal, Position); gl_Position = MVP * vec4( VertexPosition, 1.0); }
#version 400 in vec4 Position; in vec3 Normal; struct SpotLightInfo{ vec4 position; vec3 direction; vec3 intensity; float exponent; float cutoff; }; struct MaterialInfo{ vec3 Ka; vec3 Kd; vec3 Ks; float Shininess; }; uniform SpotLightInfo Spot; uniform MaterialInfo Material; vec3 adsSpotLight(vec4 position, vec3 norm) { vec3 s = normalize(vec3(Spot.position - position)); float angle = acos(dot(-s, normalize(Spot.direction))); float cutoff = radians(clamp(Spot.cutoff, 0.0, 90.0)); vec3 ambient = Spot.intensity * Material.Ka; if(angle < cutoff){ float spotFactor = pow(dot(-s, normalize(Spot.direction)), Spot.exponent); vec3 v = normalize(vec3(-position)); vec3 h = normalize(v + s); return ambient + spotFactor * Spot.intensity * (Material.Kd * max(dot(s, norm),0.0) + Material.Ks * pow(max(dot(h,norm), 0.0),Material.Shininess)); } else { return ambient; } } void main(void) { gl_FragColor = vec4(adsSpotLight(Position, Normal), 1.0); //gl_FragColor = vec4(1.0,1.0,0.5, 1.0); }
最終的效果還是有一些差別,特別是光線交界的地方。
OpenGLPro13
OpenGL 4.0 Shading Language Cookbook
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。