本文是关于偏导数函数dfdx、dfdy的个人实验理解,如有错误,请各位留言指正!
参考文档:
https://segmentfault.com/a/1190000019681666?utm_source=tag-newest
https://blog.youkuaiyun.com/herramadeus/article/details/21037503
个人认为偏导数是一个物体空间坐标与物体所渲染的屏幕坐标的一个比值。请看下面具体的验证。
顶点着色器:
#version 430 core
layout(location = 0) in vec4 vPosition;
layout(location = 1) in vec2 UV;
layout(location = 0) uniform mat4 MVP;
out vec2 texUV;
void main()
{
texUV = UV;
gl_Position = MVP*vPosition;
}
片段着色器:
#version 430 core
layout(location = 1) uniform float fdx;
in vec2 texUV;
out vec4 fColor;
void main()
{
float dp = 100.0;
dp = dFdx(texUV).x;
//根据偏导数数值进行渲染 因为浮点精度问题,不能直接相等
//UV的宽度为1
if(dp >= fdx-0.0001 && dp <= fdx + 0.0001 )
{
fColor = vec4(0.0, 0.0, 1.0, 1.0);
}
else
{
fColor = vec4(1.0, 0.0, 0.0, 1.0);
}
}
程序设置:
//平面的空间坐标
GLfloat vertices[NumVertices][3] = {
{ -0.5f, -0.5f, 0.0f},
{ 0.5f, -0.5f, 0.0f},
{ 0.5f, 0.5f, 0.0f},
{ -0.5f, 0.5f, 0.0f},
};
//平面的UV坐标
GLfloat uv[NumVertices][2] = {
{ 0.0f, 0.0f},
{ 1.0f, 0.0f},
{ 1.0f, 1.0f},
{ 0.0f, 1.0f}
};
glViewport(0, 0, 500, 500);
vmath::mat4 modelView = vmath::mat4::identity();
modelView = vmath::frustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100) * vmath::translate(0.0f, 0.0f, -1.0f);
glUniformMatrix4fv(0,1,false,modelView);
//平面处于视锥近平面上,近平面上下宽高为2,平面宽高为1,视口宽高为500,平面渲染宽高为250
//UV的宽高为1,UV的偏导数应为1.0/250.0
glUniform1f(1, 1.0/250.0f);
物体渲染受平移,旋转,缩放的影响,分别进行测试。
平移/绽放:
modelView = vmath::frustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100) * vmath::translate(0.0f, 0.0f, -2.0f);
//modelView = vmath::frustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100) * vmath::translate(0.0f, 0.0f, -1.0f)
// * vmath::scale(0.5f,0.5f,0.5f);
glUniformMatrix4fv(0,1,false,modelView);
//远离(缩小)了一倍,渲染宽高应为125
glUniform1f(1, 1.0/125.0);
旋转:
vmath::mat4 modelView = vmath::mat4::identity();
modelView = vmath::frustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100) * vmath::translate(0.0f, 0.0f, -1.0f)
* vmath::rotate(60.0f, 0.0f, 0.0f, 1.0f);
glUniformMatrix4fv(0,1,false,modelView);
//斜向上还是250个像素,一个UV单位,60度的旋转,投影到水平是0.5
glUniform1f(1, 0.5/250.0);
以上纯属个人见解!!