OpenGL使用GL_TRIANGLE_FAN将圆放在椭圆内?

标签 opengl

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

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

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

最佳答案

除了 Nicol 的评论之外,您还没有将 glBegin 调用与 glEnd 调用相匹配。您的 drawFilledCircledrawFilledElipse 例程应分别以调用 glEnd 结束。在 OpenGL 中,在 glBegin 之后调用 glBegin 而没有插入 glEnd 是一个错误。

关于OpenGL使用GL_TRIANGLE_FAN将圆放在椭圆内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805632/

相关文章:

opengl - UBO/SSBO 与 Vulkan 的着色器内存绑定(bind)有何不同?

c++ - 对 OpenGL 中着色器的行为感到困惑 - 切换声明会导致错误和崩溃

c++ - 如何在 OpenGL 中的地形上添加单个纹理?

c++ - 让火浮起来

c++ - 为什么我的相机围绕这个数学点旋转?

python - 根据pyqtgraph.opengl中的信号值调整曲面图项的颜色

opengl - 引用OpenGL中激活的程序?

multithreading - OpenGL PBO映射缓冲区: multi-threaded unpack slow, memcpy fast

opengl - Haskell:数据/类型构造函数不在范围内,但数据构造函数在范围内

java - 使用着色器时纹理不渲染?