【问题标题】:OpenGL C++ rotating objects around the center of sceneOpenGL C++ 围绕场景中心旋转对象
【发布时间】:2017-01-29 22:33:30
【问题描述】:

我正在水族箱中制作鱼,我想通过拖动鼠标围绕水族箱中心旋转水族箱的“墙壁”和底部。除了场景有点旋转为“等距”。墙壁随心所欲地飞扬(围绕中心,但会影响墙壁的形状和位置,导致它们分开)。这是它的外观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();
}

我做错了什么?

【问题讨论】:

  • 您在不同的中心旋转物体:鱼、底部和水。如果您的“绘制”命令影响所有对象,就会发生奇怪的事情。在旁注中,请查看 khronos.org/opengl/wiki/Object_Mouse_Trackball 关于使用鼠标旋转。

标签: c++ qt opengl


【解决方案1】:

从外观上看,您的墙壁正在被修剪。 看起来 opengl 的远近剪裁平面设置在墙的后面和前面,这通常没问题,但是当您旋转时,角戳穿剪裁平面并被剪裁。

我没有运行你的代码,所以我无法确定(这里是午夜)。

祝你好运

【讨论】:

  • David van rijn,你是对的。墙壁只是被场景夹住了(我以为它们是围绕自己的中心或其他轴旋转)。谢谢!
  • 很高兴它有帮助,你能接受答案吗?为了结束,以及那些甜蜜点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 2016-09-10
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多