我在旋转时遇到了麻烦。我可以让立方体旋转得很好,但我只希望那个立方体旋转。我知道我必须使用 glPushMatrix() 和 glPopMatrix() 但每次我将这两个包裹在我的绘图周围时,它都会停止旋转。
这是我设置 OpenGL 的方式:
void initOpenGL()
{
//these are the current version of OpenGL
string versions[14] =
{
"GL_VERSION_1_1",
"GL_VERSION_1_2",
"GL_VERSION_1_3",
"GL_VERSION_1_4",
"GL_VERSION_1_5",
"GL_VERSION_2_0",
"GL_VERSION_2_1",
"GL_VERSION_3_0",
"GL_VERSION_3_1",
"GL_VERSION_3_2",
"GL_VERSION_3_3",
"GL_VERSION_4_0",
"GL_VERSION_4_1",
"GL_VERSION_4_2"
};
//Determine which versions are safe to use
cerr << "OpenGL+GLEW Info: " << endl;
for (int i = 0; i < 14; ++i)
{
if (glewIsSupported(versions[i].c_str()))
cerr << versions[i] << " is supported" << endl;
else
cerr << "ERROR: " << versions[i] << " is not supported" << endl;
}
cerr << endl << "Open GL " << glGetString (GL_VERSION) << " is the current OpenGL version" << endl << endl;
glClearColor(0, 0, 0, 0);
//By setting this option, we can explicityly control how the program terminates to ensure proper cleanup
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
//setup the camera
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(MIN_X, MAX_X, MIN_Y, MAX_Y, MIN_Z, MAX_Z);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//enable access to the alpha channel
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
... some more code to load textures...
}
这是我的显示功能:
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1, 0, 0);
glRotatef(angle, 1, 1, 0);
glutSolidCube(30);
glPopMatrix();
glutSwapBuffers();
}