gberes
gberes

Reputation: 1169

Opengl light direction

I'm trying to set up a simple directional light in opengl. My scene init code:

float light_position[] = { 0, -1,0, 0.0f };
float light_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f };
float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };

float mat_ambient[] = { 1.0f, 0.0f, 0.0f, 1.0f };
float mat_diffuse[] = { 0.0f, 1.0f, 0.0f, 1.0f };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);

Then I render just one triangle:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);


glBegin(GL_TRIANGLES);
    glNormal3f(0,1,0);
    glVertex3f(0,-1,-2.0f);
    glNormal3f(0,1,0);
    glVertex3f(-1,-1,-1);
    glNormal3f(0,1,0);
    glVertex3f(1,-1,-1);
glEnd();

I except that the light is like the sun, shining from up to down (towards -infinity on y axis), so my triangle should be yellow (red ambient material + green diffuse), but my triangle is red, like if there aren't any light. If I change the light position to {0, 1, 0, 0}, then the triangle become yellow. Why? The light direction is from the position set to (0,0,0)?

Upvotes: 0

Views: 9801

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32597

The interpretation is the opposite. A light position of { 0, -1,0, 0.0f, 0.0f } defines a light in the downwards direction, shining upwards. Therefore, this light does not illumniate your triangle, while a light at { 0, 1,0, 0.0f, 0.0f } does.

Upvotes: 3

Related Questions