Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Vector multiplication.

This thread is locked; no one can reply to it. rss feed Print
Vector multiplication.
type568
Member #8,381
March 2007
avatar

I have gotten a problem.. I don't know 3d graphics. I made a 3d "BALL" of 20 polygons, with holes.. it is twisting well, each polygon has it's value, where in Z is it located, they are sorted, and in the new order, they are printed. But I know there is some other way to do it, also I would like to have each polygon 2 colors. (2 sides..)

Can some one please, explain me how is that possible, or refer me to some good tutorial? Thanks..

Johan Halmén
Member #1,550
September 2001

A ball with 20 polygons? Is that an icosahedron (just friendly google it) you have there?

If by 2 colours you mean the outside and inside of the ball, you could do the following. For each polygon, calculate the normal vector. That is, define two sides of the polygon as vectors. When you calculate the cross product of the two vectors, you get a third vector, which is a normal to the polygon (a perpendicular vector - or line - to the polygon). If the angle between this vector and the vector from the polygon to your viewing point is over 90 degrees, you see the "inner" side of the polygon, otherwise you see the outer side.

If the whole set of polygons is a polyhedron, that is an approximation of a sphere, you can draw all backside polygons first, in any order, then all frontside polygons, in any order. Something like:

1for (i = 0; i < 20; i++)
2{
3 polygon<i>.calculate_normal();
4 polygon<i>.calculate_angle_between_normal_and_viewer();
5}
6for (i = 0; i < 20; i++)
7{
8 if (polygon<i>.said_angle >= M_PI/2)
9 polygon<i>.render(back_colour);
10}
11for (i = 0; i < 20; i++)
12{
13 if (polygon<i>.said_angle < M_PI/2)
14 polygon<i>.render(front_colour);
15}

No sorting. And any holes you have in the front reveal the back side.

You need a vector class for this. And when picking the two vectors of a polygon, you have to be careful to pick them in same order, so that all normal vectors point out from the ball, not into the ball.

And the viewing point must be defined either as a point (in true perspective) or a direction (a vector will do) (in isometric perspective).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

type568
Member #8,381
March 2007
avatar

Thanks a lot for this explanation, but it doesn't help me..

I understand that three polygons, define (well, three but enough 2) vectors. I know what a vector is, in general (value with direction) but how would it look I have no idea.. a coord is float[4]- (x,y,z,1). matrix- float[x][x].

How should i define vector, and how should i fill in it?

Thanks..

Johan Halmén
Member #1,550
September 2001

A vector is usually presented as:

ai + bj + ck

where i, j and k are vectors along x-axis, y-axis and z-axis, respectively, all 1 unit long, so called unit vectors. When programming, and defining vectors as structs or classes, you just define the values of a, b and c, as floats.

So a vector struct looks quite like a coordinate struct!

One polygon of yours, say it's a triangle, has three vertices and three edges. Each vertex is defined by x-, y- and z-coordinate. Take two vertices and subtract one vertex coordinates from the other vertex coordinates respectively. Now you have the vector that goes from the first vertex to the other. Kind of. Do the same with another edge (and its two vertices, one of which is the same as in the first edge). Now you have two vectors and... goto my_first_post;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

type568
Member #8,381
March 2007
avatar

Thanks a lot. Second post makes everything much more clear to me now, though how to compute the normal vector (while have two vectors, which define a flat) I still don't understand.

Johan Halmén
Member #1,550
September 2001

Either learn vector maths or find a good vector class and learn how to use it.

The normal vector is the cross product of the two vectors that define the flat.

n = a x b

You get the cross product by creating the following matrix:

| i  j  k  |
| xa ya za |
| xb yb zb |

...and calculate the determinant of it. You end up in (something)i + (something)j + something(k), which is the normal vector, the length of which is not interesting.
Look...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: