c++ - How Can I Make This Open GL Program Rotate Without Terminating -


so have opengl program displays cube using

glulookat(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

and allows users modify/rotate along x, y , z axis using input. right looks works pretty well, however, terminates once image displayed according users input (on wether rotate along x, y or z axis).

how can modify program can rotate without terminating? in, displaying first rotation during first input.

or put simply, how can let users rotae cube along x,y , z axis in real time?

//cube.cpp #include <gl/gl.h> #include <gl/glu.h> #include <gl/glut.h> #include <iostream> using namespace std; void init(void)  {    glclearcolor (0.0, 0.0, 0.0, 0.0);    glshademodel (gl_flat); } void display(void) {    glclear (gl_color_buffer_bit);    glcolor3f (1.0, 1.0, 1.0);    glloadidentity ();             /* clear matrix */    /* viewing transformation  */       /* move x, y , z axis*/    cout << "type x, y or z rotatate cube respective axis 5 degrees." << endl;    char input;    cin >> input;    if (input == 'x')    {       glulookat (5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    }    else if (input == 'y')    {       glulookat (0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    }    else if (input == 'z')    {       glulookat (0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    }    else    {       glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    }      // glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);    glscalef (1.0, 2.0, 1.0);      /* modeling transformation */     glutwirecube (1.0);    glflush (); } void reshape (int w, int h) {    glviewport (0, 0, (glsizei) w, (glsizei) h);     glmatrixmode (gl_projection);    glloadidentity ();    glfrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);    glmatrixmode (gl_modelview); } int main(int argc, char** argv) {       glutinit(&argc, argv);    glutinitdisplaymode (glut_single | glut_rgb);    glutinitwindowsize (500, 500);     glutinitwindowposition (100, 100);    glutcreatewindow (argv[0]);    init ();    glutdisplayfunc(display);     glutreshapefunc(reshape);    glutmainloop();    return 0; } 

use glut's keyboard handling instead of iostreams:

#include <gl/glut.h>  float rx = 0; float ry = 0; float rz = 0; void display() {     glclearcolor (0.0, 0.0, 0.0, 0.0);     glclear (gl_color_buffer_bit);      glmatrixmode (gl_projection);     glloadidentity ();     glfrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);      glmatrixmode (gl_modelview);     glloadidentity ();     gltranslatef( 0, 0, -3 );      glcolor3f (1.0, 1.0, 1.0);     glscalef (1.0, 2.0, 1.0);     glrotatef( -rx, 1, 0, 0 );     glrotatef( -ry, 0, 1, 0 );     glrotatef( -rz, 0, 0, 1 );     glutwirecube (1.0);      glutswapbuffers(); }  void keyboard( unsigned char key, int x, int y ) {     if( key == 'x' ) rx += 5;     if( key == 'y' ) ry += 5;     if( key == 'z' ) rz += 5; }  void timer( int ) {     // run display() every 16ms or     gluttimerfunc( 16, timer, 0 );     glutpostredisplay(); }  int main(int argc, char** argv) {        glutinit(&argc, argv);     glutinitdisplaymode (glut_double | glut_rgba);     glutinitwindowsize (500, 500);      glutcreatewindow(argv[0]);      glshademodel (gl_flat);      glutdisplayfunc(display);      glutkeyboardfunc( keyboard );     gluttimerfunc( 0, timer, 0 );     glutmainloop();     return 0; } 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -