简体   繁体   English

如何停止使用相机移动位置光

[英]How to stop positional light moving with camera

When I rotate and/or move the camera in my openGL project, it is almost as if there is a spotlight moving with it, however I have set up my gl light to be positional and given it a static position. 当我在openGL项目中旋转和/或移动相机时,几乎就好像有聚光灯在移动,但是我将我的gl灯设置为可定位并赋予其静态位置。

void Lighting::Display()
{
    glPushMatrix();

    glTranslatef(0.f, yoffset, 0.f); // move to start
    glTranslatef(0.f, ceilHeight * scale[0], 0.f);
    DrawLight();

    glDisable(GL_LIGHTING);

    glPushAttrib(GL_ALL_ATTRIB_BITS);
    // Match colour of sphere to diffuse colour of light
    glColor4fv(specular);
    glTranslatef(0.f, -10.0f * scale[1], 0.f);
    glutSolidSphere(5.0f * scale[0], 25, 25);

    glPopAttrib();
    glPopMatrix();
    // Re-enable lighting after light source has been drawn
    glEnable(GL_LIGHTING);

    // Set properties GL_LIGHT0
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
    //glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0001f);

    GLfloat pos[4] = { 0.f, 950.f, 0.f, 1.f };
    glLightfv(GL_LIGHT0, GL_POSITION, pos);

    // enable GL_LIGHT0 with these defined properties
    glEnable(GL_LIGHT0);
}

I expected to have a single source of light hanging in the centre of the scene, with light being emitted equally in all directions from its position however a trail of light seems to be emitted as a spotlight instead. 我希望场景的中心悬挂一个单一的光源,该光源从其位置向各个方向均等地发出光,但是似乎有一束光代替聚光灯发出。

Here is an image showcasing the issue: 这是展示问题的图像:

As you can see there is an odd line of light being emitted. 如您所见,有一条奇异的光在发出。

When the light position is set by glLightfv(GL_LIGHT0, GL_POSITION, pos) , then pos is multiplied by the current model view matrix. 当通过glLightfv(GL_LIGHT0, GL_POSITION, pos)设置光源位置时, pos将乘以当前模型视图矩阵。

The intensity of the ambient light ( Ia ), diffuse light ( Id ) and specular light ( Is ), of the Blinn–Phong reflection model is calculated as follows: Blinn-Phong反射模型的环境光( Ia ),扩散光( Id )和镜面光( Is )的强度计算如下:

N  ... norlmal vector 
L  ... light vector (from the vertex position to the light) 
V  ... view vector (from the vertex position to the eye)
sh ... shininess

H     = normalize(V + L)
NdotH = max(dot(N, H), 0.0)

Ia    = 1
Id    = max(dot(N, L), 0.0)
Is    = pow(NdotH, sh)

So the ambient light ( GL_AMBIENT ) is independent of any direction. 因此,环境光( GL_AMBIENT )与任何方向无关。

The diffuse light ( GL_DIFFUSE ) depends on the normal vector of the surface ( N ) and the direction of the incident light ( L ). 漫射光( GL_DIFFUSE )取决于表面的法线向量( N )和入射光的方向( L )。 It stays constant on the lit surface, independent on the point of view. 它在照明表面上保持恒定,与视角无关。

The specular light ( GL_SPECULAR ) depends on the surfaces normal vector ( N ), the light direction ( L ). 镜面光( GL_SPECULAR )取决于表面法线向量( N ),光方向( L )。 and the viewing direction ( V ). 以及观看方向( V )。 This causes that the specular highlights change, when you move in the scene, because the viewing direction to the lit surfaces changes. 当您在场景中移动时,这会导致镜面反射高光发生变化,因为到光照表面的观看方向发生了变化。

Further note that the light calculations in the deprecated OpenGL fixed function light model are done per vertex ( Gouraud Shading ). 还要注意,已弃用的OpenGL固定功能光照模型中的光照计算是针对每个顶点进行的( Gouraud着色 )。 The calculated light is interpolated on the area, between the the corners of the primitives. 计算出的光将插值在图元的角之间的区域上。 A modern implementation would be to do the light calculation per fragment ( Phong shading ). 一个现代的实现是对每个片段进行光照计算( Phong阴影 )。 Gouraud Shading cause the spotted look of the specular highlights at the ceiling and may increase an unexpected look. Gouraud阴影会导致天花板上镜面高光的斑点外观,并可能增加意外外观。 Tessellating the areas in smaller peaces may improve that, but the best solution would be to switch to modern OpenGL write a Shader and implement a per fragment light model for your needs. 在较小的区域中对区域进行镶嵌可以改善这一点,但是最好的解决方案是切换到现代 OpenGL中编写着色器,并根据您的需要实现每个片段的灯光模型。

Your pop your matrix right before setting the lighting position. 在设置照明位置之前,请弹出矩阵。 As a result your position of your light is always what you set it too. 结果,您的灯光位置也始终是您设置的位置。 You are then multiplying everything else by your View Matrix I assume. 然后,您将其他所有内容乘以我假设的视图矩阵。 The View matrix essentially transforms the world into the view of the camera. View矩阵实质上将世界转化为相机的视图。 Essentially a camera doesn't move, the world moves around the camera. 本质上,相机不动,世界围绕相机移动。 Since the light is not multiplied by this view matrix ALSO you get a light that appears to maintain a constant position relative to the camera. 由于光线并未与此视图矩阵相乘,因此您获得的光线似乎相对于摄像机保持恒定的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM