How to use for in opengl

Hi im new at opengl i wanna make rectangular prisms
where the first is the base and the second is above the first, and so the following
to make a pyramid
Already did this, but only shows 1 prism why is that?
how should I use “for” it to show all

``````
double rotate_y=0;
double rotate_x=0;
double x=0.50;
double y=0;

// ----------------------------------------------------------
// ----------------------------------------------------------
void display(){

//  Borrar pantalla y Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

// Resetear transformaciones

glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );

//PIRAMIDE

//PRISMA1
glBegin(GL_POLYGON);

glColor3f(0, 0,  1.0 );
glVertex3f(  x,y, -x );
glVertex3f(  x,  y+.12, -x );
glVertex3f( -x,  y+.12, -x );
glVertex3f( -x, y, -x );

glEnd();

glBegin(GL_POLYGON);
glColor3f(    0, 0,  1 );
glVertex3f(  x, y, x );
glVertex3f(  x,  y+.12, x );
glVertex3f( -x,  y+.12, x);
glVertex3f( -x, y, x);
glEnd();

glBegin(GL_POLYGON);
glColor3f(  0,  1,  0 );
glVertex3f( x, y, -x );
glVertex3f( x,  y+.12, -x);
glVertex3f( x,  y+.12,  x );
glVertex3f( x, y,  x );
glEnd();

glBegin(GL_POLYGON);
glColor3f(   0,  1,  0 );
glVertex3f( -x, y,  x );
glVertex3f( -x,  y+.12,  x );
glVertex3f( -x,  y+.12, -x);
glVertex3f( -x, y, -x );
glEnd();

glBegin(GL_POLYGON);
glColor3f(   0.9,  0.0,  .10 );
glVertex3f(  x, y+.12, -x );
glVertex3f(  x, y+.12,  x );
glVertex3f( -x, y+.12,  x);
glVertex3f( -x, y+.12, -x);
glEnd();

glFlush();
glutSwapBuffers();

}

// ----------------------------------------------------------
// ----------------------------------------------------------
void specialKeys( int key, int x, int y ) {

//  Flecha derecha: aumentar rotación 5 grados
if (key == GLUT_KEY_RIGHT)
rotate_y += 5;

//  Flecha izquierda: disminuir rotación 5 grados
else if (key == GLUT_KEY_LEFT)
rotate_y -= 5;

else if (key == GLUT_KEY_UP)
rotate_x += 5;

else if (key == GLUT_KEY_DOWN)
rotate_x -= 5;

//  Solicitar actualización de visualización
glutPostRedisplay();

}

// ----------------------------------------------------------
// Función “main()”
// ----------------------------------------------------------
int main(int argc, char* argv[]){

//  Inicializar los parámetros GLUT y de usuario proceso

glutInit(&argc,argv);

//  Solicitar ventana con color real y doble buffer con Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
// Crear ventana
glutCreateWindow("Cubo asombroso");

//  Habilitar la prueba de profundidad de Z-buffer
glEnable(GL_DEPTH_TEST);

``````

// HERE IS THE FOR

``````for(int i=0;i<=3;i++){
glutDisplayFunc(display);

x=x-.10;
y=y+.12;
}
glutSpecialFunc(specialKeys);

//  Pasar el control de eventos a GLUT
glutMainLoop();

//  Regresar al sistema operativo
return 0;

}

``````

Why would you expect a loop to execute 3 times when you have a return statement at the end?

In any case, you’re misunderstanding what glut*Func() do. They don’t draw anything. They provide a callback that’s called later inside glutMainLoop() when some event happens (e.g. when your window needs redrawn, or you hit a special key).

So what you want to iterate is not this section, but rather the code inside of your display() callback that draws a prism.

Pyramid? Why are you using 4 variables in order to assign each side? Secondly Display contains glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
which pretty much whipes the buffer and sets it to the depths and colors defined previousely ( normally during init() )… you are erasing it after you draw it till you exit the for();… glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); before the for () { display(); } and it will have the effect you are asking for;

ty so much

one more thing if you want faster code change it to something more like

``````
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(int i=0;i<=3;i++)
{
glutDisplayFunc(display);
x=x-.10;
y=y+.12;
}
glFlush();
glutSwapBuffers();

``````

So you are only calling for glutSwapBuffers(); once and not having to draw 3 different buffers before the final product. This could increase the frame rate on a draw function like this by 2-5% ( estimate off the top of my head , don’t know all of your functions that are called like this ). But when you are talking about a larger number of objects it could be 100 fold faster.

[QUOTE=didydriver;1289754]one more thing if you want faster code change it to something more like

``````
for(int i=0;i<=3;i++)
{
glutDisplayFunc(display);
...
}

``````

[/QUOTE]

What are you expecting this to do?

[ul]
[li]glutDisplayFunc() man page [/li][/ul]

Sorry its a glut callback. You are right no accomplishment. I don’t use glut so I’m not to familiar with its protocols. Just trying to demonstrate how the for loop was effecting the buffers by clearing them each time it was called. And if you continually swapping buffers before you finish the scene could have a undesired side effect.