|
|
置顶随笔
摘要: 你是否担心自己编写的OpenGL程序之中存在bug?
你是否还在为找寻自己编写的OpenGL程序之中的bug发愁头疼吗?
你是否对于自己编写的超眩OpenGL程序在性能上苦苦优化无果吗?......
如果你有此强烈的感受,那么你一定是想拥有一个OpenGL超级助手来帮助你解决这一切?
现在就向你介绍一款这样的软件,它的名字就是GLDog!
这是一款面向OpenGL应用程序的调试和性能分析软件,其目的主要是2个方面:
1.帮助OpenGL应用程序开发者以最快的方式发现代码之中的bug;
2.帮助OpenGL应用程序开发者分析程序的性能并进行优化.
目前这个软件具有如下主要功能:
1.能够基于源代码级进行调试,大大提高了找寻和分析bug的效率;
2.能够显示出CPU占用率/帧率/纹理内存以及所占用的纹理内存等等性能信息;
3.能够输出大量调试信息;
4.能够把必要的纹理和Shader保存为设定格式的文件;
5.能够检测到OpenGL错误,并给出错误所在的代码位置;
6.对每个AP 阅读全文
2008年7月3日
摘要: 如果物体表面细节很多,我们可以不断的精细化物体的几何数据,但是这样会产生大量的Lighting & Transformation等计算,为了实现丰富真实的物体表面,除了贴上一般纹理之外,往往还使用Bump mapping(凹凸纹理)技术。Bump mapping并没有增加物体的几何复杂度,它只是在计算物体的光照效果时作了“弊”,不使用物体本身的法向量,而是使用了经过... 阅读全文
2008年7月2日
众所周知,OpenGL固定管线只提供了最多8盏灯光。如何使得自己的场景之中拥有更多的灯光效果呢?
这里提供一种使用GLSL shader实现更多数量的局部光照。
在GLSL里,首先建立光照参数数据结构:
struct myLightParams
  {
bool enabled;
vec4 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 spotDirection;
float spotCutoff;
float spotExponent;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
};
然后,需要app传入的参数:
const int maxLightCount = 32;
uniform myLightParams light[maxLightCount];
uniform bool bLocalViewer;
uniform bool bSeperateSpecualr;
主函数:
void main()
  {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec4 pos = gl_ModelViewMatrix * gl_Vertex;
vec3 epos = vec3(pos)/pos.w;
vec3 normal = normalize(gl_NormalMatrix * gl_Normal);
vec3 eye;
if (bLocalViewer)
eye = -normalize(epos);
else
eye = vec3(0, 0, 1.0);
vec4 amb = vec4(0);
vec4 diff = vec4(0);
vec4 spec = vec4(0);
for (int i=0; i<maxLightCount; i++)
 {
if (light[i].enabled == false)
continue;
if (light[i].position.w == 0)
 {
DirectionalLight(i, eye, epos, normal, amb, diff, spec);
}
else if (light[i].spotCutoff == 180.0)
 {
PointLight(i, eye, epos, normal, amb, diff, spec);
}
else
 {
SpotLight(i, eye, epos, normal, amb, diff, spec);
}
}
vec4 color = gl_FrontLightModelProduct.sceneColor +
amb * gl_FrontMaterial.ambient +
diff * gl_FrontMaterial.diffuse;
if (bSeperateSpecualr)
 {
gl_FrontSecondaryColor = spec * gl_FrontMaterial.specular;
}
else
 {
gl_FrontSecondaryColor = vec4(0, 0, 0, 1.0);
color += spec * gl_FrontMaterial.specular;
}
gl_FrontColor = color;
}
对于方向光源的计算:
void DirectionalLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
  {
float dotVP = max(0, dot(normal, normalize(vec3(light[i].position))));
float dotHV = max(0, dot(normal, normalize(eye+normalize(vec3(light[i].position)))));
amb += light[i].ambient;
diff += light[i].diffuse * dotVP;
spec += light[i].specular * pow(dotHV, gl_FrontMaterial.shininess);
}
对于点光源:
void PointLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
  {
vec3 VP = vec3(light[i].position) - epos;
float d = length(VP);
VP = normalize(VP);
float att = 1.0/(light[i].constantAttenuation + light[i].linearAttenuation*d + light[i].quadraticAttenuation*d*d);
vec3 h = normalize(VP+eye);
float dotVP = max(0, dot(normal, VP));
float dotHV = max(0, dot(normal, h));
amb += light[i].ambient * att;
diff += light[i].diffuse * dotVP * att;
spec += light[i].specular * pow(dotHV, gl_FrontMaterial.shininess) * att;
}
对于聚光灯:
void SpotLight(int i, vec3 eye, vec3 epos, vec3 normal,
inout vec4 amb, inout vec4 diff, inout vec4 spec)
  {
vec3 VP = vec3(light[i].position) - epos;
float d = length(VP);
VP = normalize(VP);
float att = 1.0/(light[i].constantAttenuation + light[i].linearAttenuation*d + light[i].quadraticAttenuation*d*d);
float dotSpot = dot(-VP, normalize(light[i].spotDirection));
float cosCutoff = cos(light[i].spotCutoff*3.1415926/180.0);
float spotAtt = 0;
if (dotSpot < cosCutoff)
spotAtt = 0;
else
spotAtt = pow(dotSpot, light[i].spotExponent);
att *= spotAtt;
vec3 h = normalize(VP+eye);
float dotVP = max(0, dot(normal, VP));
float dotHV = max(0, dot(normal, h));
amb += light[i].ambient * att;
diff += light[i].diffuse * dotVP * att;
spec += light[i].specular * pow(dotHV, gl_FrontMaterial.shininess) * att;
}
这样,对于场景之中的任意对象,它所能够接受计算的光源就可以突破8个的限制了。
上述光照计算是遵循OpenGL spec的,因此与固定管线的效果是一致的。
|