SlideShare a Scribd company logo
1 // DEMOSTRAÇÃO DE COMO CRIAR UM 'X' EM OPENGL
2 #include <FL/Fl.H>
3 #include <FL/fl_draw.H>
4 #include <FL/Fl_Double_Window.H>
5
6 // SIMPLE BOX WIDGET THAT DRAWS AN 'X'
7 class DrawX : public Fl_Widget {
8 public:
9 DrawX(int X, int Y, int W, int H, const char*L=0) : Fl_Widget(X,Y,W,H,L)
10 {
11 }
12 void draw(void)
13 {
14 // DRAW BLACK 'X'
15 fl_color(FL_BLACK);
16 fl_line(x(), y(), x()+w()-1, y()+h()-1);
17 fl_line(x(), y()+h()-1, x()+w()-1, y());
18 }
19 };
20
21 int main(int argc, char* argv[] )
22 {
23 Fl_Double_Window win(200,200);
24 DrawX draw_x(0, 0, win.w(), win.h());
25 win.resizable(draw_x);
26 win.show();
27 return(Fl::run());
28 }
29
30
1 /*
2 * timer.cxx
3 * Exemplo das propriedades de desenho do FLTK
4 */
5
6 //
7 // FLTK drawing example showing simple line drawing animation
8 // erco 03/22/07
9 //
10 #include <FL/Fl.H>
11 #include <FL/Fl_Double_Window.H>
12 #include <FL/Fl_Box.H>
13 #include <FL/fl_draw.h>
14 #include <math.h>
15 #include <stdio.h>
16 #include <time.h>
17 #define BG_COLOR 45
18 #define TICK_COLOR 50
19 #define CIRC_COLOR 0
20 class MyTimer : public Fl_Box
21 {
22 void draw()
23 {
24 // COMPUTE NEW COORDS OF LINE
25 static long start = time(NULL);
26 long tick = time(NULL) - start;
27 char secs[80]; sprintf(secs, "%02ld:%02ld", tick/60, tick%60);
28 float pi = 3.14 - (((tick % 60) / 60.0) * 6.28);
29 int radius = h() / 2;
30 int x1 = (int)(x() + w()/2),
31 y1 = (int)(y() + h()/2),
32 x2 = (int)(x1 + (sin(pi) * radius)),
33 y2 = (int)(y1 + (cos(pi) * radius));
34
35 // TELL BASE WIDGET TO DRAW ITS BACKGROUND
36 Fl_Box::draw();
37
38 // DRAW 'SECOND HAND' OVER WIDGET'S BACKGROUND
39 fl_color(TICK_COLOR);
40 fl_line(x1, y1, x2, y2);
41 fl_color(CIRC_COLOR);
42 fl_pie(x1-10, y1-10, 20, 20, 0.0, 360.0);
43
44 // DRAW TIMER TEXT STRING
45 fl_color(TICK_COLOR);
46 fl_font(FL_HELVETICA,16);
47 fl_draw(secs, x()+4, y()+h()-4);
48 }
49
50 static void Timer_CB(void *userdata)
51 {
52 MyTimer *o = (MyTimer*)userdata;
53 o->redraw();
54 Fl::repeat_timeout(0.25, Timer_CB, userdata);
55 }
56 public:
57 // CONSTRUCTOR
58 MyTimer(int X,int Y,int W,int H,const char*L=0) : Fl_Box(X,Y,W,H,L) {
59 box(FL_FLAT_BOX);
60 color(BG_COLOR);
61 Fl::add_timeout(0.25, Timer_CB, (void*)this);
62 }
63 };
64 // MAIN
65 int main(int argc, char* argv[])
66 {
67 Fl_Double_Window win(220, 220);
68 MyTimer tim(10, 10, win.w()-20, win.h()-20);
69 win.show();
70 return(Fl::run());
71 }
72
1C:Documents and SettingsAdministradorDesktop....OpenGL_Exercicio.Exemplo.03opengl.exemplo.cxx
/*
* opengl.exemplo.cxx
* Simple resizable 2D GL window
*/
#include <FL/gl.h>
#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.H>
class MyGlWindow : public Fl_Gl_Window
{
// DRAW METHOD
// OpenGL window: (w,h) is upper right, (-w,-h) is lower left, (0,0) is center
//
void draw()
{
// First time? init viewport, etc.
if (!valid()) {
valid(1);
glLoadIdentity();
glViewport(0,0,w(),h());
glOrtho(-w(),w(),-h(),h(),-1,1);
}
// Clear screen
glClear(GL_COLOR_BUFFER_BIT);
// Draw white 'X'
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_STRIP); glVertex2f(w(), h()); glVertex2f(-w(),-h()); glEnd();
glBegin(GL_LINE_STRIP); glVertex2f(w(),-h()); glVertex2f(-w(), h()); glEnd();
}
// HANDLE WINDOW RESIZING
// If window reshaped, need to readjust viewport/ortho
//
void resize(int X,int Y,int W,int H)
{
Fl_Gl_Window::resize(X,Y,W,H);
glLoadIdentity();
glViewport(0,0,W,H);
glOrtho(-W,W,-H,H,-1,1);
redraw();
}
public:
// CONSTRUCTOR
MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L)
{
}
};
// MAIN
int main(int argc, char* argv[])
{
Fl_Window win(500, 300);
MyGlWindow mygl(10, 10, win.w()-20, win.h()-20);
win.end();
win.resizable(mygl);
win.show();
return(Fl::run());
}
1C:Documents and SettingsAdministradorDesktop...AND.Using.OpenGL_Exercicio.Exemplo.04sphere.cxx
/*
* sphere.cxx
* Render a simple opengl shaded sphere with a single side light
* NOTE: Glut needed *only* for glutSolidSphere()
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <math.h>
#include <FL/Fl.h>
#include <FL/gl.h>
#include <FL/glut.h>
#include <FL/Fl_Window.h>
#include <FL/Fl_Gl_Window.h>
class MyGlWindow : public Fl_Gl_Window
{
public:
// RESHAPE THE VIEWPORT
void Reshape(GLfloat W, GLfloat H)
{
// (REFERENCE: SGI light.c DEMO)
GLfloat ratio = W / H;
glViewport(0, 0, (GLsizei)W, (GLsizei)H);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5*ratio, 1.5*ratio, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Desenhando a cena ...
void draw()
{
if (!valid())
{
valid(1);
Reshape(w(), h());
// (REFERENCE: SGI 'light.c' EXAMPLE)
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA
GLfloat light_position[] = { 5.0, 5.0, 0.0, 0.0 }; // XYZ
glClearColor(0.0, 0.0, 0.4, 0.0); // bg color
glShadeModel(GL_SMOOTH);
//
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 20.0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
//
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glutSolidSphere(0.5, 30, 30);
glPopMatrix();
}
//Constructor ...
MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L)
{
}
};
/*Funcao principal*/
int main(int argc, char* argv[])
{
Fl_Window win(640, 480, "sphere");
MyGlWindow mygl(10, 10, win.w()-20, win.h()-20);
win.resizable(&mygl);
win.show();
return(Fl::run());
}
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
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();
}
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 }
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
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
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
1 /**
2 * Programa exemplo utilizando OpenGL + GLUT
3 * Parametros de linkagem : -lGL -lglut
4 */
5
6 #include <GL/glut.h>
7
8 void initFunc(void)
9 {
10 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
11 glClearDepth(1.0f);
12 glEnable(GL_DEPTH_TEST);
13 glDepthFunc(GL_LEQUAL);
14 }
15
16 void reshapeFunc(int width, int height)
17 {
18 float aspect = 0.0f;
19 glViewport(0, 0, width, height);
20
21 glMatrixMode(GL_PROJECTION);
22 glLoadIdentity();
23
24 height = (height == 0 ? 1 : height);
25 aspect = (float) width / (float) height;
26
27 gluPerspective (45.0f, aspect, 0.01f, 100.0f);
28
29 glMatrixMode(GL_MODELVIEW);
30 glLoadIdentity();
31 }
32
33 void displayFunc(void)
34 {
35 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
36 glLoadIdentity();
37
38 gluLookAt(2.0f, 2.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
39
40 glLineWidth(2.0f);
41 glColor3f(0.15f, 0.45f, 0.15f);
42 glutWireCube(1.0f);
43 glColor3f(0.5f, 1.0f, 0.5f);
44 glutSolidCube(1.0f);
45
46 glutSwapBuffers();
47 }
48
49 int main(int argc, char* argv[])
50 {
51 glutInit(&argc, argv);
52 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
53 glutInitWindowSize(800, 600);
54 glutInitWindowPosition(50, 100);
55 glutCreateWindow("OpenGL Cube");
56 glutReshapeFunc(reshapeFunc);
57 glutDisplayFunc(displayFunc);
58 glutIdleFunc(displayFunc);
59 initFunc();
60 glutMainLoop();
61 return 0;
62 }
63
1C:Documents and SettingsAdministradorDesktop_exemplos.openglMySecondGlutCube.cxx
//#include <GL/glut.h>
#include "glut.h"
#include <cstdlib>
/*Estrutura que compoe o material do cubo e os efeitos de iluminacao*/
typedef struct {float ambiente[4]; float diffuse[4]; float specular[4];
float shininess[1]; } Material;
Material brass={{0.33f, 0.22f, 0.03f, 1}, {0.78f, 0.57f, 0.11f, 1},
{0.99f, 0.91f, 0.81f, 1}, {27.8f}};
void setMaterial(Material* p)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, p->ambiente);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, p->diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, p->specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, p->shininess);
}
void displayCall(void)
{
glClearColor(0.4f,0.4f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
setMaterial(&brass);
glutSolidCube(1);
glFlush();
} /* Callback de REDISPLAY */
void reshapeCall(int width, int height)
{
glViewport(0, 0, (GLsizei)(width), (GLsizei)(height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,4./3,0.5,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.5,1.5,1.2, 0,0,0, 0,0,1);
} /* Callback de RESHAPE */
void keyboardCall(unsigned char key, int x, int y)
{
switch (key) { case 27: exit(EXIT_SUCCESS); }
}/* Callback de KEYBOARD */
void initLight( )
{
float position[]={ 0.5f,2.f,0.f,1.f};
float low[]={0.2f,0.2f,0.2f,1};
float white[]={1,1,1,1};
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0, GL_AMBIENT, low);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); /* Inicializando a GLUT */
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("FCG:Simple Light");
glutDisplayFunc(displayCall);
glutReshapeFunc(reshapeCall);
glutKeyboardFunc(keyboardCall);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
initLight(); /* Inicializando a luz e o material */
glutMainLoop(); /* GLUT main loop */
return 0;
}

