You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.9KB

  1. //
  2. // "$Id: gl_start.cxx 8368 2011-02-04 23:32:53Z manolo $"
  3. //
  4. // OpenGL context routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. // You MUST use gl_visual() to select the default visual before doing
  28. // show() of any windows. Mesa will crash if you try to use a visual
  29. // not returned by glxChooseVisual.
  30. // This does not work with Fl_Double_Window's! It will try to draw
  31. // into the front buffer. Depending on the system this will either
  32. // crash or do nothing (when pixmaps are being used as back buffer
  33. // and GL is being done by hardware), work correctly (when GL is done
  34. // with software, such as Mesa), or draw into the front buffer and
  35. // be erased when the buffers are swapped (when double buffer hardware
  36. // is being used)
  37. #include <config.h>
  38. #if HAVE_GL
  39. #include <FL/Fl.H>
  40. #include <FL/Fl_Window.H>
  41. #include <FL/x.H>
  42. #include <FL/fl_draw.H>
  43. #include "Fl_Gl_Choice.H"
  44. static GLContext context;
  45. static int clip_state_number=-1;
  46. static int pw, ph;
  47. #ifdef WIN32
  48. static Fl_Gl_Choice* gl_choice;
  49. #endif
  50. #ifdef __APPLE__
  51. static Fl_Gl_Choice* gl_choice;
  52. #endif
  53. Fl_Region XRectangleRegion(int x, int y, int w, int h); // in fl_rect.cxx
  54. /** Creates an OpenGL context */
  55. void gl_start() {
  56. if (!context) {
  57. #if defined(USE_X11)
  58. context = fl_create_gl_context(fl_visual);
  59. #elif defined(WIN32)
  60. if (!gl_choice) Fl::gl_visual(0);
  61. context = fl_create_gl_context(Fl_Window::current(), gl_choice);
  62. #elif defined(__APPLE_QUARTZ__)
  63. // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
  64. context = fl_create_gl_context(Fl_Window::current(), gl_choice);
  65. #else
  66. # error Unsupported platform
  67. #endif
  68. }
  69. fl_set_gl_context(Fl_Window::current(), context);
  70. #if !defined(WIN32) && !defined(__APPLE__)
  71. glXWaitX();
  72. #endif
  73. if (pw != Fl_Window::current()->w() || ph != Fl_Window::current()->h()) {
  74. pw = Fl_Window::current()->w();
  75. ph = Fl_Window::current()->h();
  76. glLoadIdentity();
  77. glViewport(0, 0, pw, ph);
  78. glOrtho(0, pw, 0, ph, -1, 1);
  79. glDrawBuffer(GL_FRONT);
  80. }
  81. if (clip_state_number != fl_graphics_driver->fl_clip_state_number) {
  82. clip_state_number = fl_graphics_driver->fl_clip_state_number;
  83. int x, y, w, h;
  84. if (fl_clip_box(0, 0, Fl_Window::current()->w(), Fl_Window::current()->h(),
  85. x, y, w, h)) {
  86. fl_clip_region(XRectangleRegion(x,y,w,h));
  87. glScissor(x, Fl_Window::current()->h()-(y+h), w, h);
  88. glEnable(GL_SCISSOR_TEST);
  89. } else {
  90. glDisable(GL_SCISSOR_TEST);
  91. }
  92. }
  93. }
  94. /** Releases an OpenGL context */
  95. void gl_finish() {
  96. glFlush();
  97. #if !defined(WIN32) && !defined(__APPLE__)
  98. glXWaitGL();
  99. #endif
  100. }
  101. int Fl::gl_visual(int mode, int *alist) {
  102. Fl_Gl_Choice *c = Fl_Gl_Choice::find(mode,alist);
  103. if (!c) return 0;
  104. #if defined(USE_X11)
  105. fl_visual = c->vis;
  106. fl_colormap = c->colormap;
  107. #elif defined(WIN32)
  108. gl_choice = c;
  109. #elif defined(__APPLE_QUARTZ__)
  110. // warning: the Quartz version should probably use Core GL (CGL) instead of AGL
  111. gl_choice = c;
  112. #else
  113. # error Unsupported platform
  114. #endif
  115. return 1;
  116. }
  117. #endif
  118. //
  119. // End of "$Id: gl_start.cxx 8368 2011-02-04 23:32:53Z manolo $".
  120. //