GLSL - 尝试添加多个光源
GLSL - Trying to add multiple lights?
我有一个简单的片段着色器来模拟2D照明,如下所示:
struct Light
{
vec2 pos; // Light position
float spread; // Light spread
float size; // Light bulb size
};
void main(void)
{
Light light0; // Define the light
light0.pos = iMouse.xy; // Set the position to mouse coords
light0.spread = 500.0;
light0.size = 200.0;
float x_dis = light0.pos.x - gl_FragCoord.x;
float y_dis = light0.pos.y - gl_FragCoord.y;
float intensity = sqrt(pow(x_dis, 2.0) + pow(y_dis, 2.0))-light0.size; // Pythagorean Theorem - size
if(intensity < 0.0)
intensity = 0.0;
else
intensity /= light0.spread; // Divide the intensity by the spread
gl_FragColor = vec4(1.0-intensity, 1.0-intensity, 1.0-intensity, 1.0);
}
这在屏幕上放了一盏灯。(您可以在此处查看 https://www.shadertoy.com/view/XsX3Wl(
我想,"嘿,这很容易!我就做一个 for 循环,把光的强度结合起来!
这就是我所做的:
struct Light
{
vec2 pos; // Light position
float spread; // Light spread
float size; // Light bulb size
};
void main(void)
{
Light lights[2];
Light light0;
light0.pos = iMouse.xy;
light0.spread = 500.0;
light0.size = 200.0;
Light light1;
light0.pos = iMouse.xy;
light0.spread = 500.0;
light0.size = 200.0;
float intensity = 0.0;
for(int i = 0; i < 2; i++)
{
float x_dis = lights[i].pos.x - gl_FragCoord.x;
float y_dis = lights[i].pos.y - gl_FragCoord.y;
float sub_ints = sqrt(pow(x_dis, 2.0) + pow(y_dis, 2.0))-lights[i].size; // Intensity relative to this light
if(sub_ints < 0.0)
sub_ints = 0.0;
sub_ints /= lights[i].spread; // Divide the intensity by the spread
intensity += sub_ints;
}
gl_FragColor = vec4(1.0-intensity, 1.0-intensity, 1.0-intensity, 1.0);
}
您可以通过 https://www.shadertoy.com/view/4dX3Wl 看到这不起作用。但是我很困惑?如何累积特定像素上的灯光强度?
Light light1;
light0.pos = iMouse.xy;
light0.spread = 500.0;
light0.size = 200.0;
您在此处不是在初始化 light1。也许点差为零,除以它时会产生一个 NaN。
我知道会发生复制和粘贴错误,但很容易发现。在将代码发布到 SO 上之前,您应该对其进行认真的审查。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 将图像添加到资源文件夹UWP C++
- GLSL - 尝试添加多个光源