gtkgldemo.pp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. {$MODE DELPHI}
  2. {$ifdef win32}
  3. {$APPTYPE GUI}
  4. {$endif}
  5. program gtkgldemo;
  6. uses glib, gdk, gtk, gtkglarea,gl;
  7. function init(widget:PGtkWidget ):gint;cdecl;
  8. begin
  9. if (gint(True)=gtk_gl_area_make_current(PGtkGLArea(widget))) then
  10. begin
  11. glViewport(0,0, widget^.allocation.width, widget^.allocation.height);
  12. glMatrixMode(GL_PROJECTION);
  13. glLoadIdentity();
  14. glOrtho(0,100, 100,0, -1,1);
  15. glMatrixMode(GL_MODELVIEW);
  16. glLoadIdentity();
  17. end;
  18. init:= gint(TRUE);
  19. end;
  20. (* When widget is exposed its contents are redrawn. *)
  21. function draw(widget: PGtkWidget; event: PGdkEventExpose): gint; cdecl;
  22. begin
  23. (* Draw only last expose. *)
  24. if (event^.count > 0) then
  25. exit(gint(TRUE));
  26. (* OpenGL functions can be called only if make_current returns true *)
  27. if (gint(True) = gtk_gl_area_make_current(PGtkGLArea(widget))) then
  28. begin
  29. glClearColor(0,0,0,1);
  30. glClear(GL_COLOR_BUFFER_BIT);
  31. glColor3f(1,1,1);
  32. glBegin(GL_TRIANGLES);
  33. glVertex2f(10,10);
  34. glVertex2f(10,90);
  35. glVertex2f(90,90);
  36. glEnd();
  37. (* Swap backbuffer to front *)
  38. gtk_gl_area_swapbuffers(PGtkGLArea(widget));
  39. end;
  40. exit(gint(TRUE));
  41. end;
  42. (* When glarea widget size changes, viewport size is set to match the new size *)
  43. function reshape(widget:PGtkWidget; event: PGdkEventConfigure):gint; cdecl;
  44. begin
  45. (* OpenGL functions can be called only if make_current returns true *)
  46. if (gint(True) = gtk_gl_area_make_current(PGtkGLArea(widget))) then
  47. begin
  48. glViewport(0, 0, widget^.allocation.width, widget^.allocation.height);
  49. end;
  50. exit( gint(TRUE));
  51. end;
  52. var
  53. window,glarea: PGtkWidget;
  54. (* Attribute list for gtkglarea widget. Specifies a
  55. list of Boolean attributes and enum/integer
  56. attribute/value pairs. The last attribute must be
  57. GDK_GL_NONE. See glXChooseVisual manpage for further
  58. explanation.
  59. *)
  60. const
  61. attrlist: array [1..11] of LongInt=
  62. ( GDK_GL_RGBA,
  63. GDK_GL_RED_SIZE, 1,
  64. GDK_GL_GREEN_SIZE, 1,
  65. GDK_GL_BLUE_SIZE, 1,
  66. GDK_GL_DEPTH_SIZE,1,
  67. GDK_GL_DOUBLEBUFFER,
  68. GDK_GL_None
  69. );
  70. (* int attrlist[] = {
  71. GDK_GL_RGBA,
  72. GDK_GL_RED_SIZE,1,
  73. GDK_GL_GREEN_SIZE,1,
  74. GDK_GL_BLUE_SIZE,1,
  75. GDK_GL_DOUBLEBUFFER,
  76. GDK_GL_NONE
  77. };
  78. *)
  79. begin
  80. (* OpenGL functions can be called only if make_current returns true *)
  81. { if not InitGl then begin
  82. WriteLn('OpenGL is not supported on this system');
  83. Halt(2);
  84. end;}
  85. (* initialize gtk *)
  86. gtk_init(@argc, @argv);
  87. (* Attribute list for gtkglarea widget. Specifies a
  88. list of Boolean attributes and enum/integer
  89. attribute/value pairs. The last attribute must be
  90. GDK_GL_NONE. See glXChooseVisual manpage for further
  91. explanation.
  92. *)
  93. (* vendor dependent version info string *)
  94. { info_str = gdk_gl_get_info();
  95. g_print(info_str);
  96. g_free(info_str);}
  97. (* Check if OpenGL is supported. *)
  98. { if (gdk_gl_query() = FALSE) then
  99. begin
  100. g_print("OpenGL not supported\n");
  101. return 0;
  102. end;}
  103. (* Create new top level window. *)
  104. window := gtk_window_new( GTK_WINDOW_TOPLEVEL);
  105. gtk_window_set_title(GTK_WINDOW(window), 'OpenGL Output');
  106. gtk_container_set_border_width(GTK_CONTAINER(window), 10);
  107. (* Quit form main if got delete event *)
  108. gtk_signal_connect(GTK_OBJECT(window), 'delete_event',
  109. GTK_SIGNAL_FUNC(@gtk_main_quit), NULL);
  110. (* You should always delete gtk_gl_area widgets before exit or else
  111. GLX contexts are left undeleted, this may cause problems (=core dump)
  112. in some systems.
  113. Destroy method of objects is not automatically called on exit.
  114. You need to manually enable this feature. Do gtk_quit_add_destroy()
  115. for all your top level windows unless you are certain that they get
  116. destroy signal by other means.
  117. *)
  118. gtk_quit_add_destroy(1, GTK_OBJECT(window));
  119. (* Create new OpenGL widget. *)
  120. glarea := GTK_WIDGET(gtk_gl_area_new(pgint(@attrlist)));
  121. (* Events for widget must be set before X Window is created *)
  122. gtk_widget_set_events(GTK_WIDGET(glarea),
  123. GDK_EXPOSURE_MASK or
  124. GDK_BUTTON_PRESS_MASK);
  125. (* Connect signal handlers *)
  126. (* Redraw image when exposed. *)
  127. gtk_signal_connect(GTK_OBJECT(glarea), 'expose_event', GTK_SIGNAL_FUNC(@draw), Nil);
  128. (* When window is resized viewport needs to be resized also. *)
  129. gtk_signal_connect(GTK_OBJECT(glarea), 'configure_event', GTK_SIGNAL_FUNC(@reshape), Nil);
  130. (* Do initialization when widget has been realized. *)
  131. gtk_signal_connect(GTK_OBJECT(glarea), 'realize', GTK_SIGNAL_FUNC(@init), Nil);
  132. (* set minimum size *)
  133. gtk_widget_set_usize(GTK_WIDGET(glarea), 400,400);
  134. (* put glarea into window and show it all *)
  135. gtk_container_add(GTK_CONTAINER(window),GTK_WIDGET(glarea));
  136. gtk_widget_show(GTK_WIDGET(glarea));
  137. gtk_widget_show(GTK_WIDGET(window));
  138. gtk_main();
  139. end.