Creating a wheel in OpenGL

Started by
5 comments, last by MarkS 14 years, 2 months ago
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. I know that I am only drawing 2 triangles instead of 8, but it did not give me a desired result with 2 so I figured I would hold off on drawing all 8. 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! [Edited by - infinitbelt on February 7, 2010 12:45:02 AM]
Advertisement
I think you are depending on GL too much to do the work. Math is ideal here.

Pseudo code:
 #include <math.h> glBegin(GL_TRIANGLE_FAN)  segs = 8  step = (360 / segs) * radians   glVertex3f(0, 0, 0)  for(i=0; i<segs; ++i) {   x = sinf(step * i) * radius   y = cosf(step * i) * radius     glVertex3f(x, y, 0)  }  glVertex3f(radius, 0, 0) glEnd()


disclaimer: something like that...tbh i haven't coded for months! But the debugging will allow you to understand ;-)
I agree with Jezham, however, to help you analyze your code: glTranslate() and glRotate() are order specific. If you translate before you rotate, and vice versa you get a different transformation matrix. To get the effect you want you sometimes need to play with order. That said, I'd be interested in what you are getting as a result that isn't what you expected.
I had a similar problem yesterday (drawing a circle) and after some help in the gamedev chat created this little function, maybe you can modify it to use 8 triangles.


    glBegin( GL_TRIANGLE_FAN );            glColor4f( 1.0, 1.0, 1.0, 1.0 );            glVertex2d( 0, 0 );            // This draws half the circle            int tempX = -10;            while ( tempX <= 10 )            {                glVertex2d( tempX, sqrt( 10*10 - tempX*tempX ) );                tempX++;            }            // This draws the other half            tempX = -10;            while ( tempX <= 10 )            {                glVertex2d( tempX, -sqrt( 10*10 - tempX*tempX ) );                tempX++;            }    glEnd();
The code jezham posted is far better than that.

If you want to get creative, look here: http://slabode.exofire.net/circle_draw.shtml Otherwise, using sin and cos is the way to go.

No, I am not a professional programmer. I'm just a hobbyist having fun...

@The OP: Did you ever solve the problem presented in your other thread? This assignment (I'm assuming it's an assignment) seems similar in nature, so you should be able to take whatever you learned there and apply it here, to some degree.
Taking that into account...

Both of these threads are the graphical equivalent of a "Hello World" application. There seems to be more of a lack of programming knowledge than lack of OpenGL knowledge.

As others have asked, is this a school project or homework?

No, I am not a professional programmer. I'm just a hobbyist having fun...

This topic is closed to new replies.

Advertisement