2
0

morph3d.pp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. {
  2. *-
  3. * morph3d.c - Shows 3D morphing objects
  4. *
  5. * Converted to GLUT by brianp on 1/1/98
  6. * Converted to FreePascal by Peter Vreman on 9/3/2000
  7. *
  8. * This program was inspired on a WindowsNT(R)'s screen saver. It was written
  9. * from scratch and it was not based on any other source code.
  10. *
  11. * Porting it to xlock (the final objective of this code since the moment I
  12. * decided to create it) was possible by comparing the original Mesa's gear
  13. * demo with it's ported version, so thanks for Danny Sung for his indirect
  14. * help (look at gear.c in xlock source tree). NOTE: At the moment this code
  15. * was sent to Brian Paul for package inclusion, the XLock Version was not
  16. * available. In fact, I'll wait it to appear on the next Mesa release (If you
  17. * are reading this, it means THIS release) to send it for xlock package
  18. * inclusion). It will probably there be a GLUT version too.
  19. *
  20. * Thanks goes also to Brian Paul for making it possible and inexpensive
  21. * to use OpenGL at home. *
  22. * Since I'm not a native english speaker, my apologies for any gramatical
  23. * mistake.
  24. *
  25. * My e-mail addresses are
  26. *
  27. * [email protected]
  28. * and
  29. * [email protected]
  30. *
  31. * Marcelo F. Vianna (Feb-13-1997)
  32. */
  33. }
  34. program morph3d;
  35. {$mode objfpc}
  36. {
  37. This document is VERY incomplete, but tries to describe the mathematics used
  38. in the program. At this moment it just describes how the polyhedra are
  39. generated. On futhurer versions, this document will be probabbly improved.
  40. Since I'm not a native english speaker, my apologies for any gramatical
  41. mistake.
  42. Marcelo Fernandes Vianna
  43. - Undergraduate in Computer Engeneering at Catholic Pontifical University
  44. - of Rio de Janeiro (PUC-Rio) Brasil.
  45. - e-mail: [email protected] or [email protected]
  46. - Feb-13-1997
  47. POLYHEDRA GENERATION
  48. For the purpose of this program it's not sufficient to know the polyhedra
  49. vertexes coordinates. Since the morphing algorithm applies a nonlinear
  50. transformation over the surfaces (faces) of the polyhedron, each face has
  51. to be divided into smaller ones. The morphing algorithm needs to transform
  52. each vertex of these smaller faces individually. It's a very time consoming
  53. task.
  54. In order to reduce calculation overload, and since all the macro faces of
  55. the polyhedron are transformed by the same way, the generation is made by
  56. creating only one face of the polyhedron, morphing it and then rotating it
  57. around the polyhedron center.
  58. What we need to know is the face radius of the polyhedron (the radius of
  59. the inscribed sphere) and the angle between the center of two adjacent
  60. faces using the center of the sphere as the angle's vertex.
  61. The face radius of the regular polyhedra are known values which I decided
  62. to not waste my time calculating. Following is a table of face radius for
  63. the regular polyhedra with edge length = 1:
  64. TETRAHEDRON : 1/(2*sqrt(2))/sqrt(3)
  65. CUBE : 1/2
  66. OCTAHEDRON : 1/sqrt(6)
  67. DODECAHEDRON : T^2 * sqrt((T+2)/5) / 2 -> where T=(sqrt(5)+1)/2
  68. ICOSAHEDRON : (3*sqrt(3)+sqrt(15))/12
  69. I've not found any reference about the mentioned angles, so I needed to
  70. calculate them, not a trivial task until I figured out how :)
  71. Curiously these angles are the same for the tetrahedron and octahedron.
  72. A way to obtain this value is inscribing the tetrahedron inside the cube
  73. by matching their vertexes. So you'll notice that the remaining unmatched
  74. vertexes are in the same straight line starting in the cube/tetrahedron
  75. center and crossing the center of each tetrahedron's face. At this point
  76. it's easy to obtain the bigger angle of the isosceles triangle formed by
  77. the center of the cube and two opposite vertexes on the same cube face.
  78. The edges of this triangle have the following lenghts: sqrt(2) for the base
  79. and sqrt(3)/2 for the other two other edges. So the angle we want is:
  80. +-----------------------------------------------------------+
  81. | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees |
  82. +-----------------------------------------------------------+
  83. For the cube this angle is obvious, but just for formality it can be
  84. easily obtained because we also know it's isosceles edge lenghts:
  85. sqrt(2)/2 for the base and 1/2 for the other two edges. So the angle we
  86. want is:
  87. +-----------------------------------------------------------+
  88. | 2*ARCSIN((sqrt(2)/2)/1) = 90.000000000000000000 degrees |
  89. +-----------------------------------------------------------+
  90. For the octahedron we use the same idea used for the tetrahedron, but now
  91. we inscribe the cube inside the octahedron so that all cubes's vertexes
  92. matches excatly the center of each octahedron's face. It's now clear that
  93. this angle is the same of the thetrahedron one:
  94. +-----------------------------------------------------------+
  95. | 2*ARCSIN(sqrt(2)/sqrt(3)) = 109.47122063449069174 degrees |
  96. +-----------------------------------------------------------+
  97. For the dodecahedron it's a little bit harder because it's only relationship
  98. with the cube is useless to us. So we need to solve the problem by another
  99. way. The concept of Face radius also exists on 2D polygons with the name
  100. Edge radius:
  101. Edge Radius For Pentagon (ERp)
  102. ERp = (1/2)/TAN(36 degrees) * VRp = 0.6881909602355867905
  103. (VRp is the pentagon's vertex radio).
  104. Face Radius For Dodecahedron
  105. FRd = T^2 * sqrt((T+2)/5) / 2 = 1.1135163644116068404
  106. Why we need ERp? Well, ERp and FRd segments forms a 90 degrees angle,
  107. completing this triangle, the lesser angle is a half of the angle we are
  108. looking for, so this angle is:
  109. +-----------------------------------------------------------+
  110. | 2*ARCTAN(ERp/FRd) = 63.434948822922009981 degrees |
  111. +-----------------------------------------------------------+
  112. For the icosahedron we can use the same method used for dodecahedron (well
  113. the method used for dodecahedron may be used for all regular polyhedra)
  114. Edge Radius For Triangle (this one is well known: 1/3 of the triangle height)
  115. ERt = sin(60)/3 = sqrt(3)/6 = 0.2886751345948128655
  116. Face Radius For Icosahedron
  117. FRi= (3*sqrt(3)+sqrt(15))/12 = 0.7557613140761707538
  118. So the angle is:
  119. +-----------------------------------------------------------+
  120. | 2*ARCTAN(ERt/FRi) = 41.810314895778596167 degrees |
  121. +-----------------------------------------------------------+
  122. }
  123. uses
  124. GL,GLUT;
  125. type
  126. float = single;
  127. const
  128. Scale = 0.3;
  129. // #define sqr(A) ((A)*(A))
  130. // Increasing this values produces better image quality, the price is speed.
  131. // Very low values produces erroneous/incorrect plotting
  132. tetradivisions = 23;
  133. cubedivisions = 20;
  134. octadivisions = 21;
  135. dodecadivisions = 10;
  136. icodivisions = 15;
  137. tetraangle = 109.47122063449069174;
  138. cubeangle = 90.000000000000000000;
  139. octaangle = 109.47122063449069174;
  140. dodecaangle = 63.434948822922009981;
  141. icoangle = 41.810314895778596167;
  142. //#define Pi 3.1415926535897932385
  143. SQRT2 = 1.4142135623730951455;
  144. SQRT3 = 1.7320508075688771932;
  145. SQRT5 = 2.2360679774997898051;
  146. SQRT6 = 2.4494897427831778813;
  147. SQRT15 = 3.8729833462074170214;
  148. cossec36_2 = 0.8506508083520399322;
  149. cos72 = 0.3090169943749474241;
  150. sin72 = 0.9510565162951535721;
  151. cos36 = 0.8090169943749474241;
  152. sin36 = 0.5877852522924731292;
  153. {*************************************************************************}
  154. const
  155. mono : boolean=false;
  156. smooth : boolean=true;
  157. var
  158. WindH,WindW : GLint;
  159. step,seno : GLFloat;
  160. _object : glint;
  161. edgedivisions : glint;
  162. draw_object : procedure;
  163. Magnitude : glfloat;
  164. MaterialColor : array[0..19] of pglfloat;
  165. const
  166. front_shininess : array[0..0] of glfloat = (60.0);
  167. front_specular : array[0..3] of glfloat = ( 0.7, 0.7, 0.7, 1.0 );
  168. ambient : array[0..3] of glfloat = ( 0.0, 0.0, 0.0, 1.0 );
  169. diffuse : array[0..3] of glfloat = ( 1.0, 1.0, 1.0, 1.0 );
  170. position0 : array[0..3] of glfloat = ( 1.0, 1.0, 1.0, 0.0 );
  171. position1 : array[0..3] of glfloat = (-1.0,-1.0, 1.0, 0.0 );
  172. lmodel_ambient : array[0..3] of glfloat = ( 0.5, 0.5, 0.5, 1.0 );
  173. lmodel_twoside : array[0..0] of glfloat = (GL_TRUE);
  174. MaterialRed : array[0..3] of glfloat = ( 0.7, 0.0, 0.0, 1.0 );
  175. MaterialGreen : array[0..3] of glfloat = ( 0.1, 0.5, 0.2, 1.0 );
  176. MaterialBlue : array[0..3] of glfloat = ( 0.0, 0.0, 0.7, 1.0 );
  177. MaterialCyan : array[0..3] of glfloat = ( 0.2, 0.5, 0.7, 1.0 );
  178. MaterialYellow : array[0..3] of glfloat = ( 0.7, 0.7, 0.0, 1.0 );
  179. MaterialMagenta : array[0..3] of glfloat = ( 0.6, 0.2, 0.5, 1.0 );
  180. MaterialWhite : array[0..3] of glfloat = ( 0.7, 0.7, 0.7, 1.0 );
  181. MaterialGray : array[0..3] of glfloat = ( 0.2, 0.2, 0.2, 1.0 );
  182. procedure TRIANGLE(Edge,Amp:GLFloat; Divisions: longint; Z:GLFloat);
  183. var
  184. Xf,Yf,Xa,Yb,Xf2,Yf2 : Extended;
  185. Factor,Factor1,Factor2 : GLfloat;
  186. VertX,VertY,VertZ,NeiAX,NeiAY,NeiAZ,NeiBX,NeiBY,NeiBZ : GLfloat;
  187. Ax,Ay,Bx : GLfloat;
  188. Ri,Ti : longint;
  189. Vr : GLfloat;
  190. AmpVr2 : GLfloat;
  191. Zf : GLfloat;
  192. begin
  193. Vr:=(Edge)*SQRT3/3;
  194. AmpVr2:=(Amp)/sqr(Vr);
  195. Zf:=(Edge)*(Z);
  196. Ax:=(Edge)*(+0.5/(Divisions));
  197. Ay:=(Edge)*(-SQRT3/(2*Divisions));
  198. Bx:=(Edge)*(-0.5/(Divisions));
  199. for Ri:=1 to Divisions do
  200. begin
  201. glBegin(GL_TRIANGLE_STRIP);
  202. for Ti:=0 to Ri-1 do
  203. begin
  204. Xf:=(Ri-Ti)*Ax + Ti*Bx;
  205. Yf:=Vr+(Ri-Ti)*Ay + Ti*Ay;
  206. Xa:=Xf+0.001; Yb:=Yf+0.001;
  207. Xf2:=sqr(Xf);
  208. Yf2:=sqr(Yf);
  209. Factor:=1-(((Xf2)+(Yf2))*AmpVr2);
  210. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  211. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  212. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  213. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  214. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  215. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  216. glVertex3f(VertX, VertY, VertZ);
  217. Xf:=(Ri-Ti-1)*Ax + Ti*Bx;
  218. Yf:=Vr+(Ri-Ti-1)*Ay + Ti*Ay;
  219. Xa:=Xf+0.001; Yb:=Yf+0.001;
  220. Xf2:=sqr(Xf);
  221. Yf2:=sqr(Yf);
  222. Factor:=1-(((Xf2)+(Yf2))*AmpVr2);
  223. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  224. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  225. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  226. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  227. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  228. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  229. glVertex3f(VertX, VertY, VertZ);
  230. end;
  231. Xf:=Ri*Bx;
  232. Yf:=Vr+Ri*Ay;
  233. Xa:=Xf+0.001; Yb:=Yf+0.001;
  234. Xf2:=sqr(Xf);
  235. Yf2:=sqr(Yf);
  236. Factor:=1-((Xf2+Yf2)*AmpVr2);
  237. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  238. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  239. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  240. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  241. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  242. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  243. glVertex3f(VertX, VertY, VertZ);
  244. glEnd();
  245. end;
  246. end;
  247. procedure SQUARE(Edge,Amp:GLFloat; Divisions: longint; Z:GLFloat);
  248. var
  249. Xi,Yi : longint;
  250. Xf,Yf,Y,Y2,Xf2,Yf2,Xa,Yb : GLfloat;
  251. Factor,Factor1,Factor2 : GLfloat;
  252. VertX,VertY,VertZ,NeiAX,NeiAY,NeiAZ,NeiBX,NeiBY,NeiBZ : GLfloat;
  253. AmpVr2 : GLfloat;
  254. Zf : GLfloat;
  255. begin
  256. AmpVr2:=(Amp)/sqr((Edge)*SQRT2/2);
  257. Zf:=(Edge)*(Z);
  258. for Yi:=0 to Divisions-1 do
  259. begin
  260. Yf:=-((Edge)/2.0) + (Yi)/(Divisions)*(Edge);
  261. Yf2:=sqr(Yf);
  262. Y:=Yf+1.0/(Divisions)*(Edge);
  263. Y2:=sqr(Y);
  264. glBegin(GL_QUAD_STRIP);
  265. for Xi:=0 to Divisions do
  266. begin
  267. Xf:=-((Edge)/2.0) + (Xi)/(Divisions)*(Edge);
  268. Xf2:=sqr(Xf);
  269. Xa:=Xf+0.001; Yb:=Y+0.001;
  270. Factor:=1-((Xf2+Y2)*AmpVr2);
  271. Factor1:=1-((sqr(Xa)+Y2)*AmpVr2);
  272. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  273. VertX:=Factor*Xf; VertY:=Factor*Y; VertZ:=Factor*Zf;
  274. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Y-VertY; NeiAZ:=Factor1*Zf-VertZ;
  275. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  276. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  277. glVertex3f(VertX, VertY, VertZ);
  278. Xa:=Xf+0.001; Yb:=Yf+0.001;
  279. Factor:=1-((Xf2+Yf2)*AmpVr2);
  280. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  281. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  282. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  283. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  284. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  285. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  286. glVertex3f(VertX, VertY, VertZ);
  287. end;
  288. glEnd();
  289. end;
  290. end;
  291. procedure PENTAGON(Edge,Amp:GLFloat; Divisions: longint; Z:GLFloat);
  292. var
  293. Ri,Ti,Fi : longint;
  294. Xf,Yf,Xf2,Yf2,Xa,Yb : GLfloat;
  295. X,Y : array[0..5] of GLFloat;
  296. Factor,Factor1,Factor2 : GLfloat;
  297. VertX,VertY,VertZ,NeiAX,NeiAY,NeiAZ,NeiBX,NeiBY,NeiBZ : GLfloat;
  298. AmpVr2 : GLfloat;
  299. Zf : GLfloat;
  300. begin
  301. AmpVr2:=(Amp)/sqr((Edge)*cossec36_2);
  302. Zf:=(Edge)*(Z);
  303. for Fi:=0 to 5 do
  304. begin
  305. x[Fi]:=-cos( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);
  306. y[Fi]:=sin( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);
  307. end;
  308. for Ri:=1 to Divisions do
  309. begin
  310. for Fi:=0 to 4 do
  311. begin
  312. glBegin(GL_TRIANGLE_STRIP);
  313. for Ti:=0 to Ri-1 do
  314. begin
  315. Xf:=(Ri-Ti)*x[Fi] + Ti*x[Fi+1];
  316. Yf:=(Ri-Ti)*y[Fi] + Ti*y[Fi+1];
  317. Xa:=Xf+0.001; Yb:=Yf+0.001;
  318. Xf2:=sqr(Xf);
  319. Yf2:=sqr(Yf);
  320. Factor:=1-(((Xf2)+(Yf2))*AmpVr2);
  321. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  322. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  323. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  324. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  325. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  326. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  327. glVertex3f(VertX, VertY, VertZ);
  328. Xf:=(Ri-Ti-1)*x[Fi] + Ti*x[Fi+1];
  329. Yf:=(Ri-Ti-1)*y[Fi] + Ti*y[Fi+1];
  330. Xa:=Xf+0.001; Yb:=Yf+0.001;
  331. Xf2:=sqr(Xf);
  332. Yf2:=sqr(Yf);
  333. Factor:=1-(((Xf2)+(Yf2))*AmpVr2);
  334. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  335. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  336. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  337. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  338. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  339. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  340. glVertex3f(VertX, VertY, VertZ);
  341. end;
  342. Xf:=Ri*x[Fi+1];
  343. Yf:=Ri*y[Fi+1];
  344. Xa:=Xf+0.001; Yb:=Yf+0.001;
  345. Xf2:=sqr(Xf);
  346. Yf2:=sqr(Yf);
  347. Factor:=1-(((Xf2)+(Yf2))*AmpVr2);
  348. Factor1:=1-((sqr(Xa)+Yf2)*AmpVr2);
  349. Factor2:=1-((Xf2+sqr(Yb))*AmpVr2);
  350. VertX:=Factor*Xf; VertY:=Factor*Yf; VertZ:=Factor*Zf;
  351. NeiAX:=Factor1*Xa-VertX; NeiAY:=Factor1*Yf-VertY; NeiAZ:=Factor1*Zf-VertZ;
  352. NeiBX:=Factor2*Xf-VertX; NeiBY:=Factor2*Yb-VertY; NeiBZ:=Factor2*Zf-VertZ;
  353. glNormal3f(NeiAY*NeiBZ-NeiAZ*NeiBY,NeiAZ*NeiBX-NeiAX*NeiBZ,NeiAX*NeiBY-NeiAY*NeiBX);
  354. glVertex3f(VertX, VertY, VertZ);
  355. glEnd();
  356. end;
  357. end;
  358. end;
  359. procedure draw_tetra;
  360. var
  361. list : GLuint;
  362. begin
  363. list := glGenLists( 1 );
  364. glNewList( list, GL_COMPILE );
  365. TRIANGLE(2,seno,edgedivisions,0.5/SQRT6);
  366. glEndList();
  367. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[0]);
  368. glCallList(list);
  369. glPushMatrix();
  370. glRotatef(180,0,0,1);
  371. glRotatef(-tetraangle,1,0,0);
  372. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[1]);
  373. glCallList(list);
  374. glPopMatrix();
  375. glPushMatrix();
  376. glRotatef(180,0,1,0);
  377. glRotatef(-180+tetraangle,0.5,SQRT3/2,0);
  378. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[2]);
  379. glCallList(list);
  380. glPopMatrix();
  381. glRotatef(180,0,1,0);
  382. glRotatef(-180+tetraangle,0.5,-SQRT3/2,0);
  383. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[3]);
  384. glCallList(list);
  385. glDeleteLists(list,1);
  386. end;
  387. procedure draw_cube;
  388. var
  389. list : GLuint;
  390. begin
  391. list := glGenLists( 1 );
  392. glNewList( list, GL_COMPILE );
  393. SQUARE(2, seno, edgedivisions, 0.5);
  394. glEndList();
  395. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[0]);
  396. glCallList(list);
  397. glRotatef(cubeangle,1,0,0);
  398. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[1]);
  399. glCallList(list);
  400. glRotatef(cubeangle,1,0,0);
  401. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[2]);
  402. glCallList(list);
  403. glRotatef(cubeangle,1,0,0);
  404. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[3]);
  405. glCallList(list);
  406. glRotatef(cubeangle,0,1,0);
  407. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[4]);
  408. glCallList(list);
  409. glRotatef(2*cubeangle,0,1,0);
  410. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[5]);
  411. glCallList(list);
  412. glDeleteLists(list,1);
  413. end;
  414. procedure draw_octa;
  415. var
  416. list : GLuint;
  417. begin
  418. list := glGenLists( 1 );
  419. glNewList( list, GL_COMPILE );
  420. TRIANGLE(2,seno,edgedivisions,1/SQRT6);
  421. glEndList();
  422. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[0]);
  423. glCallList(list);
  424. glPushMatrix();
  425. glRotatef(180,0,0,1);
  426. glRotatef(-180+octaangle,1,0,0);
  427. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[1]);
  428. glCallList(list);
  429. glPopMatrix();
  430. glPushMatrix();
  431. glRotatef(180,0,1,0);
  432. glRotatef(-octaangle,0.5,SQRT3/2,0);
  433. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[2]);
  434. glCallList(list);
  435. glPopMatrix();
  436. glPushMatrix();
  437. glRotatef(180,0,1,0);
  438. glRotatef(-octaangle,0.5,-SQRT3/2,0);
  439. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[3]);
  440. glCallList(list);
  441. glPopMatrix();
  442. glRotatef(180,1,0,0);
  443. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[4]);
  444. glCallList(list);
  445. glPushMatrix();
  446. glRotatef(180,0,0,1);
  447. glRotatef(-180+octaangle,1,0,0);
  448. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[5]);
  449. glCallList(list);
  450. glPopMatrix();
  451. glPushMatrix();
  452. glRotatef(180,0,1,0);
  453. glRotatef(-octaangle,0.5,SQRT3/2,0);
  454. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[6]);
  455. glCallList(list);
  456. glPopMatrix();
  457. glRotatef(180,0,1,0);
  458. glRotatef(-octaangle,0.5,-SQRT3/2,0);
  459. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[7]);
  460. glCallList(list);
  461. glDeleteLists(list,1);
  462. end;
  463. procedure draw_dodeca;
  464. const
  465. TAU = ((SQRT5+1)/2);
  466. var
  467. list : GLuint;
  468. begin
  469. list := glGenLists( 1 );
  470. glNewList( list, GL_COMPILE );
  471. PENTAGON(1,seno,edgedivisions,sqr(TAU) * sqrt((TAU+2)/5) / 2);
  472. glEndList();
  473. glPushMatrix();
  474. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[0]);
  475. glCallList(list);
  476. glRotatef(180,0,0,1);
  477. glPushMatrix();
  478. glRotatef(-dodecaangle,1,0,0);
  479. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[1]);
  480. glCallList(list);
  481. glPopMatrix();
  482. glPushMatrix();
  483. glRotatef(-dodecaangle,cos72,sin72,0);
  484. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[2]);
  485. glCallList(list);
  486. glPopMatrix();
  487. glPushMatrix();
  488. glRotatef(-dodecaangle,cos72,-sin72,0);
  489. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[3]);
  490. glCallList(list);
  491. glPopMatrix();
  492. glPushMatrix();
  493. glRotatef(dodecaangle,cos36,-sin36,0);
  494. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[4]);
  495. glCallList(list);
  496. glPopMatrix();
  497. glRotatef(dodecaangle,cos36,sin36,0);
  498. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[5]);
  499. glCallList(list);
  500. glPopMatrix();
  501. glRotatef(180,1,0,0);
  502. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[6]);
  503. glCallList(list);
  504. glRotatef(180,0,0,1);
  505. glPushMatrix();
  506. glRotatef(-dodecaangle,1,0,0);
  507. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[7]);
  508. glCallList(list);
  509. glPopMatrix();
  510. glPushMatrix();
  511. glRotatef(-dodecaangle,cos72,sin72,0);
  512. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[8]);
  513. glCallList(list);
  514. glPopMatrix();
  515. glPushMatrix();
  516. glRotatef(-dodecaangle,cos72,-sin72,0);
  517. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[9]);
  518. glCallList(list);
  519. glPopMatrix();
  520. glPushMatrix();
  521. glRotatef(dodecaangle,cos36,-sin36,0);
  522. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[10]);
  523. glCallList(list);
  524. glPopMatrix();
  525. glRotatef(dodecaangle,cos36,sin36,0);
  526. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[11]);
  527. glCallList(list);
  528. glDeleteLists(list,1);
  529. end;
  530. procedure draw_ico;
  531. var
  532. list : GLuint;
  533. begin
  534. list := glGenLists( 1 );
  535. glNewList( list, GL_COMPILE );
  536. TRIANGLE(1.5,seno,edgedivisions,(3*SQRT3+SQRT15)/12);
  537. glEndList();
  538. glPushMatrix();
  539. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[0]);
  540. glCallList(list);
  541. glPushMatrix();
  542. glRotatef(180,0,0,1);
  543. glRotatef(-icoangle,1,0,0);
  544. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[1]);
  545. glCallList(list);
  546. glPushMatrix();
  547. glRotatef(180,0,1,0);
  548. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  549. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[2]);
  550. glCallList(list);
  551. glPopMatrix();
  552. glRotatef(180,0,1,0);
  553. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  554. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[3]);
  555. glCallList(list);
  556. glPopMatrix();
  557. glPushMatrix();
  558. glRotatef(180,0,1,0);
  559. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  560. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[4]);
  561. glCallList(list);
  562. glPushMatrix();
  563. glRotatef(180,0,1,0);
  564. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  565. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[5]);
  566. glCallList(list);
  567. glPopMatrix();
  568. glRotatef(180,0,0,1);
  569. glRotatef(-icoangle,1,0,0);
  570. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[6]);
  571. glCallList(list);
  572. glPopMatrix();
  573. glRotatef(180,0,1,0);
  574. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  575. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[7]);
  576. glCallList(list);
  577. glPushMatrix();
  578. glRotatef(180,0,1,0);
  579. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  580. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[8]);
  581. glCallList(list);
  582. glPopMatrix();
  583. glRotatef(180,0,0,1);
  584. glRotatef(-icoangle,1,0,0);
  585. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[9]);
  586. glCallList(list);
  587. glPopMatrix();
  588. glRotatef(180,1,0,0);
  589. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[10]);
  590. glCallList(list);
  591. glPushMatrix();
  592. glRotatef(180,0,0,1);
  593. glRotatef(-icoangle,1,0,0);
  594. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[11]);
  595. glCallList(list);
  596. glPushMatrix();
  597. glRotatef(180,0,1,0);
  598. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  599. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[12]);
  600. glCallList(list);
  601. glPopMatrix();
  602. glRotatef(180,0,1,0);
  603. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  604. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[13]);
  605. glCallList(list);
  606. glPopMatrix();
  607. glPushMatrix();
  608. glRotatef(180,0,1,0);
  609. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  610. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[14]);
  611. glCallList(list);
  612. glPushMatrix();
  613. glRotatef(180,0,1,0);
  614. glRotatef(-180+icoangle,0.5,SQRT3/2,0);
  615. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[15]);
  616. glCallList(list);
  617. glPopMatrix();
  618. glRotatef(180,0,0,1);
  619. glRotatef(-icoangle,1,0,0);
  620. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[16]);
  621. glCallList(list);
  622. glPopMatrix();
  623. glRotatef(180,0,1,0);
  624. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  625. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[17]);
  626. glCallList(list);
  627. glPushMatrix();
  628. glRotatef(180,0,1,0);
  629. glRotatef(-180+icoangle,0.5,-SQRT3/2,0);
  630. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[18]);
  631. glCallList(list);
  632. glPopMatrix();
  633. glRotatef(180,0,0,1);
  634. glRotatef(-icoangle,1,0,0);
  635. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialColor[19]);
  636. glCallList(list);
  637. glDeleteLists(list,1);
  638. end;
  639. procedure do_draw; cdecl;
  640. begin
  641. glClear( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT );
  642. glPushMatrix();
  643. glTranslatef( 0.0, 0.0, -10.0 );
  644. glScalef( Scale*WindH/WindW, Scale, Scale );
  645. glTranslatef(2.5*WindW/WindH*sin(step*1.11),2.5*cos(step*1.25*1.11),0);
  646. glRotatef(step*100,1,0,0);
  647. glRotatef(step*95,0,1,0);
  648. glRotatef(step*90,0,0,1);
  649. seno:=(sin(step)+1.0/3.0)*(4.0/5.0)*Magnitude;
  650. draw_object();
  651. glPopMatrix();
  652. glFlush();
  653. glutSwapBuffers();
  654. step:=step+0.05;
  655. end;
  656. procedure do_idle; cdecl;
  657. begin
  658. glutPostRedisplay();
  659. end;
  660. procedure do_reshape(width,height:longint); cdecl;
  661. begin
  662. WindW:=width;
  663. WindH:=height;
  664. glViewport(0, 0, width, height);
  665. glMatrixMode(GL_PROJECTION);
  666. glLoadIdentity();
  667. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 );
  668. glMatrixMode(GL_MODELVIEW);
  669. end;
  670. procedure pinit;
  671. var
  672. loop : longint;
  673. begin
  674. case _object of
  675. 1 :
  676. begin
  677. draw_object:=@draw_tetra;
  678. MaterialColor[0]:=@MaterialRed;
  679. MaterialColor[1]:=@MaterialGreen;
  680. MaterialColor[2]:=@MaterialBlue;
  681. MaterialColor[3]:=@MaterialWhite;
  682. edgedivisions:=tetradivisions;
  683. Magnitude:=2.5;
  684. end;
  685. 2:
  686. begin
  687. draw_object:=@draw_cube;
  688. MaterialColor[0]:=@MaterialRed;
  689. MaterialColor[1]:=@MaterialGreen;
  690. MaterialColor[2]:=@MaterialCyan;
  691. MaterialColor[3]:=@MaterialMagenta;
  692. MaterialColor[4]:=@MaterialYellow;
  693. MaterialColor[5]:=@MaterialBlue;
  694. edgedivisions:=cubedivisions;
  695. Magnitude:=2.0;
  696. end;
  697. 3:
  698. begin
  699. draw_object:=@draw_octa;
  700. MaterialColor[0]:=MaterialRed;
  701. MaterialColor[1]:=MaterialGreen;
  702. MaterialColor[2]:=MaterialBlue;
  703. MaterialColor[3]:=MaterialWhite;
  704. MaterialColor[4]:=MaterialCyan;
  705. MaterialColor[5]:=MaterialMagenta;
  706. MaterialColor[6]:=MaterialGray;
  707. MaterialColor[7]:=MaterialYellow;
  708. edgedivisions:=octadivisions;
  709. Magnitude:=2.5;
  710. end;
  711. 4:
  712. begin
  713. draw_object:=@draw_dodeca;
  714. MaterialColor[ 0]:=MaterialRed;
  715. MaterialColor[ 1]:=MaterialGreen;
  716. MaterialColor[ 2]:=MaterialCyan;
  717. MaterialColor[ 3]:=MaterialBlue;
  718. MaterialColor[ 4]:=MaterialMagenta;
  719. MaterialColor[ 5]:=MaterialYellow;
  720. MaterialColor[ 6]:=MaterialGreen;
  721. MaterialColor[ 7]:=MaterialCyan;
  722. MaterialColor[ 8]:=MaterialRed;
  723. MaterialColor[ 9]:=MaterialMagenta;
  724. MaterialColor[10]:=MaterialBlue;
  725. MaterialColor[11]:=MaterialYellow;
  726. edgedivisions:=dodecadivisions;
  727. Magnitude:=2.0;
  728. end;
  729. 5:
  730. begin
  731. draw_object:=@draw_ico;
  732. MaterialColor[ 0]:=MaterialRed;
  733. MaterialColor[ 1]:=MaterialGreen;
  734. MaterialColor[ 2]:=MaterialBlue;
  735. MaterialColor[ 3]:=MaterialCyan;
  736. MaterialColor[ 4]:=MaterialYellow;
  737. MaterialColor[ 5]:=MaterialMagenta;
  738. MaterialColor[ 6]:=MaterialRed;
  739. MaterialColor[ 7]:=MaterialGreen;
  740. MaterialColor[ 8]:=MaterialBlue;
  741. MaterialColor[ 9]:=MaterialWhite;
  742. MaterialColor[10]:=MaterialCyan;
  743. MaterialColor[11]:=MaterialYellow;
  744. MaterialColor[12]:=MaterialMagenta;
  745. MaterialColor[13]:=MaterialRed;
  746. MaterialColor[14]:=MaterialGreen;
  747. MaterialColor[15]:=MaterialBlue;
  748. MaterialColor[16]:=MaterialCyan;
  749. MaterialColor[17]:=MaterialYellow;
  750. MaterialColor[18]:=MaterialMagenta;
  751. MaterialColor[19]:=MaterialGray;
  752. edgedivisions:=icodivisions;
  753. Magnitude:=2.5;
  754. end;
  755. end;
  756. if (mono) then
  757. begin
  758. for loop:=0 to 19 do
  759. MaterialColor[loop]:=MaterialGray;
  760. end;
  761. if (smooth) then
  762. glShadeModel( GL_SMOOTH )
  763. else
  764. glShadeModel( GL_FLAT );
  765. end;
  766. procedure do_key(k:byte;x,y:integer); cdecl;
  767. begin
  768. case Char(k) of
  769. '1' : _object:=1;
  770. '2' : _object:=2;
  771. '3' : _object:=3;
  772. '4' : _object:=4;
  773. '5' : _object:=5;
  774. ' ' : mono:=not mono;
  775. #13 : smooth:=not smooth;
  776. #27 : halt(0);
  777. end;
  778. pinit;
  779. end;
  780. begin
  781. writeln('Morph 3D - Shows morphing platonic polyhedra');
  782. writeln('Author: Marcelo Fernandes Vianna ([email protected])');
  783. writeln(' [1] - Tetrahedron');
  784. writeln(' [2] - Hexahedron (Cube)');
  785. writeln(' [3] - Octahedron');
  786. writeln(' [4] - Dodecahedron');
  787. writeln(' [5] - Icosahedron');
  788. writeln('[SPACE] - Toggle colored faces');
  789. writeln('[RETURN] - Toggle smooth/flat shading');
  790. writeln(' [ESC] - Quit');
  791. _object:=3;
  792. glutInit(@argc, argv);
  793. glutInitWindowPosition(0,0);
  794. glutInitWindowSize(640,480);
  795. glutInitDisplayMode( GLUT_DEPTH + GLUT_DOUBLE + GLUT_RGB );
  796. if (glutCreateWindow('Morph 3D - Shows morphing platonic polyhedra') <= 0) then
  797. halt(1);
  798. glClearDepth(1.0);
  799. glClearColor( 0.0, 0.0, 0.0, 1.0 );
  800. glColor3f( 1.0, 1.0, 1.0 );
  801. glClear( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT );
  802. glFlush();
  803. glutSwapBuffers();
  804. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  805. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  806. glLightfv(GL_LIGHT0, GL_POSITION, position0);
  807. glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
  808. glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
  809. glLightfv(GL_LIGHT1, GL_POSITION, position1);
  810. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  811. glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  812. glEnable(GL_LIGHTING);
  813. glEnable(GL_LIGHT0);
  814. glEnable(GL_LIGHT1);
  815. glEnable(GL_DEPTH_TEST);
  816. glEnable(GL_NORMALIZE);
  817. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
  818. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
  819. glHint(GL_FOG_HINT, GL_FASTEST);
  820. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  821. glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
  822. pinit();
  823. glutReshapeFunc( @do_reshape );
  824. glutKeyboardFunc( @do_key );
  825. glutIdleFunc( @do_idle );
  826. glutDisplayFunc( @do_draw );
  827. glutMainLoop();
  828. end.