Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21986

Failed to rotate semi-cube

I use the word semi-cube here, isn't a real cube, it has only 3 faces.I do the following:

1. Draw three faces of a cube in blue, the first face is blue, the other two are red; 2. Rotate the semi-cube of 45 degrees, in a way that I should see half of the red face.

But then I display the cube only the blue face is there, I should see half blue and half red.
Maybe I fail to enable the depth (I use glEnable()), I have the impression that the depth dimension is ignored in my drawing.

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>

int width=500, height=500, depth=500;

void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, width, height);
    glOrtho(0, width, height, 0, 0, 1);
}

void display()
{
    glClearColor(0.9, 0.9, 0.9, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor4f(0, 0, 1, 0);
    glBegin(GL_QUADS);


    // First face
    glVertex3i(100, 100,0);
    glVertex3i(300, 100,0);
    glVertex3i(300, 300,0);
    glVertex3i(100, 300,0);

    glColor4f(1, 0, 0, 0);
    // Second face
    glVertex3i(300,100,0);
    glVertex3i(300,300,0);
    glVertex3i(300,100,300);
    glVertex3i(300,100,300);

    // Third face
    glVertex3i(100, 100,300);
    glVertex3i(300, 100,300);
    glVertex3i(300, 300,300);
    glVertex3i(100, 300,300);

    glEnd();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(45, 1, 0, 0);

    glFlush();
}

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    init();
    glutMainLoop();
    return 0;
}

This is the image of what I get instead:

Image

EDIT : I kinda resolved changing the viewport:

glOrtho(0, width, height, 0, -depth, depth);

But I'm still missing basics, for now I'll go ahead.

Upvotes: 0

Views: 105

Answers (2)

jmh
jmh

Reputation: 9356

Rotation updates the current matrix, which will affect all object drawn after the rotation.

In order to see the rotation just move it above your draw lines.

Upvotes: 0

Tim
Tim

Reputation: 35943

Rotation only effects objects that are drawn after the rotation. When you call glBegin, whatever you draw is immediately drawn using the current modelview matrix on the stack.

Modifying the matrix after drawing has no effect. You should move the rotation before the draw call.

Upvotes: 2

Related Questions