Sierpinski Gasket 3D旋转

时间:2016-02-16 01:04:21

标签: opengl 3d

我试图在OpenGL中创建一个3D旋转Sierpinski垫片。这就是我所拥有的:

/* initial tetrahedron */

GLfloat v[4][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
{-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};

GLfloat colors[4][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}, {0.0, 0.0, 0.0}};

static GLfloat spin = 0.0;
int n;

void triangle(GLfloat *va, GLfloat *vb, GLfloat *vc)
{
    glVertex3fv(va);
    glVertex3fv(vb);
    glVertex3fv(vc);
}


void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d)
{
    glColor3fv(colors[0]);
    triangle(a, b, c);
    glColor3fv(colors[1]);
    triangle(a, c, d);
    glColor3fv(colors[2]);
    triangle(a, d, b);
    glColor3fv(colors[3]);
    triangle(b, d, c);
}

void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m)
{

GLfloat mid[6][3];
int j;
if(m>0)
{
    /* compute six midpoints */

    for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
    for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
    for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
    for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
    for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
    for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;

    /* create 4 tetrahedrons by subdivision */

    divide_tetra(a, mid[0], mid[1], mid[2], m-1);
    divide_tetra(mid[0], b, mid[3], mid[5], m-1);
    divide_tetra(mid[1], mid[3], c, mid[4], m-1);
    divide_tetra(mid[2], mid[4], d, mid[5], m-1);

}
    else(tetra(a,b,c,d)); /* draw tetrahedron at end of recursion */
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    divide_tetra(v[0], v[1], v[2], v[3], n);
    glEnd();
    glFlush();
}

void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
            2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
    else
        glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
            2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    //set static value for part c)
    n=4;
    //n=atoi(argv[1]); /* or enter number of subdivision steps here */
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("3D Gasket");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glEnable(GL_DEPTH_TEST);
    glClearColor (1.0, 1.0, 1.0, 1.0);
    glutMainLoop();
}

现在我的代码创建了实际的垫片本身。我不确定如何使四面体在轴上以3D方式旋转。

这是我第一次使用OpenGL

1 个答案:

答案 0 :(得分:0)

如果你想要动画,你可以使用glutTimerFunc()来注册一个定时器回调,你可以在每次调用时增加一个角度,并告诉GLUT重绘,如下所示:

float angle = 0.0
myTimerFunc()
{
    angle += 0.01
    glutPostRedisplay();
}

display()
{
    // do setup as before, then rotate model
    glMatrixMode(GL_MODELVIEW);
    glRotate(angle, 0.0,1.0,1.0) // rotate around Y axis
    // do drawing
}