简体   繁体   中英

How to set position of light source in opengl?

I am trying to understand the effect of altering the pointed light source position in opengl but having trouble with it. Can somebody explain how different position effect the lighting of model ?

The code I was using is as follow -

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat lightPos0[] = {0.0f,0.0f, -1.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//gluLookAt(0, 6, 6.0, 0, -2, 0, 0.0f, 1.0f, 0.0f);



glTranslatef(0,0,-6);

GLfloat ambientColor[] = {0.1f, 0.1f, 0.1f, 1.0f};

glRotatef(50,1,0,0);

glRotatef(-45,0,1,0);

glBegin(GL_QUADS);


glNormal3f(-1,0,0);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1,  1);
glColor3f(0, 1, 1); glVertex3f(-1,  1,  1);
glColor3f(0, 1, 0); glVertex3f(-1,  1, -1);
glNormal3f(1,0,0);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glColor3f(1, 0, 1); glVertex3f( 1, -1,  1);
glColor3f(1, 1, 1); glVertex3f( 1,  1,  1);
glColor3f(1, 1, 0); glVertex3f( 1,  1, -1);
glNormal3f(0,-1,0);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1,  1);
glColor3f(1, 0, 1); glVertex3f( 1, -1,  1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);


glNormal3f(0,0,-1);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 1, 0); glVertex3f(-1,  1, -1);
glColor3f(1, 1, 0); glVertex3f( 1,  1, -1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glNormal3f(0,0,1);
glColor3f(0, 0, 1); glVertex3f(-1, -1,  1);
glColor3f(0, 1, 1); glVertex3f(-1,  1,  1);
glColor3f(1, 1, 1); glVertex3f( 1,  1,  1);
glColor3f(1, 0, 1); glVertex3f( 1, -1,  1);

glEnd();
glTranslatef(-1,1,0);
glRotatef(a,0,0,1);
glTranslatef(1,-1,0);

glBegin(GL_QUADS);
glNormal3f(0,1,0);
glColor3f(0, 1, 0); glVertex3f(-1,  1, -1);
glColor3f(0, 1, 1); glVertex3f(-1,  1,  1);
glColor3f(1, 1, 1); glVertex3f( 1,  1,  1);
glColor3f(1, 1, 0); glVertex3f( 1,  1, -1);

glEnd();
glFlush();
if(a>90)
    a=a-90;
a=a+1;
glutSwapBuffers();
glutPostRedisplay();
    }

I was expecting that light(0,0,-1) will shine on cube from front as I had already translated the cube to -6 . But the light was coming from back.

OpenGL doesn't put objects in reference to (0,0,0) after a transform has been performed. As you've translated the cube object, you changed the reference point to the center of the cube. Anything that you're transformaing and drawing after that will be offset from wherever the cube has been centered. You need to push and pop the transformation matrices.

glPushMatrix()

and

glPopMatrix()

Related Forum Post

If I remember correctly, this would be the format that you'd have to follow:

glPushMatrix();
    glTranslatef(0,0,-6);
    GLfloat ambientColor[] = {0.1f, 0.1f, 0.1f, 1.0f};
    glRotatef(50,1,0,0);
    glRotatef(-45,0,1,0);
    glBegin(GL_QUADS);
    //DRAW THE OBJECT
    glEnd();
glPopMatrix();
glPushMatrix();
    glTranslate(0,0,-1);
    //Draw the light
glPopMatrix();

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