PDA

View Full Version : Creating a wheel in OpenGL

infinitbelt
02-06-2010, 11:22 PM
Hi,

I need to create a model of a car and I am having trouble creating the wheels for it. The wheel has to be formed from 8 triangles rotated in a circle about a point. However, I am having difficulty with that.

Here's what I have so far:

void draw_triangle ()
{
glBegin(GL_TRIANGLES);

glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.0, 10);

glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0, -10);

glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.0, -10, 0);

glEnd();
}

void draw_wheel ()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glTranslatef(0.0, 10.0, 0.0);

draw_triangle();

glRotatef(45, 1.0, 0.0, 0.0);

draw_triangle();

glPopMatrix();

}

However, it is not giving me the desired result. If I can draw just one wheel correctly, I think I can manage drawing four wheels and placing them in the right locations but I just cannot seem to get it.

Any help is appreciated. Thank you!

Alfonse Reinheart
02-06-2010, 11:33 PM
However, it is not giving me the desired result

For one, you only drew two triangles. You'll need a loop to make this work the way you want.

For another, you really, really don't have to do it this way. Making an octagon is very simple; you can just make a "draw_octogon" function that draws 8 triangles. No need for matrix transformations. Just precompute what the vertices are.

infinitbelt
02-06-2010, 11:58 PM
For one, you only drew two triangles. You'll need a loop to make this work the way you want.

For another, you really, really don't have to do it this way. Making an octagon is very simple; you can just make a "draw_octogon" function that draws 8 triangles. No need for matrix transformations. Just precompute what the vertices are.

It didn't work right with 2 triangles so I figured it would fail even more with all 8. Could you give me an example of "draw_octagon" please?

Thank you!

Matthias Thurau
02-07-2010, 03:57 AM
You could try a GL_TRIANGLE_FAN:

http://wiki.delphigl.com/index.php/glBegin

infinitbelt
02-07-2010, 11:18 AM