I have a car model with different parts of the car described in ve开发者_运维技巧rtex groups.
You know the current position of the tyre so you use a translation matrix built from the identity matrix and negated position to translate it to the origin:
1 0 0 -x
0 1 0 -y
0 0 1 -z
0 0 0 1
Then rotate and then apply the forward transformation to put it back:
1 0 0 x
0 1 0 y
0 0 1 z
0 0 0 1
You might want to check out glPushMatrix() and glPopMatrix(). glPushMatrix() will save any translate/scale/rotate operations that you previously used (i.e. your old model view matrix). When you push your old model view matrix, you'll then be working at the origin. Then you can perform any new translate/rotate/scale operations to whatever you're currently drawing. Finally, you call glPopMatrix(). This will reload your old model view matrix. This is the cleanest and easiest way to think about these operations.
glPushMatrix();
// at this point you're working at the origin
// translate the tire here
// rotate the tire here
glPopMatrix();
In general, this is a good way to position something that you're drawing.
Here is the code:
glTranslatef(x_translate_factor,y_translate_factor,z_translate_factor);
glRotatef(global_car_rotate,0,1,0) ;
glRotatef(global_tire_rotate,0,1,0);
glRotatef(tire_speed,1,0,0);
Where tire_speed is tire spin speed, global_tire_rotate is the rotation of the tire relative to the car. Global_car_rotate is the rotation of the car (and objects with it such as wheel) relative to the z axis. *_translate_factor is the car's *-position inside your scence. It worked fine for me, hope it does for you ;).
精彩评论