在OpenGL旋转物体围绕本身 [英] Rotating object around itself in OpenGL

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

问题描述

我无法转动自己周围的房间。其中心位于(5,5,0),所以我想如果我翻译室以及带的glTranslatef在房间里的物品(5,5,5),然后glRotatef(rotateroom,0,0,1),然后绘制项目并使用glTranslate(-5,-5,0),将旋转它的房间,周围的一切5,5,0,但它似乎仍然围绕(0,0,0)旋转,我真的不知道是什么我做错了。感谢您提前帮助。

 无效drawside(){
    INT I,J;
    为/ *(; J&下; J = 0 = 8; J ++){
       在glBegin(GL_LINE_STRIP);
       对于(i = 0; I< = 30;我++)
          glEvalCoord2f((GLfloat)I / 30.0,(GLfloat)的J / 8.0);
       glEnd();
       在glBegin(GL_LINE_STRIP);
       对于(i = 0; I< = 30;我++)
          glEvalCoord2f((GLfloat)的J / 8.0,(GLfloat)I / 30.0);
       glEnd();
    } * /
    glEvalMesh2(GL_FILL,0,20,0,20);
}
无效drawRoom(){
    //地板
    在glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glNormal3f(0,0,0);
    glVertex3f(0,0,0);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //壁
    在glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(0,10,10);
    glVertex3f(0,10,10);
    glNormal3f(10,10,10);
    glVertex3f(10,10,10);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glEnd();
    // wall2
    在glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,10);
    glNormal3f(10,0,10);
    glVertex3f(10,0,10);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd();
}
无效drawObjects(){
    glColor3f(1,0,1);
    的glTranslatef(2,2,0);
    如果(conefill == 1)
        glShadeModel(GL_FLAT);
    其他
        glShadeModel(GL_SMOOTH);
    glutSolidCone(锥体,5,10,2);
    的glTranslatef(5,5,0);
    glColor3f(1,0,0);
    如果(spherefill == 1)
        glShadeModel(GL_FLAT);
    其他
        glShadeModel(GL_SMOOTH);
    glutSolidSphere(球面,20,20);}无效显示(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,空​​想的,viewz,viewx + LX,5,viewz + LY,0.0,0.0,1.0F);
    // gluLookAt(viewx,空​​想的,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    GLfloat light_position [] = {-1.0,5.0,5.0,0.0};
    // GLfloat light_direction [] = {-5,-5,-5};
    // GLfloat AMB [] = {0,0,0,1};
    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
    // glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,light_direction);
    // glLightfv(GL_LIGHT0,GL_AMBIENT,AMB);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    的glTranslatef(5,5,0);
    glRotatef(rotateroom,0,0,1);
    glPushMatrix();
    drawRoom();
    glPopMatrix();
    glColor3f(0.0,0.0,1.0);
    glPushMatrix();
    的glTranslatef(6,3,1);
    glPushMatrix();
    drawside();
    glPopMatrix();
    glPushMatrix();
    //的glTranslatef(5,0,0);
    glRotatef(90,1,0,0);
    //的glTranslatef(-5,0,0);
    的glTranslatef(0 .5 .5);
    drawside();
    glPopMatrix();
    glPushMatrix();
    //的glTranslatef(5,0,0);
    glRotatef(180,1,0,0);
    //的glTranslatef(-5,0,0);
    的glTranslatef(0,1,0);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(90,0,0,1);
    的glTranslatef( - 5,.5,0。);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(270,0,0,1);
    的glTranslatef(0.5,.5,0);
    drawside();
    glPopMatrix();
    glPopMatrix();
    drawObjects();
    的glTranslatef(-5,-5,0);
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}


解决方案

您只需要反转istruction的顺序:

  glRotatef(rotateroom,0,0,1);
的glTranslatef(5,5,0);

每当你trasform一个点,必须颠倒要应用改造中的顺序。在路上这dipend变换矩阵multiplicated。

