OpenGL 旋转的正三棱锥

#include 
#include 

GLint width=300,height=300;
static GLdouble angle,displace,scale=1,rPace,tPace;
GLdouble rAcc=0.0001d,tAcc=0.0000001d,state=1;
GLint interval=4096;

void triangle(GLfloat x1,GLfloat y1,GLfloat z1,GLfloat x2,GLfloat y2,GLfloat z2,GLfloat x3,GLfloat y3,GLfloat z3)
{
    glBegin(GL_TRIANGLES);
    glVertex3f(x1,y1,z1);
    glVertex3f(x2,y2,z2);
    glVertex3f(x3,y3,z3);
    glEnd();
}

void init(void)
{
    glClearColor(1,1,1,1);
    glEnable(GL_DEPTH_TEST);
}

void reshape(GLint w,GLint h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(0,(GLfloat)w/(GLfloat)h,1,30);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(-45,1,0,0);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glTranslatef(0,displace,0);
    glRotatef(angle,0,1,0);
    glScalef(scale,scale,scale);
    // a(0,0.12,0) b(-0.5,-0.7,-0.29) c(0.5,-0.7,-0.29) d(0,-0.7,0.58)
    glColor3f(1,0,0); // abc
    triangle(0,0.12,0,-0.5,-0.7,-0.29,0.5,-0.7,-0.29);

    glColor3f(0,1,0); // abd
    triangle(0,0.12,0,-0.5,-0.7,-0.29,0,-0.7,0.58);

    glColor3f(0,0,1); // acd
    triangle(0,0.12,0,0.5,-0.7,-0.29,0,-0.7,0.58);

    glColor3f(0.5,0.5,0.5); // bcd
    triangle(-0.5,-0.7,-0.29,0.5,-0.7,-0.29,0,-0.7,0.58);
    glPopMatrix();
    glFlush();
}

void idle()
{
    if (state>2*interval)
        state=1;

    if (state++<=interval)
    {
        angle+=rPace;
        displace+=tPace;
        scale-=0.8/interval;
        rPace+=rAcc;
        tPace+=tAcc;
    }
    else
    {
        angle+=rPace;
        displace-=tPace;
        scale+=0.8/interval;
        rPace-=rAcc;
        tPace-=tAcc;
    }
    glutPostRedisplay();
}

int main()
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width,height);
    glutCreateWindow("Tetrahedron");
    glutIdleFunc(idle);
    init();
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

你可能感兴趣的:(OpenGL 旋转的正三棱锥)