More Related Content

DOCX
Travel management
PPTX
New presentation oop
PDF
C++ Programming - 11th Study
DOCX
Programa en C++ ( escriba 3 números y diga cual es el mayor))
PDF
C++ programs
PDF
OpenGL L06-Performance
PDF
Intro to OpenGL ES 2.0
TXT
Railwaynew
Travel management
New presentation oop
C++ Programming - 11th Study
Programa en C++ ( escriba 3 números y diga cual es el mayor))
C++ programs
OpenGL L06-Performance
Intro to OpenGL ES 2.0
Railwaynew

What's hot (20)

PDF
systems programming lab programs in c
PDF
Data Structure - 2nd Study
PDF
C++ TUTORIAL 1
DOCX
Basic Programs of C++
DOCX
Opp compile
PDF
OpenGL Starter L02
PDF
C++ TUTORIAL 10
PPTX
Roslyn: el futuro de C#
PDF
C++ TUTORIAL 9
PDF
C lab programs
DOCX
Assignement of programming & problem solving u.s ass.(1)
PDF
Pattern printing-in-c(Jaydip Kikani)
PDF
C++ Programming - 4th Study
PPTX
Basic C++ 11/14 for Python Programmers
PDF
OpenGL Starter L01
PPT
Cquestions
TXT
Keypad program
PDF
C++ TUTORIAL 4
PDF
Understanding storage class using nm
systems programming lab programs in c
Data Structure - 2nd Study
C++ TUTORIAL 1
Basic Programs of C++
Opp compile
OpenGL Starter L02
C++ TUTORIAL 10
Roslyn: el futuro de C#
C++ TUTORIAL 9
C lab programs
Assignement of programming & problem solving u.s ass.(1)
Pattern printing-in-c(Jaydip Kikani)
C++ Programming - 4th Study
Basic C++ 11/14 for Python Programmers
OpenGL Starter L01
Cquestions
Keypad program
C++ TUTORIAL 4
Understanding storage class using nm
Ad

Similar to FLTK Summer Course - Part VIII - Eighth Impact - Exercises (20)

PDF
Tutorial Open GL (Listing Code)
DOCX
Computer Graphics and Multimedia lab report
DOCX
computer graphics at openGL
PPT
opengl.ppt
PPT
Intro to Computer Graphics.ppt
DOCX
square.cpp Open
PPT
01.Opengl_intro-2.ppt
DOCX
computer graphics opengl programs for cse students
DOCX
Programa de objetos 3 d wire
DOCX
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
DOCX
computer graphics at openGL (2)
DOCX
Rotate an object in open gl(glut) sample example with source
PDF
2D Drawing
TXT
Ass day3 1_bd flag
PPTX
Open gl polygon code review
TXT
Ass day2 2_rotating my name (robi)
PDF
The Ring programming language version 1.5.3 book - Part 65 of 184
PPTX
Robot In OpenGL Using Line Function
PPTX
Robot In OpenGL Using Line Function
PDF
The Ring programming language version 1.9 book - Part 65 of 210
Tutorial Open GL (Listing Code)
Computer Graphics and Multimedia lab report
computer graphics at openGL
opengl.ppt
Intro to Computer Graphics.ppt
square.cpp Open
01.Opengl_intro-2.ppt
computer graphics opengl programs for cse students
Programa de objetos 3 d wire
#includefloat angle, move, scene, roadmove,turn, on=1; int i, st.docx
computer graphics at openGL (2)
Rotate an object in open gl(glut) sample example with source
2D Drawing
Ass day3 1_bd flag
Open gl polygon code review
Ass day2 2_rotating my name (robi)
The Ring programming language version 1.5.3 book - Part 65 of 184
Robot In OpenGL Using Line Function
Robot In OpenGL Using Line Function
The Ring programming language version 1.9 book - Part 65 of 210
Ad

