C ++:在OpenGL中绘制2D磁盘 [英] C++: Drawing a 2D disk in OpenGL
本文介绍了C ++:在OpenGL中绘制2D磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个适当的函数,使用OpenGL在屏幕上绘制2D光盘几天,我根本看不出来:(
这是我当前的代码:
void Disk(Float x,Float y,Float r,const Color& ; vColor)
{
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x,y);
(Float i = 0; i <= 2 * PI + 0.1; i + = 0.1)
{
glVertex2f(x + sin(i)* r,y + cos(i)* r);
}
glEnd();
}
放大时,生成的磁盘显示峰值,而不是边缘, 。
此函数不仅绘制一个磁盘,而且始终多于一个 - 这意味着如果启用了alpha,则结果会出错。 p>
- 我需要在我的函数中更改哪些内容才能正确绘制磁盘?
解决方案
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 /
glVertex2f(x + sin(t)* r,y + cos(t)* r);
}
glEnd();
}
关于尖峰...一张图片可以讲一千个字。
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 :(
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.
- What do I need to change in my function so it properly draws a disk?
解决方案
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.
这篇关于C ++:在OpenGL中绘制2D磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文