OpenGL:帮助使用 gl 函数绘制球体/圆

发布于 2024-12-24 23:52:26 字数 1136 浏览 2 评论 0原文

在我的项目中,我需要绘制一个球体(可能是半球体就可以了)。为此,我使用 sin() 和 cos() 方法来计算 (x,y) 坐标的角度。但按照这种方法,我觉得它会影响我的项目的性能。任何人都可以帮助我用简单的 gl 函数绘制一个球体。

像下面一样,我定义了一个结构:

typedef struct
{
    float x;
    float y;
}Semi_Sphere;

Semi_Sphere Sky_SemiSphere[180000], Grnd_SemiSphere[180000];

在下面的方法中,我创建了一个数组来存储 (x,y) 坐标。我在 main() 函数中调用这个方法。

void createSemi_Sphere_Table (void)
{
float angle_d = 1.1f, angle_r=0.0;
float const Gl_Pi = 3.14;
int i = 0;

while ( angle_d < 11.0 )
{
    angle_r = Gl_Pi/angle_d;
    Sky_SemiSphere[i].y = 1.0f + (((3.50)*sin(angle_r)));
    Sky_SemiSphere[i].x = ((3.7)*cos(angle_r)) - 0.52f;

    angle_d = angle_d + 0.001;
    i = i+1;
}

} 

然后,我在下面的方法中使用这些 (x,y) 坐标来绘制我的球体。我在我的drawScene()方法中调用drawSemi_Sphere_Grnd()方法。

void drawSemi_Sphere_Grnd (void)
{
int L_Index = 0;

glPushMatrix();
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glBegin(GL_LINES);
        glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
        glVertex2f(-1.0f, -2.1f);
    glEnd();
}
glPopMatrix();
}

通过上述过程我得到了球体。但性能很慢。

In my project I have a requirement to draw a sphere (may be semi-sphere will be ok). For this purpose I used sin() and cos() methods to calculate the angle for (x,y) co-ordinates. But following this method I feel it is hitting performance of my project. Can anyone help me for drawing a sphere with simple gl functions.

Like below I defined a structure:

typedef struct
{
    float x;
    float y;
}Semi_Sphere;

Semi_Sphere Sky_SemiSphere[180000], Grnd_SemiSphere[180000];

In the below method I create an array to store the (x,y) co-ordinates. This method I call in my main() function.

void createSemi_Sphere_Table (void)
{
float angle_d = 1.1f, angle_r=0.0;
float const Gl_Pi = 3.14;
int i = 0;

while ( angle_d < 11.0 )
{
    angle_r = Gl_Pi/angle_d;
    Sky_SemiSphere[i].y = 1.0f + (((3.50)*sin(angle_r)));
    Sky_SemiSphere[i].x = ((3.7)*cos(angle_r)) - 0.52f;

    angle_d = angle_d + 0.001;
    i = i+1;
}

} 

Then, I use those (x,y) co-ordinates in the below method to draw my sphere. I call drawSemi_Sphere_Grnd() method in my drawScene() method.

void drawSemi_Sphere_Grnd (void)
{
int L_Index = 0;

glPushMatrix();
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glBegin(GL_LINES);
        glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
        glVertex2f(-1.0f, -2.1f);
    glEnd();
}
glPopMatrix();
}

By the above procedure i get sphere. But the performance is slow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-12-31 23:52:26

您可以采取许多措施来加快速度,从使用顶点数组缓冲对象到显示列表。但最简单的是:

glBegin(GL_LINES);
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
    glVertex2f(-1.0f, -2.1f);
}
glEnd();

您绘制一个 9750 行长的行列表,而不是发出 9750 个单独的 GL_LINES 基元。

There are many things you can do to speed this up, from using vertex arrays to buffer objects to display lists. But the simplest is this:

glBegin(GL_LINES);
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
    glVertex2f(-1.0f, -2.1f);
}
glEnd();

Rather than issuing 9750 separate GL_LINES primitives, you draw one list of lines that is 9750 lines long.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文