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