众所周知,OpenGL固定管线只提供了最多8盏灯光。如何使得自己的场景之中拥有更多的灯光效果呢?
这里提供一种使用GLSL shader实现更多数量的局部光照。
在GLSL里,首先建立光照参数数据结构:
struct myLightParams
;
然后,需要app传入的参数:
const int maxLightCount = 32;
uniform myLightParams light[maxLightCount];
uniform bool bLocalViewer;
uniform bool bSeperateSpecualr;
主函数:
void main()
}
对于方向光源的计算:
void DirectionalLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
}
对于点光源:
void PointLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
}
对于聚光灯:
void SpotLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
}
这样,对于场景之中的任意对象,它所能够接受计算的光源就可以突破8个的限制了。
上述光照计算是遵循OpenGL spec的,因此与固定管线的效果是一致的。
相关文章: