1998-10-20 17:25:25 +04:00
|
|
|
//
|
2019-11-08 15:58:28 +03:00
|
|
|
// OpenGL test program for the Fast Light Tool Kit (FLTK).
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
// Modified to have 2 cubes to test multiple OpenGL contexts
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2023-10-16 23:11:33 +03:00
|
|
|
// Copyright 1998-2023 by Bill Spitzak and others.
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 17:25:25 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2010-12-15 15:11:16 +03:00
|
|
|
#include <config.h>
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/Fl.H>
|
2022-02-06 17:22:24 +03:00
|
|
|
#include <FL/fl_ask.H>
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
|
|
|
#include <FL/Fl_Radio_Light_Button.H>
|
|
|
|
#include <FL/Fl_Slider.H>
|
2020-11-24 15:47:07 +03:00
|
|
|
#include <FL/Fl_Sys_Menu_Bar.H>
|
|
|
|
#include <FL/Fl_Printer.H> // demo printing
|
2023-10-16 23:11:33 +03:00
|
|
|
#include <FL/Fl_Grid.H> // grid layout
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#if !HAVE_GL
|
|
|
|
class cube_box : public Fl_Box {
|
2010-03-15 12:31:10 +03:00
|
|
|
public:
|
1998-10-06 22:21:25 +04:00
|
|
|
double lasttime;
|
|
|
|
int wire;
|
|
|
|
double size;
|
|
|
|
double speed;
|
2020-11-24 15:47:07 +03:00
|
|
|
cube_box(int x,int y,int w,int h,const char *l=0) :Fl_Box(FL_DOWN_BOX,x,y,w,h,l) {
|
|
|
|
label("This demo does\nnot work without GL");
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#include <FL/Fl_Gl_Window.H>
|
|
|
|
#include <FL/gl.h>
|
|
|
|
|
|
|
|
class cube_box : public Fl_Gl_Window {
|
2022-12-30 21:14:36 +03:00
|
|
|
void draw() FL_OVERRIDE;
|
|
|
|
int handle(int) FL_OVERRIDE;
|
1998-10-06 22:21:25 +04:00
|
|
|
public:
|
|
|
|
double lasttime;
|
|
|
|
int wire;
|
|
|
|
double size;
|
|
|
|
double speed;
|
2020-11-24 15:47:07 +03:00
|
|
|
cube_box(int x,int y,int w,int h,const char *l=0) : Fl_Gl_Window(x,y,w,h,l) {
|
2023-10-19 21:16:01 +03:00
|
|
|
end();
|
2020-11-24 15:47:07 +03:00
|
|
|
lasttime = 0.0;
|
|
|
|
box(FL_DOWN_FRAME);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The cube definition */
|
|
|
|
float v0[3] = {0.0, 0.0, 0.0};
|
|
|
|
float v1[3] = {1.0, 0.0, 0.0};
|
|
|
|
float v2[3] = {1.0, 1.0, 0.0};
|
|
|
|
float v3[3] = {0.0, 1.0, 0.0};
|
|
|
|
float v4[3] = {0.0, 0.0, 1.0};
|
|
|
|
float v5[3] = {1.0, 0.0, 1.0};
|
|
|
|
float v6[3] = {1.0, 1.0, 1.0};
|
|
|
|
float v7[3] = {0.0, 1.0, 1.0};
|
|
|
|
|
|
|
|
#define v3f(x) glVertex3fv(x)
|
|
|
|
|
|
|
|
void drawcube(int wire) {
|
|
|
|
/* Draw a colored cube */
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(0,0,255);
|
|
|
|
v3f(v0); v3f(v1); v3f(v2); v3f(v3);
|
|
|
|
glEnd();
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(0,255,255); v3f(v4); v3f(v5); v3f(v6); v3f(v7);
|
|
|
|
glEnd();
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(255,0,255); v3f(v0); v3f(v1); v3f(v5); v3f(v4);
|
|
|
|
glEnd();
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(255,255,0); v3f(v2); v3f(v3); v3f(v7); v3f(v6);
|
|
|
|
glEnd();
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(0,255,0); v3f(v0); v3f(v4); v3f(v7); v3f(v3);
|
|
|
|
glEnd();
|
|
|
|
glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
|
|
|
|
glColor3ub(255,0,0); v3f(v1); v3f(v2); v3f(v6); v3f(v5);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cube_box::draw() {
|
|
|
|
lasttime = lasttime+speed;
|
|
|
|
if (!valid()) {
|
|
|
|
glLoadIdentity();
|
Changed OpenGL support for the Mac OS X platform: use cocoa instead of deprecated AGL.
All changes are mac-specific, except a very minor change in file src/gl_draw.cxx
where string drawing wrongly claimed to support @symbol, not possible
because symbols are drawn using non-GL primitives.
Unchanged application code can use the new FLTK code.
In addition, the new code allows mac applications to draw OpenGL scenes
at high resolution on so-called 'retina' displays, but this requires some
support from app code. They must call, before opening GL windows,
Fl::use_high_resolution(1);
and change their glViewport() calls as follows
glViewport(0, 0, pxel_w(), pixel_h());
This uses 2 new member functions of the Fl_Gl_Window class,
pixel_w() and pixel_h() returning the window dimensions in pixel
units, that is, twice the w() and h() when the window is mapped
on a retina display.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10498 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
2014-12-20 10:19:23 +03:00
|
|
|
glViewport(0,0,pixel_w(),pixel_h());
|
1998-10-06 22:21:25 +04:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glFrustum(-1,1,-1,1,2,10000);
|
|
|
|
glTranslatef(0,0,-10);
|
2021-08-31 18:49:31 +03:00
|
|
|
glClearColor(0.4f, 0.4f, 0.4f, 0);
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glPushMatrix();
|
|
|
|
glRotatef(float(lasttime*1.6),0,0,1);
|
|
|
|
glRotatef(float(lasttime*4.2),1,0,0);
|
|
|
|
glRotatef(float(lasttime*2.3),0,1,0);
|
2021-08-31 18:49:31 +03:00
|
|
|
glTranslatef(-1.0f, 1.2f, -1.5f);
|
1998-10-06 22:21:25 +04:00
|
|
|
glScalef(float(size),float(size),float(size));
|
|
|
|
drawcube(wire);
|
2002-06-26 07:36:57 +04:00
|
|
|
glPopMatrix();
|
|
|
|
gl_color(FL_GRAY);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2017-08-27 11:01:55 +03:00
|
|
|
gl_font(FL_HELVETICA_BOLD, 16 );
|
2002-06-26 06:01:33 +04:00
|
|
|
gl_draw(wire ? "Cube: wire" : "Cube: flat", -4.5f, -4.5f );
|
2002-06-26 07:36:57 +04:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2016-01-17 03:44:07 +03:00
|
|
|
|
2023-10-16 23:11:33 +03:00
|
|
|
// draw additional FLTK widgets and graphics
|
2021-02-27 17:09:12 +03:00
|
|
|
Fl_Gl_Window::draw();
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
|
2005-11-04 11:23:20 +03:00
|
|
|
int cube_box::handle(int e) {
|
|
|
|
switch (e) {
|
2020-11-24 15:47:07 +03:00
|
|
|
case FL_ENTER: cursor(FL_CURSOR_CROSS); break;
|
|
|
|
case FL_LEAVE: cursor(FL_CURSOR_DEFAULT); break;
|
2005-11-04 11:23:20 +03:00
|
|
|
}
|
|
|
|
return Fl_Gl_Window::handle(e);
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:11:33 +03:00
|
|
|
// callback for overlay button (Fl_Button on OpenGL scene)
|
|
|
|
void show_info_cb(Fl_Widget*, void*) {
|
|
|
|
fl_message("This is an example of using FLTK widgets inside OpenGL windows.\n"
|
|
|
|
"Multiple widgets can be added to Fl_Gl_Windows. They will be\n"
|
|
|
|
"rendered as overlays over the scene.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// overlay a button onto an OpenGL window (cube_box)
|
2023-10-19 21:16:01 +03:00
|
|
|
// but don't change the current group Fl_Group::current()
|
2023-10-16 23:11:33 +03:00
|
|
|
void overlay_button(cube_box *cube) {
|
2023-10-19 21:16:01 +03:00
|
|
|
Fl_Group *curr = Fl_Group::current();
|
|
|
|
Fl_Group::current(0);
|
2023-10-16 23:11:33 +03:00
|
|
|
Fl_Widget *w = new Fl_Button(10, 10, 120, 30, "FLTK over GL");
|
|
|
|
w->color(FL_FREE_COLOR);
|
|
|
|
w->box(FL_BORDER_BOX);
|
|
|
|
w->callback(show_info_cb);
|
2023-10-19 21:16:01 +03:00
|
|
|
cube->add(w);
|
|
|
|
Fl_Group::current(curr);
|
2023-10-16 23:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAVE_GL
|
1998-10-06 22:21:25 +04:00
|
|
|
|
|
|
|
Fl_Window *form;
|
|
|
|
Fl_Slider *speed, *size;
|
2019-11-08 16:06:35 +03:00
|
|
|
Fl_Button *exit_button, *wire, *flat;
|
2020-11-24 15:47:07 +03:00
|
|
|
cube_box *lt_cube, *rt_cube;
|
2019-11-08 16:06:35 +03:00
|
|
|
int done = 0; // set to 1 in exit button callback
|
|
|
|
|
|
|
|
// exit button callback
|
2023-10-16 23:11:33 +03:00
|
|
|
void exit_cb(Fl_Widget *w, void *) {
|
2019-11-08 16:06:35 +03:00
|
|
|
done = 1;
|
2023-10-16 23:11:33 +03:00
|
|
|
w->window()->hide(); // necessary if built w/o GL
|
2019-11-08 16:06:35 +03:00
|
|
|
}
|
|
|
|
|
2020-11-24 15:47:07 +03:00
|
|
|
// print screen demo
|
2023-10-16 23:11:33 +03:00
|
|
|
void print_cb(Fl_Widget *w, void *data) {
|
2010-03-17 16:29:14 +03:00
|
|
|
Fl_Printer printer;
|
2010-03-14 21:07:24 +03:00
|
|
|
Fl_Window *win = Fl::first_window();
|
2023-10-16 23:11:33 +03:00
|
|
|
if (!win) return;
|
|
|
|
if (printer.start_job(1)) return;
|
|
|
|
if (printer.start_page()) return;
|
|
|
|
printer.scale(0.5, 0.5);
|
|
|
|
printer.print_widget(win);
|
2010-03-14 21:07:24 +03:00
|
|
|
printer.end_page();
|
|
|
|
printer.end_job();
|
|
|
|
}
|
2020-11-24 15:47:07 +03:00
|
|
|
|
|
|
|
// Create a form that allows resizing for A and C (GL windows) with B fixed size/centered:
|
|
|
|
//
|
|
|
|
// |<--------------------------------------->|<---------------------->|
|
2023-10-16 23:11:33 +03:00
|
|
|
// . lt_cube center : rt_cube .
|
2020-11-24 15:47:07 +03:00
|
|
|
// . 350 100 : 350 .
|
|
|
|
// . |<------------------->| |<-------->| |<------------------->| .
|
|
|
|
// ....................................................................
|
2023-10-16 23:11:33 +03:00
|
|
|
// : ....................... ............ ....................... : __
|
2020-11-24 15:47:07 +03:00
|
|
|
// : : : : : : : :
|
2023-10-16 23:11:33 +03:00
|
|
|
// : : A : : B : : C : : h = 350
|
2020-11-24 15:47:07 +03:00
|
|
|
// : : : : : : : :
|
|
|
|
// : :.....................: :..........: :.....................: : __
|
|
|
|
// :..................................................................: __ MARGIN
|
|
|
|
//
|
2023-10-16 23:11:33 +03:00
|
|
|
// | | | | | |
|
|
|
|
// MARGIN GAP GAP
|
2020-11-24 15:47:07 +03:00
|
|
|
|
2023-10-16 23:11:33 +03:00
|
|
|
#define MENUBAR_H 25 // menubar height
|
|
|
|
#define MARGIN 20 // fixed margin around widgets
|
|
|
|
#define GAP 20 // fixed gap between widgets
|
2022-02-06 17:22:24 +03:00
|
|
|
|
2020-11-24 15:47:07 +03:00
|
|
|
void makeform(const char *name) {
|
|
|
|
// Widget's XYWH's
|
2023-10-16 23:11:33 +03:00
|
|
|
int form_w = 800 + 2 * MARGIN + 2 * GAP; // main window width
|
2020-11-24 15:47:07 +03:00
|
|
|
int form_h = 350 + MENUBAR_H + 2 * MARGIN; // main window height
|
|
|
|
|
|
|
|
// main window
|
|
|
|
form = new Fl_Window(form_w, form_h, name);
|
2023-10-16 23:11:33 +03:00
|
|
|
// menu bar
|
|
|
|
Fl_Sys_Menu_Bar *menubar = new Fl_Sys_Menu_Bar(0, 0, form_w, MENUBAR_H);
|
|
|
|
menubar->add("File/Print window", FL_COMMAND+'p', print_cb);
|
|
|
|
menubar->add("File/Quit", FL_COMMAND+'q', exit_cb);
|
|
|
|
|
|
|
|
// Fl_Grid (layout)
|
|
|
|
Fl_Grid *grid = new Fl_Grid(0, MENUBAR_H, form_w, 350 + 2 * MARGIN);
|
|
|
|
grid->layout(4, 4, MARGIN, GAP);
|
|
|
|
grid->box(FL_FLAT_BOX);
|
|
|
|
|
|
|
|
// set column and row weights to control resizing behavior
|
|
|
|
int cwe[] = {50, 0, 0, 50}; // column weights
|
|
|
|
int rwe[] = { 0, 0, 50, 0}; // row weights
|
|
|
|
grid->col_weight(cwe, 4); // set weights for resizing
|
|
|
|
grid->row_weight(rwe, 4); // set weights for resizing
|
|
|
|
|
|
|
|
// set non-default gaps for special layout purposes and labels
|
|
|
|
grid->row_gap(0, 0); // no gap below wire button
|
|
|
|
grid->row_gap(2, 50); // gap below sliders for labels
|
2022-02-06 17:22:24 +03:00
|
|
|
|
2023-10-16 23:11:33 +03:00
|
|
|
// left GL window
|
|
|
|
lt_cube = new cube_box(0, 0, 350, 350);
|
2022-02-06 17:22:24 +03:00
|
|
|
|
2023-10-16 23:11:33 +03:00
|
|
|
// center group
|
|
|
|
wire = new Fl_Radio_Light_Button( 0, 0, 100, 25, "Wire");
|
|
|
|
flat = new Fl_Radio_Light_Button( 0, 0, 100, 25, "Flat");
|
|
|
|
speed = new Fl_Slider(FL_VERT_SLIDER, 0, 0, 40, 90, "Speed");
|
|
|
|
size = new Fl_Slider(FL_VERT_SLIDER, 0, 0, 40, 90, "Size");
|
|
|
|
exit_button = new Fl_Button( 0, 0, 100, 25, "Exit");
|
|
|
|
exit_button->callback(exit_cb);
|
|
|
|
|
|
|
|
// right GL window
|
|
|
|
rt_cube = new cube_box(0, 0, 350, 350);
|
|
|
|
|
|
|
|
// assign widgets to grid positions (R=row, C=col) and sizes
|
|
|
|
// RS=rowspan, CS=colspan: R, C, RS, CS, optional alignment
|
|
|
|
grid->widget(lt_cube, 0, 0, 4, 1);
|
|
|
|
grid->widget(wire, 0, 1, 1, 2);
|
|
|
|
grid->widget(flat, 1, 1, 1, 2);
|
|
|
|
grid->widget(speed, 2, 1, 1, 1, FL_GRID_VERTICAL);
|
|
|
|
grid->widget(size, 2, 2, 1, 1, FL_GRID_VERTICAL);
|
|
|
|
grid->widget(exit_button, 3, 1, 1, 2);
|
|
|
|
grid->widget(rt_cube, 0, 3, 4, 1);
|
|
|
|
|
|
|
|
#if HAVE_GL
|
|
|
|
overlay_button(lt_cube); // overlay a button onto the OpenGL window
|
|
|
|
#endif // HAVE_GL
|
2020-11-24 15:47:07 +03:00
|
|
|
|
2021-11-25 15:36:18 +03:00
|
|
|
form->end();
|
2023-10-16 23:11:33 +03:00
|
|
|
form->resizable(grid);
|
2021-11-25 15:36:18 +03:00
|
|
|
form->size_range(form->w(), form->h()); // minimum window size
|
2020-11-24 15:47:07 +03:00
|
|
|
}
|
2010-03-14 21:07:24 +03:00
|
|
|
|
2000-06-14 00:33:36 +04:00
|
|
|
int main(int argc, char **argv) {
|
2016-03-09 01:19:57 +03:00
|
|
|
Fl::use_high_res_GL(1);
|
2022-02-06 17:22:24 +03:00
|
|
|
Fl::set_color(FL_FREE_COLOR, 255, 255, 0, 75);
|
1998-10-06 22:21:25 +04:00
|
|
|
makeform(argv[0]);
|
2023-10-16 23:11:33 +03:00
|
|
|
speed->bounds(4, 0);
|
2020-11-24 15:47:07 +03:00
|
|
|
speed->value(lt_cube->speed = rt_cube->speed = 1.0);
|
2023-10-16 23:11:33 +03:00
|
|
|
size->bounds(4, 0.2);
|
|
|
|
size->value(lt_cube->size = rt_cube->size = 2.0);
|
2020-11-24 15:47:07 +03:00
|
|
|
flat->value(1); lt_cube->wire = 0; rt_cube->wire = 1;
|
2023-10-16 23:11:33 +03:00
|
|
|
form->label("Cube Demo");
|
1998-10-06 22:21:25 +04:00
|
|
|
form->show(argc,argv);
|
2020-11-24 15:47:07 +03:00
|
|
|
lt_cube->show();
|
|
|
|
rt_cube->show();
|
2023-10-16 23:11:33 +03:00
|
|
|
|
2001-03-14 20:20:02 +03:00
|
|
|
#if 0
|
|
|
|
// This demonstrates how to manipulate OpenGL contexts.
|
|
|
|
// In this case the same context is used by multiple windows (I'm not
|
|
|
|
// sure if this is allowed on Win32, can somebody check?).
|
2023-10-16 23:11:33 +03:00
|
|
|
// This fixes a bug on the XFree86 3.0 OpenGL where only one context per
|
|
|
|
// program seems to work, but there are probably better uses for this!
|
2020-11-24 15:47:07 +03:00
|
|
|
lt_cube->make_current(); // causes context to be created
|
|
|
|
rt_cube->context(lt_cube->context()); // share the contexts
|
2001-03-14 20:20:02 +03:00
|
|
|
#endif
|
2023-10-16 23:11:33 +03:00
|
|
|
|
|
|
|
#if HAVE_GL
|
1998-10-06 22:21:25 +04:00
|
|
|
for (;;) {
|
2020-11-24 15:47:07 +03:00
|
|
|
if (form->visible() && speed->value()) {
|
|
|
|
if (!Fl::check()) break; // returns immediately
|
|
|
|
} else {
|
|
|
|
if (!Fl::wait()) break; // waits until something happens
|
|
|
|
}
|
|
|
|
lt_cube->wire = wire->value();
|
|
|
|
rt_cube->wire = !wire->value();
|
|
|
|
lt_cube->size = rt_cube->size = size->value();
|
|
|
|
lt_cube->speed = rt_cube->speed = speed->value();
|
|
|
|
lt_cube->redraw();
|
|
|
|
rt_cube->redraw();
|
2019-11-08 16:06:35 +03:00
|
|
|
if (done) break; // exit button was clicked
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|
|
|
|
return 0;
|
2023-10-16 23:11:33 +03:00
|
|
|
#else
|
|
|
|
return Fl::run();
|
|
|
|
#endif
|
1998-10-06 22:21:25 +04:00
|
|
|
}
|