2017-01-29 25 views
2

私は水槽で魚を作っています。マウスをドラッグしてアクアリウムの中心の水槽の「壁」と底を回転させたいと思います。シーンが少し回転して「等角」になっていることを除けば。壁はちょうど彼らが望むように飛行します(中心の周り、壁の形と位置に影響して、それらを分離させます)。ここにそれがどのように見えるかThe bug on rotating aquarium wallsです。 メインクラス(QGLWidgetを拡張)に塗装関数である:OpenGL C++回転中心のオブジェクトを中心に回転する

void OpenGLWidget::paintGL(){ 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
//glEnable(GL_DEPTH); 
glLoadIdentity(); 
glEnable(GL_TEXTURE_2D); 
glLoadIdentity(); 
//Draw fish 
glPushMatrix(); 
glTranslatef(fish.center_x,fish.center_y,fish.center_z); 
glRotatef(20.0f,1.0f,0.0f,0.0f); // Rotation of 20 degrees around X axis to make scene isometric 
glRotatef(camera_y_rotation/32,0.0f,1.0f,0.0f); // Rotates around the Y axis width dragging the left mouse button 
glTranslatef(-fish.center_x,-fish.center_y,-fish.center_z); 
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
glTexGenf(GL_S,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR); 
glTexGenf(GL_T,GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR); 
glBindTexture(GL_TEXTURE_2D,fish_texture); 
fish.draw(); 
glPopMatrix(); 

glLoadIdentity(); 
//Draw Bottom of Aquarium 
glPushMatrix(); 
glTranslated(bottom.center_x,bottom.center_y,bottom.center_z); 
glRotatef(20.0f,1.0f,0.0f,0.0f);    //Just like 
glRotatef(camera_y_rotation/32,0.0f,1.0f,0.0f); // with fish 
glTranslated(-bottom.center_x,-bottom.center_y,-bottom.center_z); 
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
glTexGenf(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP); 
glTexGenf(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP); 
glBindTexture(GL_TEXTURE_2D,bottom_texture); 
bottom.draw(); 
glPopMatrix(); 

glLoadIdentity(); 
glPushMatrix(); 
glTranslated(water_side.center_x,water_side.center_y,water_side.center_z); 
glRotatef(20.0f,1.0f,0.0f,0.0f);    //Same here 
glRotatef(camera_y_rotation/32,0.0f,1.0f,0.0f); // 
glTranslated(-(water_side.center_x),-(water_side.center_y),-(water_side.center_z)); 
glBindTexture(GL_TEXTURE_2D,water_side_texture); 
water_side.draw(); 
glPopMatrix(); 

swapBuffers(); 

マウスドラッグ機能:水族館ヘッダの

void OpenGLWidget::mousePressEvent(QMouseEvent *event){ 
if(event->button()==Qt::LeftButton){ 
    leftMousePressed=true; 
    left_mouse_press_position_x=event->x(); 
    left_mouse_press_position_y=event->y(); 
} 
} 
void OpenGLWidget::mouseReleaseEvent(QMouseEvent *event){ 
if (event->button()==Qt::LeftButton) 
    leftMousePressed=false; 

} 

void OpenGLWidget::mouseMoveEvent(QMouseEvent *event){ 
    GLfloat delta_y=event->x()-left_mouse_press_position_x; 
    GLfloat delta_x=event->y()-left_mouse_press_position_y; 
    GLfloat rotation_x=camera_x_rotation+delta_x, rotation_y=camera_y_rotation+delta_y; 
    if (leftMousePressed){ 
     if (camera_x_rotation!=rotation_x) 
      camera_x_rotation=rotation_x; 
     if (camera_y_rotation!=rotation_y) 
      camera_y_rotation=rotation_y; 
     updateGL(); 
    } 
} 

壁:

#include <GL/gl.h> 
#include <FreeImage.h> 
#include <iostream> 

class WaterSide 
{ 
public: 
    WaterSide(); 
    void draw(); 
    GLuint load_texture(FIBITMAP * bitmap); 
public: 
    double center_x=0.0, center_y=-4.5, center_z=-11; 
}; 
水槽描画機能の

壁。

void WaterSide::draw(){ 
    glEnable(GL_TEXTURE_2D); 
    glColor4f(1.0f,1.0f,1.0f,1.0f); 
    glBegin(GL_QUADS); 
    glVertex3d(-9.0,-9.0,-2.0); 
    glVertex3d(9.0,-9.0,-2.0); 
    glVertex3d(9.0,0.0,-2.0); 
    glVertex3d(-9.0, 0.0,-2.0); 

    glVertex3d(-9.0,-9.0,-2.0); 
    glVertex3d(-9.0,-9.0,-20.0); 
    glVertex3d(-9.0,0.0,-20.0); 
    glVertex3d(-9.0, 0.0,-2.0); 

    glVertex3d(-9.0,-9.0,-20.0); 
    glVertex3d(9.0,-9.0,-20.0); 
    glVertex3d(9.0,0.0,-20.0); 
    glVertex3d(-9.0, 0.0,-20.0); 

    glVertex3d(9.0,-9.0,-20.0); 
    glVertex3d(9.0,-9.0,-2.0); 
    glVertex3d(9.0,0.0,-2.0); 
    glVertex3d(9.0, 0.0,-20.0); 
    glEnd(); 
} 

私は間違って何をしていますか?

+0

あなたは魚、底および水の別の中心に物を回転しています。あなたの '描画'コマンドがすべてのオブジェクトに影響を与える場合、奇妙なことが起こります。参考までに、マウスを回転させる方法についてはhttps://www.khronos.org/opengl/wiki/Object_Mouse_Trackballをご覧ください。 – Ripi2

答えて

1

あなたの壁は切り取られています。 openglは遠くに近いクリッピングプレーンがあなたの壁のすぐ後ろと壁の直前に設定されているようですが、通常は問題ありませんが、回転するとコーナーをクリッピングし、クリップされます。

私はあなたのコードを実行していないので、私は本当に(ここでは真夜中です)言うことができません。

幸運

+0

David van rijn、あなたは正しいです。壁はちょうど場面によって切り取られました(私は彼らが自分の中心を回転させたり、他の軸の周りを回転していると思いました)。ありがとうございました! –

+0

それは助けてよかったです、あなたは多分答えを受け入れることができますか?閉鎖のため、そしてそれらのスイートポイント。 –

関連する問題