PDA

View Full Version : Doubt About OpenGL Proyections.



j0rt3g4
04-01-2009, 02:40 PM
Hi Everyone... I have a Little problem with visualization in Opengl...
I know that exist 2 kinds of "proyections" the ortographic one and the "Frustum" one :P

So... I know how to use ortographic (at least that is what I think). Now I have this code:


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include<stdio.h>

//VARIABLES GLOBALES
long unsigned int ite=0;
long unsigned int i=0;
long unsigned int transciente=0;
double X0=0.0,Y0=0.0,Z0=0.0;




void init(void){
// Light values and coordinates
GLfloat mat_specular[] ={ 1.0f, 1.0f, 1.0f, 1.0f};// { 1.0, 1.0, 1.0, 0.15 };
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat mat_shininess[] = { 100.0 };
GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };

glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
/*

glEnable(GL_DEPTH_TEST); // Hidden surface removal
//glFrontFace(GL_CCW); // Counterclockwise polygons face out
//glEnable(GL_CULL_FACE); // Do not calculate inside of jet

// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set material properties to follow glColor values

glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine

// Light blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );

glEnable(GL_NORMALIZE);*/
}


void display(void){

double XT,YT,ZT;
//VARIABLES DE LAS FUNCIONES
double H=0.02;
double A=9.0;
double B= 28.8;
double C=(8.0/3.0);
double xmax=0.0,xmin=1e8;
double ymax=0.0,ymin=1e8;
double zmax=0.0,zmin=1e8;

///////////////////////////////////////////////
////////////////// OGL COMMANDS //////////////
///////////////////////////////////////////////
GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);




// glColor3ub((GLubyte)200,(GLubyte)0,(GLubyte)0);
glPushMatrix();
for(i=0;i<ite;i++){
XT= X0 + H*( A * (Y0-X0));
YT= Y0 + H*(X0*(B-Z0)-Y0);
ZT= Z0 + H*( (X0*Y0) -(C*Z0) );
X0=XT;
Y0=YT;
Z0=ZT;

if(i>transciente){
glLoadIdentity();
glTranslatef( (GLfloat) XT, (GLfloat) YT, (GLfloat) ZT);
//glutSolidSphere(10.0,30,30);
glutWireSphere(10.0,30,30);
//CALCULO DE LIMITES DE LA IMAGEN
//calculo xmax y xmin
if(xmax<XT) xmax=XT;
if(xmin>XT) xmin=XT;
//calculo ymax e ymin
if(ymax<YT) ymax=YT;
if(ymin>YT) ymin=YT;
//calculo zmax y zmin
if(zmax<ZT) zmax=ZT;
if(zmin>ZT) zmin=ZT;
}
}
glPopMatrix();
glutSwapBuffers();
printf("%lf,%lf\t%lf,%lf\t%lf,%lf\n",xmin,xmax,ymin,ymax,zmin,zmax);

}



void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
exit(0);
break;

default:
break;
}
}

void keyboard(unsigned char key, int x, int y){
switch (key) {
case 'q':
case 'Q':
exit(0);
break;
case 27:
exit(0);
}
}




