Allink  v0.1
Draw.cpp
1 /***********************************************************************
2 Draw: Creator and initialisation of the graphical framework
3 Copyright (C) 2008-2010 by Giovanni Marelli <sabeiro@virgilio.it>
4 
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; If not, see <http://www.gnu.org/licenses/>.
17 ***********************************************************************/
18 #include "../../include/Draw.h"
19 
20 #ifdef __glut_h__
21 
22 Draw::Draw(){
23  info = (char *)malloc(256*sizeof(char));
24  Number = (char *)malloc(100*sizeof(char));
25  InvScaleUn = 1.;
26  for(int d=0;d<3;d++){
27  Edge[d] = 1.;
28  }
29  ImHeight = 800;
30  ImWidth = 800;
31  pixel = NULL;
32  pixel = (GLubyte*)malloc(sizeof(GLubyte));
33  NLevel = 4;
34  InitConstant();
35  xRem = 0;yRem=0;
36  IncrVisDxSx = 2.;
37  IncrVisSuGiu = 2.;
38 }
39 void Draw::Shout(const char * s, ...)
40 {
41 #ifdef DRAW_DEBUG
42  va_list args;
43  va_start(args, s);
44  vfprintf(stderr, s, args);
45  fprintf(stderr, "\n");
46  va_end(args);
47 #else
48  return;
49 #endif
50 }
51 // #include <SDL.h>
52 // #include "SDL/SDL_opengl.h"
53 // int Draw::WindowSDL(){
54 // int ret = SDL_Init(SDL_INIT_EVERYTHING);
55 // if( ret < 0 ) return false;
56 // //ret = SDL_SetVideoMode(gScreenWidth, gScreenHeight, 0, SDL_OPENGL | SDL_RESIZABLE | SDL_ASYNCBLIT | SDL_HWSURFACE | SDL_ANYFORMAT);
57 // SDL_Surface *screen;
58 // screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL);
59 // if( screen == NULL ) return false;
60 // SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
61 // SDL_MapRGB(main_surface->format, 255, 255, 255);
62 // InitConstant();
63 // Illuminazione();
64 // if(ne) Nebbia();
65 // if(sp) Spot();
66 // ParticleList();
67 // ReadScript();
68 // ChooseBlend(1);
69 // SDL_WM_SetCaption("Disegna",NULL);
70 
71 // SDL_Event event;
72 
73 // SDL_WaitEvent(&event);
74 
75 // switch (event.type) {
76 // case SDL_KEYDOWN:
77 // printf("Il tasto %s e' stato premuto!\n",
78 // SDL_GetKeyName(event.key.keysym.sym));
79 // break;
80 // case SDL_QUIT:
81 // exit(0);
82 // }
83 // {
84 // SDL_Event event;
85 
86 // while ( SDL_PollEvent(&event) ) {
87 // switch (event.type) {
88 // case SDL_MOUSEMOTION:
89 // printf("Il Mouse e' stato mosso da %d,%d " \
90 // "a (%d,%d)\n",
91 // event.motion.xrel, event.motion.yrel,
92 // event.motion.x, event.motion.y);
93 // break;
94 // case SDL_MOUSEBUTTONDOWN:
95 // printf("Il pulsante %d del Mouse %d e' stato " \
96 // "premuto a (%d,%d)\n",
97 // event.button.button, event.button.x,
98 // event.button.y);
99 // break;
100 // case SDL_QUIT:
101 // exit(0);
102 // }
103 // }
104 // }
105 
106 // return true;
107 // }
108 void Draw::Window(int argc,char **argv){
109  glutInit(&argc,argv);
110  glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
111  glutInitWindowSize(ImWidth,ImHeight);//Dimensione finestra
112  //glutInitWindowPosition(100,300);//Posizione finestra
113  MainWindow = glutCreateWindow("Disegna");//Titolo
114  // sprintf(info,"There are %d particle with %d different types",Gen1->NPart,Gen1->NType);
115  InitConstant();
116  ReadConf();
117  Lista(Values);
118  // SubWindow1 = glutCreateSubWindow(MainWindow, 0, 100,
119  // (width), (height-100));
120  ParticleList();
121  ReadScript();
122  // cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << endl;
123  // cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << endl;
124  // cout << "GL_VERSION : " << glGetString(GL_VERSION) << endl;
125  // //cout << "GL_EXTENSION : " << glGetString(GL_EXTENSIONS) << endl;
126  glutTimerFunc(0,Timer,0);// tempo tra due diapositive
127  glutDisplayFunc(Figure);//Disegna la figura
128  glutReshapeFunc(reshape);
129  //glutReshapeFunc(ChangeSize);//Abilita la ridimensione per mouse
130  glutMouseFunc(mouse);
131  glutKeyboardFunc(keyboard);
132  glutMotionFunc( MouseMove );
133  glutSpecialFunc(special);
134  ChooseBlend(1);
135  Illuminazione();
136  if(ne) Nebbia();
137  if(sp) Spot();
138  //glutMainLoop();//Mantiene la finestra
139  // SubWindow2 = glutCreateSubWindow(MainWindow, 0, 0,
140  // (width), 100);
141  // gl
142  glClearColor(.0,.0,.0,.0);//colore sfondo
143 }
144 void Draw::DTimer(int v){
145  glutPostRedisplay();
146  glutTimerFunc(30,Timer, 0);
147 }
148 // void Draw::Illuminazione(){
149 
150 // }
151 void Draw::Illuminazione(void){
152  //------------------Depth------------------
153  glEnable(GL_DEPTH_TEST);
154  glDepthFunc(GL_LESS);
155  //glDepthFunc(GL_LEQUAL);
156  glClearDepth( 1.0 );
157  //glClearDepth(1.0);
158  //glDepthMask(GL_TRUE); // make Z-buffer writable
159  // glDepthMask(GL_FALSE);
160  // glDepthRange(0,-10.);
161  //----------------Blend---------------------------
162  glEnable(GL_BLEND);
163  glShadeModel(GL_SMOOTH);
164  glEnable(GL_POINT_SMOOTH);
165  glEnable(GL_LINE_SMOOTH);
166  glEnable(GL_ALPHA_TEST);
167  glAlphaFunc(GL_GREATER,0.1);
168  //------------Miscellaneus--------------------------
169  //glEnable(GL_STENCIL_TEST); /// Enable stencil test
170  //glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); ///
171  //glStencilFunc(GL_ALWAYS, uniqueStencilValue, ~0);
172  //glEnable(GL_TEXTURE_2D);
173  glReadBuffer(GL_FRONT);/* so glReadPixel() always get the right image */
174  //glEnable(GL_NORMALIZE);
175  //---------------Material----------------------
176  // glEnable(GL_COLOR_MATERIAL);// have materials set by curr color
177  //glEnable(GL_CULL_FACE);
178  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);//GL_LINE
179  glCullFace(GL_FRONT_AND_BACK);
180  glFrontFace(GL_CCW);//Senso antiorario
181  //glEnableClientState(GL_VERTEX_ARRAY);
182  //glEnableClientState(GL_NORMAL_ARRAY);
183  //glEnable(GL_VERTEX_ARRAY);
184  GLfloat ambientMaterial[4] = { .5, .5, .5,1.};
185  GLfloat diffuseMaterial[4] = { .0, .0, .0,1.};
186  GLfloat specularMaterial[4] = { .0, .0, .0,1.0};
187  GLfloat shininessMaterial = 25.0;//50.0;
188  GLfloat emissionMaterial[4] = { .0, .0, .0,1.};
189  GLfloat riflettivitaSpeculare[]= { 0.0, .0, .0,1.};
190  //glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
191  //glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
192  //glColorMaterial(GL_FRONT_AND_BACK,GL_EMISSION);
193  //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
194  //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
195  //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
196  //glDisable(GL_POLYGON_SMOOTH);// make sure not to antialias polygons
197  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,DrAmbientWhite);
198  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,DrDiffuseWhite);
199  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,DrSpecularWhite);
200  glMaterialf(GL_FRONT,GL_SHININESS,shininessMaterial);
201  glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emissionMaterial);
202  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,riflettivitaSpeculare);
203  glMaterialfv(GL_BACK, GL_AMBIENT, DrAmbientGreen);
204  glMaterialfv(GL_BACK, GL_DIFFUSE, DrDiffuseGreen);
205  glMaterialfv(GL_FRONT, GL_AMBIENT, DrAmbientBlue);
206  glMaterialfv(GL_FRONT, GL_DIFFUSE, DrDiffuseBlue);
207  glMaterialfv(GL_FRONT, GL_SPECULAR, DrSpecularWhite);
208  glMaterialf( GL_FRONT, GL_SHININESS, 25.0);
209  //--------------------Light--------------------------
210  GLfloat DrPropertiesAmbient [] = {0.50, 0.50, 0.50, 1.00};
211  GLfloat DrPropertiesDiffuse [] = {0.75, 0.75, 0.75, 1.00};
212  GLfloat DrPropertiesSpecular[] = {1.00, 1.00, 1.00, 1.00};
213 
214  glEnable(GL_LIGHTING);
215 
216  //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
217  glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1.0);
218  glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
219 
220  glEnable( GL_LIGHT0 );
221  glLightfv( GL_LIGHT0, GL_AMBIENT, DrPropertiesAmbient);
222  glLightfv( GL_LIGHT0, GL_DIFFUSE, DrPropertiesDiffuse);
223  glLightfv( GL_LIGHT0, GL_SPECULAR, DrPropertiesSpecular);
224  GLfloat LightPos0[]={xl0,yl0,zl0, 0.0f };
225  GLfloat LightPos1[]={xl1,yl1,zl1, 0.0f };
226  glLightfv(GL_LIGHT0, GL_POSITION,LightPos0);
227  glEnable(GL_LIGHT1);
228  glLightfv(GL_LIGHT1, GL_AMBIENT, DrPropertiesAmbient);
229  glLightfv(GL_LIGHT1, GL_DIFFUSE, DrPropertiesDiffuse);
230  glLightfv(GL_LIGHT1, GL_POSITION,LightPos1);
231  glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
232  glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
233  glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
234  glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
235  glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
236 }
237 void Draw::Dreshape(int w,int h){
238  glViewport(0,0,(GLsizei) w, (GLsizei) h);
239  WinWidth = (int) w;
240  WinHeight = (int) h;
241  XCenter = WinWidth / 2;
242  YCenter = WinHeight / 2;
243  glMatrixMode(GL_PROJECTION);
244  glLoadIdentity();
245  if(pr == 1){
246  //glLoadIdentity();
247  gluPerspective(30,(GLfloat) w/(GLfloat) h,.1,200.);//ang,rapp,zmin,zmax
248  glFrustum(-1.,1.,-Edge[1]*InvScaleUn,Edge[1]*InvScaleUn,-Edge[2]*InvScaleUn,Edge[2]*InvScaleUn);//Sx,Dx,giu,su,vicino,lontano//Dimensione massima dell'immagine
249  //glTranslatef(0.,0.,-2.);
250  }
251  if(pr == 0){
252  //glOrtho(-1.,1.,(float)(w-1),(float)(h-1),.1,-200.);//Sx,Dx,giu,su,vicino,lontano
253  // glTranslatef(0.,0.,1.0);
254  glOrtho(-1.,1.,-WinHeight/(float)WinWidth,WinHeight/(float)WinWidth,.1,20);//Sx,Dx,giu,su,vicino,lontano
255  glEnable(GL_DEPTH_TEST);
256  }
257  glMatrixMode(GL_MODELVIEW);
258  //gluLookAt( -1., -1., -1.,.0, .0, .0,1., 1., 1.);//osservatore,centro,orientazion
259  glLoadIdentity();
260  gluLookAt( 0., 0.,1.5/* 1.5*Edge[2]*InvScaleUn*/,
261  0., 0., 0.,
262  0., 1., 0.);//osservatore,centro,orientazion
263  // glLoadIdentity();
264  //glFrustum(-3,3.,-3.,3.,-3.,3.);//Sx,Dx,giu,su,vicino,lontano//Dimensione massima dell'immagine
265  //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations
266 }
267 void Draw::Spot(void){
268  GLfloat direzioneSpot[3] = { .0, 1.0, 1.0};
269  GLfloat coloreNebbia[] = { 0.2, 0.2, 0.2,1.};
270  GLfloat esponenteSpot = 1.5;
271  GLfloat angoloSpot = 120.;
272  glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,angoloSpot);
273  glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,direzioneSpot);
274  glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,esponenteSpot);
275 }
276 void Draw::Nebbia(void){
277  GLfloat zIniNebbia = 10.;
278  GLfloat zFinNebbia = -10.;
279  GLfloat DensitaNebbia = 1.;
280  glEnable(GL_FOG);
281  glFogf(GL_FOG_START,zIniNebbia);
282  glFogf(GL_FOG_END,zFinNebbia);
283  glFogi(GL_FOG_MODE,GL_EXP);
284  glFogf(GL_FOG_DENSITY,DensitaNebbia);
285 }
286 void Draw::ChangeSize(GLsizei w,GLsizei h){
287  GLfloat aspectRatio;
288  WinWidth = (int) w;
289  WinHeight = (int) h;
290  if(h==0)h=1;
291  aspectRatio=(GLfloat)w/(GLfloat)h;
292  glViewport(0,0,w,h);
293  glMatrixMode(GL_PROJECTION);
294  glLoadIdentity();
295  if(w<=h)
296  glOrtho(-100.,100.,-100./aspectRatio,100.
297  /aspectRatio,1.,-1.);
298  else
299  glOrtho(-100*aspectRatio,-100*aspectRatio,
300  -100,100,1.,-1.);
301  glMatrixMode(GL_MODELVIEW);
302  glLoadIdentity();
303 }
304 void Draw::Numera(double *Pos,int n){
305  char *Number = (char *)calloc(50,sizeof(char));
306  sprintf(Number,"%d",n);
307  glRasterPos3f((Pos[0]*InvScaleUn),
308  (Pos[1]*InvScaleUn),
309  (Pos[2]*InvScaleUn));
310  for (int l = 0; l < strlen(Number); l++) {
311  glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, Number[l]);
312  }
313  glCallList(Point);
314  free(Number);
315 }
316 
317 #endif// __glut_h__
double InvScaleUn
Rescale the three orthogonal directions.
Definition: Draw.h:181
void Dreshape(int w, int h)
Principal reshape function.
Definition: Draw.cpp:237
GLfloat IncrVisDxSx
Increment visual DxSx, SuGiu.
Definition: Draw.h:177
void ReadScript()
Reads and draw a script file.
Definition: DrawFile.cpp:285
int ne
Enables/disables fog.
Definition: Draw.h:224
GLuint XCenter
Center of the frame.
Definition: Draw.h:212
int sp
Enables/disables spot light.
Definition: Draw.h:222
GLfloat xl1
Position of the light1.
Definition: Draw.h:155
int Values
Number of values to divide the edge in squares.
Definition: Draw.h:242
int WinHeight
Height of the window.
Definition: Draw.h:248
GLuint YCenter
Center of the frame.
Definition: Draw.h:214
int ImWidth
Width and height of the image.
Definition: Draw.h:153
int MainWindow
Refers to optional different windows.
Definition: Draw.h:226
void DTimer(int v)
Not working.
Definition: Draw.cpp:144
void InitConstant()
Initializes all the view constants.
GLfloat xl0
Position of the light0.
Definition: Draw.h:155
int yRem
Old y position of the mouse.
Definition: Draw.h:252
void Lista(int NSquare)
Definition of the primitives.
double Edge[3]
Box size.
Definition: Draw.h:262
int xRem
Old x position of the mouse.
Definition: Draw.h:250
void ChooseBlend(int Which)
Choose a different blending function.
char * info
Info line.
Definition: Draw.h:276
void Window(int argc, char **argv)
Initial definition of the window.
Definition: Draw.cpp:108
int WinWidth
Width of the window.
Definition: Draw.h:246
char * Number
Characters for the grid.
Definition: Draw.h:272
int NLevel
Levels of the images data (usually 4=RGBA)
Definition: Draw.h:256
void Numera(double *Pos, int n)
Print the.
Definition: Draw.cpp:304
GLubyte * pixel
Principal image (always allocated)
Definition: Draw.h:270
void ReadConf()
Reads and applies external configurations.
Definition: DrawFile.cpp:6