Tarta
Tarta

Reputation: 2063

From gluOrtho2D to 3D

I followed a guide to draw a Lorenz system in 2D.

I want now to extend my project and switch from 2D to 3D. As far as I know I have to substitute the gluOrtho2D call with either gluPerspective or glFrustum. Unfortunately whatever I try is useless. This is my initialization code:

// set the background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

/// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);*/

// set the foreground (pen) color
glColor4f(1.0f, 1.0f, 1.0f, 0.02f);

// enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// enable point smoothing
glEnable(GL_POINT_SMOOTH);
glPointSize(1.0f);

// set up the viewport
glViewport(0, 0, 400, 400);

// set up the projection matrix (the camera)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
    //gluOrtho2D(-2.0f, 2.0f, -2.0f, 2.0f);
gluPerspective(45.0f, 1.0f, 0.1f, 100.0f); //Sets the frustum to perspective mode


// set up the modelview matrix (the objects)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

while to draw I do this:

glClear(GL_COLOR_BUFFER_BIT);

// draw some points
glBegin(GL_POINTS);

// go through the equations many times, drawing a point for each iteration
for (int i = 0; i < iterations; i++) {

    // compute a new point using the strange attractor equations
    float xnew=z*sin(a*x)+cos(b*y);
    float ynew=x*sin(c*y)+cos(d*z);
    float znew=y*sin(e*z)+cos(f*x);

    // save the new point
    x = xnew;
    y = ynew;
    z = znew;

    // draw the new point
    glVertex3f(x, y, z);
}
glEnd();

// swap the buffers
glutSwapBuffers();

the problem is that I don't visualize anything in my window. It's all black. What am I doing wrong?

Upvotes: 0

Views: 1275

Answers (1)

datenwolf
datenwolf

Reputation: 162164

The name "gluOrtho2D" is a bit misleading. In fact gluOrtho2D is probably the most useless function ever. The definition of gluOrtho2D is

void gluOrtho2D(
    GLdouble left,
    GLdouble right,
    GLdouble bottom,
    GLdouble top )
{
    glOrtho(left, right, bottom, top, -1, 1);
}

i.e. the only thing it does it calling glOrtho with default values for near and far. Wow, how complicated and ingenious </sarcasm>.

Anyway, even if it's called ...2D, there's nothing 2-dimensional about it. The projection volume still has a depth range of [-1 ; 1] which is perfectly 3-dimensional.

Most likely the points generated lie outside the projection volume, which has a Z value range of [0.1 ; 100] in your case, but your points are confined to the range [-1 ; 1] in either axis (and IIRC the Z range of the strange attractor is entirely positive). So you have to apply some translation to see something. I suggest you choose

  • near = 1
  • far = 10

and apply a translation of Z: -5.5 to move things into the center of the viewing volume.

Upvotes: 7

Related Questions