我一直在画康奈尔盒子。我想在灯中放光(灯是放在右墙上的白色立方体)。幻灯闪烁。但是我不能把光放到立方体里。我尝试设置视图变换,然后设置灯光位置,但是它不起作用。
这是我的简单代码:(灯箱已移动glTranslatef(0.63, 0.3, 0.0)
)
void display(void)
{
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 600, 600);
glClearColor(0.32, 0.32, 0.32, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw cube
glPushMatrix();
glTranslatef(0.2, -0.3, 0.4);
glColor3d(1.0, 0.38, 0.0);
glutSolidCube(0.2);
glPopMatrix();
// draw sphere
glPushMatrix();
glTranslatef(0.2, -0.1, 0.4);
glColor3d(0.71, 0.0, 0.59);
glutSolidSphere(0.1,100,100);
glPopMatrix();
// draw parallelepiped
glPushMatrix();
glTranslatef(-0.2, -0.2, 0.25);
glRotatef(20, 0.0, 1.0, 0.0);
glColor3d(1.0, 0.38, 0.0);
glScalef(1, 1.8, 1);
glutSolidCube(0.2);
glPopMatrix();
// draw lamp
glPushMatrix();
glTranslatef(0.63, 0.3, 0.0);
glScalef(1.5, 0.3, 1);
glColor3d(1.0, 1.0, 1.0);
glutSolidCube(0.2);
glPopMatrix();
glBegin(GL_QUADS);
glNormal3f(0, 1.0, 0);
glColor3d(0.69, 0.69, 0.69); // bottom
glVertex3d(-0.5, -0.5, 0.5);
glVertex3d(-0.5, -0.5, -0.5);
glVertex3d(0.5, -0.5, -0.5);
glVertex3d(0.5, -0.5, 0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0, -1.0, 0);
glColor3d(0.69, 0.69, 0.69); // top
glVertex3d(-0.5, 0.5, 0.5);
glVertex3d(-0.5, 0.5, -0.5);
glVertex3d(0.5, 0.5, -0.5);
glVertex3d(0.5, 0.5, 0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0, 0.0);
glColor3d(0.0, 0.79, 0.05); // right
glVertex3d(0.5, -0.5, 0.5);
glVertex3d(0.5, 0.5, 0.5);
glVertex3d(0.5, 0.5, -0.5);
glVertex3d(0.5, -0.5, -0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(1.0, 0.0, 0.0);
glColor3d(1.0, 0.16, 0.0); // left
glVertex3d(-0.5, -0.5, 0.5);
glVertex3d(-0.5, 0.5, 0.5);
glVertex3d(-0.5, 0.5, -0.5);
glVertex3d(-0.5, -0.5, -0.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glColor3d(0.69, 0.69, 0.69); // back
glVertex3d(-0.5, -0.5, -0.5);
glVertex3d(-0.5, 0.5, -0.5);
glVertex3d(0.5, 0.5, -0.5);
glVertex3d(0.5, -0.5, -0.5);
glEnd();
glFlush();
glPopMatrix();
glutSwapBuffers();
}
void Initialize() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat global_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
GLfloat ambient[] = { 0.0,0.0,0.0,1.0 };
GLfloat diffuse[] = { 1.0,1.0,1.0,1.0 };
GLfloat spec[] = { 1,1,1,1 };
GLfloat specref[] = { 1,1,1,1 };
GLfloat lpos[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specref);
glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 64);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
}
void exitFunc(unsigned char key, int x, int y) {
switch (key) {
case 27:
exit(0);
break;
}
}
void reshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.35, 0.35, -0.35, 0.35, 1.2, 400.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0, 0, -2);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Cornell Box");
Initialize();
glutDisplayFunc(display);
glutKeyboardFunc(exitFunc);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
答案 0 :(得分:3)
请注意,自几十年来,不赞成使用glBegin
/ glEnd
序列绘制每个顶点光模型的固定功能管线矩阵堆栈和固定功能管线。
阅读有关Fixed Function Pipeline的信息,并参阅Vertex Specification和Shader了解最新的渲染方式。
无论如何,当灯光位置由glLightfv(GL_LIGHT0, GL_POSITION, pos)
设置时,
pos
乘以当前模型视图矩阵。
这意味着,如果在设置模型视图矩阵之前设置位置,则将灯光位置放置在相对于世界的绝对坐标中。
如果在设置模型视图矩阵之后进行设置,则可以在模型的坐标系中设置灯光位置。
将抽灯时的灯光位置设置到“灯”的中心:
// draw lamp
glPushMatrix();
glTranslatef(0.63, 0.3, 0.0);
glScalef(1.5, 0.3, 1);
GLfloat lpos[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glColor3d(1.0, 1.0, 1.0);
glutSolidCube(0.2);
glPopMatrix();
OpenGL固定功能管线计算每个顶点的光:Gouraud shading。
如果光矢量以相对于表面平面的锐角撞击顶点坐标,则可能导致您无法“看到”光。
在您的情况下,绿色墙壁几乎没有照明,因为光源几乎直接在墙壁上。 lam本身不发光,因为光源在灯内部,并且是从背面发出的光。
请参见OpenGL Lighting on texture plane is not working
稍微改变灯光位置,将其放在墙的前面,并将右墙细分为4个四边形,以“看到”我的意思:
GLfloat lpos[] = { -0.1, 0.0, 0.0, 1.0 };
// right
glBegin(GL_QUADS);
glNormal3f(-1.0, 0.0, 0.0);
glColor3d(0.0, 0.79, 0.05);
glVertex3d(0.5, 0.3, 0.5);
glVertex3d(0.5, 0.5, 0.5);
glVertex3d(0.5, 0.5, 0.0);
glVertex3d(0.5, 0.3, 0.0);
glVertex3d(0.5, 0.3, 0.0);
glVertex3d(0.5, 0.5, 0.0);
glVertex3d(0.5, 0.5, -0.5);
glVertex3d(0.5, 0.3, -0.5);
glVertex3d(0.5, -0.5, 0.5);
glVertex3d(0.5, 0.3, 0.5);
glVertex3d(0.5, 0.3, 0.0);
glVertex3d(0.5, -0.5, 0.0);
glVertex3d(0.5, -0.5, 0.0);
glVertex3d(0.5, 0.3, 0.0);
glVertex3d(0.5, 0.3, -0.5);
glVertex3d(0.5, -0.5, -0.5);
glEnd();