PDA

View Full Version : Cube doesn't rotate



ramy1989
11-13-2012, 06:18 AM
Here I am trying to draw a cube with three faces.One face is red, one green and one blue.
Then I rotate the cube in a way that I should see half of the red face and half of the green face, of 45 degrees around the y axis.I should see a half red and half green cube, but I only see the red face, that's the code:



#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, depth);
}


void display()
{
glClearColor(0.9, 0.9, 0.9, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(45, 0, 1, 0);

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

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

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

glEnd();
glFlush();
}


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


PS: Code outdated because my university is outdated :(

Dan Bartlett
11-13-2012, 06:36 AM
You need to switch the positions of:

gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
and

glMatrixMode(GL_MODELVIEW);

since the first time display is called you will still be in GL_PROJECTION matrix mode, so it will mess up your projection matrix.

It should probably read:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);

ramy1989
11-13-2012, 06:57 AM
I modified the code this way:



glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(250, 250, 0, -250, 250, 250, 0, 1, 0);
glRotatef(45, 0, 1, 0);


But I still see only a red face.
Also, I really can't understand when to be in modelview mode and when in projection mode.When I rotate, translate and scale I am in modelview mode, but for the other functions, how to know if I must use them in mdoelview or projection mode?

ramy1989
11-13-2012, 07:31 AM
I reviewed the code and got this way to draw an entire cube:



#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, -depth, depth);
}


void drawCube()
{
glBegin(GL_QUADS);

glColor4f(1,0,0,0);
glVertex3i(100, 100, 0);
glVertex3i(300, 100, 0);
glVertex3i(300, 300, 0);
glVertex3i(100, 300, 0);

glVertex3i(100, 100, 300);
glVertex3i(300, 100, 300);
glVertex3i(300, 300, 300);
glVertex3i(100, 300, 300);

glColor4f(0, 1, 0, 0);
glVertex3i(100, 100, 0);
glVertex3i(100, 100, 300);
glVertex3i(100, 300, 300);
glVertex3i(100, 300, 0);

glVertex3i(300, 100, 0);
glVertex3i(300, 100, 300);
glVertex3i(300, 300, 300);
glVertex3i(300, 300, 0);

glColor4f(0, 0, 1, 0);
glVertex3i(100, 100, 0);
glVertex3i(100, 100, 300);
glVertex3i(300, 100, 300);
glVertex3i(300, 100, 0);

glVertex3i(100, 300, 0);
glVertex3i(100, 300, 300);
glVertex3i(300, 300, 300);
glVertex3i(300, 300, 0);

glEnd();
}




void display()
{
glClearColor(0.9, 0.9, 0.9, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotatef(45, 1, 1, 0);
drawCube();
glLoadIdentity();
glFlush();
}




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


And it does rotate.