void reshape(GLsizei w, GLsizei h){

// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-10.0,8.0,-8.0,10.0,1.0,100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(20.0,30.0,50.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

//la ventana que voy a dibujar va a tener Ancho=800,largo=600

int main(int argc, char* argv[]){
FILE *in=NULL;
in=fopen("in.dat","r");
if(in==NULL){
printf("error con el archivo de entrada\n");
exit(1);
}


printf("Cargando datos\n");
fscanf(in,"%*s\t%lf",&amp;X0); printf("x0:%lf\n",X0);
fscanf(in,"%*s\t%lf",&amp;Y0); printf("y0:%lf\n",Y0);
fscanf(in,"%*s\t%lf",&amp;Z0); printf("z0:%lf\n",Z0);
fscanf(in,"%*s\t%lu",&amp;ite); printf("Iteraciones: %lu\n",ite);
fscanf(in,"%*s\t%lu",&amp;transciente); printf("trans: %lu\n\n",transciente);
printf("Fin de carga e inicio de operaciones\n\n");
fclose(in);


glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA| GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("Atractor de Lorentz");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}



Now I don't know why If I put in the line where I draw the spheres:
glutWireSphere(1.0,30,30); (for example)
I dont know how to get closer to the "scene"... so I guess I dont see anything.... :S

I tried to move the parameters in the Frustum function and the Lookat but I haven found a good way to model this...
Thank you in ad...
PD: The questions is that I dont know what is the relation between the parameters en glFrustum and glLookAt to get a good "view" or and appropiate view in this case... And how could be implemented to any case...

dletozeun
04-01-2009, 03:37 PM
The projection typically set with glFrustum is perspective projection. The values you passed to your function are very strange, you don't set a symetric frustum and I think it is not what you want.

Look at the glFrustum man page (http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml).

If you consider the view pyramid in eye space which is camera placed at origin looking toward the -z axis. Facing the camera that is at the top of the pyramid, you have two clipping planes which are the near and far clipping planes.
Now if you consider the near clipping plane, the points (left, bottom, near) and (right, top, near) are respectively, the lower left corner and the upper right corner of this clipping plane.

With all these information, opengl can build the perpective view pyramid and then the projection matrix.

Typical call to glFrustum is:

glFrustum( -x, x, -y, y, near, far)

where x, y, near and far values depend on the view volume you need (related to the scene size).

j0rt3g4
04-01-2009, 03:57 PM
Well Right now, the xrange is:[-21.400698,20.616706] the yrange is [-28.797468,27.431107] and the zrange is [4.806254,58.180357]
but I really dont know how to get the Objects inside the scene...
In other words, with that ranges what are the optimal values of -x,x,-y,y and near far ?
Should I modify gluLookAt too?

PD: I think it should work as:
glFrustum(-30.0,30.0,-25.0,25.0,30.0,30.0); ?

dletozeun
04-02-2009, 02:44 AM
Look at this:
http://www.opengl.org/resources/faq/technical/viewing.htm#view0070

You seem to not understand the two last parameters of glFrustum. Have you read the man page?

these ones set the position of the near and far clipping plane. By setting 30 for both parameters the view volume is empty. In your case you should set for example:

glFrustum(-30.0,30.0,-25.0,25.0,1.0,60.0);

this way your zrange is included in the [znear, zfar] range.

j0rt3g4
04-02-2009, 05:43 AM
Look at this:
You seem to not understand the two last parameters of glFrustum. Have you read the man page?


Well is not a missunderstanding issue.... I'm kind of confused... that's all ... I'm going to read it again carefully. But I chose 30,30, because the 1st 30 it supposed to be negative, that was why...

scratt
04-02-2009, 06:20 AM
Try this.. Imagine it's describing a box.

Anything inside the box you can see.

Anything outside you can't.

Assuming that you have the identity matrix in the Projection Matrix before you call glFrustum, your head is at 0,0,0 looking into the distance down Z.

dletozeun
04-02-2009, 11:33 AM
But I chose 30,30, because the 1st 30 it supposed to be negative


the znear and zfar values you give have to be positive as said in the man page. Actually, because the z axis in the one orthogonal to the screen and turned toward you, all points you see in the eye space have a negative z value.
It is just a convenience because, it is easier to consider that the distance z between the eye and a point in the scene is a positive value ( a negative distance does not have any sense).

In other words, the znear and zfar values are the distance from the eye to the near and far clipping planes. Between these planes the geometry is raterized, outside, it is clipped.

j0rt3g4
04-03-2009, 01:22 PM
Thanks for answering... Well What I did was to rescale everything between [0,1] and try make the drawing by X[i]-X[i-1] on each component, but I couldn't get a clear representation of that space in -30,30; -30,30; 1-30 wbat I did was to rescale everything, and then draw from 0-1 in a cube of size 1 ...

j0rt3g4
04-05-2009, 08:51 PM
Thanks to you guys I have create this:

http://img25.imageshack.us/img25/1064/pantallazo51.th.png (http://img25.imageshack.us/my.php?image=pantallazo51.png) http://img19.imageshack.us/img19/9711/pantallazo3q.th.png (http://img19.imageshack.us/my.php?image=pantallazo3q.png) http://img15.imageshack.us/img15/6295/pantallazo4v.th.png (http://img15.imageshack.us/my.php?image=pantallazo4v.png)