简体   繁体   English

OpenGL的圆形绘图变得椭圆形

[英]OpenGL circle drawing getting an ellipse

I'm trying to draw a circle in opengl, but i can't seem to get the right coordinates, so i always get an ellipsis no matter what method i use. 我试图在opengl中画一个圆,但是我似乎无法获得正确的坐标,因此无论使用哪种方法,我总是会得到省略号。 The current code is as follows: 当前代码如下:

void Ball::Render() {
    float center_position_x = _body->GetWorldCenter().x;
    float center_position_y = _body->GetWorldCenter().y;
    float radius = static_cast<b2CircleShape*>(_body->GetFixtureList()->GetShape())->m_radius;

    glBegin(GL_LINE_STRIP);
        for(float angle = 0; angle <= 360; ++angle) {
            float angleInRadians = glm::radians(angle);
            float x = glm::cos(angleInRadians);
            float y = glm::sin(angleInRadians);

            glVertex3f(x,y,0);
        }
    glEnd();
}

( I know that code should draw the circle at the origin, but even then it's not a perfect circle, if i understand correctly that should draw a circle at the origin with radius=1 ) (我知道代码应该在原点处画一个圆,但是即使这样我也不是一个完美的圆,如果我正确理解的话,应该在半径= 1的原点处画一个圆)

The other methods i used were: http://slabode.exofire.net/circle_draw.shtml http://www.opengl.org/discussion_boards/showthread.php/167955-drawing-a-smooth-circle 我使用的其他方法是: http : //slabode.exofire.net/circle_draw.shtml http://www.opengl.org/discussion_boards/showthread.php/167955-drawing-a-smooth-circle

This is my OpenGL window setup code (It's a stub program so i'm starting with hardcoded values): gluOrtho2D(-10, 15, -10, 15); 这是我的OpenGL窗口设置代码(这是一个存根程序,所以我从硬编码值开始): gluOrtho2D(-10, 15, -10, 15);

Use gluOrtho2D(-WIDOWS_WIDTH, WIDOWS_WIDTH, -WINDOWS_HEIGHT, WINDOWS_HEIGHT); 使用gluOrtho2D(-WIDOWS_WIDTH,WIDOWS_WIDTH,-WINDOWS_HEIGHT,WINDOWS_HEIGHT);

By the way your are using fixed pipeline in 2013. With a vertex shader this would be much easy to understand. 顺便说一句,您在2013年使用固定管线。有了顶点着色器,这将很容易理解。

The aspect ratio of your 2D projection matrix defined by gluOrtho2d must be same as the viewport/window on which you are rendering otherwise you'll notice distortions in the figures you are rendering. 由gluOrtho2d定义的2D投影矩阵的长宽比必须与您要在其上渲染的视口/窗口相同,否则您会注意到所渲染图形中的变形。 You can use the above gluOrtho2d statement or other way of writing it is - 您可以使用上述gluOrtho2d语句或其他书写方式-

float ar = (float)WindowWidth/WindowHeight; float ar =(float)WindowWidth / WindowHeight;

gluOrtho2D(-1*ar, ar, -1, 1) gluOrtho2D(-1 * ar,ar,-1,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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