More from Michel Alves (20)

PDF
Texture Synthesis: An Approach Based on GPU Use
PDF
Intelligent Transfer of Thematic Harmonic Color Palettes
PDF
A Framework for Harmonic Color Measures
PDF
Effectiveness of Image Quality Assessment Indexes
PDF
Introduction to Kernel Functions
PDF
About Perception and Hue Histograms in HSV Space
PDF
Color Harmonization - Results
PDF
Wave Simulation Using Perlin Noise
PDF
Similarity Maps Using SSIM Index
PDF
Qualifying Exam - Image-Based Reconstruction With Color Harmonization
PDF
TMS - Schedule of Presentations and Reports
PDF
Month Presentations Schedule - March/2015 - LCG/UFRJ
PDF
Color Palettes in R
PDF
Sigmoid Curve Erf
PDF
Hue Wheel Prototype
PDF
Cosine Curve
PDF
Triangle Mesh Plot
PDF
Triangle Plot
PDF
Capacity-Constrained Point Distributions :: Video Slides
PDF
Capacity-Constrained Point Distributions :: Density Function Catalog
Texture Synthesis: An Approach Based on GPU Use
Intelligent Transfer of Thematic Harmonic Color Palettes
A Framework for Harmonic Color Measures
Effectiveness of Image Quality Assessment Indexes
Introduction to Kernel Functions
About Perception and Hue Histograms in HSV Space
Color Harmonization - Results
Wave Simulation Using Perlin Noise
Similarity Maps Using SSIM Index
Qualifying Exam - Image-Based Reconstruction With Color Harmonization
TMS - Schedule of Presentations and Reports
Month Presentations Schedule - March/2015 - LCG/UFRJ
Color Palettes in R
Sigmoid Curve Erf
Hue Wheel Prototype
Cosine Curve
Triangle Mesh Plot
Triangle Plot
Capacity-Constrained Point Distributions :: Video Slides
Capacity-Constrained Point Distributions :: Density Function Catalog

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PDF
Insiders guide to clinical Medicine.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
Pre independence Education in Inndia.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
Insiders guide to clinical Medicine.pdf
Microbial disease of the cardiovascular and lymphatic systems
Microbial diseases, their pathogenesis and prophylaxis
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
Pre independence Education in Inndia.pdf

