Sean
Sean

Reputation: 867

Lorenz Attractor in OpenGL

I am trying to model the Lorenz attractor in 3D space using OpenGL. I have written the following code in my display function:

void display()
{
    //  Clear the image
    glClear(GL_COLOR_BUFFER_BIT);
    //  Reset previous transforms
    glLoadIdentity();
    //  Set view angle
    glRotated(ph,1,0,0);
    glRotated(th,0,1,0);

    glColor3f(1,1,0);
    glPointSize(1);

    float x = 0.1, y = 0.1, z = 0.1;
    glBegin(GL_POINTS);

    int i;    
    for (i = 0; i < initialIterations; i++) {    
        // compute a new point using the strange attractor equations
        float xnew = sigma*(y-x);
        float ynew = x*(r-z) - y;
        float znew = x*y - b*z;
    
        // save the new point
        x = x+xnew*dt;
        y = y+ynew*dt;
        z = z+znew*dt;        
    
        glVertex4f(x,y,z,i);
    }


    glEnd();

    //  Draw axes in white
    glColor3f(1,1,1);
    glBegin(GL_LINES);
    glVertex3d(0,0,0);
    glVertex3d(1,0,0);
    glVertex3d(0,0,0);
    glVertex3d(0,1,0);
    glVertex3d(0,0,0);
    glVertex3d(0,0,1);
    glEnd();
    //  Label axes
    glRasterPos3d(1,0,0);
    Print("X");
    glRasterPos3d(0,1,0);
    Print("Y");
    glRasterPos3d(0,0,1);
    Print("Z");
    //  Display parameters
    glWindowPos2i(5,5);
    Print("View Angle=%d,%d  %s",th,ph,text[mode]);
    //  Flush and swap
    glFlush();
    glutSwapBuffers();
}

However, I can't get the right attractor. I believe my equations for x, y, z are correct. I am just not sure how to display it the right way to get the right attractor. Thanks for any help. below is what my program is currently putting out:

enter image description hereHello

Upvotes: 1

Views: 2428

Answers (1)

James
James

Reputation: 11

Okay so I had this problem and there are a few things you want to do, First off when you go do draw the point with glVertex4f() you want to either change it to glVertex3f or change your w value to 1. with glVertex3f it will set w to 1 by default. The w value changes the scaling of the points so you will end up with some crazy number all the way out with an i of 50000 or so.

Second after fixing that you're going to find that the values are way out of your visual range so you need to scale it down. I would do this at the time you draw the points so in your case I would use glVertex3f(x*.05,y*.05,z*.05). if .05 is too large or too small adjust it to fit your needs.

finally make sure that your dt value is .001 and your starting point should be around 1 for x,y,and z.

Then ideally you want to put all these points in an array then read that array to draw your points instead of doing the calculations each time you call display. So do your calculations elsewhere and just send the points to display. Hope this helped.

Upvotes: 1

Related Questions