View Full Version : From gluOrtho2D to 3D

12-10-2014, 03:17 AM
Hello everybody,
I am a newbie with openGL and I am trying to experiment my first project in this field. 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 it's 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

// enable point smoothing

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

// set up the projection matrix (the camera)
//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)

while to draw I do this:


// draw some 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);

// swap the buffers

the problem is that I don't visualize anything in my window. It's all black. May you kindly help me figuring out what I do wrong? Thanks in advance!

12-10-2014, 01:21 PM
// set up the modelview matrix (the objects)


Try adding

glTranslatef(0, 0, -2);

immediately after the above code, to move the rendered points in front of the viewpoint.

gluOrtho2D sets the near distance to -1 (i.e. points can be 1 unit behind the viewpoint and still be rendered), while perspective projections require the near distance to be positive (in this case, it's 0.1). Also, in a perspective projection, points which are very close to the viewpoint won't be visible unless they're roughly in front of it (in this case, within an angle of 22.5 degrees).

If the attractor is centred on the origin, and the viewpoint is also at the origin, it's entirely possible that everything is outside at least one of the edges of the frustum.

The attached image shows the difference between the view volumes generated by your gluOrtho2D (red) and gluPerspective (blue) calls.


12-11-2014, 08:58 AM
It works :) Thanks a lot for your help! you might see me again soon with something similar :D Thanks once more!