PDA

View Full Version : From gluOrtho2D to 3D



Henry90
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
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. May you kindly help me figuring out what I do wrong? Thanks in advance!

GClements
12-10-2014, 01:21 PM
// set up the modelview matrix (the objects)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

[/CODE]

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.

https://www.opengl.org/discussion_boards/attachment.php?attachmentid=1539&stc=1&d=1418247445

Henry90
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!