OpenGL 旋轉(zhuǎn)的正三棱錐

#include <windows.h>
#include <GL/glut.h>

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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容