## class fltk::GlWindow

### Class Hierarchy

    fltk::Widget
       |
       +----fltk::Window
               |
               +----fltk::GlWindow

### Include Files

    #include <fltk/GlWindow.h>

### Description

The `fltk::GlWindow` widget sets things up so OpenGL works, and also keeps an OpenGL "context" for that window, so that changes to the lighting and projection may be reused between redraws. fltk::GlWindow also flushes the OpenGL streams and swaps buffers after `draw()` returns.

OpenGL hardware typically provides some overlay bit planes, which are very useful for drawing UI controls atop your 3D graphics. If the overlay hardware is not provided, FLTK tries to simulate the overlay, This works pretty well if your graphics are double buffered, but not very well for single-buffered.

### Methods

- fltk::GlWindow
- ~GlWindow
- can_do
- context

- can_do_overlay
- draw
- draw_overlay
- handle

- hide
- invalidate
- make_current

- make_overlay_current
- mode
- ortho

- redraw_overlay
- swap_buffers
- valid

#### fltk::GlWindow::GlWindow(int x, int y, int w, int h, const char *label = 0)

Creates a new `fltk::GlWindow` widget using the given position, size, and label string. The default boxtype is `fltk::NO_BOX`. The default mode is `fltk::RGB|fltk::DOUBLE|fltk::DEPTH`.

#### virtual fltk::GlWindow::~GlWindow()

The destructor removes the widget and destroys the OpenGL context associated with it.

#### virtual void fltk::GlWindow::draw(void)

`fltk::GlWindow::draw()` is a pure virtual method. You must subclass `fltk::GlWindow` and provide an implementation for `draw()`. You may also provide an implementation of draw_overlay() if you want to draw into the overlay planes. You can avoid reinitializing the viewport and lights and other things by checking `valid()` at the start of `draw()` and only doing the initialization if it is false.

The `draw()` method can only use OpenGL calls. Do not attempt to call X, any of the functions in <fltk/draw.h>, or `glX` directly. Do not call `gl_start()` or `gl_finish()`.

If double-buffering is enabled in the window, the back and front buffers are swapped after this function is completed.

#### const int fltk::GlWindow::mode() const
int fltk::GlWindow::mode(int m)

Set or change the OpenGL capabilites of the window. The value can be any of the following OR'd together:

- `fltk::RGB` - RGB color (not indexed)
- `fltk::RGB8` - RGB color with at least 8 bits of each color
- `fltk::INDEX` - Indexed mode
- `fltk::SINGLE` - not double buffered
- `fltk::DOUBLE` - double buffered
- `fltk::ACCUM` - accumulation buffer
- `fltk::ALPHA` - alpha channel in color
- `fltk::DEPTH` - depth buffer
- `fltk::STENCIL` - stencil buffer
- `fltk::MULTISAMPLE` - multisample antialiasing
- `fltk::STEREO` - stereo (if hardware supports it)
- `fltk::NO_AUTO_SWAP` - disable the automatic call to swap_buffers() after draw() is called.
- `fltk::NO_ERASE_OVERLAY` - disable the automatic call to `glClear` before `draw_overlay()` is called.

`fltk::RGB` and `fltk::SINGLE` have a value of zero, so they are "on" unless you give `fltk::INDEX` or `fltk::DOUBLE`.

If the desired combination cannot be done, FLTK will try turning off `fltk::MULTISAMPLE`. If this also fails the `show()` will call `fltk::error()` and not show the window.

You can change the mode while the window is displayed. This is most useful for turning double-buffering on and off. Under X this will cause the old X window to be destroyed and a new one to be created. If this is a top-level window this will unfortunately also cause the window to blink, raise to the top, and be de-iconized, and the `xid()` will change, possibly breaking other code. It is best to make the GL window a child of another window if you wish to do this!

#### static int fltk::GlWindow::can_do(int)
int fltk::GlWindow::can_do() const

Returns non-zero if the hardware supports the given or current OpenGL mode.

#### void* fltk::GlWindow::context() const;
void fltk::GlWindow::context(void*, int destroy_flag = false);

Return or set a pointer to the GLContext that this window is using. This is a system-dependent structure, but it is portable to copy the context from one window to another. You can also set it to NULL, which will force FLTK to recreate the context the next time `make_current()` is called, this is useful for getting around bugs in OpenGL implementations.

If destroy_flag is true the context will be destroyed by fltk when the window is destroyed, or when the `mode()` is changed, or the next time `context(x)` is called.

#### char fltk::GlWindow::valid() const
void fltk::GlWindow::valid(char i)

`fltk::GlWindow::valid()` is turned off when FLTK creates a new context for this window or when the window resizes, and is turned on after `draw()` is called. You can use this inside your `draw()` method to avoid unneccessarily initializing the OpenGL context. Just do this:

    void mywindow::draw() {
      if (!valid()) {
        glViewport(0,0,w(),h());
        glFrustum(...);
        glLight(...);
        ...other initialization...
      }
      ... draw your geometry here ...
    }

You can turn `valid()` on by calling `valid(1)`. You should only do this after fixing the transformation inside a `draw()` or after `make_current()`. This is done automatically after `draw()` returns.

#### void fltk::GlWindow::invalidate()

The `invalidate()` method turns off `valid()` and is equivalent to calling `value(0)`.

#### void fltk::GlWindow::ortho()

Set the projection so 0,0 is in the lower left of the window and each pixel is 1 unit wide/tall. If you are drawing 2D images, your `draw()` method may want to call this if `valid()` is false.

#### void fltk::GlWindow::make_current()

The `make_current()` method selects the OpenGL context for the widget. It is called automatically prior to the `draw()` method being called and can also be used to implement feedback and/or selection within the `handle()` method.

#### void fltk::GlWindow::make_overlay_current()

The `make_overlay_current()` method selects the OpenGL context for the widget's overlay. It is called automatically prior to the `draw_overlay()` method being called and can also be used to implement feedback and/or selection within the `handle()` method.

#### void fltk::GlWindow::swap_buffers()

The `swap_buffers()` method swaps the back and front buffers. It is called automatically after the `draw()` method is called.

#### void fltk::GlWindow::hide()

Hides the window and destroys the OpenGL context.

#### int fltk::GlWindow::can_do_overlay()

Returns true if the hardware overlay is possible. If this is false, FLTK will try to simulate the overlay, with significant loss of update speed. Calling this will cause FLTK to open the display.

#### void fltk::GlWindow::redraw_overlay()

This method causes `draw_overlay` to be called at a later time. Initially the overlay is clear, if you want the window to display something in the overlay when it first appears, you must call this immediately after you `show()` your window.

#### virtual void fltk::GlWindow::draw_overlay()

You must implement this virtual function if you want to draw into the overlay. The overlay is cleared before this is called. You should draw anything that is not clear using OpenGL. You must use `gl_color(i)` to choose colors (it allocates them from the colormap using system-specific calls), and remember that you are in an indexed OpenGL mode and drawing anything other than flat-shaded will probably not work.

Both this function and `fltk::GlWindow::draw()` should check `fltk::GlWindow::valid()` and set the same transformation. If you don't your code may not work on other systems. Depending on the OS, and on whether overlays are real or simulated, the OpenGL context may be the same or different between the overlay and main window.