首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenGL C++围绕场景中心旋转物体

OpenGL C++围绕场景中心旋转物体
EN

Stack Overflow用户
提问于 2017-01-29 22:33:30
回答 1查看 607关注 0票数 2

我在水族馆里做鱼,我想用鼠标把水族箱的“墙壁”和水族箱的底部绕着水族箱的中心旋转。只是场景有点旋转为“等距”。墙壁只是像他们想要的那样飞(围绕中心,但影响墙壁的形状和位置,导致他们分开)。下面是旋转水族馆壁上的臭虫的外观。主类(扩展QGLWidget)中的绘画功能是:

代码语言:javascript
复制
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();

鼠标拖动功能:

代码语言:javascript
复制
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();
    }
}

水族馆头墙:

代码语言:javascript
复制
#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;
};

水族馆墙壁的绘制功能:

代码语言:javascript
复制
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();
}

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-29 23:34:16

从你的外表看,你的墙被剪掉了。它看起来就像opengl在你的墙后面和前面设置了远和近的剪裁平面,这通常是很好的,但是当你旋转的时候,角穿过剪裁平面,然后被剪裁。

我没有运行你的代码,所以我不知道(这里已经是午夜了)。

祝好运

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41926717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档