简体   繁体   English

OpenGL的。 移动光源

[英]OpenGL. Moving light source

in my program I use OpenTK with C#. 在我的程序中,我将OpenTK与C#结合使用。 And, I have a trouble with light source. 而且,我在光源方面遇到麻烦。 I can't tie it to the camera. 我不能把它绑在相机上。 It only stay on fixed position. 它仅停留在固定位置。 Here is code of glControl1_Load(): 这是glControl1_Load()的代码:

     float[] light_ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
        float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
        float[] light_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
        float[] spotdirection = { 0.0f, 0.0f, -1.0f };

        GL.Light(LightName.Light0, LightParameter.Ambient, light_ambient);
        GL.Light(LightName.Light0, LightParameter.Diffuse, light_diffuse);
        GL.Light(LightName.Light0, LightParameter.Specular, light_specular);

        GL.Light(LightName.Light0, LightParameter.ConstantAttenuation, 1.8f);
        GL.Light(LightName.Light0, LightParameter.SpotCutoff, 45.0f);
        GL.Light(LightName.Light0, LightParameter.SpotDirection, spotdirection);
        GL.Light(LightName.Light0, LightParameter.SpotExponent, 1.0f);

        GL.LightModel(LightModelParameter.LightModelLocalViewer, 1.0f);
        GL.LightModel(LightModelParameter.LightModelTwoSide, 1.0f);
        GL.Enable(EnableCap.Light0);
        GL.Enable(EnableCap.Lighting);
        GL.Enable(EnableCap.DepthTest);
        GL.Enable(EnableCap.ColorMaterial);
        GL.ShadeModel(ShadingModel.Flat);

glControl1_Paint(): glControl1_Paint():

        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref cameramatrix);
        GL.Light(LightName.Light0, LightParameter.Position, new float[]{0.0f, 0.0f, 0.0f, 1.0f});

If I'm not wrong, the coordinates light source stored in eye space coord. 如果我没看错,则将坐标光源存储在眼睛空间中。 So, what's wrong? 那怎么了

LoadIdentity instead of your camera matrix for the model view. 使用LoadIdentity代替模型视图的相机矩阵。 Your light source will always stay at the same location relative to your camera. 光源将始终位于相对于相机的相同位置。

"If the w value is nonzero, the light is positional, and the (x, y, z) values specify the location of the light in homogeneous object coordinates. (See Appendix F.) This location is transformed by the modelview matrix and stored in eye coordinates." “如果w值不为零,则光源处于位置,并且(x,y,z)值指定光源在齐次物体坐标中的位置。(请参阅附录F。)此位置由模型视图矩阵转换并存储在眼坐标上。”

more details here Look for "Example 5-7" 在此处查看更多详细信息寻找“示例5-7”

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

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