1

我想让这个房间有灯泡效果,但我不确定它有什么问题。到目前为止,光线似乎只是照在球体的顶部和圆锥体的侧面,而墙壁没有得到任何光线。我试着摆弄位置和方向,但没有任何效果。

#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
    glClearColor(0,0,0,1);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,1,1,100);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    //glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,10,0);
    glVertex3f(10,10,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glVertex3f(0,10,0);
    glVertex3f(0,10,10);
    glVertex3f(10,10,10);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glVertex3f(10,10,0);
    glVertex3f(10,10,10);
    glVertex3f(10,0,10);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    //draw cone
    glShadeModel(GL_FLAT);
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    glutSolidCone(cone,5,10,2);
    //draw sphere
    glShadeModel(GL_SMOOTH);
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    glutSolidSphere(sphere,500,500);

}
void move(unsigned char key, int x, int y){
    switch(key){
        case 'y': 
            viewy++;
            glutPostRedisplay();
            break;
        case 'x':
            viewx++;
            glutPostRedisplay();
            break;
        case 'z':
            viewz++;
            glutPostRedisplay();
            break;
        case 'Z':
            viewz--;
            glutPostRedisplay();
            glutPostRedisplay();
            break;
        //moves head
        case 'd':
            headup--;
            headright--;
            glutPostRedisplay();
            break;
        case 'a':
            headup++;
            headright++;
            glutPostRedisplay();
            break;
    }
}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    drawRoom();
    GLfloat light_position[] = {5.0,5.0,10.0,0.0};
    GLfloat light_direction[] = {-5,-5,-5};
    GLfloat amb[] = {0,0,0,1};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction);
    glLightfv(GL_LIGHT0, GL_AMBIENT,amb);
    drawObjects();
    glutSwapBuffers();
    glFlush();
}
int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Room");
    myinit();
    glutDisplayFunc(display);
    glutKeyboardFunc(move);
    //glutMotionFunc(moveHead);
    glutMainLoop();
    return 0;
}
4

1 回答 1

3

第一:您必须提供法线。

第二:OpenGL 固定函数管道仅在顶点评估照明,然后进行重心颜色插值。盒子内部某处有光源的角落接收的光线最少。使用仅由 6 个四边形组成的盒子,您不会得到很好的衰减,而只会得到昏暗的墙壁。您要么必须使用片段着色器来实现每个片段照明(推荐),要么将四边形细分为形成墙壁的合理数量的较小四边形,以便有足够的插值采样点来评估照明。

于 2012-11-20T22:16:06.297 回答