Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: How to use for in opengl

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    4

    Unhappy 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

    Code cpp:
    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
    Code cpp:
    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;
     
    }
    Last edited by Dark Photon; 12-15-2017 at 05:39 AM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,215
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    22
    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;

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    4

    Red face ty

    ty so much

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    22
    one more thing if you want faster code change it to something more like
    Code :
    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.

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,215
    Quote Originally Posted by didydriver View Post
    one more thing if you want faster code change it to something more like
    Code :
    for(int i=0;i<=3;i++)
    {
     glutDisplayFunc(display);
    ...
    }
    What are you expecting this to do?


  7. #7
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    22
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •