glutdemo.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. GL units for Free Pascal - GLUT demo
  3. 1999 Sebastian Guenther, [email protected]
  4. You may use this source as starting point for your own programs; consider it
  5. as Public Domain.
  6. }
  7. {$MODE objfpc}
  8. {$H+}
  9. program GLUTDemo;
  10. uses GL, GLUT;
  11. const
  12. FPCImg: array[0..4, 0..10] of Byte =
  13. ((1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1),
  14. (1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0),
  15. (1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0),
  16. (1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0),
  17. (1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1));
  18. var
  19. counter: Integer;
  20. const
  21. colors: array[0..7, 0..2] of Single =
  22. ((0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
  23. (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1));
  24. corners: array[0..7, 0..2] of Single =
  25. ((-1, -1, -1), (+1, -1, -1), (+1, +1, -1), (-1, +1, -1),
  26. (-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1));
  27. procedure DrawCube;
  28. procedure DrawSide(i1, i2, i3, i4: Integer);
  29. begin
  30. glColor4f (colors [i1, 0], colors [i1, 1], colors [i1, 2], 0.5);
  31. glVertex3f(corners[i1, 0], corners[i1, 1], corners[i1, 2]);
  32. glColor4f (colors [i2, 0], colors [i2, 1], colors [i2, 2], 0.5);
  33. glVertex3f(corners[i2, 0], corners[i2, 1], corners[i2, 2]);
  34. glColor4f (colors [i3, 0], colors [i3, 1], colors [i3, 2], 0.5);
  35. glVertex3f(corners[i3, 0], corners[i3, 1], corners[i3, 2]);
  36. glVertex3f(corners[i4, 0], corners[i4, 1], corners[i4, 2]);
  37. end;
  38. begin
  39. glBegin(GL_QUADS);
  40. DrawSide(4, 5, 6, 7); // Front
  41. DrawSide(3, 2, 1, 0); // Back
  42. DrawSide(2, 3, 7, 6); // Top
  43. DrawSide(0, 1, 5, 4); // Bottom
  44. DrawSide(4, 7, 3, 0); // Left
  45. DrawSide(1, 2, 6, 5); // Right
  46. glEnd;
  47. end;
  48. procedure DisplayWindow; cdecl;
  49. var
  50. x, y: Integer;
  51. begin
  52. Inc(counter);
  53. glClearColor(0, 0, 0.2, 1);
  54. glClear([GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT]);
  55. glPushMatrix;
  56. glTranslatef(0, 0, Sin(Single(counter) / 20.0) * 5.0 - 5.0);
  57. glRotatef(Sin(Single(counter) / 200.0) * 720.0, 0, 1, 0);
  58. glRotatef(counter, 0, 0, 1);
  59. for y := 0 to 4 do
  60. for x := 0 to 10 do
  61. if FPCImg[y, x] > 0 then begin
  62. glPushMatrix;
  63. glRotatef(x * Sin(Single(counter) / 5.0), 0, 1, 0);
  64. glRotatef(y * Sin(Single(counter) / 12.0) * 4.0, 0, 0, 1);
  65. glTranslatef((x - 5) * 1, (2 - y) * 1, 0);
  66. glScalef(0.4, 0.4, 0.4);
  67. glRotatef(counter, 0.5, 1, 0);
  68. DrawCube;
  69. glPopMatrix;
  70. end;
  71. glPopMatrix;
  72. Inc(counter);
  73. glutSwapBuffers;
  74. end;
  75. procedure OnTimer(value: Integer); cdecl;
  76. begin
  77. glutPostRedisplay;
  78. glutTimerFunc(20, @OnTimer, 0);
  79. end;
  80. begin
  81. WriteLn('Dumping unresolved GL* function names; having unresolved functions ');
  82. WriteLn('is NOT a problem as long as the application doesn''t use them!');
  83. WriteLn('(Most unresolved functions will be propietary extensions which');
  84. WriteLn(' should be an official GL extension)');
  85. WriteLn;
  86. GLDumpUnresolvedFunctions := True;
  87. GLUTDumpUnresolvedFunctions := True;
  88. if not InitGl then begin
  89. WriteLn('OpenGL is not supported on this system');
  90. Halt(2);
  91. end;
  92. if not InitGLU then begin
  93. WriteLn('Couldn''t load GLU module');
  94. Halt(3);
  95. end;
  96. if not InitGLX then begin
  97. WriteLn('Couldn''t load GLX module');
  98. Halt(4);
  99. end;
  100. if not InitGLUT then begin
  101. WriteLn('Couldn''t load GLUT module');
  102. Halt(5);
  103. end;
  104. glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH);
  105. glutCreateWindow('Free Pascal GLUT demo');
  106. glutDisplayFunc(@DisplayWindow);
  107. glutTimerFunc(20, @OnTimer, 0);
  108. WriteLn;
  109. WriteLn('GL info:');
  110. WriteLn(' Vendor: ', glGetString(GL_VENDOR));
  111. WriteLn(' Renderer: ', glGetString(GL_RENDERER));
  112. WriteLn(' Version: ', glGetString(GL_VERSION));
  113. WriteLn(' Extensions: ', glGetString(GL_EXTENSIONS));
  114. // Enable backface culling
  115. glEnable(GL_CULL_FACE);
  116. // Set up depth buffer
  117. glEnable(GL_DEPTH_TEST);
  118. glDepthFunc(GL_LESS);
  119. // Set up projection matrix
  120. glMatrixMode(GL_PROJECTION);
  121. glLoadIdentity;
  122. gluPerspective(90, 1.3, 0.1, 100);
  123. glMatrixMode(GL_MODELVIEW);
  124. glLoadIdentity;
  125. glTranslatef(0, 0, -5.5);
  126. glutMainLoop;
  127. end.