A_humble_programmer
A_humble_programmer

Reputation: 1

for loops and GLbegin

I am trying to make a Chaos Game in c using OpenGL and for some reason I am only creating a triangle and one point right now, when I am expecting to get 60,000 points. I think that this is because of the begin and end statements being inside of a for loop, and if not, then I'm not sure what is wrong. Any help solving the problem would be appreciated!

BTW the chaos game is where you start with one point, and then choose a random point on the triangle and then place a new point halfway between the chosen point and the original point and continue, and in my case I will repeat this 60,000 times. :)

    void display(void)
{

   glClear (GL_COLOR_BUFFER_BIT); //clears all pixels

    int choice, i;
    float x = 0.15, y = 0.85;

    glColor3f (0.0, 0.0, 0.0); //this sets the drawing color to black
    glBegin(GL_LINE_LOOP); //The triangle
        glVertex3f(0.2, 0.2, 0.0);
        glVertex3f(0.5, 0.8, 0.0);
        glVertex3f(0.8, 0.2, 0.0);
        glVertex3f(0.2, 0.2, 0.0);
    glEnd();

    glPointSize(3.0); //sets point size
    glBegin(GL_POINTS); //this is the original point
        glVertex3f(x, y, 0.0); 
    glEnd();


    for(i=0; i<60,000; i++)
    {
        choice = (rand()%3);
        switch(choice)
        {
            case 0:
                    x = (x + 0.5)/2; //this is the x1+x2/2 part of the midpoint theorem
                    y = (y + 0.8)/2; //this is the y1+y2/2 part of the midpoint theorem
                glBegin(GL_POINTS); 
                    glVertex3f(x, y, 0.0);
                glEnd();
            break;

            case 1:             
                    x = (x + 0.8)/2; 
                    y = (y + 0.2)/2;
                glBegin(GL_POINTS); 
                    glVertex3f(x, y, 0.0);
                glEnd();
            break;

            case 2:
                    x = (x + 0.2)/2;
                    y = (y + 0.2)/2;
                glBegin(GL_POINTS); 
                    glVertex3f(x, y, 0.0);
                glEnd();

            default:
            break;
        }
    }

    glFlush ();
}

void init (void) 
{
    glClearColor (1.0, 1.0, 1.0, 0.0); //this sets the background color to white

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (250, 250); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow(argv[0]);
    init ();
    glutDisplayFunc(display); 
    glutMainLoop();
    return 0;
}

Upvotes: 0

Views: 998

Answers (1)

Topological Sort
Topological Sort

Reputation: 2787

Try replacing 60,000 with 60000. In C, 60,000 evaluates to 0.

For more on the comma operator, see the Wikipedia entry on it.

Upvotes: 2

Related Questions