本文介绍了OpenGL,如何旋转彼此独立的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止我的代码

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
}

但是现在我所有的对象同时旋转,我该如何旋转我注意到要旋转的对象,而又使我注意到不想旋转的对象呢?

However at the moment all my objects rotate at the same time, how can I rotate the object I've noted to want to rotate while leaving the ones I've noted to not want to rotate still?

推荐答案

由于@karlphillip指出,您需要将旋转值放入glPush/glPop矩阵封装中.我认为这段代码应该可以工作:

As @karlphillip states you need to put your rotation in to glPush/glPop matrix encapsulation. I think this code should work :

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
}

这篇关于OpenGL,如何旋转彼此独立的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:00