计算机图形学 – Flat Shading、Gouraud Shading、Phong Shading的区别
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:计算机图形学 – Flat Shading、Gouraud Shading、Phong Shading的区别
原文链接:https://www.stubbornhuang.com/2149/
发布于:2022年06月01日 11:11:39
修改于:2022年06月02日 10:09:52

1 Flat Shading、Gouraud Shading、Phong Shading的区别
从下图可以看出Flat Shading、Gouraud Shading、Phong Shading三种着色模型的区别,其中a为Flat Shading,b为Gouraud Shading,c为Phong Shading。


1.1 三种Shading Model的比较
1.1.1 算法的提出
Flat Shading >> Gouraud Shading >> Phong Shading
1.1.2 从简单到复杂
Flat Shading >> Gouraud Shading >> Phong Shading
1.1.3 原理上
- Flat Shading:constant surface shading,三角形的顶点没有法向量,三角形的整个面才有法向量,打光时整个三角形只呈现一种颜色;
- Gouraud Shading:color interpolation shading,三角形的各个顶点都有法向量,打光时三个顶点都有各自的颜色,然后通过双线性内插(bilinear interpolation)插值整个三角面片的颜色,这导致整个三角形会存在渐变的颜色效果;
- Phong Shading:vertex normal interpolation shading,三角形的各个顶点都有各自的法向量,先对三角形整个面做法向量的双线性内插,然后打光求整个三角形的颜色;
1.1.4 复杂度
假设三角形面积为A,三角形个数为N,打一次光需要6次乘法和2次加法和1次查表的计算量(疑惑),将打一次光的运算量设定为L,做一次双线性内插的运算设定为B,则
- Flat Shading:N * L
- Gouraud Shading:N * (3 * L+ b * A)
- Phong Shading:(B + L) * N * A
所以计算复杂度是:Flading Shading < Gouraud Shading < Phong Shading
1.1.5 三者的着色示意图如下

