所以,我已经开始寻求在不使用OpenGL照明系统的情况下实现真棒照明。我成功实现了Phong漫射照明。镜面反射给我带来了麻烦。
我需要知道我正在使用的OpenGL常量占用了哪些空格,因为它们似乎被错误转换,并且会导致照明故障。
通过成功加载和运行Phong漫反射着色器,我已经确认我的C ++代码没有问题。但是,C ++代码可能会将无效数据传递给着色器,这是我担心的事情之一。我将使用注释粘贴我的着色器,以及直接与着色器相关的所有C ++代码(尽管我90%确定问题出在着色器中)。
在这些图像中,光源是大点,并显示轴。 光在围绕一个球体旋转,y = 0。
这是漫反射,所以你知道模型是什么...... 注意我还没有完成每像素...
这是菲涅耳照明,如源代码所示...... 请注意点亮的面部是如何面向光线的,而不是光线和相机之间的某处
这是Blinn-Phong,我不得不乘以30 ... 再次注意点亮的面部指向光源的方式,以及我必须将镜面反射因子(S)乘以30以实现此目的
顶点着色器源(从“dirlight.vs”加载)
const int MAXLIGHTS = 4;
uniform bool justcolor = false;
uniform int lightcount;
uniform vec4 lightposs[MAXLIGHTS];
uniform vec4 lightdirs[MAXLIGHTS];
uniform vec4 lightdifs[MAXLIGHTS];
uniform vec4 lightambs[MAXLIGHTS];
//diffuse
vec4 D;
//specular, normaldotlight
float S, NdotL[MAXLIGHTS];
//normal, eyevec, lightvecs, halfvecs
vec3 N, E, L[MAXLIGHTS], H[MAXLIGHTS];
void main() {
//if(lightcount > MAXLIGHTS) lightcount = MAXLIGHTS;
D = vec4(0.0, 0.0, 0.0, 0.0);
S = 0.0;
N = gl_Normal;
E = normalize(vec3(-gl_Vertex));
for(int i = 0; i < lightcount; i++)
{
//calculating direction to light source
L[i] = normalize(vec3(lightposs[i] - gl_Vertex));
//normal dotted with direction to light source
NdotL[i] = max(dot(N, L[i]), 0.0);
//diffuse term, works just fine
D += gl_Color * lightdifs[i] * NdotL[i];
if(NdotL[i] >= 0.0)
{
//halfvector = normalize(lightdir + eyedir)
H[i] = normalize(L[i] + E);
//Blinn-Phong, only lights up faces whose normals
//point directly to the light source for some reason...
//S += max(0.0, dot(H[i], N));
//Fresnel, lights up more than Blinn-Phong
//but the faces still point directly to the light source,
//not somewhere between the lightsource and myself, like they should.
S += pow(max(0.0, dot(reflect(L[i], N), E)), 50.0);
}
else
{
H[i] = vec3(0.0, 0.0, 0.0);
}
}
//currently only showing specular. To show diffuse add D.
gl_FrontColor = justcolor ? gl_Color : vec4(S * 0.3, S * 0.3, S * 0.3, 1.0);
gl_Position = ftransform();
}
片段着色器源(从“dirlight.fs”加载)
void main()
{
gl_FragColor = gl_Color;
}
摘自C ++主要初始化...
//class program manages shaders
Program shaders = Program();
//attach a vertex shader, compiled from source in dirlight.vs
shaders.addShaderFile(GL_VERTEX_SHADER, "dirlight.vs");
//attach a fragment shader compiled from source in dirlight.fs
shaders.addShaderFile(GL_FRAGMENT_SHADER, "dirlight.fs");
//link program
shaders.link();
//use program
shaders.use();
//Program::getUniformLoc(const char* name) grabs the location
//of the uniform specified
GLint sTime = shaders.getUniformLoc("time");
GLint lightcount = shaders.getUniformLoc("lightcount");
GLint lightdir = shaders.getUniformLoc("lightdirs");
GLint lightdif = shaders.getUniformLoc("lightdifs");
GLint lightamb = shaders.getUniformLoc("lightambs");
GLint lightpos = shaders.getUniformLoc("lightposs");
GLint justcolor = shaders.getUniformLoc("justcolor");
glUniform1i(justcolor, 0);
glUniform1i(lightcount, 2);
//diffuse light colors
GLfloat lightdifs[] = {1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.f, 1.f};
glUniform4fv(lightdif, 2, lightdifs);
glUniform4f(lightamb, 0.4f, 0.4f, 0.4f, 1.f);
摘自C ++主循环...
//My lights rotate around the origin, where I have placed an icosphere
GLfloat lightposs[] = {-4 * sinf(newTime), lighth, -4 * cosf(newTime), 0.0f,
-4 * sinf(newTime + M_PI), lighth, -4 * cosf(newTime + M_PI), 0.0f};
glUniform4fv(lightpos, 2, lightposs);
答案 0 :(得分:4)
您的代码中缺少一些重要的内容。首先,你应该将顶点位置和法线转换为眼睛空间。照明计算最简单。顶点位置使用模型视图矩阵进行变换,法线变换使用模型视图的转置反转。通常光位置是世界坐标,因此从世界到眼睛坐标提供额外的矩阵是有意义的。