gluCylinder look at (solved)

Started by
21 comments, last by Zakwayda 18 years, 7 months ago
Hello, I have a normalized direction vector, but I'm having problems coming up with the rotation matrix needed to make a tapered gluCylinder (a cone basically) point in the same direction. Does anyone have any idea on how to do this? Any help would be much appreciated. [Edited by - taby on September 1, 2005 12:04:53 AM]
Advertisement
There are a couple ways to get an appropriate rotation matrix. You can decompose the direction vector into spherical coordinates, map them to a Euler angle pair, and then use glRotate(). Or, you could construct an orthonormal basis from the direction vector and load it directly to OpenGL.

Let me know if you need more details.

[Oop, one more method, and probably the easiest: call glRotate() with an axis-angle pair constructed from the direction vector and whichever world axis your object is aligned with.]

[Edited by - jyk on August 31, 2005 3:16:15 PM]
To be honest, any of these sound appealing, but I don't have the OpenGL know-how to go about performing any of them.

So far I've tried performing a cross product of the cylinder's direction (0, 0, 1) and the direction vector, then used that as the axis for glRotatef, but no luck.

Thanks very much for the head-start. I'll try looking into these methods if you don't get back with a more hands-on explanation. :)
Quote:So far I've tried performing a cross product of the cylinder's direction (0, 0, 1) and the direction vector, then used that as the axis for glRotatef, but no luck.
How exactly does it not work?

Here are a few things to keep in mind:

1. I think glRotate() normalizes the axis for you, but you might normalize it yourself to be sure.

2. You should detect the case where the direction vector is very nearly equal to (0,0,1), and load the identity matrix instead.

3. Given your input, the angle can be found as acos(a.Dot(b)). There are other, perhaps better, ways to get the angle, but that should work. Don't forget to clamp the input to acos() to the range [-1,1].

4. OpenGL uses degrees rather than radians, so you'll probably have to convert before calling glRotate().

If none of the above clears up your problem, you might post the relevant code.
This is the code I have so far, based on some help from ETG #opengl, where temp_ are my normalized direction vector components:

float lenx = sqrtf(temp_z*temp_z + temp_y*temp_y);float leny = sqrtf(temp_x*temp_x + temp_z*temp_z);glRotatef( acos( temp_x/leny )*180.0f/3.14f, 0, 1, 0 );glRotatef( acos( temp_z/lenx )*180.0f/3.14f, 1, 0, 0 );


Someone also told me it would be easiest to convert the vector to Euler angles, but I'm not sure how to do that.
Quote:Someone also told me it would be easiest to convert the vector to Euler angles, but I'm not sure how to do that.
That was actually one of my initial suggestions. Here's an implementation:

// x,y,z are direction vector componentsfloat yaw;if (fabs(x) < epsilon && fabs(z) < epsilon) // epsilon is some small number    yaw = 0.0f;else    yaw = atan2(x,z);float pitch = -atan2(y,sqrt(x*x+z*z));// * Convert to degrees here *glRotatef(yaw, 0.0f, 1.0f, 0.0f);glRotatef(pitch, 1.0f, 0.0f, 0.0f);

Can't guarantee I got that right, but you might give it a try.
It's working now...



The colour of the cone is determined by hue. that is: direction vector . static reference vector* -- Add 1.0 to the result, then multiply by 180 to get a value between 0 and 360 (colour circle)

At least the colouring is working :)

// * static reference vector is:static const float orientor_x = 0.57735f;static const float orientor_y = 0.57735f;static const float orientor_z = -0.57735f;


A no license implementation of the code is available for performing HSBtoRGB, if requested.

[Edited by - taby on September 1, 2005 11:49:35 AM]
Are the cones supposed to be aligned with the lines passing through them? If so, I think the problem is not (necessarily) how you're constructing the rotation matrix, but how you're handling the cone transformation in general. If I were you I'd post the code that sets up the transform for and renders a single cone. I'm guessing there may be an obvious error in there somewhere that will be easy to fix.
A ZIP of the working SDL C++ source is at:
Click Here

Hopefully it doesn't lock up on you, like reported elsewhere.
Controls are in the console window launched via main. 'v' for velocity cones, in particular.

[Edited by - taby on September 1, 2005 11:07:42 AM]
Quick question. When you tried the last bit of code I posted, did you convert yaw and pitch from radians to degrees before calling glRotate()?

If so, and it still didn't work, I would try the following. Stick some simple rotation in there, such as 90 degrees about a world axis, and see if it works. If it doesn't, the problem is probably somewhere else, rather than in the rotation code itself.

This topic is closed to new replies.

Advertisement