bounce.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. {
  2. $Id$
  3. Bouncing ball demo. Color index mode only!
  4. This program is in the public domain
  5. Brian Paul
  6. Converted to Pascal by Peter Vreman
  7. }
  8. program bounce;
  9. {$mode objfpc}
  10. uses
  11. gl,glut;
  12. const
  13. RED=1;
  14. WHITE=2;
  15. CYAN=3;
  16. var
  17. IndexMode : Boolean;
  18. Ball : GLuint;
  19. const
  20. Zrot : GLfloat = 0.0;
  21. Zstep : GLfloat = 6.0;
  22. Xpos : GLfloat = 0.0;
  23. Ypos : GLfloat = 1.0;
  24. Xvel : GLfloat = 0.2;
  25. Yvel : GLfloat = 0.0;
  26. Xmin : GLfloat = -4.0;
  27. Xmax : GLfloat = 4.0;
  28. Ymin : GLfloat = -3.8;
  29. Ymax : GLfloat = 4.0;
  30. G : GLfloat = -0.1;
  31. function make_ball:GLuint;
  32. var
  33. list : GLuint;
  34. a,b,
  35. ar,br : GLFloat;
  36. da,db,
  37. dar : GLFloat;
  38. radius : GLFloat;
  39. color : boolean;
  40. x,y,z : GLFloat;
  41. begin
  42. da:=18.0;
  43. db:=18.0;
  44. radius:=1.0;
  45. list := glGenLists(1);
  46. glNewList(list, GL_COMPILE);
  47. color := false;
  48. a:=-90.0;
  49. while (a+da<=90.0) do
  50. begin
  51. glBegin(GL_QUAD_STRIP);
  52. b:=0.0;
  53. while (b<=360.0) do
  54. begin
  55. if (color) then
  56. begin
  57. glIndexi(RED);
  58. glColor3f(1, 0, 0);
  59. end
  60. else
  61. begin
  62. glIndexi(WHITE);
  63. glColor3f(1, 1, 1);
  64. end;
  65. ar:=a * 3.14159/180.0;
  66. br:=b * 3.14159/180.0;
  67. x := COS(br) * COS(ar);
  68. y := SIN(br) * COS(ar);
  69. z := SIN(ar);
  70. glVertex3f(x, y, z);
  71. dar:=da * 3.14159/180.0;
  72. x := radius * COS(br) * COS(ar + dar);
  73. y := radius * SIN(br) * COS(ar + dar);
  74. z := radius * SIN(ar + dar);
  75. glVertex3f(x, y, z);
  76. color := not color;
  77. b:=b+db;
  78. end;
  79. glEnd();
  80. a:=a+da;
  81. end;
  82. glEndList();
  83. make_ball:=list;
  84. end;
  85. procedure reshape(width,height:longint); cdecl;
  86. var
  87. aspect : glFloat;
  88. begin
  89. aspect := glfloat(width) / glfloat(height);
  90. glViewport(0, 0, width, height);
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. glOrtho(-6.0 * aspect, 6.0 * aspect, -6.0, 6.0, -6.0, 6.0);
  94. glMatrixMode(GL_MODELVIEW);
  95. end;
  96. procedure key(k:byte;x,y:longint); cdecl;
  97. begin
  98. case k of
  99. 27 :
  100. halt(0);
  101. end;
  102. end;
  103. procedure draw; cdecl;
  104. var
  105. i : GLint;
  106. begin
  107. glClear(GL_COLOR_BUFFER_BIT);
  108. glIndexi(CYAN);
  109. glColor3f(0, 1, 1);
  110. glBegin(GL_LINES);
  111. for i:=-5 to 5 do
  112. begin
  113. glVertex2i(i, -5);
  114. glVertex2i(i, 5);
  115. end;
  116. for i:=-5 to 5 do
  117. begin
  118. glVertex2i(-5, i);
  119. glVertex2i(5, i);
  120. end;
  121. for i:=-5 to 5 do
  122. begin
  123. glVertex2i(i, -5);
  124. glVertex2f(i * 1.15, -5.9);
  125. end;
  126. glVertex2f(-5.3, -5.35);
  127. glVertex2f(5.3, -5.35);
  128. glVertex2f(-5.75, -5.9);
  129. glVertex2f(5.75, -5.9);
  130. glEnd();
  131. glPushMatrix();
  132. glTranslatef(Xpos, Ypos, 0.0);
  133. glScalef(2.0, 2.0, 2.0);
  134. glRotatef(8.0, 0.0, 0.0, 1.0);
  135. glRotatef(90.0, 1.0, 0.0, 0.0);
  136. glRotatef(Zrot, 0.0, 0.0, 1.0);
  137. glCallList(Ball);
  138. glPopMatrix();
  139. glFlush();
  140. glutSwapBuffers();
  141. end;
  142. const
  143. vel0 : glfloat = -100.0;
  144. procedure idle; cdecl;
  145. begin
  146. Zrot:=Zrot+Zstep;
  147. Xpos:=Xpos+Xvel;
  148. if (Xpos >= Xmax) then
  149. begin
  150. Xpos := Xmax;
  151. Xvel := -Xvel;
  152. Zstep := -Zstep;
  153. end;
  154. if (Xpos <= Xmin) then
  155. begin
  156. Xpos := Xmin;
  157. Xvel := -Xvel;
  158. Zstep := -Zstep;
  159. end;
  160. Ypos:=Ypos+Yvel;
  161. Yvel:=Yvel+G;
  162. if (Ypos < Ymin) then
  163. begin
  164. Ypos := Ymin;
  165. if (vel0 = -100.0) then
  166. vel0 := abs(Yvel);
  167. Yvel := vel0;
  168. end;
  169. glutPostRedisplay();
  170. end;
  171. procedure visible(vis:longint); cdecl;
  172. begin
  173. if (vis=GLUT_VISIBLE) then
  174. glutIdleFunc(@idle)
  175. else
  176. glutIdleFunc(nil);
  177. end;
  178. begin
  179. glutInit(@argc, argv);
  180. glutInitWindowPosition(0, 0);
  181. glutInitWindowSize(600, 450);
  182. if paramcount>1 then
  183. IndexMode:=(paramstr(1)='-ci');
  184. if (IndexMode) then
  185. glutInitDisplayMode(GLUT_INDEX or GLUT_DOUBLE)
  186. else
  187. glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE);
  188. glutCreateWindow('Bounce');
  189. Ball := make_ball();
  190. glCullFace(GL_BACK);
  191. glEnable(GL_CULL_FACE);
  192. glDisable(GL_DITHER);
  193. glShadeModel(GL_FLAT);
  194. glutDisplayFunc(@draw);
  195. glutReshapeFunc(@reshape);
  196. glutVisibilityFunc(@visible);
  197. glutKeyboardFunc(@key);
  198. if (IndexMode) then
  199. begin
  200. glutSetColor(RED, 1.0, 0.0, 0.0);
  201. glutSetColor(WHITE, 1.0, 1.0, 1.0);
  202. glutSetColor(CYAN, 0.0, 1.0, 1.0);
  203. end;
  204. glutMainLoop();
  205. end.