morph3d.pp 30 KB

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