Jump to content
  • Advertisement
Sign in to follow this  
infinitbelt

OpenGL Creating a wheel in OpenGL

This topic is 3114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 ;-)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!