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 40KB

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