PDA

View Full Version : How to use for in opengl



deividwoh
12-14-2017, 07:32 PM
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;

// ----------------------------------------------------------
// Función de retrollamada “display()”
// ----------------------------------------------------------
void display(){

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

// Resetear transformaciones
glLoadIdentity();


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

//PIRAMIDE


//PRISMA1
//LADO FRONTAL
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();

// LADO TRASERO
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();

// LADO DERECHO
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();

// LADO IZQUIERDO
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();



// LADO TAPA
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();



}

// ----------------------------------------------------------
// Función de retrollamada “specialKeys()”
// ----------------------------------------------------------
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);

// Funciones de retrollamada

// 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;

}

Dark Photon
12-15-2017, 04:45 AM
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.

didydriver
12-17-2017, 05:29 AM
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;

deividwoh
12-17-2017, 06:43 PM
ty so much

didydriver
12-17-2017, 07:51 PM
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.

Dark Photon
12-18-2017, 05:53 AM
one more thing if you want faster code change it to something more like


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


What are you expecting this to do?


glutDisplayFunc() man page (https://www.opengl.org/resources/libraries/glut/spec3/node46.html)

didydriver
12-22-2017, 08:48 AM
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.