简体   繁体   English

如何在opengl中设置光源的位置?

[英]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. 我试图了解在opengl中更改指向光源位置的效果,但是遇到了麻烦。 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 . 我期望light(0,0,-1)会从前面照在立方体上,因为我已经将立方体转换为-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. 执行转换后,OpenGL不会将对象引用到(0,0,0)。 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() glPushMatrix()

and

glPopMatrix() 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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