FLTK Summer Course - Part VIII - Eighth Impact - Exercises

  • 1. 1 // DEMOSTRAÇÃO DE COMO CRIAR UM 'X' EM OPENGL 2 #include <FL/Fl.H> 3 #include <FL/fl_draw.H> 4 #include <FL/Fl_Double_Window.H> 5 6 // SIMPLE BOX WIDGET THAT DRAWS AN 'X' 7 class DrawX : public Fl_Widget { 8 public: 9 DrawX(int X, int Y, int W, int H, const char*L=0) : Fl_Widget(X,Y,W,H,L) 10 { 11 } 12 void draw(void) 13 { 14 // DRAW BLACK 'X' 15 fl_color(FL_BLACK); 16 fl_line(x(), y(), x()+w()-1, y()+h()-1); 17 fl_line(x(), y()+h()-1, x()+w()-1, y()); 18 } 19 }; 20 21 int main(int argc, char* argv[] ) 22 { 23 Fl_Double_Window win(200,200); 24 DrawX draw_x(0, 0, win.w(), win.h()); 25 win.resizable(draw_x); 26 win.show(); 27 return(Fl::run()); 28 } 29 30
  • 2. 1 /* 2 * timer.cxx 3 * Exemplo das propriedades de desenho do FLTK 4 */ 5 6 // 7 // FLTK drawing example showing simple line drawing animation 8 // erco 03/22/07 9 // 10 #include <FL/Fl.H> 11 #include <FL/Fl_Double_Window.H> 12 #include <FL/Fl_Box.H> 13 #include <FL/fl_draw.h> 14 #include <math.h> 15 #include <stdio.h> 16 #include <time.h> 17 #define BG_COLOR 45 18 #define TICK_COLOR 50 19 #define CIRC_COLOR 0 20 class MyTimer : public Fl_Box 21 { 22 void draw() 23 { 24 // COMPUTE NEW COORDS OF LINE 25 static long start = time(NULL); 26 long tick = time(NULL) - start; 27 char secs[80]; sprintf(secs, "%02ld:%02ld", tick/60, tick%60); 28 float pi = 3.14 - (((tick % 60) / 60.0) * 6.28); 29 int radius = h() / 2; 30 int x1 = (int)(x() + w()/2), 31 y1 = (int)(y() + h()/2), 32 x2 = (int)(x1 + (sin(pi) * radius)), 33 y2 = (int)(y1 + (cos(pi) * radius)); 34 35 // TELL BASE WIDGET TO DRAW ITS BACKGROUND 36 Fl_Box::draw(); 37 38 // DRAW 'SECOND HAND' OVER WIDGET'S BACKGROUND 39 fl_color(TICK_COLOR); 40 fl_line(x1, y1, x2, y2); 41 fl_color(CIRC_COLOR); 42 fl_pie(x1-10, y1-10, 20, 20, 0.0, 360.0); 43 44 // DRAW TIMER TEXT STRING 45 fl_color(TICK_COLOR); 46 fl_font(FL_HELVETICA,16); 47 fl_draw(secs, x()+4, y()+h()-4); 48 } 49 50 static void Timer_CB(void *userdata) 51 { 52 MyTimer *o = (MyTimer*)userdata; 53 o->redraw(); 54 Fl::repeat_timeout(0.25, Timer_CB, userdata); 55 } 56 public: 57 // CONSTRUCTOR 58 MyTimer(int X,int Y,int W,int H,const char*L=0) : Fl_Box(X,Y,W,H,L) { 59 box(FL_FLAT_BOX); 60 color(BG_COLOR); 61 Fl::add_timeout(0.25, Timer_CB, (void*)this); 62 } 63 }; 64 // MAIN 65 int main(int argc, char* argv[]) 66 { 67 Fl_Double_Window win(220, 220); 68 MyTimer tim(10, 10, win.w()-20, win.h()-20); 69 win.show(); 70 return(Fl::run()); 71 } 72
  • 3. 1C:Documents and SettingsAdministradorDesktop....OpenGL_Exercicio.Exemplo.03opengl.exemplo.cxx /* * opengl.exemplo.cxx * Simple resizable 2D GL window */ #include <FL/gl.h> #include <FL/Fl.H> #include <FL/Fl_Gl_Window.H> class MyGlWindow : public Fl_Gl_Window { // DRAW METHOD // OpenGL window: (w,h) is upper right, (-w,-h) is lower left, (0,0) is center // void draw() { // First time? init viewport, etc. if (!valid()) { valid(1); glLoadIdentity(); glViewport(0,0,w(),h()); glOrtho(-w(),w(),-h(),h(),-1,1); } // Clear screen glClear(GL_COLOR_BUFFER_BIT); // Draw white 'X' glColor3f(1.0, 1.0, 0.0); glBegin(GL_LINE_STRIP); glVertex2f(w(), h()); glVertex2f(-w(),-h()); glEnd(); glBegin(GL_LINE_STRIP); glVertex2f(w(),-h()); glVertex2f(-w(), h()); glEnd(); } // HANDLE WINDOW RESIZING // If window reshaped, need to readjust viewport/ortho // void resize(int X,int Y,int W,int H) { Fl_Gl_Window::resize(X,Y,W,H); glLoadIdentity(); glViewport(0,0,W,H); glOrtho(-W,W,-H,H,-1,1); redraw(); } public: // CONSTRUCTOR MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L) { } }; // MAIN int main(int argc, char* argv[]) { Fl_Window win(500, 300); MyGlWindow mygl(10, 10, win.w()-20, win.h()-20); win.end(); win.resizable(mygl); win.show(); return(Fl::run()); }
  • 4. 1C:Documents and SettingsAdministradorDesktop...AND.Using.OpenGL_Exercicio.Exemplo.04sphere.cxx /* * sphere.cxx * Render a simple opengl shaded sphere with a single side light * NOTE: Glut needed *only* for glutSolidSphere() */ #ifdef _WIN32 #include <windows.h> #endif #include <math.h> #include <FL/Fl.h> #include <FL/gl.h> #include <FL/glut.h> #include <FL/Fl_Window.h> #include <FL/Fl_Gl_Window.h> class MyGlWindow : public Fl_Gl_Window { public: // RESHAPE THE VIEWPORT void Reshape(GLfloat W, GLfloat H) { // (REFERENCE: SGI light.c DEMO) GLfloat ratio = W / H; glViewport(0, 0, (GLsizei)W, (GLsizei)H); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.5*ratio, 1.5*ratio, -1.5, 1.5, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } // Desenhando a cena ... void draw() { if (!valid()) { valid(1); Reshape(w(), h()); // (REFERENCE: SGI 'light.c' EXAMPLE) GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; // RGBA GLfloat light_position[] = { 5.0, 5.0, 0.0, 0.0 }; // XYZ glClearColor(0.0, 0.0, 0.4, 0.0); // bg color glShadeModel(GL_SMOOTH); // glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialf(GL_FRONT, GL_SHININESS, 20.0); glLightfv(GL_LIGHT0, GL_POSITION, light_position); // glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glColor3f(0.5, 0.5, 0.5); glutSolidSphere(0.5, 30, 30); glPopMatrix(); } //Constructor ... MyGlWindow(int X,int Y,int W,int H,const char*L=0) : Fl_Gl_Window(X,Y,W,H,L) { } }; /*Funcao principal*/ int main(int argc, char* argv[]) { Fl_Window win(640, 480, "sphere"); MyGlWindow mygl(10, 10, win.w()-20, win.h()-20); win.resizable(&mygl); win.show(); return(Fl::run()); }
  • 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
  • 11. 1 /** 2 * Programa exemplo utilizando OpenGL + GLUT 3 * Parametros de linkagem : -lGL -lglut 4 */ 5 6 #include <GL/glut.h> 7 8 void initFunc(void) 9 { 10 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 11 glClearDepth(1.0f); 12 glEnable(GL_DEPTH_TEST); 13 glDepthFunc(GL_LEQUAL); 14 } 15 16 void reshapeFunc(int width, int height) 17 { 18 float aspect = 0.0f; 19 glViewport(0, 0, width, height); 20 21 glMatrixMode(GL_PROJECTION); 22 glLoadIdentity(); 23 24 height = (height == 0 ? 1 : height); 25 aspect = (float) width / (float) height; 26 27 gluPerspective (45.0f, aspect, 0.01f, 100.0f); 28 29 glMatrixMode(GL_MODELVIEW); 30 glLoadIdentity(); 31 } 32 33 void displayFunc(void) 34 { 35 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 36 glLoadIdentity(); 37 38 gluLookAt(2.0f, 2.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 39 40 glLineWidth(2.0f); 41 glColor3f(0.15f, 0.45f, 0.15f); 42 glutWireCube(1.0f); 43 glColor3f(0.5f, 1.0f, 0.5f); 44 glutSolidCube(1.0f); 45 46 glutSwapBuffers(); 47 } 48 49 int main(int argc, char* argv[]) 50 { 51 glutInit(&argc, argv); 52 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); 53 glutInitWindowSize(800, 600); 54 glutInitWindowPosition(50, 100); 55 glutCreateWindow("OpenGL Cube"); 56 glutReshapeFunc(reshapeFunc); 57 glutDisplayFunc(displayFunc); 58 glutIdleFunc(displayFunc); 59 initFunc(); 60 glutMainLoop(); 61 return 0; 62 } 63
  • 12. 1C:Documents and SettingsAdministradorDesktop_exemplos.openglMySecondGlutCube.cxx //#include <GL/glut.h> #include "glut.h" #include <cstdlib> /*Estrutura que compoe o material do cubo e os efeitos de iluminacao*/ typedef struct {float ambiente[4]; float diffuse[4]; float specular[4]; float shininess[1]; } Material; Material brass={{0.33f, 0.22f, 0.03f, 1}, {0.78f, 0.57f, 0.11f, 1}, {0.99f, 0.91f, 0.81f, 1}, {27.8f}}; void setMaterial(Material* p) { glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, p->ambiente); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, p->diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, p->specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, p->shininess); } void displayCall(void) { glClearColor(0.4f,0.4f,0.4f,1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); setMaterial(&brass); glutSolidCube(1); glFlush(); } /* Callback de REDISPLAY */ void reshapeCall(int width, int height) { glViewport(0, 0, (GLsizei)(width), (GLsizei)(height)); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,4./3,0.5,10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.5,1.5,1.2, 0,0,0, 0,0,1); } /* Callback de RESHAPE */ void keyboardCall(unsigned char key, int x, int y) { switch (key) { case 27: exit(EXIT_SUCCESS); } }/* Callback de KEYBOARD */ void initLight( ) { float position[]={ 0.5f,2.f,0.f,1.f}; float low[]={0.2f,0.2f,0.2f,1}; float white[]={1,1,1,1}; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightfv(GL_LIGHT0, GL_AMBIENT, low); glLightfv(GL_LIGHT0, GL_DIFFUSE, white); glLightfv(GL_LIGHT0, GL_SPECULAR, white); glLightfv(GL_LIGHT0, GL_POSITION, position); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } int main(int argc, char **argv) { glutInit(&argc, argv); /* Inicializando a GLUT */ glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(640, 480); glutCreateWindow("FCG:Simple Light"); glutDisplayFunc(displayCall); glutReshapeFunc(reshapeCall); glutKeyboardFunc(keyboardCall); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); initLight(); /* Inicializando a luz e o material */ glutMainLoop(); /* GLUT main loop */ return 0; }