Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

morph3d.c 41KB

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