c++ - 如何将点光源转换为卵形/椭圆形?

标签 c++ opengl glsl 2d lighting

我希望通过具有可以具有不同 x 和 y 值的 vec2 半径将我当前的圆形光变成椭圆形。有没有什么方法可以根据我当前在片段着色器中的代码执行此操作?

uniform struct Light
{
vec4 colour;
vec3 position;
vec2 radius;
float intensity;
} allLights[MAX_LIGHTS];

vec4 calculateLight(Light light)
{
vec2 lightDir = fragmentPosition.xy - light.position.xy;
float lightDistance = length(lightDir);
if (lightDistance >= light.radius.x)
{
    return vec4(0, 0, 0, 1); //outside of radius make it black
}
return light.intensity * (1 - lightDistance / light.radius.x) * light.colour;
}

最佳答案

用椭圆的半轴将 vector 分成光源,并检查 vector 的长度是否大于1.0:

if (length(lightDir / light.radius) >= 1.0)
    return vec4(0, 0, 0, 1); //outside of radius make it black
return light.intensity * (1 - length(lightDir / light.radius)) * light.colour;

关于c++ - 如何将点光源转换为卵形/椭圆形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69345094/

相关文章:

java - OpenGL-GLSL纹理()调用: 1282 Invalid Operation

c++ - 如何获得 boost 多精度数字部分?

c++ - UTF8 字符到十六进制值字符串

c++ - 程序因对象 vector 而崩溃

c++ - OpenGL 纹理不通过 GLSL 渲染

c++ - glUseProgram() 和速度之后 uniform 的行为

c++ - Caffe Euclidean 图像损失计算

c++ - Matrix4转GLSL统一值

c++ - 使用 C++ 在 OpenGL 中移动自动旋转 3D 多边形

opengl - EmitVertex 给出 "error C1067: too little data in type constructor"