1

我正在尝试使用 opengl绘制一个 6 面的方尖碑。

下面是我用来模拟方尖碑底面的代码:

void drawObelisk() {
    glColor3f(1.0,1.0,0.0);

    glBegin(GL_POLYGON);
    for ( x=0.0; x<2.0*pi/3; x=x+inc )
    {   
        glVertex3f(0.3*cos(x),0.3*sin(x),0.0);
    }
    glEnd();
}

此代码用于渲染模型:

void myDisplay(void)
{
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    drawObelisk();
    glPopMatrix();
    glFlush();
}

void SetupRC(void)
{
    glClearColor(0.0, 0.0, 1.0, 1.0);
    glOrtho(-6.0,6.0,-6.0,6.0,-6.0,6.0);
}

void main(void)
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,15);
    glutCreateWindow("Test");
    glutDisplayFunc(myDisplay);
    SetupRC();
    glutIdleFunc(idle);
    glutMainLoop();
}

由于某些原因,我绘制的多边形表面在模型渲染后并没有显示在屏幕上。

4

1 回答 1

1

试一试:

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

void drawObelisk() 
{
    double pi = 3.14159;
    double inc = (2*pi) / 6;

    glColor3f(1.0,1.0,0.0);

    glBegin(GL_POLYGON);
    for( double theta = 0.0; theta < 2*pi; theta += inc )
    {   
        double scale = 2.0;
        double x = scale * cos( theta );
        double y = scale * sin( theta );
        glVertex3d(x,y,0.0);
    }
    glEnd();
}

void myDisplay(void)
{
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);

    glClearColor(0.0, 0.0, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-6.0,6.0,-6.0,6.0,-6.0,6.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    drawObelisk();
    glPopMatrix();

    glFlush();
}

void main( int argc, char** argv )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,15);
    glutCreateWindow("Test");
    glutDisplayFunc(myDisplay);
    glutMainLoop();
}

输出

不靠谱的事情:

于 2012-07-29T04:23:56.607 回答