C++:在 OpenGL 中绘制 2D 磁盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5094992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Drawing a 2D disk in OpenGL
提问by Janx
I've tried to write a proper function for drawing a 2D disk on the screen with OpenGL for a few days now, and I simply can't seem to get it right :(
几天来,我一直在尝试编写一个适当的函数来使用 OpenGL 在屏幕上绘制 2D 磁盘,但我似乎无法正确完成:(
This is my current code:
这是我当前的代码:
void Disk( Float x, Float y, Float r, const Color& vColor )
{
glBegin( GL_TRIANGLE_FAN );
glVertex2f( x, y );
for( Float i = 0; i <= 2 * PI + 0.1; i += 0.1 )
{
glVertex2f( x + sin( i ) * r, y + cos( i ) * r );
}
glEnd();
}
When zooming in, the resulting disk shows spikes, not as in edges but really spikes pointing out.
放大时,生成的磁盘会显示尖峰,而不是像边缘那样,而是尖峰指出。
Also the function doesn't draw one disk only, but always a bit more than one - which means that if alpha is enabled, the results look wrong.
此外,该函数不会只绘制一个磁盘,而是总是多于一个——这意味着如果启用了 alpha,结果看起来是错误的。
- What do I need to change in my function so it properly draws a disk?
- 我需要在我的函数中更改什么才能正确绘制磁盘?
回答by datenwolf
void circle(float x, float y, float r, int segments)
{
glBegin( GL_TRIANGLE_FAN );
glVertex2f(x, y);
for( int n = 0; n <= segments; ++n ) {
float const t = 2 * M_PI * (float)n / (float)segments;
glVertex2f(x + sin(t) * r, y + cos(t) * r);
}
glEnd();
}
that should get you rid of the overdraw. About the spikes... a picture could tell a thousand words.
这应该让你摆脱透支。关于尖峰……一张图片可以说出一千个字。