为什么在glOrtho之后镜面不起作用?

时间:2012-08-29 05:03:30

标签: opengl

我尝试了很多不同的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;
}

2 个答案:

答案 0 :(得分:1)

您尚未为GL_QUADS指定任何glNormal。您可能还需要考虑glEnable(GL_NORMALIZE)以在模型视图转换后保留单位长度。

答案 1 :(得分:0)

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