使用glNormal3f法向量繪製立方體

#if 0
#include <GLFW/glut.h>

GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };  /* Red diffuse light. */
GLfloat light_position[] = { 0.0, 0.0, 5.0, 0.0 };  /* Infinite light location. */

GLuint cl1;

GLuint idleCounter = 0;

void
list1(void)
{

    glBegin(GL_QUADS);
    glNormal3f(-1, 0, 0);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, 1, 1);

    glNormal3f(0, 1, 0);
    glVertex3f(-1, 1, 1);
    glVertex3f(-1, 1, -1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, 1, 1);

    glNormal3f(1, 0, 0);
    glVertex3f(1, 1, 1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, -1, -1);
    glVertex3f(1, -1, 1);


    //4

    glNormal3f(0, -1, 0);
    glVertex3f(1, -1, 1);
    glVertex3f(1, -1, -1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, -1, 1);


    //5 +z

    glNormal3f(0, 0, 0);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, 1, 1);
    glVertex3f(1, 1, 1);
    glVertex3f(1, -1, 1);
    
    //6 -z
    glNormal3f(1, 0, 0);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, 1, -1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, -1, -1);
    glEnd();

    glFlush();
    glutSwapBuffers();
}


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glCallList(cl1);
    list1();
}

void idle(void)
{
    if ((idleCounter++ % 0x10000) == 0)
    {
        glRotatef(0.1, 0.0, 0.0, 1.0);
        glutPostRedisplay();
    }
}


void reshape(void)
{

}

void
init(void)
{
    /* Enable a single OpenGL light. */
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    /* Use depth buffering for hidden surface elimination. */
    glEnable(GL_DEPTH_TEST);


    cl1 = glGenLists(1);
    glNewList(cl1, GL_COMPILE);
    list1();
    glEndList();

    /* Setup the view of the cube. */
    glMatrixMode(GL_PROJECTION);
    gluPerspective( /* field of view in degree */ 40.0,
        /* aspect ratio */ 1.0,
        /* Z near */ 1.0, /* Z far */ 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0, 0.0, -10.0,  /* eye is at (0,0,5) */
        0.0, 0.0, 0.0,      /* center is at (0,0,0) */
        0.0, 1.0, 0.0);      /* up is in positive Y direction */

#if 0
    gluLookAt(-10, 0.0, 0.0,  /* eye is at (0,0,5) */
        0.0, 0.0, 0.0,      /* center is at (0,0,0) */
        0.0, 0.0, 1.0);      /* up is in positive Y direction */
    GLfloat light_position[] = { -5.0, 0.0, 0.0, 0.0 };
#endif

                            /* Adjust cube position to be asthetic angle. */
    //glTranslatef(0.0, 0.0, -1.0);
    //glRotatef(60, 1.0, 0.0, 0.0);
    //glRotatef(-20, 0.0, 0.0, 1.0);


}

int
main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("3D lighted cube");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    init();
    glutMainLoop();
    return 0;             /* ANSI C requires main to return int. */
}

#endif
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章