纹理滤波
在纹理映射的过程中,如果图元的大小不等于纹理的大小,OpenGL便会对纹理进行缩放以适应图元的尺寸。我们可以通过设置纹理滤镜来决定OpenGL对某个纹理采用的放大、缩小的算法
滤镜 | 描述 |
---|---|
GL_NEAREST | 取最邻近像素 |
GL_LINEAR | 线性内部插值 |
GL_NEAREST_MIPMAP_NEAREST | 最近多贴图等级的最邻近像素 |
GL_NEAREST_MIPMAP_LINEAR | 在最近多贴图等级的内部线性插值 |
GL_LINEAR_MIPMAP_NEAREST | 在最近多贴图等级的外部线性插值 |
GL_LINEAR_MIPMAP_LINEAR | 在最近多贴图等级的外部和内部线性插值 |
光源
环境光
因为我们使用的OpenGL 3.x API,使用可编程管线,不使用glLightfv
修改着色器代码
- 颜色=环境光强度(0.5)* 环境光颜色 * 物体本身颜色
static const char *vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute lowp vec3 colAttr;\n"
"varying lowp vec3 col;\n"
"uniform highp mat4 projection;\n"
"uniform highp mat4 view;\n"
"uniform highp mat4 model;\n"
"attribute vec2 a_texcoord;\n"
"varying highp vec2 v_texcoord;\n"
"void main() {\n"
" gl_Position = projection * view * model * posAttr;\n"
" v_texcoord = a_texcoord;\n"
" col = colAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec3 col;\n"
"uniform sampler2D texture;\n"
"uniform lowp vec3 ambientColAttr;\n"
"varying highp vec2 v_texcoord;\n"
"void main() {\n"
" float ambientStrength = 0.5f;\n"
" gl_FragColor = vec4(ambientStrength * ambientColAttr * col, 1.0f);\n"
"}\n";
获取 环境光属性
//环境光属性
m_ambientColAttr = m_program->uniformLocation("ambientColAttr");
绘制时候设置环境光
QVector3D ambient(1.0f, 1.0f, 1.0f);
m_program->setUniformValue(m_ambientColAttr, ambient);
效果
之所以有这样的效果,是因为左上角白色受环境光影响,反射了环境光颜色
-
白色环境光
-
红色环境光
-
绿色环境光
-
蓝色环境光
点光源-漫反射
修改着色器代码
我们追加了点光源颜色,光源位置,物体的法线(实际具有增幅/衰减光亮的效果)
static const char *vertexShaderSource =
"attribute highp vec3 posAttr;\n"
"attribute lowp vec3 colAttr;\n"
"varying lowp vec3 col;\n"
"uniform lowp vec3 aNormal;\n"
"uniform highp mat4 projection;\n"
"uniform highp mat4 view;\n"
"uniform highp mat4 model;\n"
"attribute vec2 a_texcoord;\n"
"varying highp vec2 v_texcoord;\n"
"varying vec3 Normal;\n"
"varying vec3 FragPos;\n"
"void main() {\n"
" gl_Position = projection * view * model * vec4(posAttr, 1.0);\n"
" FragPos = vec3(model * vec4(posAttr, 1.0));\n"
" v_texcoord = a_texcoord;\n"
" Normal = mat3(transpose(inverse(model))) * aNormal;\n"
" col = colAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec3 col;\n"
"uniform sampler2D texture;\n"
"uniform lowp vec3 ambientColAttr;\n"
"uniform lowp vec3 lightColAttr;\n"
"uniform lowp vec3 lightPosAttr;\n"
"varying highp vec2 v_texcoord;\n"
"varying vec3 Normal;\n"
"varying vec3 FragPos;\n"
"void main() {\n"
" vec3 norm = normalize(Normal);\n"
" vec3 lightDir = normalize(lightPosAttr - FragPos);\n"
" float ambientStrength = 0.1;\n"
" float diffuse = max(dot(norm, lightDir), 0.0);\n"
" gl_FragColor = vec4((ambientStrength + diffuse) * lightColAttr * col, 1.0);\n"
"}\n";
获取 点光源属性
//点光源属性
m_lightColAttr = m_program->uniformLocation("lightColAttr");
m_lightPosAttr = m_program->uniformLocation("lightPosAttr");
m_aNormal = m_program->uniformLocation("aNormal");
绘制时候设置点光
//环境光
QVector3D ambient(1.0f, 1.0f, 1.0f);
m_program->setUniformValue(m_ambientColAttr, ambient);
//点光源-漫反射
QVector3D pointLightCol(1.0f, 1.0f, 1.0f);
m_program->setUniformValue(m_lightColAttr, pointLightCol);
QVector3D pointLightPos(2.0f, 0.0f, 2.0f);
m_program->setUniformValue(m_lightPosAttr, pointLightPos);
//法线=强度
QVector3D objNormal(0.0f, 1.0f, 1.0f);
m_program->setUniformValue(m_aNormal, objNormal);
效果
高光-镜面反射
光源照射到物体然后反射到人的眼睛里时,物体上最亮的那个点就是高光,
所以我们需要知道眼睛(摄像机)的位置,然后根据光的方向,法线求出反射
修改着色器代码
static const char *vertexShaderSource =
"attribute highp vec3 posAttr;\n"
"attribute lowp vec3 colAttr;\n"
"varying lowp vec3 col;\n"
"uniform lowp vec3 aNormal;\n"
"uniform highp mat4 projection;\n"
"uniform highp mat4 view;\n"
"uniform highp mat4 model;\n"
"attribute vec2 a_texcoord;\n"
"varying highp vec2 v_texcoord;\n"
"varying vec3 Normal;\n"
"varying vec3 FragPos;\n"
"void main() {\n"
" gl_Position = projection * view * model * vec4(posAttr, 1.0);\n"
" FragPos = vec3(model * vec4(posAttr, 1.0));\n"
" v_texcoord = a_texcoord;\n"
" Normal = mat3(transpose(inverse(model))) * aNormal;\n"
" col = colAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"varying lowp vec3 col;\n"
"uniform sampler2D texture;\n"
"uniform lowp vec3 ambientColAttr;\n"
"uniform lowp vec3 lightColAttr;\n"
"uniform lowp vec3 lightPosAttr;\n"
"uniform lowp vec3 viewPosAttr;\n"
"varying highp vec2 v_texcoord;\n"
"varying vec3 Normal;\n"
"varying vec3 FragPos;\n"
"void main() {\n"
//漫反射
" vec3 norm = normalize(Normal);\n"
" vec3 lightDir = normalize(lightPosAttr - FragPos);\n"
" float ambientStrength = 0.1;\n"
" float diffuse = max(dot(norm, lightDir), 0.0);\n"
//高光
" vec3 viewDir = normalize(viewPosAttr - FragPos);\n"
" vec3 reflecDir = reflect(-lightDir, norm);\n"
" float specularStrength = 0.5;\n"
" float specular = pow(max(dot(viewDir, reflecDir), 0.0), 64) * specularStrength;\n"
" gl_FragColor = vec4((ambientStrength + diffuse + specular) * lightColAttr * col, 1.0);\n"
"}\n";
获取 视点属性
//视点属性
m_viewPosAttr = m_program->uniformLocation("viewPosAttr");
绘制时候设置视点
//视点为摄像机位置
m_program->setUniformValue(m_viewPosAttr, camera);
效果
仔细看,有高光