FLTK (pronounced "fulltick") is a cross-platform C++ GUI toolkit for UNIX®/Linux® (X11), Microsoft® Windows®, and MacOS® X. FLTK provides modern GUI functionality without the bloat and supports 3D graphics via OpenGL® and its built-in GLUT emulation. FLTK is designed to be small and modular enough to be statically linked, but works fine as a shared library. FLTK also includes an excellent UI builder called FLUID that can be used to create applications in minutes. FLTK is provided under the terms of the GNU Library Public License, Version 2 with exceptions that allow for static linking. More informations in http://www.fltk.org.
5. 1 /**
2 * Um primeiro exemplo utilizando OpenGL + GLUT
3 **/
4
5 //Inclusão da biblioteca GLUT
6 #include <glut.h>
7
8 /*Função que executara o desenho na janela*/
9 void display(void)
10 {
11 /*Limpando a cor de fundo da janela e definindo a nova cor de desenho*/
12 glClear(GL_COLOR_BUFFER_BIT);
13 glColor3f(0.0,0.0,0.0);
14
15 /*Desenhando um polígono*/
16 glBegin(GL_POLYGON);
17 glVertex2f(0.25,0.25);
18 glVertex2f(0.75,0.25);
19 glVertex2f(0.75,0.75);
20 glVertex2f(0.25,0.75);
21 glEnd();
22
23 /*Forçando a execução dos comandos OpenGL*/
24 glFlush();
25 }
26
27 /*Função principal do programa*/
28 int main(int argc, char **argv)
29 {
30 glutInit(&argc,argv);
31 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
32 glutInitWindowSize(256,256);
33 glutInitWindowPosition(100,100);
34 glutCreateWindow(argv[0]);
35 glClearColor(1.0,1.0,1.0,0.0);
36 glShadeModel(GL_FLAT);
37 glOrtho(0,1,0,1,-1,1);
38 glutDisplayFunc(display);
39 glutMainLoop();
40 return 0;
41 }
42
6. 1C:Documents and SettingsAdministradorDesktop_exemplos.openglMyShapePolygonWindow.cxx
//
// Tiny OpenGL demo program for the Fast Light Tool Kit (FLTK).
//
#include "config.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/math.h>
#if HAVE_GL
#include <FL/gl.h>
#include <FL/Fl_Gl_Window.H>
class MyShapePolygonWindow : public Fl_Gl_Window
{
void draw();
public:
MyShapePolygonWindow(int x,int y,int w,int h,const char *l=0);
};
MyShapePolygonWindow::MyShapePolygonWindow(int x,int y,int w,int h,const char *l):
Fl_Gl_Window(x,y,w,h,l)
{
}
void MyShapePolygonWindow::draw()
{
// the valid() property may be used to avoid reinitializing your
// GL transformation for each redraw:
if (!valid())
{
valid(1);
glLoadIdentity();
glViewport(0, 0, w(), h());
}
// draw an amazing graphic:
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}
#else
#include <FL/Fl_Box.H>
class MyShapePolygonWindow : public Fl_Box
{
public:
MyShapePolygonWindow(int x,int y,int w,int h,const char *l=0)
:Fl_Box(FL_DOWN_BOX,x,y,w,h,l)
{
this->label("Esse exemplo não trabalha sem OpenGL !");
}
};
#endif
int main(int argc, char *argv[])
{
Fl_Window window(300, 300);
MyShapePolygonWindow sw(10, 10, 280, 280);
window.resizable(&sw);
window.end();
window.show(argc,argv);
return Fl::run();
}
7. 1 /**
2 * Um programa OpenGL simples que desenha um quadrado em uma janela GLUT.
3 */
4 #include <glut.h>
5
6 // Função callback chamada para fazer o desenho
7 void Desenha(void)
8 {
9 // Inicializa o sistema de coordenadas
10 glMatrixMode(GL_MODELVIEW);
11 glLoadIdentity();
12
13 // Limpa a janela de visualização com a cor de fundo especificada
14 glClear(GL_COLOR_BUFFER_BIT);
15
16 // Especifica que a cor corrente é amarela
17 // R G B
18 glColor3f(1.0f, 1.0f, 0.0f);
19
20 // Desenha um quadrado preenchido com a cor corrente
21 glBegin(GL_QUADS);
22 glVertex2i(100,150);
23 glVertex2i(100,100);
24 // Especifica que a cor corrente é vermelha
25 glColor3f(1.0f, 0.0f, 0.0f);
26 glVertex2i(150,100);
27 glVertex2i(150,150);
28 glEnd();
29
30 // Executa os comandos OpenGL
31 glFlush();
32 }
33
34 // Inicializa parâmetros de rendering
35 void Inicializa (void)
36 {
37 // Define a cor de fundo da janela de visualização como preta
38 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
39 }
40
41 // Função callback chamada quando o tamanho da janela é alterado
42 void AlteraTamanhoJanela(GLsizei w, GLsizei h)
43 {
44 // Evita a divisao por zero
45 if(h == 0) h = 1;
46
47 // Especifica as dimensões da Viewport
48 glViewport(0, 0, w, h);
49
50 // Inicializa o sistema de coordenadas
51 glMatrixMode(GL_PROJECTION);
52 glLoadIdentity();
53
54 // Estabelece a janela de seleção (left, right, bottom, top)
55 if (w <= h)
56 gluOrtho2D (0.0f, 250.0f, 0.0f, 250.0f*h/w);
57 else
58 gluOrtho2D (0.0f, 250.0f*w/h, 0.0f, 250.0f);
59 }
60
61 // Programa Principal
62 int main(int argc, char* argv[])
63 {
64 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
65 glutInitWindowSize(400,350);
66 glutInitWindowPosition(10,10);
67 glutCreateWindow("O primeiro quadrado ...");
68 glutDisplayFunc(Desenha);
69 glutReshapeFunc(AlteraTamanhoJanela);
70 Inicializa();
71 glutMainLoop();
72 }
8. 1 /**
2 * Exemplo de programa usando OpenGL + GLUT.
3 **/
4 #include <cstdlib>
5 #include <glut.h>
6
7 //Função callback de desenho da janela de visualização
8 void Desenha(void)
9 {
10 //Limpa a janela de visualização com a cor branca
11 glClearColor(1.0,1.0,1.0,0);
12 glClear(GL_COLOR_BUFFER_BIT);
13
14 //Define a cor de desenho: vermelho
15 glColor3f(1.0,0.0,0.0);
16
17 //Desenha um triangulo no centro da janela com interpolaçao de cores
18 glBegin(GL_TRIANGLES);
19 glColor3f(1.0,0.0,0.0); glVertex3f(-0.5,-0.5,0);
20 glColor3f(0.0,1.0,0.0); glVertex3f( 0.0, 0.5,0);
21 glColor3f(0.0,0.0,1.0); glVertex3f( 0.5,-0.5,0);
22 glEnd();
23
24 //Executa os comandos OpenGL
25 glFlush();
26 }
27
28 //Função callback chamada para gerenciar eventos de teclas
29 void Teclado(unsigned char key, int x, int y)
30 {
31 if (key == 27 ) exit(EXIT_SUCCESS);
32 }
33
34 //Funcção responsável por inicializar parametros e variaveis
35 void Inicializa(void)
36 {
37 //Define a janela de visualização 2D
38 glMatrixMode(GL_PROJECTION);
39 gluOrtho2D(-1.0,1.0,-1.0,1.0);
40 glMatrixMode(GL_MODELVIEW);
41 }
42
43 //Programa Principal
44 int main(int argc, char* argv[])
45 {
46
47 //Define o modo de operação da GLUT
48 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
49
50 //Especifica o tamanho inicial em pixels da janela GLUT
51 glutInitWindowSize(400,400);
52
53 //Cria a janela passando como argumento o título da mesma
54 glutCreateWindow("Primeiro Programa usando GLUT ...");
55
56 //Registra a função callback de redesenho da janela de visualização
57 glutDisplayFunc(Desenha);
58
59 //Registra a função callback para tratamento das teclas ASCII
60 glutKeyboardFunc(Teclado);
61
62 //Chama a função responsável por fazer as inicializações
63 Inicializa();
64
65 //Inicia o processamento e aguarda interações do usuário
66 glutMainLoop();
67
68 return EXIT_SUCCESS;
69 }
70
9. 1 //
2 // Tiny OpenGL demo program for the Fast Light Tool Kit (FLTK).
3 //
4
5 #include "config.h"
6 #include <FL/Fl.H>
7 #include <FL/Fl_Window.H>
8 #include <FL/Fl_Hor_Slider.H>
9 #include <FL/math.h>
10
11 #if HAVE_GL
12
13 #include <FL/gl.h>
14 #include <FL/Fl_Gl_Window.H>
15
16 class shape_window : public Fl_Gl_Window {
17 void draw();
18 public:
19 int sides;
20 shape_window(int x,int y,int w,int h,const char *l=0);
21 };
22
23 shape_window::shape_window(int x,int y,int w,int h,const char *l) :
24 Fl_Gl_Window(x,y,w,h,l) {
25 sides = 3;
26 }
27
28 void shape_window::draw() {
29 // the valid() property may be used to avoid reinitializing your
30 // GL transformation for each redraw:
31 if (!valid()) {
32 valid(1);
33 glLoadIdentity();
34 glViewport(0, 0, w(), h());
35 }
36 // draw an amazing graphic:
37 glClear(GL_COLOR_BUFFER_BIT);
38 glColor3f(.5,.6,.7);
39 glBegin(GL_POLYGON);
40 for (int j=0; j<sides; j++) {
41 double ang = j*2*M_PI/sides;
42 glVertex3f(cos(ang),sin(ang),0);
43 }
44 glEnd();
45 }
46
47 #else
48
49 #include <FL/Fl_Box.H>
50 class shape_window : public Fl_Box {
51 public:
52 int sides;
53 shape_window(int x,int y,int w,int h,const char *l=0)
54 :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
55 label("This demo doesnnot work without GL");
56 }
57 };
58
59 #endif
60
61 // when you change the data, as in this callback, you must call redraw():
62 void sides_cb(Fl_Widget *o, void *p) {
63 shape_window *sw = (shape_window *)p;
64 sw->sides = int(((Fl_Slider *)o)->value());
65 sw->redraw();
66 }
67
68 int main(int argc, char **argv) {
69
70 Fl_Window window(300, 330);
71
72 // the shape window could be it's own window, but here we make it
10. 73 // a child window:
74 shape_window sw(10, 10, 280, 280);
75 // make it resize:
76 window.resizable(&sw);
77 // window.size_range(300,330,0,0,1,1,1);
78 // add a knob to control it:
79 Fl_Hor_Slider slider(50, 295, window.w()-60, 30, "Sides:");
80 slider.align(FL_ALIGN_LEFT);
81 slider.callback(sides_cb,&sw);
82 slider.value(sw.sides);
83 slider.step(1);
84 slider.bounds(3,40);
85
86 window.end();
87 window.show(argc,argv);
88
89 return Fl::run();
90 }
91
92 //
93 // End of "$Id: shape.cxx 5845 2007-05-20 00:01:06Z mike $".
94 //
95