简体   繁体   中英

Circle doesn't appear on the screen

The following code doesn't show the circle on the screen, why doesn't work? I can't see any error.

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);

    int circle_points=100;
    int i;
    double theta,cx=200, cy=300,r=100;

    int MyCircle(){
        glBegin(GL_LINE_LOOP);
        glColor3f(1.0,1.0,1.0); //preto
        for(i=0;i<circle_points;i++){
            theta=(2*pi*i)/circle_points;
            glVertex2f(cx+r*cos(theta),cy+r*sin(theta));
        }
        glEnd();
    }
    glFlush();
}

No idea why you are trying to declare a function inside a function. I'm not quite sure how that compiled, much less ran.

The logic is sound though:

在此处输入图片说明

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

void MyCircle( void )
{
    const int circle_points=100;
    const float cx=0, cy=0, r=100;
    const float pi = 3.14159f;
    int i = 0;

    glBegin(GL_LINE_LOOP);
    glColor3f(1.0,1.0,1.0); //preto
    for(i=0;i<circle_points;i++)
    {
        const float theta=(2*pi*i)/circle_points;
        glVertex2f(cx+r*cos(theta),cy+r*sin(theta));
    }
    glEnd();
}

void display( void )
{
    const double w = glutGet( GLUT_WINDOW_WIDTH );
    const double h = glutGet( GLUT_WINDOW_HEIGHT );
    const double ar = w / h;

    glClear( GL_COLOR_BUFFER_BIT );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -150 * ar, 150 * ar, -150, 150, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    MyCircle();

    glutSwapBuffers();
}

int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );
    glutCreateWindow( "GLUT" );
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM