OpenGL-制作跟随摄影机的点光源
OpenGL - Making a point light that follows the camera
我目前正在创建一个3D场景,我想知道创建跟随相机的点光源的最佳方法是什么。到目前为止,我的代码如下:
顶点着色器:
// Materials
uniform vec3 materialAmbient;
uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform float materialShininess;
uniform float att_quadratic = 0.1;
// Lights
struct AMBIENT
{
vec3 color;
};
struct DIRECTIONAL
{
vec3 direction;
vec3 diffuse;
};
struct POINT
{ int on;
int frag;
vec3 position;
vec3 diffuse;
vec3 specular;
};
uniform AMBIENT lightAmbient;
uniform DIRECTIONAL lightDir;
uniform POINT lightPoint1, lightPoint2, lightPoint3;
layout (location = 0) in vec3 aVertex;
layout (location = 2) in vec3 aNormal;
layout (location = 3) in vec2 aTexCoord;
// Output (sent to Fragment Shader)
out vec4 color;
out vec4 position;
out vec4 normal;
out vec2 texCoord;
out float gravelFactor; // gravelFactor is 1 within the gravel circle and 0 outside the circle
vec4 compAmbient(vec3 material, AMBIENT light)
{
return vec4(material * light.color, 1);
}
vec4 compDirectional(vec3 material, DIRECTIONAL light)
{
vec3 L = normalize(mat3(matrixView) * light.direction).xyz;
float NdotL = dot(normal.xyz, L);
if (NdotL > 0)
return vec4(light.diffuse * material * NdotL, 1);
else
return vec4(0, 0, 0, 1);
}
vec4 compPoint(vec3 materialDiffuse, vec3 materialSpecular, float materialShininess, POINT light)
{
vec4 result = vec4(0, 0, 0, 1);
// diffuse
vec3 L = normalize(matrixView * vec4(light.position, 1) - position).xyz;
float NdotL = dot(L, normal.xyz);
if (NdotL > 0)
result += vec4(light.diffuse * materialDiffuse, 1) * NdotL;
// specular
vec3 V = normalize(-position.xyz);
vec3 R = reflect(-L, normal.xyz);
float RdotV = dot(R, V);
if (NdotL > 0 && RdotV > 0)
result += vec4(light.specular * materialSpecular * pow(RdotV, materialShininess), 1);
//attentuation
float dist = length(matrixView * vec4(light.position, 1) - position);
float att = 1 / (att_quadratic * dist * dist);
return result * att;
}
void main(void)
{
// calculate position & normal
position = matrixModelView * vec4(aVertex, 1.0);
gl_Position = matrixProjection * position;
normal = vec4(normalize(mat3(matrixModelView) * aNormal), 1);
// calculate texture coordinate
texCoord = aTexCoord;
// calculate the colour
color = vec4(0, 0, 0, 0);
// ambient light
color += compAmbient(materialAmbient, lightAmbient);
// directional lights
color += compDirectional(materialDiffuse, lightDir);
// point lights
if (lightPoint1.on == 1 && lightPoint1.frag == 0)
color += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint1);
if (lightPoint2.on == 1 && lightPoint2.frag == 0)
color += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint2);
if (lightPoint3.on == 1 && lightPoint3.frag == 0)
color += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint3);
// calculation of the gravelFactor:
// 0 outside the 8-unit radius from the center
// 1 within 7 units from the centre
// between 0 and 1 at the border zone
gravelFactor = clamp(-length(aVertex.xz) + 8, 0, 1);
}
片段着色器:
// input variables
in vec4 color;
in vec4 position;
in vec4 normal;
in vec2 texCoord;
in float gravelFactor;
// output variable
out vec4 outColor;
// uniforms - material parameters
uniform vec3 materialAmbient;
uniform vec3 materialDiffuse;
uniform vec3 materialSpecular;
uniform float materialShininess;
uniform float att_quadratic = 0.1;
// This uniform variable may be used to take different actions for the terrain and not-terrain
uniform int terrain;
// view matrix (needed for lighting)
uniform mat4 matrixView;
struct POINT
{ int on;
int frag;
vec3 position;
vec3 diffuse;
vec3 specular;
};
uniform POINT lightPoint1, lightPoint2, lightPoint3;
vec4 compPoint(vec3 materialDiffuse, vec3 materialSpecular, float materialShininess, POINT light)
{
vec4 result = vec4(0, 0, 0, 1);
// diffuse
vec3 L = normalize(matrixView * vec4(light.position, 1) - position).xyz;
float NdotL = dot(L, normal.xyz);
if (NdotL > 0)
result += vec4(light.diffuse * materialDiffuse, 1) * NdotL;
// specular
vec3 V = normalize(-position.xyz);
vec3 R = reflect(-L, normal.xyz);
float RdotV = dot(R, V);
if (NdotL > 0 && RdotV > 0)
result += vec4(light.specular * materialSpecular * pow(RdotV, materialShininess), 1);
//attentuation
float dist = length(matrixView * vec4(light.position, 1) - position);
float att = 1 / (att_quadratic * dist * dist);
return result * att;
}
// Texture Samplers
uniform sampler2D textureGrass;
uniform sampler2D textureGravel;
uniform sampler2D texture;
uniform sampler2D bumpmap;
uniform sampler2D textureNormal;
vec4 bump_normal = texture(textureNormal, texCoord.st) * 2 - 1;
void main(void)
{
outColor = color;
if (lightPoint1.on == 1 && lightPoint1.frag == 1)
outColor += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint1);
if (lightPoint2.on == 1 && lightPoint2.frag == 1)
outColor += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint2);
if (lightPoint3.on == 1 && lightPoint3.frag == 1)
outColor += compPoint(materialDiffuse, materialSpecular, materialShininess, lightPoint3);
if (terrain == 1)
{
// Rendering Terrain
// Terrain is a mix of the Grass and Gravel texture
outColor *= mix(texture(textureGrass, texCoord.st), texture(textureGravel, texCoord.st), gravelFactor);
}
else
{
outColor *= texture(texture, texCoord.st) + bump_normal;
}
}
还有我的点灯声明:
//setup point light1
glUniform1i(glGetUniformLocation(idProg, "lightPoint1.on"), 1);
glUniform1i(glGetUniformLocation(idProg, "lightPoint1.frag"), 1);
glUniform3f(glGetUniformLocation(idProg, "lightPoint1.position"), 0, 3.1, 0.0);
glUniform3f(glGetUniformLocation(idProg, "lightPoint1.diffuse"), 1.0, 0.0, 0.0);
glUniform3f(glGetUniformLocation(idProg, "lightPoint1.specular"), 1.0, 0.0, 0.0);
解决这个问题的最佳方法是什么?应该在主代码中还是在着色器中更改灯光的位置?我怎么能这么做?
我肯定会更改主代码中的灯光位置,然后将其传递给着色器。
因此,例如,您更改位置的代码看起来像这样:
//called whenever you redraw your scene
void render()
{
//or however you want to position the light relative to your camera
glUniform3f
(
glGetUniformLocation(idProg, "lightPoint1.position"),
get_camera_pos_x(),
get_camera_pos_y(),
get_camera_pos_z()
);
glUniform3f
(
glGetUniformLocation(idProg, "lightPoint1.direction"),
get_camera_dir_x(),
get_camera_dir_y(),
get_camera_dir_z()
)
//...rest of your drawing code...
}
这样做的主要优点是可以消除多余的计算。如果让顶点着色器根据相机位置更新灯光位置,这是可行的,但每帧都会重复多次该计算。请记住,顶点着色器会在绘制的每个顶点上执行。如果每次都是相同的,则无需重新计算灯光在每个顶点上的位置。
根据OP的评论更新:OP表示,他希望能够像手电筒一样使用相机改变光线的方向。要执行此操作,您需要在着色器中为灯光结构添加一个额外的统一体。我在上面称之为"方向"(归一化向量3)。然后,您可以在主代码中计算相机的方向,并像正常情况一样将其传递给着色器。在着色器中如何处理它取决于您,但本教程可能会帮助
相关文章:
- 试试完美的正方形,你能给点小费吗
- 无法将结构注册为增强几何体3D点
- C++将浮点指针值舍入为小数位数
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 为什么在浮点中从大到小会引入更多的误差
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何对点云数据进行排序
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- QML按钮点击功能执行顺序
- 当用户超过按钮点击限制时报告
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- C++Lua用户数据扰乱了Mathfu浮点值
- 将CHW格式的浮点向量转换为cv::Mat
- DirectX 11 - 点光源阴影
- 做点光源的最佳方法
- iPhone中GLSL ES 2.0上多点光源的2D照明
- 点光源阴影贴图和照明着色器
- OpenGL-制作跟随摄影机的点光源