[英]OpenGL, how to rotate objects independent of each other?
我的代码到目前为止
void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
gluLookAt( camera[0][0], camera[0][1], camera[0][2],
camera[1][0], camera[1][1], camera[1][2],
camera[2][0], camera[2][1], camera[2][2] ); //Set the point looking at
glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f); //rotate on x axis
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f); //rotate on y axis
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f); //rotate on z axis
switch ( Rendermode ) { //different render mode
case 'f':
//Draw object I want to rotate
break;
case 'v':
//Draw object I want to rotate
break;
case 'e':
//Draw object I want to rotate
break;
glLoadIdentity();
}
//Draw object I DO NOT want to rotate
glutSwapBuffers ( ); // Swap The Buffers To Not Be Left With A Clear Screen
}
然而,目前我的所有物体同时旋转,我怎样才能旋转我注意到要旋转的物体,同时留下我不想旋转的物体呢?
使用gl Push / Pop Matrix()封装线性变换并绘制对象。 这些函数保存/恢复矩阵的当前状态,因此它们不会影响其他图形。
glPushMatrix();
glRotatef(...);
// glTranslatef(...),
//glScalef(...);
drawObject1();
glPopMatrix();
glPushMatrix();
glRotatef(...);
// glTranslatef(...),
//glScalef(...);
drawObject2();
glPopMatrix();
正如@karlphillip所说,你需要将你的旋转放入glPush / glPop矩阵封装中。 我认为这段代码应该有效:
void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
gluLookAt( camera[0][0], camera[0][1], camera[0][2],
camera[1][0], camera[1][1], camera[1][2],
camera[2][0], camera[2][1], camera[2][2] ); //Set the point looking at
glPushMatrix(); //-------------------- Encapsulate Rotation ----------------
glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f); //rotate on x axis
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f); //rotate on y axis
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f); //rotate on z axis
switch ( Rendermode ) { //different render mode
case 'f':
//Draw object I want to rotate
break;
case 'v':
//Draw object I want to rotate
break;
case 'e':
//Draw object I want to rotate
break;
}
glPopMatrix(); //------------------- Encapsulate Rotation Ends -------
//Draw object I DO NOT want to rotate
glutSwapBuffers ( ); // Swap The Buffers To Not Be Left With A Clear Screen
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.