Viktor Kalashnykov
Viktor Kalashnykov

Reputation: 71

OpenGL C++ rotating objects around the center of scene

I'm making the fish in aquarium and I want to rotate the "walls" and bottom of aquarium around the center of aquarium with dragging the mouse. Except that the scene a bit rotated to be "isometric". The walls just flying like they want (around center, but affecting the shape and position of walls causing them to separate). Here is how it looksThe bug on rotating aquarium walls. The painting function in main class (extending the QGLWidget) is:

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

Mouse dragging functions:

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

Walls of Aquarium header:

#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;
};

Walls of aquarium draw function:

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

What am I doing wrong?

Upvotes: 2

Views: 667

Answers (1)

David van rijn
David van rijn

Reputation: 2220

From what it looks like, your walls are being clipped. It looks like opengl has the far and near clipping planes set just behind and just before your walls, which normally would be fine, but when you rotate, the corners poke through the clipping planes, and get clipped.

I didn't run your code so i cannot really tell (it's midnight here).

Good luck

Upvotes: 1

Related Questions