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_Wi...
1 /*
2 * timer.cxx
3 * Exemplo das propriedades de desenho do FLTK
4 */
5
6 //
7 // FLTK drawing example showing simple li...
1C:Documents and SettingsAdministradorDesktop....OpenGL_Exercicio.Exemplo.03opengl.exemplo.cxx
/*
* opengl.exemplo.cxx
* S...
1C:Documents and SettingsAdministradorDesktop...AND.Using.OpenGL_Exercicio.Exemplo.04sphere.cxx
/*
* sphere.cxx
* Render a...
1 /**
2 * Um primeiro exemplo utilizando OpenGL + GLUT
3 **/
4
5 //Inclusão da biblioteca GLUT
6 #include <glut.h>
7
8 /*F...
1C:Documents and SettingsAdministradorDesktop_exemplos.openglMyShapePolygonWindow.cxx
//
// Tiny OpenGL demo program for t...
1 /**
2 * Um programa OpenGL simples que desenha um quadrado em uma janela GLUT.
3 */
4 #include <glut.h>
5
6 // Função ca...
1 /**
2 * Exemplo de programa usando OpenGL + GLUT.
3 **/
4 #include <cstdlib>
5 #include <glut.h>
6
7 //Função callback d...
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...
73 // a child window:
74 shape_window sw(10, 10, 280, 280);
75 // make it resize:
76 window.resizable(&sw);
77 // window.s...
1 /**
2 * Programa exemplo utilizando OpenGL + GLUT
3 * Parametros de linkagem : -lGL -lglut
4 */
5
6 #include <GL/glut.h>...
1C:Documents and SettingsAdministradorDesktop_exemplos.openglMySecondGlutCube.cxx
//#include <GL/glut.h>
#include "glut.h"...
Upcoming SlideShare
Loading in …5
×

FLTK Summer Course - Part VIII - Eighth Impact - Exercises

605 views

Published on

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.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
605
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

FLTK Summer Course - Part VIII - Eighth Impact - Exercises

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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; }

×