在opengl中旋转对象 [英] rotating objects in opengl

查看:149
本文介绍了在opengl中旋转对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下一个键时,我需要在一个轴上旋转场景。这是我实现它的方式

  int rotateFlag; 

void rotateScene(int x){
if(x)//在x轴上旋转45度
glRotatef(45,1,0,0);
else // roatate y轴45度
glRotatef(45,0,1,0);
}

//这是我在glutDisplayFunc中调用的函数
void drawScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//我正在看原点(场景的中心)
gluLookAt(x,y,z,0,0,0,0.0,1.0,0.0);

rotateScene(rotateFlag);

drawObjects(); //绘制场景中的对象

}

当我按左键或向上键时,将值赋给rotateFlag。它只旋转场景一次,因为我的函数rotateScene always 将glRotatef应用于我应该避免的场景的原始布局。有人知道我如何保存旋转的场景,这样我就可以对其应用我的功能(旋转的场景),并且每次按下箭头键时旋转场景?我希望自己清楚了。



我尝试了另一种解决方案,在按下键的同时增加了X和Y的角度

 如果按键左键
angleX + = 45;
如果按键按下
angleY + = 45;

并将我的rotateScene调整为

  void rotateScene(){
glRotatef(angleX,1,0,0);
glRotatef(angleY,0,1,0);

$ / code>

当我在单个轴上执行旋转时,一切正常,但是,当我首先在Y上旋转,然后在X上执行旋转,场景的旋转不正确。也许这是因为opengl在Y上旋转之前首先执行旋转,因为函数调用中的顺序。我无法正确使用这个函数,因为在我的应用程序中,旋转场景的顺序非常重要。 使用 glPushMatrix()将矩阵推送到矩阵堆栈,然后 glPopMatrix()将其返回。


I need to rotate the scene in an axis when I press a key. Here's how I implemented it

    int rotateFlag;

    void rotateScene(int x){
         if(x)//rotate 45 deg on x axis
             glRotatef(45,1,0,0);
         else // roatate 45 deg on y axis
             glRotatef(45,0,1,0); 
    }

    //this is the function I call in  glutDisplayFunc
    void drawScene(void){
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
         glMatrixMode(GL_MODELVIEW); 
         glLoadIdentity();

         //I'm looking at the origin (center of my scene)
         gluLookAt (x, y, z, 0,0,0, 0.0, 1.0, 0.0); 

         rotateScene(rotateFlag);

         drawObjects();//draw Objects in Scene 

    }

I assign value to rotateFlag when i press left key or up key. It rotates the scene once only since my function rotateScene always applies glRotatef on the original arrangement of the scene which I should avoid. Does somebody knows how I can save the rotated scene so I can apply my function on it(the rotated scene) and rotate the scene everytime I press an arrow key? I hope I made myself clear.

I tried another solution by incrementing the angle in X and Y as I press the key

    If keypress left 
         angleX+=45;
    else if keypress up
         angleY+=45;

and adjust my rotateScene to

    void rotateScene(){
        glRotatef(angleX,1,0,0);
        glRotatef(angleY,0,1,0);
    }

Everything is going right when i performed rotation on individual axis, however, when I do Rotate on Y first then I perform rotate on X, the rotation of the scene is incorrect. Maybe this is because opengl executed rotate on X first before rotate on Y because of the ordering in the function call. I can't use this function properly since order in rotating the scene is important in my application.

解决方案

Use glPushMatrix() to push a matrix to the matrix-stack and glPopMatrix() to retreive it back.

这篇关于在opengl中旋转对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