如何使用opengl绘制螺旋 [英] how to draw a spiral using opengl

查看:88
本文介绍了如何使用opengl绘制螺旋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何绘制螺旋线.

I want to know how to draw a spiral.

我写了这段代码:

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    GLfloat x,y,z = -50,angle;
    glBegin(GL_POINTS);

    for(angle = 0; angle < 360; angle += 1)
    {   
        x = 50 * cos(angle);
        y = 50 * sin(angle);
        glVertex3f(x,y,z);
        z+=1;
    }
    glEnd();
    glutSwapBuffers();
}

如果我不包括z项,我会得到一个完美的圆圈,但是当我包括z时,我会得到3个点.可能会发生什么?

If I don't include the z terms I get a perfect circle but when I include z, then I get 3 dots that's it. What might have happened?

我使用glviewport(0,0,w,h)设置了视口

I set the viewport using glviewport(0,0,w,h)

要包含z,我应该做任何事情来设置z方向上的视口吗?

To include z should i do anything to set viewport in z direction?

推荐答案

您会看到点,因为您正在使用glBegin(GL_POINTS)绘制点. 尝试将其替换为glBegin(GL_LINE_STRIP).

You see points because you are drawing points with glBegin(GL_POINTS). Try replacing it by glBegin(GL_LINE_STRIP).

注意::当您看到该圆时,您也只画了点,但画得足够近以显示为连接的圆.

NOTE: when you saw the circle you also drew only points, but drawn close enough to appear as a connected circle.

此外,您可能尚未设置深度缓冲区以接受所使用的z = [-50,310]范围内的值.这些参数应在gluPerspectiveglOrtho()glFrustum()调用中作为 zNear zFar 剪切平面提供.

Also, you may have not setup the depth buffer to accept values in the range z = [-50, 310] that you use. These arguments should be provided as zNear and zFar clipping planes in your gluPerspective, glOrtho() or glFrustum() call.

注意:这将解释为什么使用z值时您只能看到几个点:其他点被裁剪,因为它们不在z缓冲区范围内.

NOTE: this would explain why with z value you only see a few points: the other points are clipped because they are outside the z-buffer range.

显示代码后进行更新:

glOrtho(-100*aspectratio,100*aspectratio,-100,100,1,-1);仅允许[-1,1]范围内的z值,这就是为什么只绘制带有z = -1z = 0z = 1的三个点(因此是3个点)的原因.

glOrtho(-100*aspectratio,100*aspectratio,-100,100,1,-1); would only allow z-values in the [-1, 1] range, which is why only the three points with z = -1, z = 0 and z = 1 will be drawn (thus 3 points).

最后,您可能会从顶部直接在旋转轴方向上查看螺旋.如果您不使用透视投影(而是等轴测投影),则螺旋线仍将显示为圆形.您可能要使用gluLookAt()更改视图.

Finally, you're probably viewing the spiral from the top, looking directly in the direction of the rotation axis. If you are not using a perspective projection (but an isometric one), the spiral will still show up as a circle. You might want to change your view with gluLookAt().

设置示例示例

以下代码来自出色的OpenGL教程通过NeHe:

The following code is taken from the excellent OpenGL tutorials by NeHe:

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
glLoadIdentity();                           // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);                     // Select The Modelview Matrix
glLoadIdentity();                           // Reset The Modelview Matrix

然后,在您的绘制循环中,看起来像这个:

Then, in your draw loop would look something like this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer
glLoadIdentity();   
glTranslatef(-1.5f,0.0f,-6.0f);                 // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES);                      // Drawing Using Triangles
    glVertex3f( 0.0f, 1.0f, 0.0f);              // Top
    glVertex3f(-1.0f,-1.0f, 0.0f);              // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f);              // Bottom Right
glEnd();    

当然,您应该根据需要更改此示例代码.

Of course, you should alter this example code your needs.

这篇关于如何使用opengl绘制螺旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