Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使此Open GL程序在不终止的情况下旋转_C++_Opengl - Fatal编程技术网

C++ 如何使此Open GL程序在不终止的情况下旋转

C++ 如何使此Open GL程序在不终止的情况下旋转,c++,opengl,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 <G

所以我有一个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;
}
//cube.cpp
#包括
#包括
#包括
#包括
使用名称空间std;
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GLU平面);
}
作废显示(作废)
{
glClear(GLU颜色缓冲位);
GL3F(1.0,1.0,1.0);
glLoadIdentity();/*清除矩阵*/
/*观察变换*/
/*绕x、y和z轴移动*/
不能输入;
如果(输入='x')
{
gluLookAt(5.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
else if(输入='y')
{
gluLookAt(0.0,5.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
else if(输入='z')
{
gluLookAt(0.0,0.0,10.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);
}
//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);/*建模转换*/
立方体(1.0);
glFlush();
}
空洞重塑(整数w,整数h)
{
GLVIEW(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_投影);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
glMatrixMode(GLU模型视图);
}
int main(int argc,字符**argv)
{   
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500500);
位置(100100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(显示器);
GLUTREFORUNC(重塑);
glutMainLoop();
返回0;
}

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

#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;
}
#包括
浮点数rx=0;
浮动ry=0;
浮点数rz=0;
无效显示()
{
glClearColor(0.0,0.0,0.0,0.0);
glClear(GLU颜色缓冲位);
glMatrixMode(GL_投影);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,1.5,20.0);
glMatrixMode(GLU模型视图);
glLoadIdentity();
glTranslatef(0,0,-3);
GL3F(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);
立方体(1.0);
glutSwapBuffers();
}
无效键盘(无符号字符键,整数x,整数y)
{
如果(键=='x')rx+=5;
如果(键=='y')ry+=5;
如果(键=='z')rz+=5;
}
无效计时器(整数额外)
{
//每隔16毫秒左右运行display()
glutTimerFunc(16,定时器,0);
再发现();
}
int main(int argc,字符**argv)
{   
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(500500);
glutCreateWindow(argv[0]);
glShadeModel(GLU平面);
glutDisplayFunc(显示器);
键盘Func(键盘);
glutTimerFunc(0,定时器,0);
glutMainLoop();
返回0;
}

使用GLM库和最新的编码实践。