tags:

views:

584

answers:

3

Hey guys I was starting to study OpenGL, and i wanted 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? Thanks

+4  A: 

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
A: 

No sir not that, i used points itself and i got the circle in points....thats fine but when i incude z also i get just 3 points wheres i shud b getting more than 30

codemax
perhaps you should try the second part of the answer. If you z values are out of range the point won't show up
basszero
This should be written as a comment on the other answer, not as an answer in itself (since it makes no attempt at answering the question). This also makes no sense by itself -- for example, assume the other answer gets deleted. Will somebody reading this have a clue of what you're talking about?
Jerry Coffin
oh sorry i am quite new to this site thats y...................
codemax
hey thanks man got it at last............thanks a lot
codemax
A: 

sir, i am sorry but i dont know glu perspective shall i give my entire code here?

#include <iostream>
#include "Xlib.h"
#include "gl.h"
#include "glu.h"
#include "glut.h"
#include <math.h>

#define GL_PI 3.1415f
void SetupRC()
{
    // Black background
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    // Set drawing color to green
    glColor3f(0.0f, 1.0f, 0.0f);
    glPointSize(4);
}

// Called to draw scene
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();
}

void ChangeSize(GLint w ,GLint h)
{
    if(h==0)
     h=1;

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat aspectratio = (GLfloat)w/(GLfloat)h;
    if(w<=h)
     glOrtho(-100,100,-100/aspectratio,100/aspectratio, 1,-1);
    else
     glOrtho(-100*aspectratio,100*aspectratio,-100,100,1,-1);

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();
}

int main(int argc , char **argv)
{
    glutInit(&argc ,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Chelsea");
    SetupRC();
    glutDisplayFunc(RenderScene);
    glutReshapeFunc(ChangeSize);
    glutMainLoop();
    return 0;
}

Can you point out the mistake? thanks

codemax
Stop posting answers to your question, use comments or edit your original question if you have source to post.
Ron Warholic
No one can read your code because you didn't read the formatting guide as I advised: http://stackoverflow.com/editing-help
Daniel Straight
Try editing this code into your original question instead of posting it in an answer that doesn't (even try to) answer the question. Also, when you paste it in, indent it all by four spaces to keep the formatting.
Jerry Coffin
You know, answering your own questions with screwed formatting in the hope of getting the solution quicker won't buy you anything here. Just calm down, test the code suggested by others, digest the answers slowly and with comprehension, give it some time, update your original question if other answers changed your perspective (not just after toying with proposed code for 30 seconds). If you must then ask further questions via comments. And advice #1: be mature.
Wojciech Kaczmarek
oh sorry guys, as i told i am quite new here and thanks for all those tips, i really appreciate it
codemax