opengl light减少反射

时间:2018-05-17 13:19:55

标签: c++ opengl material lighting opengl-compat

我正在尝试在我的场景中应用灯光。 然而,所有物体都反射极其且颜色变形。

enter image description here

如何获得类似于此示例的反射:

enter image description here

我认为我选择了错误的材料值?

这就是我设置灯光和材质的方法:

void createLight() {

    GLfloat mat_specular[] = {0.3, 0.3, 0.3, 1.0};
    GLfloat mat_shininess[] = { 1.0 };
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);

    // Lighting set up
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    // Set lighting intensity and color
    GLfloat qaAmbientLight[]    = {0.2, 0.2, 0.2, 1.0};
    GLfloat qaDiffuseLight[]    = {0.8, 0.8, 0.8, 1.0};
    GLfloat qaSpecularLight[]   = {1.0, 1.0, 1.0, 1.0};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);

    // Set the light position
    GLfloat qaLightPosition[]   = {12, 8, 12, 0.4};
    glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);

}

1 个答案:

答案 0 :(得分:3)

光的值很高。请注意,在标准灯光模型中,灯光的环境,漫反射和镜面反射部分会相加。

标准的OpenGL Blinn-Phong灯光模型计算如下:

gdb ../main.o -ex "info line main.c:39" -ex "info line main.c:41" --batch
Line 39 of "main.c" starts at address 0x36 <main+54> and ends at 0x5e <main+94>.
Line 41 of "main.c" starts at address 0x5e <main+94> and ends at 0x70 <main+112>.

由于您的镜面反射光值由Ka ... ambient material Kd ... difusse material Ks ... specular material La ... ambient light Ld ... diffuse light Ls ... specular light sh ... shininess N ... norlmal vector L ... light vector (from the vertex postion to the light) V ... view vector (from the vertex psotion to the eye) Id = max(dot(N, L), 0.0); H = normalize(V + L); NdotH = max(dot(N, H), 0.0); Is = (sh + 2.0) * pow(NdotH, sh) / (2.0 * 3.14159265); fs = Ka*La + Id*Kd*Ld + Is*Ks*Ls; 初始化,因此在对象的法向矢量指向光的情况下,这可能会给出大于1.0的和。这会导致渲染的对象看起来几乎是白色。

我建议使用一个小的环境光部件,因为它会独立于入射光添加到整个物体。

使用强烈的朗伯漫反射光成分,反射光线的方向 见How does this faking the light work on aerotwist?

使用镜面光组件,您必须进行实验。它的效果很大程度上取决于光泽参数。但是首先使用镜面光线的小值和大约10.0的光泽度。

{1.0, 1.0, 1.0, 1.0};