Archived

This topic is now archived and is closed to further replies.

Lizzard

drawing a pyramid out of the axis vector and the base lenght

Recommended Posts

Hello! I would like to know if anybody could help me with my problem. For a programm i want to write i need to create a function that does this: The function takes two vectors (e.g. 10,10,5 and 5,3,4) and a float (e.g. 3). The function should then draw a pyramid with the top at 15, 13, 9 (the two vectors added together) and the base running through 10,10,5, the base lenght should be 3. I thought I could do it with glRotate(angle, x, y, z); glTranslate(10.0, 10.0, 5.0); But my problem is that i don''t know how to extract the angle from the vector given (here 5, 3, 4). Is there a function similar to glRotate that does what i need? Thank you! Lizzard

Share this post


Link to post
Share on other sites
You can get an angle from the dot product. By definition,
AdotB = A B cos(a),
where A and B are the lengths of the vectors and a is the angle between them. Also,
AdotB = A.x*B.x + A.y*B.y + A.z*B.z.
Using these two formulas, you can calculate the angle.

Share this post


Link to post
Share on other sites
For a vector {v} = a{i} + b{j} + c{k} whose length is
l = sqrt(a*a + b*b + c*c)
the direction cosines are
cos(a) = a/l
cos(b) = b/l
cos(c) = c/l

Is this what your after? I''m not sure what you want to do. You want to draw the edges or faces? of a pyramid. That would be 5 vertices, 8 edges, and 5 faces, correct?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thank you IndirectX and j p!

I figured out something with the 3 angles:

void xtraquad(float x, float y, float z)
{
float len = (float)sqrt(x*x+y*y+z*z);
float piover180 = 3.141592654f/180.0f;
float other = (float) 1/ piover180;
float xang = (float)(other*acos(x/len));
float yang = (float)(other*acos(y/len));
float zang = (float)(other*acos(z/len));
glRotatef(xang, 1,0,0);
glRotatef(yang, 0,1,0);
glRotatef(zang, 0,0,1);
quad();
}
//This function draws a cube whose axis is defined by the vector x, y, z.


But I wonder wheter it doesn''t matter in which order I rotate. But I had another idea how i could make it.

The axis vector is given. Then i would like to draw the cube along this vector, but using only one glRotatef. I do it like this: I take the vector that is given (x, y, z) and the y axis standard vector (0,1,0) with the cross product i get a vector (say a,b,c) that is rectangular to both of those vectors. By using the dot product i can find out the angle between the y axis and the given vector (e.g. alpha).
Then I use the function
glRotatef(alpha, a, b, c);

I think that should work.

>p j

It''s a general problem, sometimes i need the edges, sometimes the faces.

But what I m doing includes all kinds of objects. I want to build something like an editor by which you can create some objects and save those objects. And later I want to reuse this objects. For example i construct first a cube and a pyramid. Later i construct a house by putting some of the cubes and pyramids together. Later i could build a city by "recycling" the house i created. And that for I need a location vector and a direction vector.

Thanks again for your help!

Lizzard



Share this post


Link to post
Share on other sites
Thank you IndirectX and j p!

I figured out something with the 3 angles:

void xtraquad(float x, float y, float z)
{
float len = (float)sqrt(x*x+y*y+z*z);
float piover180 = 3.141592654f/180.0f;
float other = (float) 1/ piover180;
float xang = (float)(other*acos(x/len));
float yang = (float)(other*acos(y/len));
float zang = (float)(other*acos(z/len));
glRotatef(xang, 1,0,0);
glRotatef(yang, 0,1,0);
glRotatef(zang, 0,0,1);
quad();
}
//This function draws a cube whose axis is defined by the vector x, y, z.


But I wonder wheter it doesn''t matter in which order I rotate. But I had another idea how i could make it.

The axis vector is given. Then i would like to draw the cube along this vector, but using only one glRotatef. I do it like this: I take the vector that is given (x, y, z) and the y axis standard vector (0,1,0) with the cross product i get a vector (say a,b,c) that is rectangular to both of those vectors. By using the dot product i can find out the angle between the y axis and the given vector (e.g. alpha).
Then I use the function
glRotatef(alpha, a, b, c);

I think that should work.

>p j

It''s a general problem, sometimes i need the edges, sometimes the faces.

But what I m doing includes all kinds of objects. I want to build something like an editor by which you can create some objects and save those objects. And later I want to reuse this objects. For example i construct first a cube and a pyramid. Later i construct a house by putting some of the cubes and pyramids together. Later i could build a city by "recycling" the house i created. And that for I need a location vector and a direction vector.

Thanks again for your help!

Lizzard



Share this post


Link to post
Share on other sites