/packages/opengl/examples/bounce.pp

https://github.com/slibre/freepascal · Puppet · 246 lines · 204 code · 42 blank · 0 comment · 14 complexity · 5aa9c32944375f4fdfca4efc40dd5957 MD5 · raw file

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