i'm having trouble rotating a room around itself. its center is at (5,5,0) so i thought if i translated the room and the objects in the room with glTranslatef(5,5,5) then glRotatef(rotateroom,0,0,1) and then draw the items and use glTranslate(-5,-5,0) it would rotate the room and everything in it around 5,5,0 but it seems to still be rotating around (0,0,0) and i'm not really sure what I'm doing wrong. Thanks for the help in advance.

void drawside(){
    int i,j;
    /*for (j = 0; j <= 8; j++) {
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
          glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
       glEnd();
       glBegin(GL_LINE_STRIP);
       for (i = 0; i <= 30; i++)
          glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
       glEnd();
    }*/
    glEvalMesh2(GL_FILL, 0, 20, 0, 20);
}
void drawRoom(){
    //floor
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glNormal3f(0,0,0);
    glVertex3f(0,0,0);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd(
    );
    //wall
    glBegin(GL_POLYGON);
    glColor3f(0,0,1);
    glNormal3f(0,10,0);
    glVertex3f(0,10,0);
    glNormal3f(0,10,10);
    glVertex3f(0,10,10);
    glNormal3f(10,10,10);
    glVertex3f(10,10,10);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glEnd();
    //wall2
    glBegin(GL_POLYGON);
    glColor3f(0,1,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,0);
    glNormal3f(10,10,0);
    glVertex3f(10,10,10);
    glNormal3f(10,0,10);
    glVertex3f(10,0,10);
    glNormal3f(10,0,0);
    glVertex3f(10,0,0);
    glEnd();
}
void drawObjects(){
    glColor3f(1,0,1);
    glTranslatef(2,2,0);
    if(conefill == 1)
        glShadeModel(GL_FLAT);
    else
        glShadeModel(GL_SMOOTH);
    glutSolidCone(cone,5,10,2);
    glTranslatef(5,5,0);
    glColor3f(1,0,0);
    if(spherefill == 1)
        glShadeModel(GL_FLAT);
    else 
        glShadeModel(GL_SMOOTH);
    glutSolidSphere(sphere,20,20);

}

void display(){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(viewx,viewy, viewz,viewx +lx, 5, viewz + ly,0.0f, 0.0f,  1.0f);
    //gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    GLfloat light_position[] = {-1.0,5.0,5.0,0.0};
    //GLfloat light_direction[] = {-5,-5,-5};
    //GLfloat amb[] = {0,0,0,1};
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction);
    //glLightfv(GL_LIGHT0, GL_AMBIENT,amb);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef(5,5,0);
    glRotatef(rotateroom,0,0,1);
    glPushMatrix(); 
    drawRoom();
    glPopMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glPushMatrix();
    glTranslatef(6,3,1);
    glPushMatrix ();
    drawside();
    glPopMatrix();
    glPushMatrix();
    //glTranslatef(5,0,0);
    glRotatef(90,1,0,0);
    //glTranslatef(-5,0,0);
    glTranslatef(0,.5,.5);
    drawside();
    glPopMatrix();
    glPushMatrix();
    //glTranslatef(5,0,0);
    glRotatef(180,1,0,0);
    //glTranslatef(-5,0,0);
    glTranslatef(0,1,0);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(90,0,0,1);
    glTranslatef(-.5,.5,0);
    drawside();
    glPopMatrix();
    glPushMatrix();
    glRotatef(270,0,0,1);
    glTranslatef(.5,.5,0);
    drawside();
    glPopMatrix();
    glPopMatrix();
    drawObjects();
    glTranslatef(-5,-5,0);
    glPopMatrix();
    glutSwapBuffers();
    glFlush();
}

解决方案

You only need to invert the order of the istruction:

glRotatef(rotateroom,0,0,1);
glTranslatef(5,5,0);

Whenever you trasform a point you must invert the sequence of trasformation you want to apply. This dipend on the way the transformation matrix are multiplicated.

这篇关于在OpenGL旋转物体围绕本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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