opengl Draw Oval not Circle

时间:2014-06-27 13:22:00

标签: c++ opengl math geometry geometry-shader

如果我调用函数DrawFilledCircleo()它会形成一个椭圆形而不是圆形,即使我放了glBegin(GL_LINE_LOOP);它总是让Oval

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

    //GLfloat radius = 0.8f; //radius
    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))
            );
        }
    glEnd();
}

我为绘制圆圈尝试了不同的功能,但它总是使椭圆形。

我把它放在:

static void Draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glViewport(0, 0, widthX, heightY);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    texture[0] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        "img.jpg",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
         SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
    );
// allocate a texture name
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_POLYGON);

    glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  0.0f);

    glEnd();

    drawFilledCircle(0.5f,0.5f,0.1f);

    glutSwapBuffers();
}

更新:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(widthX, heightY);
    glutCreateWindow("earthQuake");
    glutReshapeFunc(resize);
    glutDisplayFunc(Draw);
    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);
    /////////////////////////////////////
    glEnable(GL_TEXTURE_2D);
   // glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    //glClearDepth(1.0f);
    //glEnable(GL_DEPTH_TEST);
   // glDepthFunc(GL_LEQUAL);
   glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glutMainLoop();

widthX = 800; heightY = 400;

2 个答案:

答案 0 :(得分:1)

很可能您的视口和窗口大小比例未对齐。

以下回答可能会有所帮助: https://stackoverflow.com/a/15725280/3301533

编辑:

感谢此代码:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

你有你的椭圆形。尝试将其更改为:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-widthX/2, width/X2, -heightY/2, heightY/2, -1, 1);

答案 1 :(得分:0)

之前的答案显示了获得圆圈的一种方法,其副作用是必须更改坐标。我认为这个解决方案通常更方便,因为你仍然可以用你已经拥有的相同坐标绘制:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double aspectRatio = (double)width / (double)height;
glOrtho(-aspectRatio, aspectRatio, -1.0, 1.0, -1.0, 1.0);