c++ - 如何使这个 Open GL 程序在不终止的情况下旋转

标签 c++ opengl

所以我有这个 OpenGL 程序,它使用

gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

它允许用户使用输入沿 x、y 和 z 轴修改/旋转它。现在它看起来工作得很好,但是,一旦根据用户输入显示图像(无论是否沿 x、y 或 z 轴旋转),它就会终止。

我如何修改这个程序,以便您可以在不终止的情况下旋转它?就像在第一次输入期间仅显示第一次旋转。

或者简单的说,如何让用户实时的沿着x,y,z轴旋转这个立方体?

//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 the matrix */
   /* viewing transformation  */
      /* Move about the x, y and z axis*/
   cout << "Type x, y or z to rotatate the cube about that respective axis by 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;
}

最佳答案

使用 GLUT 的键盘处理而不是 iostream:

#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 extra )
{
    // run display() every 16ms or so
    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;
}

关于c++ - 如何使这个 Open GL 程序在不终止的情况下旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892037/

相关文章:

c++ - 如何在 C++ 中存储大矩阵

c++ - 像素在自行初始化时移动,但不在循环中移动?

c++ - OpenGL - 使用 glDrawElements 进行索引绘制

java - 高效合并交错索引数据

c++ - 如何对段进行排序 C++

c++ - MultiByteToWideChar 转换

c++ - 为什么#include 只完成声明和定义?

c - OpenGL翻译

c++ - 无法手动设置每个纹理 mipmap 级别

c++ - 在 protoc 插件中处理自定义选项