PDA

View Full Version : Reseting the matrix (wrong way to do it)



j0rt3g4
04-05-2009, 08:34 PM
Well I have several problems in this case:

First of all the code:


///defining materials
//Defino un material Rojo (METACRILATO)
GLfloat mat_ambient_metac[] = {0.05, 0.05, 0.05, 1.0f};
GLfloat mat_diffuse_metac[] = {1.0 , 0.35, 0.5 , 0.8f};
GLfloat mat_specular_metac[]= {0.9 , 0.8 , 0.8 , 1.0f};

//Defino un material Gris (GELATINA)
GLfloat mat_ambient_gel[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_gel[] = {1.0, 1.0, 1.0, 0.8f};
GLfloat mat_specular_gel[]= {0.9, 0.9, 0.9, 1.0f};

//Defino un material (AMARILLO) Radicales libres
GLfloat mat_ambient_rl[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_rl[] = {0.9, 0.9, 0.0, 0.8f};
GLfloat mat_specular_rl[]= {0.8, 0.8, 0.8, 1.0f};

//Defino un material Gris (METACRILATOS + GELATINA)
GLfloat mat_ambient_mege[] = {0.1, 0.1, 0.1, 1.0f};
GLfloat mat_diffuse_mege[] = {0.5, 1.0, 0.5, 0.8f};
GLfloat mat_specular_mege[]= {0.9, 0.9, 0.9, 1.0f};
/////////////////////////////// VECTORES Xs Ys Zs /////////////////////////////
CUBOU1=calloc(M,sizeof(*CUBOU1));
CUBOU2=calloc(N,sizeof(*CUBOU2));
CUBOU3=calloc(P,sizeof(*CUBOU3));
for(i=0;i<M;i++){
CUBOU1[i]=(double) ( (double)i/(double)M );
//CUBOU1[i]-=0.5;
// CUBOU1[i]*=scale;
//printf("U1[%u]:%lf\n",i,CUBOU1[i]);
}
for(i=0;i<N;i++){
CUBOU2[i]=(double) ( (double)i/(double)N );
//CUBOU2[i]-=0.5;
// CUBOU2[i]*=scale;
}
for(i=0;i<P;i++){
CUBOU3[i]=(double) ( (double)i/(double)P );
//CUBOU2[i]-=0.5;
// CUBOU2[i]*=scale;
}

//////////////////////LIMPIO VISTA INICIAL/////////////////
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glLoadIdentity();
polarView(veo, giro, elevacion, azimitud); //vista

////////////////////// LISTAS ////////////////////
metacrilatos = glGenLists(1); //listo con 1
gelatinatos = glGenLists(2); //con 2
meta_gela = glGenLists(3); //gelatinas y metacrilatos 3
radicalatos = glGenLists(4); //con 4

glNewList(metacrilatos, GL_COMPILE);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_metac);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_metac);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_metac);
glMaterialf (GL_FRONT, GL_SHININESS, 90.0f);
glutSolidSphere (0.05, 10, 10);
glEndList();

glNewList(gelatinatos, GL_COMPILE);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_gel);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_gel);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_gel);
glMaterialf (GL_FRONT, GL_SHININESS, 90.0f);
glutSolidSphere (0.05, 10, 10);
glEndList();

glNewList(meta_gela, GL_COMPILE);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_mege);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_mege);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_mege);
glMaterialf (GL_FRONT, GL_SHININESS, 90.0f);
glutSolidSphere (0.05, 10, 10);
glEndList();

glNewList(radicalatos, GL_COMPILE);
glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient_rl);
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse_rl);
glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular_rl);
glMaterialf (GL_FRONT, GL_SHININESS, 90.0f);
glutSolidSphere (0.05, 10, 10);
glEndList();
for(k=0;k<P;k++){
for(j=0;j<N;j++){
for(i=0;i<M;i++){
switch(I[i][j][k]){
case 0:
continue;
break;

case 1: //metas
// if(i==0 || j==0 || k==0)
// glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
// else glPopMatrix();
glLoadIdentity();
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
glCallList (metacrilatos);
break;
case 2: //gelatinazas
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
glCallList (gelatinatos);
break;
case 3:
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
glCallList (meta_gela);
break;

default:
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
glCallList (radicalatos);
break;
}
}
}
}


I have an unsigned int 3-dimensional array called I:
In "I" i have some information that I want to draw inside a cube with transparent spheres on each point of it.

The problems is the acumulative efect in the funtions:
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);

I would like to know if there's a way to reset the coordenates before drawing (calling glCallList (); )

I have read about it and "glLoadIdentity();" is a way... but if I use this in each case... It doesn't draw anything...
SO HELPPPPPPPPPPPP :( pls
and Thank you in advance

PD: In the case 1 of the switch... i try the glLoadIdentity(); and doesn't draw anything so I can't reset the coordenates this way... :S

PD2: Any idea(s)?

matchStickMan
04-06-2009, 01:27 AM
Hello j0rt3g4,


To avoid the cummulative effect of the glTranslatef, do this:


glMatrixMode( GL_MODELVIEW );
glPushMatrix();

////
//Add your glTranslatef calls here.
glTranslatef((GLfloat)CUBOU1[i], (GLfloat)CUBOU2[j], (GLfloat)CUBOU3[k]);
glCallList (metacrilatos);
////

glPopMatrix();



What the glPushMatrix() does is, it retains the previous settings of the stack and the and glPopMatrix() retrieves it when required.


Add these two commands inside each case: statement.

I hope this helps..

j0rt3g4
04-06-2009, 08:33 AM
matchStickMan... This helps a lot!!!! :) thank you very much I was having a terrible nightmare with that acumulative effect and I did the lorent atractor making the inverse transformation each call of glTranslated(); but with this :).... Resolve A lots of my problems :)

See the result :) (It takes a moment to draw but its ok)

(What I could do to improve perfomance is a VBO array, Im reading about it)
http://img218.imageshack.us/img218/4233/pantallazou.th.png (http://img218.imageshack.us/my.php?image=pantallazou.png)

I like to share my screenshoots for any answer I got from this forum...

It's a way to say thank you :)

matchStickMan
04-07-2009, 08:35 PM
Hi j0rt3g4,

I'm glad that solved your issue.

It's always a pleasure to contribute to the community. :)