开发者

how to draw a spiral using opengl

I want to know how to draw a spiral.

I wrote this code:

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();
}

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?

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

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


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.

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.

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.

UPDATE AFTER YOU HAVE SHOWN YOUR CODE:

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).

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().

EXAMPLE OF SETTING UP PERSPECTIVE

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.


catchmeifyoutry provides a perfectly capable method, but will not draw a spatially accurate 3D spiral, as any render call using a GL_LINE primitive type will rasterize to fixed pixel width. This means that as you change your perspective / view, the lines will not change width. In order to accomplish this, use a geometry shader in combination with GL_LINE_STRIP_ADJACENCY to create 3D geometry that can be rasterized like any other 3D geometry. (This does require that you use the post fixed-function pipeline however)

I recommended you to try catchmeifyoutry's method first as it will be much simpler. If you are not satisfied, try the method I described. You can use the following post as guidance:

http://prideout.net/blog/?tag=opengl-tron


Here is my Spiral function in C. The points are saved into a list which can be easily drawn by OpenGL (e.g. connect adjacent points in list with GL_LINES).

  • cx,cy ... spiral centre x and y coordinates
  • r ... max spiral radius
  • num_segments ... number of segments the spiral will have

    SOME_LIST* UniformSpiralPoints(float cx, float cy, float r, int num_segments)
    {
    SOME_LIST *sl = newSomeList();
    int i;
    
    for(i = 0; i < num_segments; i++)
    {
    float theta = 2.0f * 3.1415926f * i / num_segments; //the current angle
    
    float x = (r/num_segments)*i * cosf(theta); //the x component
    float y = (r/num_segments)*i * sinf(theta); //the y component
    
    //add (x + cx, y + cy) to list sl
    
    }
    return sl; 
    }
    

An example image with r = 1, num_segments = 1024:

how to draw a spiral using opengl

P.S. There is difference in using cos(double) and cosf(float). You use a float variable for a double function cos.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