bounce.pp 4.3 KB

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