Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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