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

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