
Vector multiplication. 
type568
Member #8,381
March 2007

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:
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 what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
type568
Member #8,381
March 2007

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 xaxis, yaxis and zaxis, 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 zcoordinate. 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 what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
type568
Member #8,381
March 2007

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. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. 
