0

我尝试了许多不同的 GL_SPECULAR,但没有一个能提供我期望的闪亮效果。

代码有什么问题?

GLfloat mat_ambient[]={0.329412, 0.223529, 0.027451};                                                                                                                                                                                                                         
GLfloat mat_diffuse[]={0.780392, 0.568627, 0.113725};
GLfloat mat_specular[]={0.992157, 0.941176, 0.807843};
GLfloat mat_shininess[]={0.21794872 * 128};


GLfloat light_position[]={1.0,1.0,1.0,0.0};
GLfloat light_ambient[]={1.0,1.0,1.0,1.0};
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light_specular[]={1.0,1.0,1.0,1.0};

unsigned char last_key = 0;
unsigned char channel = 'r';

void init(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_SMOOTH);

    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
    glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
    glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
    glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);

    glLightfv(GL_LIGHT0,GL_POSITION, light_position);
    glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0,GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0,GL_SPECULAR, light_specular);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
}


void display(void) {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    const int length = 100;
    glBegin(GL_QUADS);
        glVertex2f(50, 50);
        glVertex2f(50 + length, 50);
        glVertex2f(50 + length, 50 + length);
        glVertex2f(50, 50 + length);
    glEnd();
    glFlush();
}


void reshape(int w, int h) {
    glViewport(0,0,(GLsizei) w,(GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, w, 0, h,-10.0,10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc,char**argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("lighting demo");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
4

2 回答 2

1

您没有. GL_QUADS您可能还需要考虑glEnable(GL_NORMALIZE)在模型视图转换后保留单位长度。

于 2012-08-29T12:44:50.017 回答
0

我认为该代码没有任何问题,但您没有显示您正在绘制的内容。如果您没有正确的顶点法线,那么光泽将不起作用。

于 2012-08-29T06:22:36.833 回答