HydroSDL/oGL_graphics/graphics_main.cpp

Go to the documentation of this file.
00001 
00010 #include "graphics_relative.h"
00011 
00012 #include "../hydro_source/dataFillings.h"
00013 
00014 
00015 
00016 GLuint  base;                                                   // Base Display List For The Font Set
00017 
00018 
00019 bool    keys[256];                                                                      // Array Used For The Keyboard Routine
00020 bool    active=TRUE;                                                            // Window Active Flag Set To TRUE By Default
00021 bool    fullscreen=TRUE;                                                        // Fullscreen Flag Set To Fullscreen Mode By Default
00022 
00023 bool    light;                                                                  // Lighting ON / OFF
00024 
00025 
00026 
00027 GLfloat viewAngle1 = 0.0;  
00028 GLfloat viewAngle2 = 0.3;  
00029 GLfloat dviewAngle = 0.01;
00030 GLfloat viewDist = 50.0;   
00031 GLfloat dviewDist = 0.3;
00032 
00033 
00034 GLfloat LightAmbient[]= { 0.35f, 0.35f, 0.5f, 1.0f };                           // Ambient Light Values ( NEW )
00035 GLfloat LightDiffuse[]= { 0.6f, 0.6f, 0.6f, 1.0f };                              // Diffuse Light Values ( NEW )
00036 GLfloat LightPosition[]= { -10.0f, 20.0f, 150.0f, 0.0f };                                // Light Position ( NEW )
00037 //GLfloat LightPosition[]= { 0.0f, -10.0f, 10.0f, 1.0f };                                // Light Position ( NEW )
00038 
00039 //MSG   msg;                                                            // Windows Message Structure
00040 bool    done=FALSE;                                                     // Bool Variable To Exit Loop
00041 
00042 //joystick stuff
00043 bool doX = TRUE;        //TRUE: use jostick X-axis, FALSE: use keyboard
00044 bool doY = TRUE;        //TRUE: use jostick Y-axis, FALSE: use keyboard
00045 bool doZ = TRUE;        //TRUE: use jostick Z-axis, FALSE: use keyboard
00046 bool doR = TRUE;        //TRUE: use jostick R-axis, FALSE: use keyboard
00047 float dirX = 1.0f;  //change axis direction: -1.0f
00048 float dirY = -1.0f;
00049 float dirZ = 1.0f;
00050 float dirR = 1.0f;
00051 float time_before = 0.0; //for keyboard controll and fps computing
00052 float time_elapsed; //for keyboard controll and fps computing
00053 float fps = 0.0; //frames per second
00054 bool callibratingPhase = TRUE;
00055 bool onBoardView = FALSE;
00056 
00057         /* Color depth in bits of our window. */ 
00058         int bpp = 0; 
00059         /* Flags we will pass into SDL_SetVideoMode. */ 
00060         int flags = 0; 
00061 
00062 
00063 TTF_Font *font = NULL;
00064 TTF_Font *fontBIG = NULL;
00065 SDL_Surface *screen;
00066 
00067 
00068 SDL_Color text_color;
00069 SDL_Rect text_position;
00070 
00071 // the ID of the texture we're going to create
00072 GLuint texture[2];
00073 
00074 
00075 
00076 GLuint* get_texture()
00077 {
00078         return texture;
00079 }
00080 
00081 bool getCallibatingPhase()
00082 {
00083         return callibratingPhase;
00084 }
00085 
00086 
00087 int my_round(double x)
00088 {
00089         return (int)(x + 0.5);
00090 }
00091 
00092 int nextpoweroftwo(int x)
00093 {
00094         double logbase2 = log(x) / log(2);
00095         return my_round(pow(2,ceil(logbase2)));
00096 }
00097 
00098 
00099 
00100 
00101 void SDL_GL_RenderText(char *text, 
00102                       TTF_Font *font,
00103                       SDL_Color color,
00104                       SDL_Rect *location)
00105 {
00106         SDL_Surface *initial;
00107         SDL_Surface *intermediary;
00108         SDL_Rect rect;
00109         int w,h;
00110         GLuint texture;
00111         
00112         /* Use SDL_TTF to render our text */
00113         initial = TTF_RenderText_Blended(font, text, color);
00114         
00115         /* Convert the rendered text to a known format */
00116         w = nextpoweroftwo(initial->w);
00117         h = nextpoweroftwo(initial->h);
00118         
00119         intermediary = SDL_CreateRGBSurface(0, w, h, 32, 
00120                         0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff);
00121                         
00122         SDL_BlitSurface(initial, 0, intermediary, 0);
00123         
00124         /* Tell GL about our new texture */
00125         glGenTextures(1, &texture);
00126         glBindTexture(GL_TEXTURE_2D, texture);
00127         glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA, 
00128                         GL_UNSIGNED_BYTE, intermediary->pixels );
00129         
00130         /* GL_NEAREST looks horrible, if scaled... */
00131         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00132         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);       
00133 
00134         /* prepare to render our texture */
00135         glEnable(GL_TEXTURE_2D);
00136         glBindTexture(GL_TEXTURE_2D, texture);
00137         glColor4f(1.0f, 1.0f, 1.0f,1.0f);
00138         
00139         /* Draw a quad at location */
00140         glBegin(GL_QUADS);
00141                 /* Recall that the origin is in the lower-left corner
00142                    That is why the TexCoords specify different corners
00143                    than the Vertex coors seem to. */
00144                 /*glTexCoord2f(0.0f, 1.0f); 
00145                         glVertex2f(location->x    , location->y);
00146                 glTexCoord2f(1.0f, 1.0f); 
00147                         glVertex2f(location->x + w, location->y);
00148                 glTexCoord2f(1.0f, 0.0f); 
00149                         glVertex2f(location->x + w, location->y + h);
00150                 glTexCoord2f(0.0f, 0.0f); 
00151                         glVertex2f(location->x    , location->y + h);*/
00152                         
00153                 glTexCoord2f(0.0f, 0.0f); 
00154                         glVertex2f(location->x    , location->y + h);
00155                 glTexCoord2f(1.0f, 0.0f); 
00156                         glVertex2f(location->x + w, location->y + h);
00157                 glTexCoord2f(1.0f, 1.0f); 
00158                         glVertex2f(location->x + w, location->y);
00159                 glTexCoord2f(0.0f, 1.0f); 
00160                         glVertex2f(location->x    , location->y);
00161 
00162         glEnd();
00163         
00164         /* Bad things happen if we delete the texture before it finishes */
00165         glFinish();
00166         
00167         /* return the deltas in the unused w,h part of the rect */
00168         location->w = initial->w;
00169         location->h = initial->h;
00170         
00171         /* Clean up */
00172         SDL_FreeSurface(initial);
00173         SDL_FreeSurface(intermediary);
00174         glDeleteTextures(1, &texture);
00175 }
00176 
00177 void glEnable2D()
00178 {
00179 
00180         GLfloat vPort[4];
00181   
00182         glGetFloatv(GL_VIEWPORT, vPort);
00183   
00184 //      glMatrixMode(GL_PROJECTION);
00185 //      glPushMatrix();
00186 //      glLoadIdentity();
00187   
00188 //      glOrtho(0.0, vPort[2], 0.0, vPort[3], -1.0, 1.0);
00189         glMatrixMode(GL_MODELVIEW);
00190         glPushMatrix();
00191         glLoadIdentity();
00192         
00194         
00195         SDL_Surface *screen = SDL_GetVideoSurface();
00196 
00197         /* Note, there may be other things you need to change,
00198            depending on how you have your OpenGL state set up.
00199         */
00200         glPushAttrib(GL_ENABLE_BIT);
00201         glDisable(GL_DEPTH_TEST);
00202         glDisable(GL_CULL_FACE);
00203         glEnable(GL_TEXTURE_2D);
00204 
00205         /* This allows alpha blending of 2D textures with the scene */
00206         glEnable(GL_BLEND);
00207         glBlendFunc(GL_ONE, GL_ONE);
00208         //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00209 
00210         glViewport(0, 0, screen->w, screen->h);
00211 
00212         glMatrixMode(GL_PROJECTION);
00213         glPushMatrix();
00214         glLoadIdentity();
00215 
00216         //glOrtho(0.0, 600, 0.0, 300, 0.0, 1.0);
00217         glOrtho(0.0, (GLdouble)screen->w, 0.0 , (GLdouble)screen->h, -1.0, 1.0);
00218 
00219 //      glMatrixMode(GL_MODELVIEW);
00220 //      glPushMatrix();
00221 //      glLoadIdentity();
00222 
00223         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00224 }
00225 
00226 void glDisable2D()
00227 {
00228 //      glMatrixMode(GL_MODELVIEW);
00229 //      glPopMatrix();
00230 
00231         glMatrixMode(GL_PROJECTION);
00232         glPopMatrix();
00233 
00234         glPopAttrib();
00235         
00237 //      glMatrixMode(GL_PROJECTION);
00238 //      glPopMatrix();   
00239         glMatrixMode(GL_MODELVIEW);
00240         glPopMatrix();  
00241         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00242 }
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251 
00252 
00253 
00254 
00255 
00256 void setup_opengl(int width, int height) 
00257 {
00258         glShadeModel(GL_SMOOTH);                                                // Enables Smooth Shading
00259         glClearColor(0.3f, 0.3f, 1.0f, 0.0f);                   // Blue Background
00260 
00261 
00262         glClearDepth(1.0f);                                                             // Depth Buffer Setup
00263         glEnable(GL_DEPTH_TEST);                                                // Enables Depth Testing
00264         glDepthFunc(GL_LEQUAL);                                                 // The Type Of Depth Test To Do
00265 
00266         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);                      // Really Nice Perspective Calculations
00267 
00268         glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);                         // Setup The Ambient Light
00269         glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);                         // Setup The Diffuse Light
00270         glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);                        // Position The Light
00271         glEnable(GL_LIGHT0);                                                    // Enable Light One
00272         glEnable(GL_LIGHTING);
00273         glEnable( GL_COLOR_MATERIAL );
00274         glEnable(GL_NORMALIZE);
00275         glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00276         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0f );
00277         //?
00278         glViewport(0, 0, width, height);                                        // Reset The Current Viewport
00279         
00280         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
00281         glLoadIdentity();
00282         gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 1.0f, 200.0f); //calculate aspect ratio of the window
00283         glMatrixMode(GL_MODELVIEW);
00284                 glLoadIdentity();
00285 }
00286 
00287 
00288 
00289 
00290 void load_textures()
00291 {
00293         // generate space for the texture and make it the current texture
00294     glGenTextures( 2, texture );
00295 //sail:
00296     glBindTexture( GL_TEXTURE_2D, texture[0] );
00297     
00298     // load the bitmap and lock the memory
00299     SDL_Surface *tex = SDL_LoadBMP( "voile.bmp" );
00300         //fprintf(stderr, "Bitmap loading: %s \n", SDL_GetError( ));
00301     SDL_LockSurface( tex );
00302     
00303     // create the texture from the bitmap
00304     glTexImage2D( GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels );
00305     
00306     // free up the bitmap memory
00307     SDL_UnlockSurface( tex );
00308     SDL_FreeSurface( tex );
00309         
00310         // draw it nicely
00311     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00312     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00313         
00314         
00315 //panorama:
00316     glBindTexture( GL_TEXTURE_2D, texture[1] );
00317     
00318     // load the bitmap and lock the memory
00319     tex = SDL_LoadBMP( "pano.bmp" );
00320         //fprintf(stderr, "Bitmap loading: %s \n", SDL_GetError( ));
00321     SDL_LockSurface( tex );
00322     
00323     // create the texture from the bitmap
00324     glTexImage2D( GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels );
00325     
00326     // free up the bitmap memory
00327     SDL_UnlockSurface( tex );
00328     SDL_FreeSurface( tex );
00329         
00330         // draw it nicely
00331     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00332     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00333 }
00334 
00335 
00336 
00337 void resize_window(int height, int width)
00338 { 
00339         //if( SDL_SetVideoMode(width, height,32,SDL_OPENGL|SDL_HWSURFACE|SDL_RESIZABLE ) ==0)
00340         //if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 )
00341         screen = SDL_SetVideoMode( width, height, bpp, flags );
00342         if (screen == 0)
00343                 { 
00344                         fprintf( stderr, "Video mode resize failed: %s\n", 
00345                         SDL_GetError( ) ); 
00346                         quit_tutorial( 1 ); 
00347                 } 
00348         setup_opengl(width, height);
00349         // Reload textures needed after call to SDL_setVideoMode()
00350         load_textures();
00351 }
00352 
00353 
00354 
00355 
00356 void setup_SDL(int width, int height)
00357 { 
00358         /* Information about the current video settings. */ 
00359         const SDL_VideoInfo* info = NULL; 
00360         /* Dimensions of our window. */ 
00361 //      int width = 0; 
00362 //      int height = 0; 
00363         /* Color depth in bits of our window. */ 
00364         //int bpp = 0; 
00365         /* Flags we will pass into SDL_SetVideoMode. */ 
00366         //int flags = 0; 
00367         
00368         /* First, initialize SDLÕs video subsystem. */ 
00369         if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
00370         { 
00371                 /* Failed, exit. */ 
00372                 fprintf( stderr, "Video initialization failed: %s\n", 
00373                 SDL_GetError( ) ); 
00374                 quit_tutorial( 1 ); 
00375         } 
00376         
00377         /* LetÕs get some video information. */ 
00378         info = SDL_GetVideoInfo( ); 
00379         if( !info )
00380         { 
00381                 /* This should probably never happen. */ 
00382                 fprintf( stderr, "Video query failed: %s\n", 
00383                 SDL_GetError( ) ); 
00384                 quit_tutorial( 1 ); 
00385         } 
00386         
00387         bpp = info->vfmt->BitsPerPixel;
00388         
00389          
00390         SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );    //5
00391         SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );  //5
00392         SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );   //5
00393         SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); //16
00394         SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); 
00395 
00396         
00397         flags =  SDL_RESIZABLE|SDL_OPENGL;// | SDL_FULLSCREEN;
00398         
00399         screen = SDL_SetVideoMode( width, height, bpp, SDL_RESIZABLE|SDL_OPENGL );
00400         //if( SDL_SetVideoMode( width, height, bpp, SDL_RESIZABLE|SDL_OPENGL ) == 0 )
00401         if( screen == 0)
00402         { 
00403                 /* 
00404                 * This could happen for a variety of reasons, 
00405                 * including DISPLAY not being set, the specified 
00406                 * resolution not being available, etc. 
00407                 */ 
00408                 fprintf( stderr, "Video mode set failed: %s\n", 
00409                 SDL_GetError( ) ); 
00410                 quit_tutorial( 1 ); 
00411         } 
00412         
00413         SDL_WM_SetCaption("Hydropter Simulator", 0);
00414         
00415         load_textures();        
00416 }
00417 
00418 
00419 
00420 
00421 
00422 
00423 void setup_font()
00424 {
00425         TTF_Init();  // Init font library
00426         
00427         font = TTF_OpenFont("ActionMan.ttf", 18 );
00428         if (font==NULL)  fprintf( stderr, "Could not open font \n");
00429         else fprintf( stderr, "Font opened \n");
00430         
00431         fontBIG = TTF_OpenFont("ActionMan.ttf", 24 );
00432         if (font==NULL)
00433         {
00434                 fprintf( stderr, "Could not open big font \n");
00435                 fontBIG = font;
00436         }
00437         else fprintf( stderr, "Big font opened \n");
00438 }
00439 
00440 void close_font()
00441 {
00442         if (font != NULL) TTF_CloseFont( font );
00443         TTF_Quit();
00444 }
00445 
00446 
00447 /*
00448 void SDL_GL_RenderText(char *text, 
00449                       TTF_Font *font,
00450                       SDL_Color color,
00451                       SDL_Rect *location)
00452 */                                        
00453                                           
00454 void SDL_GL_PrintText(TTF_Font *font,
00455                       SDL_Color color,
00456                       SDL_Rect *location,
00457                                           const char *fmt, ...)
00458 {
00459         char            text[256];                              // Holds Our String
00460         va_list         ap;                                             // Pointer To List Of Arguments
00461         
00462         if (fmt == NULL)                                        // If There's No Text
00463                 return;
00464                 
00465         va_start(ap, fmt);                                      // Parses The String For Variables
00466             vsprintf(text, fmt, ap);            // And Converts Symbols To Actual Numbers
00467         va_end(ap);
00468         
00469         SDL_GL_RenderText(text, font, color, location);
00470 }
00471 
00472 
00473 
00474 
00475 
00476 void draw_panorama()
00477 {
00478         float PanCenter, PanAngle;
00479         
00480         PanCenter = -(viewAngle1 / 6.28) + 0.25;
00481         PanAngle = 0.1;
00482 
00483         glBindTexture( GL_TEXTURE_2D, texture[1] );
00484         glEnable( GL_TEXTURE_2D );
00485         glDisable(GL_LIGHTING);
00486 
00487         glBegin( GL_QUADS );
00488 
00489         glColor3f(1.0f,1.0f,1.0f);
00490 
00491 //font-sky
00492         glTexCoord2f( PanCenter-PanAngle, 0.0f );
00493         glVertex3f( 80.0f, 60.0f, -100.0f );
00494 
00495         glTexCoord2f( PanCenter+PanAngle, 0.0f );
00496         glVertex3f( -80.0f, 60.0f, -100.0f );
00497 
00498         glTexCoord2f( PanCenter+PanAngle, 1.0f );
00499         glVertex3f( -80.0f, -20.0f, -100.0f );
00500 
00501         glTexCoord2f( PanCenter-PanAngle, 1.0f );
00502         glVertex3f( 80.0f, -20.0f, -100.0f );
00503         
00504 //top-sky
00505 
00506         glTexCoord2f( PanCenter-PanAngle, 0.0f );
00507         glVertex3f( 80.0f, 200.0f, 0.0f );
00508 
00509         glTexCoord2f( PanCenter+PanAngle, 0.0f );
00510         glVertex3f( -80.0f, 200.0f, 0.0f );
00511         
00512         glTexCoord2f( PanCenter+PanAngle, 0.01f );
00513         glVertex3f( -80.0f, 59.0f, -100.0f );
00514 
00515         glTexCoord2f( PanCenter-PanAngle, 0.01f );
00516         glVertex3f( 80.0f, 59.0f, -100.0f );
00517 
00518         glEnd(); 
00519 
00520         glEnable(GL_LIGHTING);
00521         glDisable( GL_TEXTURE_2D );
00522 
00523 }
00524 
00525 
00526 
00527 
00528 
00529 int DrawGLScene(datas *d)//, graphicDatas *gd)//(datas *d, seaDatas *sd, boatDatas *bd)                                                         // Here's Where We Do All The Drawing
00530 {
00531         double sign;
00532         float X, Y, Z, R; //joystick axes
00533         double pilot[3];
00534         double a, b, c, A, B, C, phi, theta, psi;
00535         phi = d->phi;
00536         theta = d->theta;
00537         psi = d->psi;
00538 
00539         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                     // Clear The Screen And The Depth Buffer
00540                                                         
00541 
00542         if (abs((1.0f/time_elapsed))<300.0f) fps=0.99*fps+0.01*(1.0f/time_elapsed); //frames per second 
00543 
00544 //On Screen Display informations (text and instruments):
00545         glLoadIdentity();
00546 
00547         glDisable(GL_LIGHTING);
00548 
00549         glTranslatef(0.0f,0.0f,-1.0f);  //translate for text display
00551 //      glRasterPos2f(-0.5, -0.35);             //font position
00552 //      glPrint("Speed %3.2f km/h    Course %3.0f deg", 
00553 //                      3.6f*sqrt(d->dx*d->dx + d->dy*d->dy),
00554 //                      -57.2958f*atan(d->dy/d->dx));   // Print GL Text To The Screen  
00555 //      glRasterPos2f(-0.5, -0.375);            //font position
00556 //      glPrint("Wind Speed %3.2f km/h,    %3.1f fps", 3.6f*d->Wind, fps);
00557 //      glRasterPos2f(-0.5, -0.4);              //font position
00558 //      glPrint("Relative Wind Speed %3.2f km/h", 3.6*sqrt((d->Wind_x - d->dx)*(d->Wind_x - d->dx)+(d->Wind_y - d->dy)*(d->Wind_y - d->dy)));
00559 
00560         glLoadIdentity();
00561         glTranslatef(-0.45f,0.4f,-1.0f);
00562         glScalef(0.08f,0.08f,0.08f);
00563         Girouette_draw(d);
00564 
00565         if (is_there_a_joystick()) 
00566         {
00567                 joystick_getXYZR(&d->target_x, &d->target_y, &d->Hslider_x, &d->Vslider_x, doX, doY, doZ, doR);
00568                 if (doX) d->target_x *= dirX;
00569                 if (doY) d->target_y *= dirY;
00570                 if (doZ) d->Hslider_x *= dirZ;
00571                 if (doR) d->Vslider_x *= dirR;
00572         }
00573 
00574         angles_update(d); //update cal angles from target/sliders position
00575         
00576         
00577 
00578         glLoadIdentity();
00579         glTranslatef(0.48f,-0.35f,-1.0f);
00580         glScalef(0.05f,0.05f,0.04f);
00581         Target_draw(d->target_x, d->target_y); //draw target instrument
00582 
00583         glLoadIdentity();
00584         glTranslatef(0.35f,-0.35f,-1.0f);
00585         glScalef(0.1f,0.1f,0.1f);
00586         HSlider_draw(d->Hslider_x);  //draw horizontal slider instrument
00587 
00588         glLoadIdentity();
00589         glTranslatef(0.25f,-0.35f,-1.0f);
00590         glScalef(0.1f,0.1f,0.1f);
00591         VSlider_draw(d->Vslider_x);  //draw vertical slider instrument
00592 
00593         glEnable(GL_LIGHTING);
00594 
00595 //3D graphics:
00596         glLoadIdentity();                               //back to identity
00597 
00598         if (cos(viewAngle2) >= 0) sign=1.0; //avoid looking up-side-down when going over 90deg of vertical view angle
00599         else sign=-1.0;
00600 
00601         if (!onBoardView)
00602         {
00603                 gluLookAt(    d->x - cos(viewAngle1)*cos(viewAngle2)*viewDist,   //look at boat
00604                                         d->y + sin(viewAngle1)*cos(viewAngle2)*viewDist,
00605                                         d->z + sin(viewAngle2)*viewDist, 
00606 
00607                                         d->x,d->y,d->z+12.0,
00608 
00609                                         sin(viewAngle2)*cos(viewAngle1) * sign,
00610                                         -sin(viewAngle2)*sin(viewAngle1) * sign,
00611                                         cos(viewAngle2) * sign); 
00612         }
00613         else
00614         {
00615                                   
00616                 //pilot position (boat coordinates):
00617                 a = -15.0;
00618                 b = 0.0;
00619                 c = 4.0;
00620                 //look at point (boat coordinates)
00621                 A = 100.0;
00622                 B = 0.0;
00623                 C = 0.0;
00624                 pilot[0] = d->x   +   cos(psi)*cos(theta)*a-sin(psi)*cos(theta)*b+sin(theta)*c;
00625                 pilot[1] = d->y   +   (sin(psi)*cos(phi)+cos(psi)*sin(theta)*sin(phi))*a+(cos(psi)*cos(phi)-sin(psi)*sin(theta)*sin(phi))*b-cos(theta)*sin(phi)*c;
00626                 pilot[2] = d->z   +      (sin(psi)*sin(phi)-cos(psi)*sin(theta)*cos(phi))*a+(cos(psi)*sin(phi)+sin(psi)*sin(theta)*cos(phi))*b+cos(theta)*cos(phi)*c;
00627                   
00628                 gluLookAt( pilot[0], pilot[1], pilot[2],
00629         
00630                                 d->x + cos(psi)*cos(theta)*A-sin(psi)*cos(theta)*B+sin(theta)*C, 
00631                                 d->y +  (sin(psi)*cos(phi)+cos(psi)*sin(theta)*sin(phi))*A+(cos(psi)*cos(phi)-sin(psi)*sin(theta)*sin(phi))*B-cos(theta)*sin(phi)*C, 
00632                                 d->z  +   (sin(psi)*sin(phi)-cos(psi)*sin(theta)*cos(phi))*A+(cos(psi)*sin(phi)+sin(psi)*sin(theta)*cos(phi))*B+cos(theta)*cos(phi)*C,
00633                            
00634                                 sin(d->theta),
00635                                 -cos(d->theta)*sin(d->phi),
00636                                 cos(d->theta)*cos(d->phi));
00637         }
00638 
00639         glTranslatef(d->x,d->y,0.0);
00640 
00641         wave_z_compute(d);//, gd);
00642         Draw_sea();//gd);
00643 
00644         glTranslatef(0.0,0.0,d->z);
00645         glRotatef(57.2958*d->phi,1.0f,0.0f,0.0f);
00646         glRotatef(57.2958*d->theta,0.0f,1.0f,0.0f);
00647         glRotatef(57.2958*d->psi,0.0f,0.0f,1.0f);
00648         
00649 
00650         Boat_draw();
00651         Sail_create_and_draw(d);
00652         RelativeWind_draw(d);
00653 
00654         glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); //?ici?
00655         
00656         
00657         glLoadIdentity();
00658         if (cos(viewAngle2)<0.0)
00659         {
00660                  // If looking from the other side, put the panorama on the opposite background
00661                 glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
00662                 glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
00663         }
00664         glRotatef(57.2958*(sign*viewAngle2 - 0.3f) ,1.0f,0.0f,0.0f);
00665         draw_panorama();
00666         
00667         
00668         
00669         /* Go in HUD-drawing mode */
00670                 glEnable2D();
00671                 glDisable(GL_DEPTH_TEST);
00672                 
00673                 if (!callibratingPhase)
00674                 {
00675                         /* Draw some text */
00676                         text_color.r = (Uint8)200;
00677                         text_color.g = (Uint8)200;
00678                         text_color.b = (Uint8)200;
00679                         //text_color.unused = (Uint8)255;
00680                 
00681                         text_position.x = 1*text_position.h;
00682                         text_position.y = 5*text_position.h;
00683                         //SDL_GL_RenderText("abc ABC 123.2 km/h", font, text_color, &text_position);
00684                         //SDL_GL_PrintText(font, text_color, &text_position, "Speed %3.2f km/h Course %3.0f deg", 3.6f*sqrt(d->dx*d->dx + d->dy*d->dy),-57.2958f*atan(d->dy/d->dx));
00685                         SDL_GL_PrintText(font, text_color, &text_position, "Speed  %3.2f", 3.6f*sqrt(d->dx*d->dx + d->dy*d->dy));
00686                         text_position.x += 110; SDL_GL_PrintText(font, text_color, &text_position, "km/h"); text_position.x -= 110;
00687                         text_position.y -= text_position.h;
00688                         SDL_GL_PrintText(font, text_color, &text_position, "Course  %3.0f",-57.2958f*atan(d->dy/d->dx));
00689                         text_position.x += 110; SDL_GL_PrintText(font, text_color, &text_position, "deg"); text_position.x -= 110;
00690                         text_position.y -= text_position.h;
00691                         SDL_GL_PrintText(font, text_color, &text_position, "Wind Speed  %3.2f", 3.6f*d->Wind);
00692                         text_position.x += 160; SDL_GL_PrintText(font, text_color, &text_position, "km/h"); text_position.x -= 160;
00693                         text_position.y -= text_position.h;
00694                         SDL_GL_PrintText(font, text_color, &text_position, "Rel Wind Speed  %3.2f",  3.6*sqrt((d->Wind_x - d->dx)*(d->Wind_x - d->dx)+(d->Wind_y - d->dy)*(d->Wind_y - d->dy)));
00695                         text_position.x += 180; SDL_GL_PrintText(font, text_color, &text_position, "km/h"); text_position.x -= 180;
00696                         text_position.y -= text_position.h;
00697                         SDL_GL_PrintText(font, text_color, &text_position, "FPS  %3.1f",  fps);
00698                 }
00699                 //Callibration Phase message:
00700                 if (callibratingPhase)
00701                 {
00702                         text_color.r = (Uint8)255;
00703                         text_color.g = (Uint8)100;
00704                         text_color.b = (Uint8)100;
00705                         
00706                         
00707                         text_position.x = screen->w /2 - 100;
00708                         text_position.y = screen->h /2;
00709                         SDL_GL_PrintText(fontBIG, text_color, &text_position, "Joystick Callibration");
00710                         
00711                         text_color.r = (Uint8)100;
00712                         text_color.g = (Uint8)255;
00713                         
00714                         text_position.y -= text_position.h;
00715                         text_position.x = screen->w /2 - 140;
00716                         SDL_GL_PrintText(fontBIG, text_color, &text_position, "Move all axes and hit SPACE");
00717                         
00718                         
00719                         
00720                 }
00721         
00722         
00723                 
00724                 /* Come out of HUD mode */
00725                 glEnable(GL_DEPTH_TEST);
00726                 glDisable2D();
00727 
00728         return TRUE;                                                            // Everything Went OK
00729 }
00730 
00731 
00732 
00733 void process_events( void ) 
00734 { 
00735         /* Our SDL event placeholder. */ 
00736         SDL_Event event; 
00737 
00738         /* Grab all the events off the queue. */ 
00739         while( SDL_PollEvent( &event ) )
00740         { 
00741                 switch( event.type )
00742                 { 
00743                 case SDL_KEYDOWN: 
00744                         /* Handle key presses. */ 
00745                         handle_key_down( &event.key.keysym ); 
00746                         break; 
00747                 case SDL_QUIT: 
00748                         /* Handle quit requests (like Ctrl-c). */ 
00749                         quit_tutorial( 0 ); 
00750                         break; 
00751                 case SDL_VIDEORESIZE:
00752                         resize_window(event.resize.h, event.resize.w);
00753                         break;
00754                 } 
00755         } 
00756 }
00757 
00758 
00759 
00760 void handle_key_down( SDL_keysym* keysym ) 
00761 { 
00762         switch( keysym->sym )
00763         { 
00764         case SDLK_ESCAPE: 
00765                 quit_tutorial( 0 ); 
00766                 break; 
00767                 
00768         case SDLK_y: 
00769                 dirY*=-1.0f;
00770                 break; 
00771         case SDLK_x: 
00772                 dirX*=-1.0f;
00773                 break; 
00774         case SDLK_c: 
00775                 dirZ*=-1.0f;
00776                 break; 
00777         case SDLK_v: 
00778                 dirR*=-1.0f;
00779                 break; 
00780                 
00781         case SDLK_1: 
00782                 doX=!doX;
00783                 break; 
00784         case SDLK_2: 
00785                 doY=!doY;
00786                 break; 
00787         case SDLK_3: 
00788                 doZ=!doZ;
00789                 break; 
00790         case SDLK_4: 
00791                 doR=!doR;
00792                 break; 
00793         case SDLK_SPACE:
00794                 callibratingPhase = FALSE;
00795                 break;
00796         case SDLK_o:
00797                 onBoardView = !onBoardView;
00798                 break;
00799                 
00800         default: 
00801                 break; 
00802         } 
00803 } 
00804 
00805 
00806 void do_on_keystate(datas *d)
00807 {
00808         Uint8 *keystate = SDL_GetKeyState(NULL);
00809         time_elapsed = d->t - time_before;
00810         time_before = d->t;
00811         
00812         if ( keystate[SDLK_q] ) viewAngle1 += dviewAngle;
00813         if ( keystate[SDLK_w] ) viewAngle1 -= dviewAngle;
00814         
00815         if ( keystate[SDLK_e] ) viewAngle2 += dviewAngle;
00816         if ( keystate[SDLK_r] ) viewAngle2 -= dviewAngle;
00817         
00818         if ( keystate[SDLK_t] ) viewDist += dviewDist;
00819         if ( keystate[SDLK_z] ) viewDist -= dviewDist;
00820         
00821         
00822         if (keystate[SDLK_UP]           &&      d->target_y<1.0f)        d->target_y += 1.5f*time_elapsed;
00823         if (keystate[SDLK_DOWN] &&      d->target_y>-1.0f) d->target_y -= 1.5f*time_elapsed;
00824 
00825         if (keystate[SDLK_RIGHT]        &&      d->target_x<1.0f) d->target_x += 1.5f*time_elapsed;
00826         if (keystate[SDLK_LEFT] &&      d->target_x>-1.0f) d->target_x -= 1.5f*time_elapsed;
00827 
00828         if (keystate[SDLK_m]    &&      d->Hslider_x<1.0f) d->Hslider_x += 1.5f*time_elapsed;
00829         if (keystate[SDLK_n]    &&      d->Hslider_x>-1.0f) d->Hslider_x -= 1.5f*time_elapsed;
00830 
00831         if (keystate[SDLK_h]    &&      d->Vslider_x<1.0f) d->Vslider_x += 1.5f*time_elapsed;
00832         if (keystate[SDLK_b]    &&      d->Vslider_x>-1.0f) d->Vslider_x -= 1.5f*time_elapsed;
00833         
00834         
00835         //Wind:
00836         if (keystate[SDLK_9]){ //more wind
00837                 d->Wind += 2.0f*time_elapsed;
00838                 d->Wind_x = -d->Wind*cos(d->Wind_angle);
00839                 d->Wind_y = -d->Wind*sin(d->Wind_angle);
00840         }
00841         if (keystate[SDLK_8] && d->Wind>0){ //less wind
00842                 d->Wind -= 2.0f*time_elapsed;
00843                 d->Wind_x = -d->Wind*cos(d->Wind_angle);
00844                 d->Wind_y = -d->Wind*sin(d->Wind_angle);
00845         }
00846 
00847         //Wave amplitude:
00848         if (keystate[SDLK_7]) {
00849                 d->Wave_amp0 += 0.15f*time_elapsed;
00850                 d->Wave_amp[0] = d->Wave_amp0/2.0;
00851                 d->Wave_amp[1] = d->Wave_amp0/4.0;
00852                 d->Wave_amp1 = d->Wave_amp[0];
00853                 d->Wave_amp2 = d->Wave_amp[1];
00854         }
00855         if (keystate[SDLK_6] && d->Wave_amp0>0.0f) {
00856                 d->Wave_amp0 -= 0.15f*time_elapsed;
00857                 d->Wave_amp[0] = d->Wave_amp0/2.0;
00858                 d->Wave_amp[1] = d->Wave_amp0/4.0;
00859                 d->Wave_amp1 = d->Wave_amp[0];
00860                 d->Wave_amp2 = d->Wave_amp[1];
00861         }
00862         
00863         //sail:
00864         if (keystate[SDLK_k] && d->max_abs_angle_baume<1.4835f) { //max 85 degree
00865                 d->max_abs_angle_baume += 0.1745*time_elapsed; // 10 degrees/second
00866         }
00867         if (keystate[SDLK_l] && d->max_abs_angle_baume>0.0f) { //min 0.0 degree
00868                 d->max_abs_angle_baume -= 0.1745*time_elapsed; // 10 degrees/second
00869         }
00870         //Sail goes with the wind and is constrained by +- d->max_abs_angle_baume:
00871         d->tanAngle_sail = tan(d->angle_girouette);
00872         if (atan(d->tanAngle_sail)>d->max_abs_angle_baume) d->tanAngle_sail=tan(d->max_abs_angle_baume);
00873         if (atan(d->tanAngle_sail)< -d->max_abs_angle_baume) d->tanAngle_sail=tan(-d->max_abs_angle_baume);
00874 }
00875 
00876 
00877 
00878 
00879 
00880 
00881 void pass_joy_parameters(joy_parameters *jP) //pass parameters here (comming from MATLAB)
00882 {
00883         doX = jP->doX;
00884         doY = jP->doY;
00885         doZ = jP->doZ;
00886         doR = jP->doR;
00887 
00888         dirX = jP->dirX;
00889         dirY = jP->dirY;
00890         dirZ = jP->dirZ;
00891         dirR = jP->dirR;
00892 }

Generated on Wed Sep 20 14:30:04 2006 for hydroSDL by  doxygen 1.4.7