2

当然,我正在尝试绘制一个椭圆并在其中放置一个圆圈,使用不同的颜色。我可以自己绘制形状,但不能让它们同时出现在屏幕上。

这是我到目前为止的代码:

#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;
}
4

2 回答 2

3

除了 Nicol 的评论之外,您还没有将您的glBegin呼叫与您的glEnd呼叫匹配。您的drawFilledCircledrawFilledElipse例程应该以调用glEnd每个结束。glBeginglBegin没有干预的情况下调用是 OpenGL 中的错误glEnd

于 2013-02-11T05:47:10.343 回答
3

当您将绘图分成两个单独的函数时,您犯了一个复制和粘贴错误。你两个都打glutSwapBuffers电话。您只想在绘制完所有内容后交换缓冲区,而不仅仅是一个对象。

于 2013-02-11T03:54:32.213 回答