1.1.6 三者的GLSL shader
1.1.6.1 Gouraud Shader
vertex shader
#version 420 core
// Per-vertex inputs
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
// Matrices we'll need
layout (std140) uniform constants
{
mat4 mv_matrix;
mat4 view_matrix;
mat4 proj_matrix;
};
// Light and material properties
uniform vec3 light_pos = vec3(100.0, 100.0, 100.0);
uniform vec3 diffuse_albedo = vec3(0.5, 0.2, 0.7);
uniform vec3 specular_albedo = vec3(0.7);
uniform float specular_power = 128.0;
uniform vec3 ambient = vec3(0.1, 0.1, 0.1);
// Outputs to the fragment shader
out VS_OUT
{
vec3 color;
} vs_out;
void main(void) {
// Calculate view-space coordinate
vec4 P = mv_matrix * position;
// Calculate normal in view space
vec3 N = mat3(mv_matrix) * normal;
// Calculate view-space light vector
vec3 L = light_pos - P.xyz;
// Calculate view vector (simply the negative of the
// view-space position)
vec3 V = -P.xyz;
// Normalize all three vectors
N = normalize(N);
L = normalize(L);
V = normalize(V);
// Calculate R by reflecting -L around the plane defined by N
vec3 R = reflect(-L, N);
// Calculate the diffuse and specular contributions
vec3 diffuse = max(dot(N, L), 0.0) * diffuse_albedo;
vec3 specular = pow(max(dot(R, V), 0.0), specular_power) *
specular_albedo;
// Send the color output to the fragment shader
vs_out.color = ambient + diffuse + specular;
// Calculate the clip-space position of each vertex
gl_Position = proj_matrix * P;
}
fragment shader
#version 420 core
// Output
layout (location = 0) out vec4 color;
// Input from vertex shader
in VS_OUT
{
vec3 color;
} fs_in;
void main(void)
{
// Write incoming color to the framebuffer
color = vec4(fs_in.color, 1.0);
}
1.1.6.2 Phong Shader
vertex shader
#version 420 core
// Per-vertex inputs
layout (location = 0) in vec4 position;
layout (location = 1) in vec3 normal;
// Matrices we'll need
layout (std140) uniform constants
{
mat4 mv_matrix;
mat4 view_matrix;
mat4 proj_matrix;
};
// Inputs from vertex shader
out VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} vs_out;
// Position of light
uniform vec3 light_pos = vec3(100.0, 100.0, 100.0);
void main(void) {
// Calculate view-space coordinate
vec4 P = mv_matrix * position;
// Calculate normal in view-space
vs_out.N = mat3(mv_matrix) * normal;
// Calculate light vector
vs_out.L = light_pos - P.xyz;
// Calculate view vector
vs_out.V = -P.xyz;
// Calculate the clip-space position of each vertex
gl_Position = proj_matrix * P;
}
fragment shader
#version 420 core
// Output
layout (location = 0) out vec4 color;
// Input from vertex shader
in VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} fs_in;
// Material properties
uniform vec3 diffuse_albedo = vec3(0.5, 0.2, 0.7);
uniform vec3 specular_albedo = vec3(0.7);
uniform float specular_power = 128.0;
void main(void)
{
// Normalize the incoming N, L, and V vectors
vec3 N = normalize(fs_in.N);
vec3 L = normalize(fs_in.L);
vec3 V = normalize(fs_in.V);
// Calculate R locally
vec3 R = reflect(-L, N);
// Compute the diffuse and specular components for each
// fragment
vec3 diffuse = max(dot(N, L), 0.0) * diffuse_albedo;
vec3 specular = pow(max(dot(R, V), 0.0), specular_power) *
specular_albedo;
// Write final color to the framebuffer
color = vec4(diffuse + specular, 1.0);
}
参考链接
当前分类随机文章推荐
- 计算几何 - C++计算两个二维向量的夹角 阅读2102次,点赞3次
- Marching Cube(C++ OpenGl代码)读取医学三维图像*.raw进行三维重建 阅读5960次,点赞3次
- BVH动作文件下载地址整理 阅读1044次,点赞0次
- 默认的左手坐标系与右手坐标系的比较 阅读3119次,点赞2次
- 书籍翻译 - Cloth Simulation for Computer Graphics 中文翻译 阅读31次,点赞0次
- 三维点云重建(曲面重建)的经典算法 阅读5916次,点赞6次
- 左手坐标系与右手坐标系 阅读2419次,点赞0次
- 三维旋转 - 欧拉角和旋转矩阵的基本概念以及相互转换 阅读428次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第6章 Transformation Matrices中文翻译 阅读834次,点赞5次
- 计算机图形学 - 三维空间中的左、右手坐标系旋转矩阵与旋转变换 阅读5630次,点赞2次
全站随机文章推荐
- UnrealEngine4 - Can not find such file SceneRenderTargets.h,在UE4 C++层中正确的使用FSceneRenderTargets类 阅读2185次,点赞0次
- OpenCV - 打开摄像头并对摄像头获取的每一帧图像进行Canny算子边缘化提取操作 阅读3421次,点赞0次
- 计算机图形学 - 三维空间中的左、右手坐标系旋转矩阵与旋转变换 阅读5630次,点赞2次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读426次,点赞0次
- WordPress - 使用插件的方式作出类似向主题function.php中添加代码的功能 阅读2065次,点赞0次
- OpenCV - Mat与lplImage和CvMat的相互转换 阅读3065次,点赞0次
- Google Adsense - 后台统计面板中CTR/CPC/RPM的含义 阅读1836次,点赞0次
- 资源分享 - Introduction to 3D Game Programming with DirectX 9.0 英文高清PDF下载 阅读465次,点赞0次
- 书籍翻译 - Fundamentals of Computer Graphics, Fourth Edition,第1章 Introduction中文翻译 阅读2320次,点赞7次
- 资源分享 - Python深度学习:基于PyTorch (吴茂贵著) 高清PDF下载 阅读2727次,点赞0次
评论
153