简体   繁体   中英

OpenGL put circle inside an ellipse using GL_TRIANGLE_FAN?

I'm trying to draw an ellipse and put a circle inside of it, using different colors, of course. I can draw the shapes by themselves just fine, but can't get them to appear on screen at the same time.

Here is the code I have so far:

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

#include <math.h>


#define PI 3.14159

const int ScreenWidth = 640;
const int ScreenHeight = 480;


void myInit(void)
{
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, (GLdouble)ScreenWidth, 0.0, (GLdouble)ScreenHeight);
}


void drawFilledElipse(GLfloat x, GLfloat y, GLfloat radius)
{
    int i;
    int triangleAmount = 20; //# of triangles used to draw circle
    glColor3f(0.0f, 0.0f, 0.0f);

    GLfloat twicePi = 2.0f * PI;

    GLint x1 =320, y1 = 240, r1 = 75;

    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(x, y); // center of circle
    for (i = 0; i <= triangleAmount; i++) {
        glVertex2f(
            1.4   * ( x + (radius * cos(i *  twicePi / triangleAmount))),
            1.111 * (y + (radius * sin(i * twicePi / triangleAmount)))
        );
    }

    glutSwapBuffers();
}

void drawFilledCircle(GLfloat x, GLfloat y, GLfloat radius)
{
    int i;
    int triangleAmount = 20; //# of triangles used to draw circle
    glColor3f(1.0f, 0.0f, 0.0f);

    GLfloat twicePi = 2.0f * PI;

    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(x, y); // center of circle
    for(i = 0; i <= triangleAmount;i++) 
    {
        glVertex2f(
            (x + (radius * cos(i * twicePi / triangleAmount))),
            (y + (radius * sin(i * twicePi / triangleAmount)))
        );
    }

    glutSwapBuffers();
}

void myDisplay(void)
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    drawFilledCircle(320,240,75);
    drawFilledElipse(225,240,100);

    glEnd();
    glFlush();
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(ScreenWidth, ScreenHeight);
    glutInitWindowPosition(100, 150);
    glutCreateWindow("Random dots...");
    glutDisplayFunc(myDisplay);
    myInit();
    glutMainLoop();
    return 0;
}

You made a copy-and-paste error when you broke your drawing out into two separate functions. You called glutSwapBuffers after both . You only want to swap buffers after you're finished drawing everything , not just one object.

In addition to Nicol's comments, you haven't matched your glBegin calls to your glEnd calls. Your drawFilledCircle and drawFilledElipse routines should end with a call to glEnd each. It's an error in OpenGL to call glBegin after glBegin without an intervening glEnd .

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