OpenGL C ++围绕场景中心旋转对象

时间:2017-01-29 22:33:30

标签: c++ qt opengl

我在鱼缸里制作鱼,我想旋转#34;墙壁"拖动鼠标,在水族馆中心周围的水族馆底部。除了场景稍微旋转为"等距"。墙壁就像他们想要的那样飞行(围绕中心,但影响墙壁的形状和位置,导致它们分开)。它的外观如下The bug on rotating aquarium walls。 主类中的绘制函数(扩展QGLWidget)是:

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();
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

从它的外观来看,你的墙被剪裁了。 看起来opengl有远近剪裁平面设置在你的墙后面,通常会很好,但是当你旋转时,角落穿过剪裁平面,然后被剪裁。

我没有运行你的代码,所以我无法告诉它(这是午夜)。

祝你好运