1998-10-20 00:46:58 +04:00
|
|
|
//
|
2000-06-06 01:21:24 +04:00
|
|
|
// "$Id: Fl_Gl_Window.cxx,v 1.12.2.13 2000/06/05 21:20:51 mike Exp $"
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// OpenGL window code for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2000-04-26 02:17:00 +04:00
|
|
|
// Copyright 1998-2000 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
// USA.
|
|
|
|
//
|
2000-06-06 01:21:24 +04:00
|
|
|
// Please report all bugs and problems to "fltk-bugs@fltk.org".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 23:14:55 +04:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#if HAVE_GL
|
|
|
|
|
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/x.H>
|
|
|
|
#include <FL/Fl_Gl_Window.H>
|
|
|
|
#include "Fl_Gl_Choice.H"
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// The symbol SWAP_TYPE defines what is in the back buffer after doing
|
|
|
|
// a glXSwapBuffers().
|
|
|
|
|
|
|
|
// The OpenGl documentation says that the contents of the backbuffer
|
|
|
|
// are "undefined" after glXSwapBuffers(). However, if we know what
|
|
|
|
// is in the backbuffers then we can save a good deal of time. For
|
|
|
|
// this reason you can define some symbols to describe what is left in
|
|
|
|
// the back buffer.
|
|
|
|
|
|
|
|
// The default of SWAP_SWAP works on an SGI, and will also work (but
|
|
|
|
// is sub-optimal) on machines that should be SWAP_COPY or SWAP_NODAMAGE.
|
|
|
|
// The win32 emulation of OpenGL can use COPY, but some (all?) OpenGL
|
|
|
|
// cards use SWAP.
|
|
|
|
|
|
|
|
// contents of back buffer after glXSwapBuffers():
|
|
|
|
#define UNDEFINED 0 // unknown
|
|
|
|
#define SWAP 1 // former front buffer
|
|
|
|
#define COPY 2 // unchanged
|
|
|
|
#define NODAMAGE 3 // unchanged even by X expose() events
|
|
|
|
|
|
|
|
#ifdef MESA
|
|
|
|
#define SWAP_TYPE NODAMAGE
|
|
|
|
#else
|
|
|
|
#define SWAP_TYPE SWAP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int Fl_Gl_Window::can_do(int a, const int *b) {
|
|
|
|
return Fl_Gl_Choice::find(a,b) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::show() {
|
2000-04-07 20:55:46 +04:00
|
|
|
#ifndef _WIN32
|
1998-10-06 23:14:55 +04:00
|
|
|
if (!shown()) {
|
|
|
|
if (!g) {
|
|
|
|
g = Fl_Gl_Choice::find(mode_,alist);
|
|
|
|
if (!g) {Fl::error("Insufficient GL support"); return;}
|
|
|
|
}
|
|
|
|
Fl_X::make_xid(this, g->vis, g->colormap);
|
|
|
|
if (overlay && overlay != this) ((Fl_Gl_Window*)overlay)->show();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Fl_Window::show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::invalidate() {
|
|
|
|
valid(0);
|
2000-04-07 20:55:46 +04:00
|
|
|
#ifndef _WIN32
|
1998-10-06 23:14:55 +04:00
|
|
|
if (overlay) ((Fl_Gl_Window*)overlay)->valid(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int Fl_Gl_Window::mode(int m, const int *a) {
|
|
|
|
if (m == mode_ && a == alist) return 0;
|
|
|
|
mode_ = m; alist = a;
|
2000-04-07 20:55:46 +04:00
|
|
|
#ifdef _WIN32
|
1998-10-06 23:14:55 +04:00
|
|
|
// destroy context and g:
|
|
|
|
if (shown()) {hide(); show();}
|
|
|
|
#else
|
|
|
|
// under X, if the visual changes we must make a new X window (!):
|
|
|
|
if (shown()) {
|
|
|
|
Fl_Gl_Choice *g1 = g;
|
|
|
|
g = Fl_Gl_Choice::find(mode_,alist);
|
|
|
|
if (!g || g->vis->visualid != g1->vis->visualid || g->d != g1->d) {
|
|
|
|
hide(); show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::make_current() {
|
|
|
|
if (!context) {
|
2000-04-07 20:55:46 +04:00
|
|
|
#ifdef _WIN32
|
1999-09-16 09:34:27 +04:00
|
|
|
context = wglCreateContext(fl_private_dc(this, mode_,&g));
|
1998-10-06 23:14:55 +04:00
|
|
|
if (fl_first_context) wglShareLists(fl_first_context, (GLXContext)context);
|
|
|
|
else fl_first_context = (GLXContext)context;
|
1999-09-16 09:34:27 +04:00
|
|
|
#else
|
|
|
|
context = glXCreateContext(fl_display, g->vis, fl_first_context, 1);
|
|
|
|
if (!fl_first_context) fl_first_context = (GLXContext)context;
|
|
|
|
#endif
|
1998-10-06 23:14:55 +04:00
|
|
|
valid(0);
|
|
|
|
}
|
1999-09-16 09:34:27 +04:00
|
|
|
fl_set_gl_context(this, (GLXContext)context);
|
2000-04-07 20:55:46 +04:00
|
|
|
#if defined(_WIN32) && USE_COLORMAP
|
1998-11-08 20:02:40 +03:00
|
|
|
if (fl_palette) {
|
1998-11-08 19:37:21 +03:00
|
|
|
fl_GetDC(fl_xid(this));
|
1998-11-08 20:02:40 +03:00
|
|
|
SelectPalette(fl_gc, fl_palette, FALSE);
|
1998-11-08 18:55:23 +03:00
|
|
|
RealizePalette(fl_gc);
|
|
|
|
}
|
1999-09-16 09:34:27 +04:00
|
|
|
#endif // USE_COLORMAP
|
1998-10-06 23:14:55 +04:00
|
|
|
glDrawBuffer(GL_BACK);
|
2000-06-03 12:37:09 +04:00
|
|
|
current_ = this;
|
1998-10-06 23:14:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::ortho() {
|
2000-03-18 13:04:18 +03:00
|
|
|
// Alpha NT seems to have a broken OpenGL that does not like negative coords:
|
|
|
|
#ifdef _M_ALPHA
|
|
|
|
glLoadIdentity();
|
|
|
|
glViewport(0, 0, w(), h());
|
|
|
|
glOrtho(0, w(), 0, h(), -1, 1);
|
|
|
|
#else
|
|
|
|
GLint v[2];
|
|
|
|
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, v);
|
1998-10-06 23:14:55 +04:00
|
|
|
glLoadIdentity();
|
2000-03-18 13:04:18 +03:00
|
|
|
glViewport(w()-v[0], h()-v[1], v[0], v[1]);
|
|
|
|
glOrtho(w()-v[0], w(), h()-v[1], h(), -1, 1);
|
|
|
|
#endif
|
1998-10-06 23:14:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::swap_buffers() {
|
2000-04-07 20:55:46 +04:00
|
|
|
#ifdef _WIN32
|
1998-10-06 23:14:55 +04:00
|
|
|
SwapBuffers(Fl_X::i(this)->private_dc);
|
|
|
|
#else
|
|
|
|
glXSwapBuffers(fl_display, fl_xid(this));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-04-07 20:55:46 +04:00
|
|
|
#if HAVE_GL_OVERLAY && defined(_WIN32)
|
1998-10-06 23:14:55 +04:00
|
|
|
uchar fl_overlay; // changes how fl_color() works
|
2000-04-07 20:55:46 +04:00
|
|
|
int fl_overlay_depth = 0;
|
1998-10-06 23:14:55 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void Fl_Gl_Window::flush() {
|
2000-03-24 11:42:03 +03:00
|
|
|
uchar save_valid = valid_;
|
2000-06-03 12:37:09 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
// SGI 320 messes up overlay with user-defined cursors:
|
|
|
|
bool fixcursor =
|
|
|
|
Fl_X::i(this)->cursor && Fl_X::i(this)->cursor != fl_default_cursor;
|
|
|
|
if (fixcursor) SetCursor(0);
|
|
|
|
#endif
|
1998-10-06 23:14:55 +04:00
|
|
|
|
2000-04-07 20:55:46 +04:00
|
|
|
#if HAVE_GL_OVERLAY && defined(_WIN32)
|
2000-03-24 11:42:03 +03:00
|
|
|
if (overlay && overlay != this &&
|
2000-06-03 12:37:09 +04:00
|
|
|
((damage()&(FL_DAMAGE_OVERLAY|FL_DAMAGE_EXPOSE))
|
2000-03-24 11:42:03 +03:00
|
|
|
|| !save_valid)) {
|
|
|
|
// Draw into hardware overlay planes
|
|
|
|
fl_set_gl_context(this, (GLXContext)overlay);
|
2000-04-07 20:55:46 +04:00
|
|
|
if (fl_overlay_depth)
|
|
|
|
wglRealizeLayerPalette(Fl_X::i(this)->private_dc, 1, TRUE);
|
2000-03-24 11:42:03 +03:00
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
fl_overlay = 1;
|
|
|
|
draw_overlay();
|
|
|
|
fl_overlay = 0;
|
|
|
|
valid(save_valid);
|
|
|
|
if (damage() == FL_DAMAGE_OVERLAY) {
|
|
|
|
wglSwapLayerBuffers(Fl_X::i(this)->private_dc,WGL_SWAP_OVERLAY1);
|
2000-04-07 20:55:46 +04:00
|
|
|
if (fixcursor) SetCursor(Fl_X::i(this)->cursor);
|
2000-03-24 11:42:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
1998-10-06 23:14:55 +04:00
|
|
|
#endif
|
|
|
|
|
2000-03-24 11:42:03 +03:00
|
|
|
make_current();
|
|
|
|
|
1998-10-06 23:14:55 +04:00
|
|
|
if (g->d) {
|
|
|
|
|
|
|
|
#if SWAP_TYPE == NODAMAGE
|
|
|
|
|
|
|
|
// don't draw if only overlay damage or expose events:
|
2000-03-24 11:42:03 +03:00
|
|
|
if ((damage()&~(FL_DAMAGE_OVERLAY|FL_DAMAGE_EXPOSE)) || !save_valid) draw();
|
1998-10-06 23:14:55 +04:00
|
|
|
swap_buffers();
|
|
|
|
|
|
|
|
#elif SWAP_TYPE == COPY
|
|
|
|
|
|
|
|
// don't draw if only the overlay is damaged:
|
2000-03-24 11:42:03 +03:00
|
|
|
if (damage() != FL_DAMAGE_OVERLAY || !save_valid) draw();
|
1998-10-06 23:14:55 +04:00
|
|
|
swap_buffers();
|
|
|
|
|
|
|
|
#else // SWAP_TYPE == SWAP || SWAP_TYPE == UNDEFINED
|
|
|
|
|
|
|
|
if (overlay == this) { // Use CopyPixels to act like SWAP_TYPE == COPY
|
|
|
|
|
|
|
|
// don't draw if only the overlay is damaged:
|
2000-03-24 11:42:03 +03:00
|
|
|
if (damage1_ || damage() != FL_DAMAGE_OVERLAY || !save_valid) draw();
|
1998-10-06 23:14:55 +04:00
|
|
|
// we use a seperate context for the copy because rasterpos must be 0
|
|
|
|
// and depth test needs to be off:
|
2000-03-05 09:51:07 +03:00
|
|
|
static GLXContext ortho_context = 0;
|
|
|
|
static Fl_Gl_Window* ortho_window = 0;
|
1998-10-06 23:14:55 +04:00
|
|
|
int init = !ortho_context;
|
2000-04-07 20:55:46 +04:00
|
|
|
if (init) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
ortho_context = wglCreateContext(Fl_X::i(this)->private_dc);
|
1998-10-06 23:14:55 +04:00
|
|
|
#else
|
|
|
|
ortho_context = glXCreateContext(fl_display,g->vis,fl_first_context,1);
|
|
|
|
#endif
|
2000-04-07 20:55:46 +04:00
|
|
|
}
|
1999-09-16 09:34:27 +04:00
|
|
|
fl_set_gl_context(this, ortho_context);
|
2000-03-05 09:51:07 +03:00
|
|
|
if (init || !save_valid || ortho_window != this) {
|
1998-10-06 23:14:55 +04:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glReadBuffer(GL_BACK);
|
|
|
|
glDrawBuffer(GL_FRONT);
|
2000-01-12 09:06:24 +03:00
|
|
|
glLoadIdentity();
|
|
|
|
glViewport(0, 0, w(), h());
|
|
|
|
glOrtho(0, w(), 0, h(), -1, 1);
|
|
|
|
glRasterPos2i(0,0);
|
2000-03-24 11:42:03 +03:00
|
|
|
ortho_window = this;
|
1998-10-06 23:14:55 +04:00
|
|
|
}
|
|
|
|
glCopyPixels(0,0,w(),h(),GL_COLOR);
|
|
|
|
make_current(); // set current context back to draw overlay
|
|
|
|
damage1_ = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
#if SWAP_TYPE == SWAP
|
|
|
|
uchar old_damage = damage();
|
|
|
|
clear_damage(damage1_|old_damage); draw();
|
|
|
|
swap_buffers();
|
|
|
|
damage1_ = old_damage;
|
|
|
|
#else // SWAP_TYPE == UNDEFINED
|
|
|
|
clear_damage(~0); draw();
|
|
|
|
swap_buffers();
|
|
|
|
damage1_ = ~0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (overlay==this) { // fake overlay in front buffer
|
|
|
|
glDrawBuffer(GL_FRONT);
|
|
|
|
draw_overlay();
|
|
|
|
glDrawBuffer(GL_BACK);
|
|
|
|
glFlush();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // single-buffered context is simpler:
|
|
|
|
|
1999-08-09 10:19:33 +04:00
|
|
|
draw();
|
1998-10-06 23:14:55 +04:00
|
|
|
if (overlay == this) draw_overlay();
|
|
|
|
glFlush();
|
|
|
|
}
|
|
|
|
|
2000-06-03 12:37:09 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (fixcursor) SetCursor(Fl_X::i(this)->cursor);
|
|
|
|
#endif
|
1998-10-06 23:14:55 +04:00
|
|
|
valid(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::resize(int X,int Y,int W,int H) {
|
|
|
|
if (W != w() || H != h()) valid(0);
|
|
|
|
Fl_Window::resize(X,Y,W,H);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::hide() {
|
|
|
|
if (context) {
|
1999-09-16 09:34:27 +04:00
|
|
|
fl_no_gl_context();
|
2000-04-07 20:55:46 +04:00
|
|
|
if (context != fl_first_context) {
|
|
|
|
#ifdef _WIN32
|
1998-10-06 23:14:55 +04:00
|
|
|
wglDeleteContext((GLXContext)context);
|
|
|
|
#else
|
|
|
|
glXDestroyContext(fl_display, (GLXContext)context);
|
2000-04-07 20:55:46 +04:00
|
|
|
#endif
|
|
|
|
}
|
1998-10-06 23:14:55 +04:00
|
|
|
#ifdef GLX_MESA_release_buffers
|
|
|
|
glXReleaseBuffersMESA(fl_display, fl_xid(this));
|
|
|
|
#endif
|
|
|
|
context = 0;
|
|
|
|
}
|
2000-04-07 20:55:46 +04:00
|
|
|
#if HAVE_GL_OVERLAY && defined(_WIN32)
|
|
|
|
if (overlay && overlay != this && (GLXContext)overlay != fl_first_context) {
|
|
|
|
wglDeleteContext((GLXContext)overlay);
|
|
|
|
overlay = 0;
|
|
|
|
}
|
|
|
|
#endif
|
1998-10-06 23:14:55 +04:00
|
|
|
Fl_Window::hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
Fl_Gl_Window::~Fl_Gl_Window() {
|
|
|
|
hide();
|
|
|
|
// delete overlay; this is done by ~Fl_Group
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::init() {
|
|
|
|
end(); // we probably don't want any children
|
|
|
|
box(FL_NO_BOX);
|
|
|
|
mode_ = FL_RGB | FL_DEPTH | FL_DOUBLE;
|
|
|
|
alist = 0;
|
|
|
|
context = 0;
|
|
|
|
g = 0;
|
|
|
|
overlay = 0;
|
|
|
|
damage1_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fl_Gl_Window::draw_overlay() {}
|
|
|
|
|
|
|
|
#endif
|
1998-10-20 00:46:58 +04:00
|
|
|
|
|
|
|
//
|
2000-06-06 01:21:24 +04:00
|
|
|
// End of "$Id: Fl_Gl_Window.cxx,v 1.12.2.13 2000/06/05 21:20:51 mike Exp $".
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|