Difference between positioned and direct light in

2019-09-14 16:45发布

问题:

I just did a tutorial from videotutorialsrock.com on lighting in C++ OpenGL. I understand ambient light, but don't understand the difference between positioned light and direct light, since the idea and the code of both looked very similar. Here's my code for positioned light:

//Add positioned light
GLfloat lightColor0[] = {.6, .6, .6, 1};
GLfloat lightPos0[] = {4, 0, 8, 1};
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

And for directional light:

//Add direct light
GLfloat lightColor1[] = {.5, .2, .2, 1};
GLfloat lightPos1[] = {-1, .5, .5, 0};
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);

Could someone explain?

回答1:

In directional, the light is only coming from a single direction, while a point light radiates light out in every direction from it.

Directional light can be used to simulate a point light extremely far away, such as the sun shining on earth.

(image credit to okino.com)



回答2:

Different w in the position vector(xyzw).

GLfloat lightPos0[] = {4, 0, 8, 1};
w = 1 means point (3d position)

GLfloat lightPos1[] = {-1, .5, .5, 0};
w = 0 means vector (3d direction)

https://gamedev.stackexchange.com/questions/14115/do-i-need-the-w-component-in-my-vector-class