简体   繁体   中英

OpenGL. Moving light source

in my program I use OpenTK with 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():

     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():

        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. 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."

more details here Look for "Example 5-7"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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