RobotRock
RobotRock

Reputation: 4459

OpenGL rotation of the camera

I have a cube with ofcourse 6 sides. Now I want not to render all sides when some of these sides are out of view. So I pass the rotations of the camera to the draw function seen below:

void Voxel::draw(QGLWidget *parent, GLfloat rotationX, GLfloat rotationY, GLfloat rotationZ) {

    glBegin(GL_QUADS);
    parent->qglColor(color);

    if ((rotationY > -90 && rotationY < 90) | rotationY < -270) {
        // Side top
        glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad
        glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
        glVertex3f( 1.0f,  1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
        glVertex3f(-1.0f,  1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad
    }

    // Side bottom
    glVertex3f(-1.0f,  -1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f( 1.0f,  -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f( 1.0f,  -1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  -1.0f,  1.0f);  // Top Left Of The Texture and Quad

    // Side 1
    glVertex3f(-1.0f, -1.0f,  1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f( 1.0f, -1.0f,  1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f( 1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Left Of The Texture and Quad

    // Side 2
    glVertex3f(-1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f( 1.0f,  1.0f,  -1.0f);  // Top Right Of The Texture and Quad
    glVertex3f( 1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f(-1.0f, -1.0f,  -1.0f);  // Bottom Left Of The Texture and Quad

    // Side 3
    glVertex3f(1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad
    glVertex3f(1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f(1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(1.0f, -1.0f,   1.0f);  // Bottom Left Of The Texture and Quad

    // Side 4
    glVertex3f(-1.0f, -1.0f,   1.0f);  // Bottom Left Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  1.0f);  // Top Right Of The Texture and Quad
    glVertex3f(-1.0f,  1.0f,  -1.0f);  // Top Left Of The Texture and Quad
    glVertex3f(-1.0f, -1.0f,  -1.0f);  // Bottom Right Of The Texture and Quad

    glEnd();
}

For some reason however, this does not fully work. Especially while rotating around the X axis. At some points of rotation it does not show the quad, while it should be in display.

I'm setting the rotation like this:

glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);

If I need to explain more, please ask. If one knows some good documentation about these things, please say so.

Upvotes: 0

Views: 289

Answers (2)

rotoglup
rotoglup

Reputation: 5283

This might not be the answer you're expecting, but, usually OpenGL hardware is quite good at not display the faces that are not facing the camera, this is called backface culling. Modern desktop hardware can discard about 4 billion triangles / second. So this optimization may not be one, and may not be worth of the time you're spending with it.

More over, assuming that you are using a perspective projection, your assumption about the visibility of a cube face being related to its rotation may not be true, regarding the position of the cube relative to the 'camera'.

Upvotes: 1

Deepak
Deepak

Reputation: 480

I think there is a silly mistake in your code in the line

if ((rotationY > -90 && rotationY < 90) | rotationY < -270) {

you should use || in place of |

Upvotes: 1

Related Questions