简体   繁体   中英

Ghost light following main light at point of origin

I have a spotlight in my OpenGL project, the light currently shines down the -z axis like a ceiling light, towards the floor, which is just a big quad.

I currently have a cube at the origin of the light, that follows the light around, so I can see exactly where the light is at all times.

My problem is that there is another brighter spotlight that follows the main light, it starts at the origin, or the bottom left corner of the floor.

I'd like to remove this white light, as I don't know what's causing it, or how to remove it.

I have tried to play around with some of the variables and see their impact, but I've had little to no success.

Any assistance would be greatly appreciated.

The error:

错误

//ceiling light
GLfloat Light_Ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat Light_Diffuse[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat Light_Position[] = { Sun.X, Sun.Y, Sun.Z, 1.0f };
GLfloat Spot_Direction[] = { 0.0f, 0.0f, -1.0f };

//ambient
GLfloat Light_Ambient1[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat Light_Diffuse1[] = { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat Light_Position1[] = { Sun.X, Sun.Y, Sun.Z, 1.0f };

//ceiling light
glLightfv(GL_LIGHT0, GL_AMBIENT, Light_Ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light_Diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, Light_Position);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 55);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Spot_Direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 5);
glEnable(GL_LIGHT0);

//ambient
glLightfv(GL_LIGHT1, GL_AMBIENT, Light_Ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, Light_Diffuse1);
glLightfv(GL_LIGHT1, GL_POSITION, Light_Position1);
glEnable(GL_LIGHT1);

I changed some things around and I seem to have solved it.

//ceiling light
GLfloat Light_Ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat Light_Diffuse[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat Light_Position[] = { Sun.X, Sun.Y, Sun.Z, 1.0f };
GLfloat Spot_Direction[] = { 0.0f, 0.0f, -1.0f };

//ambient
GLfloat Light_Ambient1[] = { 0.6f, 0.6f, 0.6f, 1.0f };
GLfloat Light_Diffuse1[] = { 1.0f, 1.0f, 1.0f, 0.0f };
GLfloat Light_Position1[] = { 0, 0, 0, 0.0f };

//ceiling light
glLightfv(GL_LIGHT0, GL_AMBIENT, Light_Ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light_Diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, Light_Position);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 55);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, Spot_Direction);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20);
glEnable(GL_LIGHT0);

//ambient
glLightfv(GL_LIGHT1, GL_AMBIENT, Light_Ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, Light_Diffuse1);
glLightfv(GL_LIGHT1, GL_POSITION, Light_Position1);
glEnable(GL_LIGHT1);

UPDATE:

I'm returning to edit this months later simply because I feel that this answer may help a lot of people with OpenGL and this confusion with lights.

If your lights are broken (like mine were) there's actually a good chance there's nothing wrong with your lights at all, but how you're rendering the world. It's worthwhile to remember that opengl looks a little like this:

https://imgur.com/a/saYxy

What I was actually doing wrong, as you can see from my initial question i was convinced that the Z was the Y and the Y was the Z, and vice versa, this broke all of my lighting calculations back then. If your lights look like mine do, there's a good chance that you're confusing your axis, it isn't that hard to do this when moving from 2d to 3D opengl.

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