/* $Id: morph3d.c,v 1.4 2000/06/27 17:04:43 brianp Exp $ */ /* * $Log: morph3d.c,v $ * Revision 1.4 2000/06/27 17:04:43 brianp * fixed compiler warnings * * Revision 1.3 1999/12/15 13:00:45 brianp * moved #define to column 0 * * Revision 1.2 1999/09/17 12:27:01 brianp * silenced some warnings * * Revision 1.1.1.1 1999/08/19 00:55:40 jtg * Imported sources * * Revision 3.1 1998/06/29 02:37:30 brianp * minor changes for Windows (Ted Jump) * * Revision 3.0 1998/02/14 18:42:29 brianp * initial rev * */ /*- * morph3d.c - Shows 3D morphing objects * * Converted to GLUT by brianp on 1/1/98 * * This program was inspired on a WindowsNT(R)'s screen saver. It was written * from scratch and it was not based on any other source code. * * Porting it to xlock (the final objective of this code since the moment I * decided to create it) was possible by comparing the original Mesa's gear * demo with it's ported version, so thanks for Danny Sung for his indirect * help (look at gear.c in xlock source tree). NOTE: At the moment this code * was sent to Brian Paul for package inclusion, the XLock Version was not * available. In fact, I'll wait it to appear on the next Mesa release (If you * are reading this, it means THIS release) to send it for xlock package * inclusion). It will probably there be a GLUT version too. * * Thanks goes also to Brian Paul for making it possible and inexpensive * to use OpenGL at home. * * Since I'm not a native english speaker, my apologies for any gramatical * mistake. * * My e-mail addresses are * * vianna@cat.cbpf.br * and * marcelo@venus.rdc.puc-rio.br * * Marcelo F. Vianna (Feb-13-1997) */ /* This document is VERY incomplete, but tries to describe the mathematics used in the program. At this moment it just describes how the polyhedra are generated. On futhurer versions, this document will be probabbly improved. Since I'm not a native english speaker, my apologies for any gramatical mistake. Marcelo Fernandes Vianna - Undergraduate in Computer Engeneering at Catholic Pontifical University - of Rio de Janeiro (PUC-Rio) Brasil. - e-mail: vianna@cat.cbpf.br or marcelo@venus.rdc.puc-rio.br - Feb-13-1997 POLYHEDRA GENERATION For the purpose of this program it's not sufficient to know the polyhedra vertexes coordinates. Since the morphing algorithm applies a nonlinear transformation over the surfaces (faces) of the polyhedron, each face has to be divided into smaller ones. The morphing algorithm needs to transform each vertex of these smaller faces individually. It's a very time consoming task. In order to reduce calculation overload, and since all the macro faces of the polyhedron are transformed by the same way, the generation is made by creating only one face of the polyhedron, morphing it and then rotating it around the polyhedron center. What we need to know is the face radius of the polyhedron (the radius of the inscribed sphere) and the angle between the center of two adjacent faces using the center of the sphere as the angle's vertex. The face radius of the regular polyhedra are known values which I decided to not waste my time calculating. Following is a table of face radius for the regular polyhedra with edge length = 1: TETRAHEDRON : 1/(2*sqrt(2))/sqrt(3) CUBE : 1/2 OCTAHEDRON : 1/sqrt(6) DODECAHEDRON : T^2 * sqrt((T+2)/5) / 2 -> where T=(sqrt(5)+1)/2 ICOSAHEDRON : (3*sqrt(3)+sqrt(15))/12 I've not found any reference about the mentioned angles, so I needed to calculate them, not a trivial task until I figured out how :) Curiously these angles are the same for the tetrahedron and octahedron. A way to obtain this value is inscribing the tetrahedron inside the cube by matching their vertexes. So you'll notice that the remaining unmatched vertexes are in the same straight line starting in the cube/tetrahedron center and crossing the center of each tetrahedron's face. At this point it's easy to obtain the bigger angle of the isosceles triangle formed by the center of the cube and two opposite vertexes on the same cube face. The edges of this triangle have the following lenghts: sqrt(2) for the base and sqrt(3)/2 for the other two other edges. So the angle we want is: +-----------------------------------------------------------+ | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees | +-----------------------------------------------------------+ For the cube this angle is obvious, but just for formality it can be easily obtained because we also know it's isosceles edge lenghts: sqrt(2)/2 for the base and 1/2 for the other two edges. So the angle we want is: +-----------------------------------------------------------+ | 2*ARCSIN((sqrt(2)/2)/1) = 90.000000000000000000 degrees | +-----------------------------------------------------------+ For the octahedron we use the same idea used for the tetrahedron, but now we inscribe the cube inside the octahedron so that all cubes's vertexes matches excatly the center of each octahedron's face. It's now clear that this angle is the same of the thetrahedron one: +-----------------------------------------------------------+ | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees | +-----------------------------------------------------------+ For the dodecahedron it's a little bit harder because it's only relationship with the cube is useless to us. So we need to solve the problem by another way. The concept of Face radius also exists on 2D polygons with the name Edge radius: Edge Radius For Pentagon (ERp) ERp = (1/2)/TAN(36 degrees) * VRp = 0.6881909602355867905 (VRp is the pentagon's vertex radio). Face Radius For Dodecahedron FRd = T^2 * sqrt((T+2)/5) / 2 = 1.1135163644116068404 Why we need ERp? Well, ERp and FRd segments forms a 90 degrees angle, completing this triangle, the lesser angle is a half of the angle we are looking for, so this angle is: +-----------------------------------------------------------+ | 2*ARCTAN(ERp/FRd) = 63.434948822922009981 degrees | +-----------------------------------------------------------+ For the icosahedron we can use the same method used for dodecahedron (well the method used for dodecahedron may be used for all regular polyhedra) Edge Radius For Triangle (this one is well known: 1/3 of the triangle height) ERt = sin(60)/3 = sqrt(3)/6 = 0.2886751345948128655 Face Radius For Icosahedron FRi= (3*sqrt(3)+sqrt(15))/12 = 0.7557613140761707538 So the angle is: +-----------------------------------------------------------+ | 2*ARCTAN(ERt/FRi) = 41.810314895778596167 degrees | +-----------------------------------------------------------+ */ #include #include #ifndef _WIN32 #include #endif #include #include #include #define Scale 0.3 #define VectMul(X1,Y1,Z1,X2,Y2,Z2) (Y1)*(Z2)-(Z1)*(Y2),(Z1)*(X2)-(X1)*(Z2),(X1)*(Y2)-(Y1)*(X2) #define sqr(A) ((A)*(A)) /* Increasing this values produces better image quality, the price is speed. */ /* Very low values produces erroneous/incorrect plotting */ #define tetradivisions 23 #define cubedivisions 20 #define octadivisions 21 #define dodecadivisions 10 #define icodivisions 15 #define tetraangle 109.47122063449069174 #define cubeangle 90.000000000000000000 #define octaangle 109.47122063449069174 #define dodecaangle 63.434948822922009981 #define icoangle 41.810314895778596167 #ifndef Pi #define Pi 3.1415926535897932385 #endif #define SQRT2 1.4142135623730951455 #define SQRT3 1.7320508075688771932 #define SQRT5 2.2360679774997898051 #define SQRT6 2.4494897427831778813 #define SQRT15 3.8729833462074170214 #define cossec36_2 0.8506508083520399322 #define cos72 0.3090169943749474241 #define sin72 0.9510565162951535721 #define cos36 0.8090169943749474241 #define sin36 0.5877852522924731292 /*************************************************************************/ static int mono=0; static int smooth=1; static GLint WindH, WindW; static GLfloat step=0; static GLfloat seno; static int object; static int edgedivisions; static void (*draw_object)( void ); static float Magnitude; static float *MaterialColor[20]; static float front_shininess[] = {60.0}; static float front_specular[] = { 0.7, 0.7, 0.7, 1.0 }; static float ambient[] = { 0.0, 0.0, 0.0, 1.0 }; static float diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; static float position0[] = { 1.0, 1.0, 1.0, 0.0 }; static float position1[] = {-1.0,-1.0, 1.0, 0.0 }; static float lmodel_ambient[] = { 0.5, 0.5, 0.5, 1.0 }; static float lmodel_twoside[] = {GL_TRUE}; static float MaterialRed[] = { 0.7, 0.0, 0.0, 1.0 }; static float MaterialGreen[] = { 0.1, 0.5, 0.2, 1.0 }; static float MaterialBlue[] = { 0.0, 0.0, 0.7, 1.0 }; static float MaterialCyan[] = { 0.2, 0.5, 0.7, 1.0 }; static float MaterialYellow[] = { 0.7, 0.7, 0.0, 1.0 }; static float MaterialMagenta[] = { 0.6, 0.2, 0.5, 1.0 }; static float MaterialWhite[] = { 0.7, 0.7, 0.7, 1.0 }; static float MaterialGray[] = { 0.2, 0.2, 0.2, 1.0 }; #define TRIANGLE(Edge, Amp, Divisions, Z) \ { \ GLfloat Xf,Yf,Xa,Yb,Xf2,Yf2; \ GLfloat Factor,Factor1,Factor2; \ GLfloat VertX,VertY,VertZ,NeiAX,NeiAY,NeiAZ,NeiBX,NeiBY,NeiBZ; \ GLfloat Ax,Ay,Bx; \ int Ri,Ti; \ GLfloat Vr=(Edge)*SQRT3/3; \ GLfloat AmpVr2=(Amp)/sqr(Vr); \ GLfloat Zf=(Edge)*(Z); \ \ Ax=(Edge)*(+0.5/(Divisions)), Ay=(Edge)*(-SQRT3/(2*Divisions)); \ Bx=(Edge)*(-0.5/(Divisions)); \ \ for (Ri=1; Ri<=(Divisions); Ri++) { \ glBegin(GL_TRIANGLE_STRIP); \ for (Ti=0; Ti