simple car and chopstick man
how can I draw a simple car with one 3d cube and 4 spheres under it and a chopstick man. Can you help me with writing some codes about translations,scale and rotate I can't place 4 sphere under a cube or a sphere onto a thin cube.
Well, if you can set up the screen for drawing, then the rest of the code will basically look like this:
That'll draw the car. It's just a cube with balls for wheels, but you get what you ask for. And it's late right now.
void draw_car(){ glPushMatrix(); glutWireCube(10.0); glTranslatef(-5.0, -5.0, 4.0); glutWireSphere(0.5, 8, 8); glTranslatef(0, 0, -8); glutWireSphere(0.5, 8, 8); glTranslatef(10, 0, 0); glutWireSphere(0.5, 8, 8); glTranslatef(0, 0, 8); glutWireSphere(0.5, 8, 8); glPopMatrix(); return;}
That'll draw the car. It's just a cube with balls for wheels, but you get what you ask for. And it's late right now.
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
I don't mean to accuse, but the question sounds like a set problem (i.e. homework problem or equivalent). Although DuncanBojangles has already given you a solution to the first part I hope nobody will post a direct solution to the second because I think you'll learn a lot more by figuring it out by yourself.
I'm going to assume you're completely new to OpenGL. Please bear with me if I over-explain things. Better you have too much information than too little [smile]. It looks like you already know the basic transformations: scale, rotate and translate. DuncanBojangles has given you commands to draw wire cubes (glutWireCube) and wire spheres (glutWireSphere).
I would encourage you to try and recreate the car that DuncanBojangles posted, without using his exact code. Start with the second NeHe tutorial. This gives you code to display a simple polygon. Remove all the glTranslate, glBegin, glVertex and glEnd calls. This gives you a blank framework to start with.
Now add a simple wire cube. Choose a size off the top of your head. Remember that OpenGL will begin drawing at the origin of the world, which is where the camera is located, so you'll need to move it away from the camera so that it becomes visible.
Now try to add a sphere immediately after it. You'll notice that the sphere appears in exactly the same place as the cube. This is because OpenGL uses a local coordinate system. When you used the translate command, you didn't move the cube, you moved the coordinate system. The cube and the sphere were both then drawn relative to that coordinate system.
Now try moving the sphere so that it is underneath one corner of the cube. If the sphere is too big you'll need to scale it. You can use glScale for this, but remember this will scale the coordinate system, not the object. If you scale down by one half then you will need to move twice as far under the new coordinate system as you did under the unscaled one in order to get to the same place.
If you think about it you'll realise that once you've successfully positioned one sphere the other three should be easy. You just need to start from the centre of the cube and move in a different direction - but you've moved the coordinate system, how do you move it back? There are two ways. Either perform the exact opposite series of transformations in the opposite order to get back to where you were, or use glPushMatrix.
glPushMatrix 'saves' the current coordinate system. You can then scale, translate and rotate the coordinate system to your hearts content and then pop the old coordinate system to get back to where you were by using glPopMatrix. Each call to glPushMatrix must be matched by a call to glPopMatrix.
Have a play around and don't get discouraged if you don't get the hang of things straight away. Just remember that you are transforming the coordinate system and not the models. Rotation will probably be the hardest part for you get to grips with (it was for me). If you have any further problems then please continue to post them, but show us what you've tried and why it isn't working - it's much easier for us to help you then.
Good luck,
Enigma
I'm going to assume you're completely new to OpenGL. Please bear with me if I over-explain things. Better you have too much information than too little [smile]. It looks like you already know the basic transformations: scale, rotate and translate. DuncanBojangles has given you commands to draw wire cubes (glutWireCube) and wire spheres (glutWireSphere).
I would encourage you to try and recreate the car that DuncanBojangles posted, without using his exact code. Start with the second NeHe tutorial. This gives you code to display a simple polygon. Remove all the glTranslate, glBegin, glVertex and glEnd calls. This gives you a blank framework to start with.
Now add a simple wire cube. Choose a size off the top of your head. Remember that OpenGL will begin drawing at the origin of the world, which is where the camera is located, so you'll need to move it away from the camera so that it becomes visible.
Now try to add a sphere immediately after it. You'll notice that the sphere appears in exactly the same place as the cube. This is because OpenGL uses a local coordinate system. When you used the translate command, you didn't move the cube, you moved the coordinate system. The cube and the sphere were both then drawn relative to that coordinate system.
Now try moving the sphere so that it is underneath one corner of the cube. If the sphere is too big you'll need to scale it. You can use glScale for this, but remember this will scale the coordinate system, not the object. If you scale down by one half then you will need to move twice as far under the new coordinate system as you did under the unscaled one in order to get to the same place.
If you think about it you'll realise that once you've successfully positioned one sphere the other three should be easy. You just need to start from the centre of the cube and move in a different direction - but you've moved the coordinate system, how do you move it back? There are two ways. Either perform the exact opposite series of transformations in the opposite order to get back to where you were, or use glPushMatrix.
glPushMatrix 'saves' the current coordinate system. You can then scale, translate and rotate the coordinate system to your hearts content and then pop the old coordinate system to get back to where you were by using glPopMatrix. Each call to glPushMatrix must be matched by a call to glPopMatrix.
Have a play around and don't get discouraged if you don't get the hang of things straight away. Just remember that you are transforming the coordinate system and not the models. Rotation will probably be the hardest part for you get to grips with (it was for me). If you have any further problems then please continue to post them, but show us what you've tried and why it isn't working - it's much easier for us to help you then.
Good luck,
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement