Allink
v0.1
|
00001 /*********************************************************************** 00002 Draw: Creator and initialisation of the graphical framework 00003 Copyright (C) 2008-2010 by Giovanni Marelli <sabeiro@virgilio.it> 00004 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 You should have received a copy of the GNU General Public License 00016 along with this program; If not, see <http://www.gnu.org/licenses/>. 00017 ***********************************************************************/ 00018 #include "../../include/Draw.h" 00019 00020 #ifdef __glut_h__ 00021 00022 Draw::Draw(){ 00023 info = (char *)malloc(256*sizeof(char)); 00024 Number = (char *)malloc(100*sizeof(char)); 00025 InvScaleUn = 1.; 00026 for(int d=0;d<3;d++){ 00027 Edge[d] = 1.; 00028 } 00029 ImHeight = 800; 00030 ImWidth = 800; 00031 pixel = NULL; 00032 pixel = (GLubyte*)malloc(sizeof(GLubyte)); 00033 NLevel = 4; 00034 InitConstant(); 00035 xRem = 0;yRem=0; 00036 IncrVisDxSx = 2.; 00037 IncrVisSuGiu = 2.; 00038 } 00039 void Draw::Shout(const char * s, ...) 00040 { 00041 #ifdef DRAW_DEBUG 00042 va_list args; 00043 va_start(args, s); 00044 vfprintf(stderr, s, args); 00045 fprintf(stderr, "\n"); 00046 va_end(args); 00047 #else 00048 return; 00049 #endif 00050 } 00051 // #include <SDL.h> 00052 // #include "SDL/SDL_opengl.h" 00053 // int Draw::WindowSDL(){ 00054 // int ret = SDL_Init(SDL_INIT_EVERYTHING); 00055 // if( ret < 0 ) return false; 00056 // //ret = SDL_SetVideoMode(gScreenWidth, gScreenHeight, 0, SDL_OPENGL | SDL_RESIZABLE | SDL_ASYNCBLIT | SDL_HWSURFACE | SDL_ANYFORMAT); 00057 // SDL_Surface *screen; 00058 // screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL); 00059 // if( screen == NULL ) return false; 00060 // SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 00061 // SDL_MapRGB(main_surface->format, 255, 255, 255); 00062 // InitConstant(); 00063 // Illuminazione(); 00064 // if(ne) Nebbia(); 00065 // if(sp) Spot(); 00066 // ParticleList(); 00067 // ReadScript(); 00068 // ChooseBlend(1); 00069 // SDL_WM_SetCaption("Disegna",NULL); 00070 00071 // SDL_Event event; 00072 00073 // SDL_WaitEvent(&event); 00074 00075 // switch (event.type) { 00076 // case SDL_KEYDOWN: 00077 // printf("Il tasto %s e' stato premuto!\n", 00078 // SDL_GetKeyName(event.key.keysym.sym)); 00079 // break; 00080 // case SDL_QUIT: 00081 // exit(0); 00082 // } 00083 // { 00084 // SDL_Event event; 00085 00086 // while ( SDL_PollEvent(&event) ) { 00087 // switch (event.type) { 00088 // case SDL_MOUSEMOTION: 00089 // printf("Il Mouse e' stato mosso da %d,%d " \ 00090 // "a (%d,%d)\n", 00091 // event.motion.xrel, event.motion.yrel, 00092 // event.motion.x, event.motion.y); 00093 // break; 00094 // case SDL_MOUSEBUTTONDOWN: 00095 // printf("Il pulsante %d del Mouse %d e' stato " \ 00096 // "premuto a (%d,%d)\n", 00097 // event.button.button, event.button.x, 00098 // event.button.y); 00099 // break; 00100 // case SDL_QUIT: 00101 // exit(0); 00102 // } 00103 // } 00104 // } 00105 00106 // return true; 00107 // } 00108 void Draw::Window(int argc,char **argv){ 00109 glutInit(&argc,argv); 00110 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 00111 glutInitWindowSize(ImWidth,ImHeight);//Dimensione finestra 00112 //glutInitWindowPosition(100,300);//Posizione finestra 00113 MainWindow = glutCreateWindow("Disegna");//Titolo 00114 // sprintf(info,"There are %d particle with %d different types",Gen1->NPart,Gen1->NType); 00115 InitConstant(); 00116 ReadConf(); 00117 Lista(Values); 00118 // SubWindow1 = glutCreateSubWindow(MainWindow, 0, 100, 00119 // (width), (height-100)); 00120 ParticleList(); 00121 ReadScript(); 00122 // cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << endl; 00123 // cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << endl; 00124 // cout << "GL_VERSION : " << glGetString(GL_VERSION) << endl; 00125 // //cout << "GL_EXTENSION : " << glGetString(GL_EXTENSIONS) << endl; 00126 glutTimerFunc(0,Timer,0);// tempo tra due diapositive 00127 glutDisplayFunc(Figure);//Disegna la figura 00128 glutReshapeFunc(reshape); 00129 //glutReshapeFunc(ChangeSize);//Abilita la ridimensione per mouse 00130 glutMouseFunc(mouse); 00131 glutKeyboardFunc(keyboard); 00132 glutMotionFunc( MouseMove ); 00133 glutSpecialFunc(special); 00134 ChooseBlend(1); 00135 Illuminazione(); 00136 if(ne) Nebbia(); 00137 if(sp) Spot(); 00138 //glutMainLoop();//Mantiene la finestra 00139 // SubWindow2 = glutCreateSubWindow(MainWindow, 0, 0, 00140 // (width), 100); 00141 // gl 00142 glClearColor(.0,.0,.0,.0);//colore sfondo 00143 } 00144 void Draw::DTimer(int v){ 00145 glutPostRedisplay(); 00146 glutTimerFunc(30,Timer, 0); 00147 } 00148 // void Draw::Illuminazione(){ 00149 00150 // } 00151 void Draw::Illuminazione(void){ 00152 //------------------Depth------------------ 00153 glEnable(GL_DEPTH_TEST); 00154 glDepthFunc(GL_LESS); 00155 //glDepthFunc(GL_LEQUAL); 00156 glClearDepth( 1.0 ); 00157 //glClearDepth(1.0); 00158 //glDepthMask(GL_TRUE); // make Z-buffer writable 00159 // glDepthMask(GL_FALSE); 00160 // glDepthRange(0,-10.); 00161 //----------------Blend--------------------------- 00162 glEnable(GL_BLEND); 00163 glShadeModel(GL_SMOOTH); 00164 glEnable(GL_POINT_SMOOTH); 00165 glEnable(GL_LINE_SMOOTH); 00166 glEnable(GL_ALPHA_TEST); 00167 glAlphaFunc(GL_GREATER,0.1); 00168 //------------Miscellaneus-------------------------- 00169 //glEnable(GL_STENCIL_TEST); /// Enable stencil test 00170 //glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); /// 00171 //glStencilFunc(GL_ALWAYS, uniqueStencilValue, ~0); 00172 //glEnable(GL_TEXTURE_2D); 00173 glReadBuffer(GL_FRONT);/* so glReadPixel() always get the right image */ 00174 //glEnable(GL_NORMALIZE); 00175 //---------------Material---------------------- 00176 // glEnable(GL_COLOR_MATERIAL);// have materials set by curr color 00177 //glEnable(GL_CULL_FACE); 00178 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);//GL_LINE 00179 glCullFace(GL_FRONT_AND_BACK); 00180 glFrontFace(GL_CCW);//Senso antiorario 00181 //glEnableClientState(GL_VERTEX_ARRAY); 00182 //glEnableClientState(GL_NORMAL_ARRAY); 00183 //glEnable(GL_VERTEX_ARRAY); 00184 GLfloat ambientMaterial[4] = { .5, .5, .5,1.}; 00185 GLfloat diffuseMaterial[4] = { .0, .0, .0,1.}; 00186 GLfloat specularMaterial[4] = { .0, .0, .0,1.0}; 00187 GLfloat shininessMaterial = 25.0;//50.0; 00188 GLfloat emissionMaterial[4] = { .0, .0, .0,1.}; 00189 GLfloat riflettivitaSpeculare[]= { 0.0, .0, .0,1.}; 00190 //glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); 00191 //glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); 00192 //glColorMaterial(GL_FRONT_AND_BACK,GL_EMISSION); 00193 //glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); 00194 //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); 00195 //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE); 00196 //glDisable(GL_POLYGON_SMOOTH);// make sure not to antialias polygons 00197 glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,DrAmbientWhite); 00198 glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,DrDiffuseWhite); 00199 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,DrSpecularWhite); 00200 glMaterialf(GL_FRONT,GL_SHININESS,shininessMaterial); 00201 glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emissionMaterial); 00202 glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,riflettivitaSpeculare); 00203 glMaterialfv(GL_BACK, GL_AMBIENT, DrAmbientGreen); 00204 glMaterialfv(GL_BACK, GL_DIFFUSE, DrDiffuseGreen); 00205 glMaterialfv(GL_FRONT, GL_AMBIENT, DrAmbientBlue); 00206 glMaterialfv(GL_FRONT, GL_DIFFUSE, DrDiffuseBlue); 00207 glMaterialfv(GL_FRONT, GL_SPECULAR, DrSpecularWhite); 00208 glMaterialf( GL_FRONT, GL_SHININESS, 25.0); 00209 //--------------------Light-------------------------- 00210 GLfloat DrPropertiesAmbient [] = {0.50, 0.50, 0.50, 1.00}; 00211 GLfloat DrPropertiesDiffuse [] = {0.75, 0.75, 0.75, 1.00}; 00212 GLfloat DrPropertiesSpecular[] = {1.00, 1.00, 1.00, 1.00}; 00213 00214 glEnable(GL_LIGHTING); 00215 00216 //glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); 00217 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,1.0); 00218 glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0); 00219 00220 glEnable( GL_LIGHT0 ); 00221 glLightfv( GL_LIGHT0, GL_AMBIENT, DrPropertiesAmbient); 00222 glLightfv( GL_LIGHT0, GL_DIFFUSE, DrPropertiesDiffuse); 00223 glLightfv( GL_LIGHT0, GL_SPECULAR, DrPropertiesSpecular); 00224 GLfloat LightPos0[]={xl0,yl0,zl0, 0.0f }; 00225 GLfloat LightPos1[]={xl1,yl1,zl1, 0.0f }; 00226 glLightfv(GL_LIGHT0, GL_POSITION,LightPos0); 00227 glEnable(GL_LIGHT1); 00228 glLightfv(GL_LIGHT1, GL_AMBIENT, DrPropertiesAmbient); 00229 glLightfv(GL_LIGHT1, GL_DIFFUSE, DrPropertiesDiffuse); 00230 glLightfv(GL_LIGHT1, GL_POSITION,LightPos1); 00231 glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); 00232 glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); 00233 glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2); 00234 glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0); 00235 glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); 00236 } 00237 void Draw::Dreshape(int w,int h){ 00238 glViewport(0,0,(GLsizei) w, (GLsizei) h); 00239 WinWidth = (int) w; 00240 WinHeight = (int) h; 00241 XCenter = WinWidth / 2; 00242 YCenter = WinHeight / 2; 00243 glMatrixMode(GL_PROJECTION); 00244 glLoadIdentity(); 00245 if(pr == 1){ 00246 //glLoadIdentity(); 00247 gluPerspective(30,(GLfloat) w/(GLfloat) h,.1,200.);//ang,rapp,zmin,zmax 00248 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 00249 //glTranslatef(0.,0.,-2.); 00250 } 00251 if(pr == 0){ 00252 //glOrtho(-1.,1.,(float)(w-1),(float)(h-1),.1,-200.);//Sx,Dx,giu,su,vicino,lontano 00253 // glTranslatef(0.,0.,1.0); 00254 glOrtho(-1.,1.,-WinHeight/(float)WinWidth,WinHeight/(float)WinWidth,.1,20);//Sx,Dx,giu,su,vicino,lontano 00255 glEnable(GL_DEPTH_TEST); 00256 } 00257 glMatrixMode(GL_MODELVIEW); 00258 //gluLookAt( -1., -1., -1.,.0, .0, .0,1., 1., 1.);//osservatore,centro,orientazion 00259 glLoadIdentity(); 00260 gluLookAt( 0., 0.,1.5/* 1.5*Edge[2]*InvScaleUn*/, 00261 0., 0., 0., 00262 0., 1., 0.);//osservatore,centro,orientazion 00263 // glLoadIdentity(); 00264 //glFrustum(-3,3.,-3.,3.,-3.,3.);//Sx,Dx,giu,su,vicino,lontano//Dimensione massima dell'immagine 00265 //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations 00266 } 00267 void Draw::Spot(void){ 00268 GLfloat direzioneSpot[3] = { .0, 1.0, 1.0}; 00269 GLfloat coloreNebbia[] = { 0.2, 0.2, 0.2,1.}; 00270 GLfloat esponenteSpot = 1.5; 00271 GLfloat angoloSpot = 120.; 00272 glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,angoloSpot); 00273 glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,direzioneSpot); 00274 glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,esponenteSpot); 00275 } 00276 void Draw::Nebbia(void){ 00277 GLfloat zIniNebbia = 10.; 00278 GLfloat zFinNebbia = -10.; 00279 GLfloat DensitaNebbia = 1.; 00280 glEnable(GL_FOG); 00281 glFogf(GL_FOG_START,zIniNebbia); 00282 glFogf(GL_FOG_END,zFinNebbia); 00283 glFogi(GL_FOG_MODE,GL_EXP); 00284 glFogf(GL_FOG_DENSITY,DensitaNebbia); 00285 } 00286 void Draw::ChangeSize(GLsizei w,GLsizei h){ 00287 GLfloat aspectRatio; 00288 WinWidth = (int) w; 00289 WinHeight = (int) h; 00290 if(h==0)h=1; 00291 aspectRatio=(GLfloat)w/(GLfloat)h; 00292 glViewport(0,0,w,h); 00293 glMatrixMode(GL_PROJECTION); 00294 glLoadIdentity(); 00295 if(w<=h) 00296 glOrtho(-100.,100.,-100./aspectRatio,100. 00297 /aspectRatio,1.,-1.); 00298 else 00299 glOrtho(-100*aspectRatio,-100*aspectRatio, 00300 -100,100,1.,-1.); 00301 glMatrixMode(GL_MODELVIEW); 00302 glLoadIdentity(); 00303 } 00304 void Draw::Numera(double *Pos,int n){ 00305 char *Number = (char *)calloc(50,sizeof(char)); 00306 sprintf(Number,"%d",n); 00307 glRasterPos3f((Pos[0]*InvScaleUn), 00308 (Pos[1]*InvScaleUn), 00309 (Pos[2]*InvScaleUn)); 00310 for (int l = 0; l < strlen(Number); l++) { 00311 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, Number[l]); 00312 } 00313 glCallList(Point); 00314 free(Number); 00315 } 00316 00317 #endif// __glut_h__