glutdemo.pp 4.0 KB

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