Hi folks,
I am very newbie in OpenGL programming. Currently, I am trying to make a moving cube (forward/backward along z axis and rotate left/right around y axis). My code in display functions as follows:
void display()
{
glLoadIdentity();
glRotatef(spin_y, 0, 1, 0);
glTranslatef(0, 0, dist_z);
glutWireCube(1.0);
glFlush();
}
where spin_y and dist_z can be changed according to key presses. The problem is that If I move the cube forward ot backward then rotate it, then it seems that the cube will not rotate around itself. Rather, it rotates around the “original” origin which is not I expect.
Can anyone have a solution for this problem please?
Thank you very much.
DND
PS: I would like to know how to put 2D texture on 1 surface of my cube created using glutWireCube(). There is no command to specify vertex here, thus I do not know how to put texture coordinate.
That is one of the first problems new people have in understanding how the matrix operations work.
OpenGL translate/rotate operations work in reverse.
The order in which you call translate/rotate makes a big diffrence.
All rotations are done around 0,0,0 so they must adjust for this.
void display()
{
glLoadIdentity();
glTranslatef(0, 0, dist_z); // translate along the Z axis.
glRotatef(spin_y, 0, 1, 0); // Rotate around the Cubes axis
glutWireCube(1.0);
glFlush();
}
In my above example the operations work in reverse order.
First the Cube is rotated around 0,0,0; Note just happens on the predrawn glut objects 0,0,0 is there center.
Second the cube is translated on the z-axis.
You can not apply a texture to a glut object, because it does not have texture coordinates and there is no way to add them.
Use glu object instead, see my example of a textured glu object at my website. www.angelfire.com/linux/nexusone.
Or on nehe.gamedev.net has a texture cube demo.
Thank for your answer.
However, I still do not get expected result as if I put glTranslate before glRotate, then, the cube will always move in the same direction (i.e., the “original” z axis here). I want my cube can change forward/backward direction according to rotation transformation so I can move my cube freely any position on (x,z) plane.
Could you give any suggestion on this please?
Add a second glRotate.
glRotate //Direction of travel from 0,0,0
glTranslate //Distance from 0,0,0
glRotate //Cube rotation own it axis
Draw_cube();
Note you will want to have one glRotate for each axis.
glRotatef( x_angle, 1,0,0);
glRotatef( y_angle, 0,1,0);
glRotatef( z_angle, 0,0,1);
the other option and a better one is to use vector math a find the point in which to draw it as it movies in some angle out. Lot’s of tutors on 3D math on the web.
Originally posted by anhyeuem2004:
[b]Thank for your answer.
However, I still do not get expected result as if I put glTranslate before glRotate, then, the cube will always move in the same direction (i.e., the “original” z axis here). I want my cube can change forward/backward direction according to rotation transformation so I can move my cube freely any position on (x,z) plane.
Could you give any suggestion on this please?[/b]