PDA

View Full Version : Help With Cube.

Leandro220
07-16-2011, 10:00 PM
Was my first time rendering a cube and the result was weird at first then I worked up my brain and camed with this:

But im having issues when visualizing it.

Below code I explain what the matter is.

here the code:

Variables:

float myX = 300;
float myY = 250;
float myZ = 0;
float width = 100;
float height = 100;

I rendered it this way

glRotatef(0,45,45,45);

glColor3f(1, 0, 0);

glVertex3f( myX, myY, myZ + 1);
glVertex3f( myX + width, myY, myZ + 1);
glVertex3f( myX + width, myY + height, myZ + 1);
glVertex3f( myX, myY + height, myZ + 1);

glEnd();

glColor3f(0, 0, 1);

glVertex3f( myX -50, myY -50 , myZ);
glVertex3f( myX + width - 100, myY, myZ + 1);
glVertex3f( myX, myY + 100, myZ + 1);
glVertex3f( myX -50 , myY + height -50, myZ + 1);

glEnd();

glColor3f(0, 1, 1);

glVertex3f( myX + 50, myY -50 , myZ);
glVertex3f( myX + width, myY, myZ + 1);
glVertex3f( myX + width, myY + 100, myZ + 1);
glVertex3f( myX + 50, myY + height -50, myZ + 1);

glEnd();

glColor3f(1, 1, 0);

glVertex3f( myX + 50, myY - 50, myZ + 1);
glVertex3f( myX + width, myY, myZ + 1);
glVertex3f( myX + width - 100, myY, myZ + 1);
glVertex3f (myX + width - 150, myY - 50, myZ + 1);

glEnd();

glColor3f(0, 1, 0);

glVertex3f( myX -50, myY -50 , myZ);
glVertex3f( myX + width -50, myY -50, myZ);
glVertex3f( myX + width - 50, myY + height -50, myZ);
glVertex3f( myX -50 , myY + height -50, myZ);

glEnd();

glColor3f(1, 0, 1);

glVertex3f( myX- 50, myY + 50, myZ + 1);
glVertex3f( myX + width - 50, myY + 50, myZ + 1);
glVertex3f( myX + width, myY + height, myZ + 1);
glVertex3f( myX, myY + height, myZ + 1);

glEnd();

But how im supossed to change its angle because the Rotatef is quite annoying

if i do change something on the first argument it began to behave very weird, but if i change something on the next arfuments nothing will happen :confused:

Please help with this and also is there a painless way to render the cube cause seriuosly no highschool prepare you for all that calculation :sleeping:

Leandro220
07-16-2011, 10:01 PM
I use SDL for screen and keys BTW... no glut

Just informing as for most tutorials they use it

zeoverlord
07-17-2011, 03:45 AM
glRotatef does not work that way.
The first value is how many degrees to rotate and the folowing 3 is the vector to rotate around

so it should look something like this
glRotatef(45,1,0,0);
glRotatef(45,0,1,0);
glRotatef(45,0,0,1);

glRotatef(0,45,45,45);

Leandro220
07-17-2011, 09:32 AM
So you mean is a glRotatef state for each axis?

I mean:

glRotatef(45,1,0,0); - > X,

glRotatef(45,0,1,0); - > Y,

glRotatef(45,0,1,0); -> Z.

ok but now the problem is that the cube dissapears

I Explained the situation here:

robmx
07-17-2011, 01:35 PM
I just want to point out a couple of things that I saw:

One is the setup of projection matrix: Usually, glOrtho is used to set up the projection matrix and it is done every time that the window is resized, not every time the frame is drawn.

So in the GL initialization you should have something like:

// Setup projection matrix
glMatrixMode(GL_PROJECTION);
glOrtho(...)
// Change to ModelView mode as normally all transformations
// are done in model space
glMatrixMode(GL_MODELVIEW);

// Other initialization

Second: in you draw function you must reset the modelview matrix every time, otherwise, the transformation will accumulate.

// Assuming the current matrix mode didn't change after calling init GL code.
// Reset modelview matrix to avoid accumulation of transformation
// Otherwise, transformation will get applied to the one done previously

//Do translation / rotation etc
glTranslatef(...)
glRotatef(...)

// Draw the geometry
glBegin(...);
glVertex3f(...);
...
...
glEnd();

MaxH
07-17-2011, 07:26 PM
So you mean is a glRotatef state for each axis? Read the description of glRotate in the manual (http://www.opengl.org/sdk/docs/man/xhtml/glRotate.xml)? Do you understand what it is saying? If not, the question to the forum should be, "I've read the manual and still don't understand what glRotate is supposed to do. Can someone explain it to me?"