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.

df_morph3d.c 45KB

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