[OpenGL]纹理数组的一些小细节

以二维纹理数组为例

GLSL
texture(tex, vec3(x, y, index))

实际会取到的纹理由 index 四舍五入得到。此外,index 使用时会被限制在 0~数组大小–1 之间

如果有一个三层的纹理数组

index 为-1 则取第一个;为 1.2 取第二个;为 1.9 取第三个(你可以理解为强制 GL_NEAREST)

OpenGL


[GLSL]HSV 转为 RGB

GLSL
vec3 rgb(float h, float s, float v)
{
    int hi = int(mod(floor(h / 60), 6));
    float f = h / 60 - hi;
    float p = v * (1 - s);
    float q = v * (1 - f * s);
    float t = v * (1 - (1 - f) * s);
    switch(hi)
    {
    case 0:
        return vec3(v, t, p);
    case 1:
        return vec3(q, v, p);
    case 2:
        return vec3(p, v, t);
    case 3:
        return vec3(p, q, v);
    case 4:
        return vec3(t, p, v);
    case 5:
        return vec3(v, p, q);
    }
}

参考:颜色空间 RGB 与 HSV(HSL)的转换_hsb 色彩模式圆椎形-CSDN 博客

OpenGL

[GLSL]Mipmap 相关函数

  • int textureQueryLevels(sampler):返回该材质 Mipmap 数量
  • vec2 textureQueryLod(sampler, vec2):返回 Mipmap 与 LOD 所在等级
OpenGL

[OpenGL]error C7539: GLSL 1.20 does not allow nested structs

请看以下代码:

GLSL
// ...

uniform struct
{
    struct
    {
        vec3 pos;
    } list[10];
} lights;

// ...

有的显卡可以正常处理以上定义,有的不行(即禁止在结构中定义结构)。

请改用以下方式声明:

GLSL
// ...

struct whatever
{
    vec3 pos;
};

uniform struct
{
     struct whatever list[10];
} lights;

// ...
OpenGL