Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to draw a spiral using opengl

Tags:

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?

like image 356
codemax Avatar asked Dec 14 '09 17:12

codemax


2 Answers

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.

like image 163
catchmeifyoutry Avatar answered Sep 21 '22 20:09

catchmeifyoutry


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

like image 40
Sir Digby Chicken Caesar Avatar answered Sep 20 '22 20:09

Sir Digby Chicken Caesar