- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

# D:/Basile/VisualCppProjects/hydroMEX3/hydroMEX3/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 HGLRC           hRC=NULL;                                                       // Permanent Rendering Context
    00015 HDC             hDC=NULL;                                                       // Private GDI Device Context
    00016 HWND            hWnd=NULL;                                                      // Holds Our Window Handle
    00017 HINSTANCE       hInstance;                                                      // Holds The Instance Of The Application
    00018
    00019 GLuint  base;                                                   // Base Display List For The Font Set
    00020
    00021
    00022 bool    keys[256];                                                                      // Array Used For The Keyboard Routine
    00023 bool    active=TRUE;                                                            // Window Active Flag Set To TRUE By Default
    00024 bool    fullscreen=TRUE;                                                        // Fullscreen Flag Set To Fullscreen Mode By Default
    00025
    00026 bool    light;                                                                  // Lighting ON / OFF
    00027
    00028
    00029
    00030 GLfloat viewAngle1 = 0.0;
    00031 GLfloat viewAngle2 = 0.3;
    00032 GLfloat dviewAngle = 0.01;
    00033 GLfloat viewDist = 50.0;
    00034 GLfloat dviewDist = 0.3;
    00035
    00036
    00037 GLfloat LightAmbient[]= { 0.35f, 0.35f, 0.5f, 1.0f };                           // Ambient Light Values ( NEW )
    00038 GLfloat LightDiffuse[]= { 0.6f, 0.6f, 0.6f, 1.0f };                              // Diffuse Light Values ( NEW )
    00039 GLfloat LightPosition[]= { -10.0f, 20.0f, 150.0f, 0.0f };                                // Light Position ( NEW )
    00040 //GLfloat LightPosition[]= { 0.0f, -10.0f, 10.0f, 1.0f };                                // Light Position ( NEW )
    00041
    00042 MSG     msg;                                                            // Windows Message Structure
    00043 BOOL    done=FALSE;                                                     // Bool Variable To Exit Loop
    00044
    00045 //joystick stuff
    00046 int joystick_exist;
    00047 bool doX = TRUE;        //TRUE: use jostick X-axis, FALSE: use keyboard
    00048 bool doY = TRUE;        //TRUE: use jostick Y-axis, FALSE: use keyboard
    00049 bool doZ = TRUE;        //TRUE: use jostick Z-axis, FALSE: use keyboard
    00050 bool doR = TRUE;        //TRUE: use jostick R-axis, FALSE: use keyboard
    00051 bool allowChangeDoX = TRUE; //ON-OFF behaviour
    00052 bool allowChangeDoY = TRUE;
    00053 bool allowChangeDoZ = TRUE; //ON-OFF behaviour
    00054 bool allowChangeDoR = TRUE;
    00055 float dirX = 1.0f;  //change axis direction: -1.0f
    00056 float dirY = 1.0f;
    00057 float dirZ = 1.0f;
    00058 float dirR = 1.0f;
    00059 bool allowChangeDirX = TRUE; //ON-OFF behaviour
    00060 bool allowChangeDirY = TRUE;
    00061 bool allowChangeDirZ = TRUE;
    00062 bool allowChangeDirR = TRUE;
    00063 float time_before = 0.0; //for keyboard controll and fps computing
    00064 float time_elapsed; //for keyboard controll and fps computing
    00065 float fps = 0.0; //frames per second
    00066
    00067
    00068
    00070
    00074 GLvoid BuildFont(GLvoid)                                        // Build Our Bitmap Font
    00075 {
    00076         HFONT   font;                                           // Windows Font ID
    00077         HFONT   oldfont;                                        // Used For Good House Keeping
    00078
    00079         base = glGenLists(96);                                  // Storage For 96 Characters ( NEW )
    00080
    00081         font = CreateFont(      -16,                            // Height Of Font ( NEW )
    00082                                 0,                              // Width Of Font
    00083                                 0,                              // Angle Of Escapement
    00084                                 0,                              // Orientation Angle
    00085                                 FW_BOLD,                        // Font Weight
    00086                                 FALSE,                          // Italic
    00087                                 FALSE,                          // Underline
    00088                                 FALSE,                          // Strikeout
    00089                                 ANSI_CHARSET,                   // Character Set Identifier
    00090                                 OUT_TT_PRECIS,                  // Output Precision
    00091                                 CLIP_DEFAULT_PRECIS,            // Clipping Precision
    00092                                 ANTIALIASED_QUALITY,            // Output Quality
    00093                                 FF_DONTCARE|DEFAULT_PITCH,      // Family And Pitch
    00094                                 "Courier New");                 // Font Name
    00095
    00096         oldfont = (HFONT)SelectObject(hDC, font);               // Selects The Font We Want
    00097         wglUseFontBitmaps(hDC, 32, 96, base);                   // Builds 96 Characters Starting At Character 32
    00098         SelectObject(hDC, oldfont);                             // Selects The Font We Want
    00099         DeleteObject(font);                                     // Delete The Font
    00100 }
    00101
    00102
    00104
    00107 GLvoid KillFont(GLvoid)                                         // Delete The Font List
    00108 {
    00109         glDeleteLists(base, 96);                                // Delete All 96 Characters ( NEW )
    00110 }
    00111
    00113
    00116 GLvoid glPrint(const char *fmt, ...)                            // Custom GL "Print" Routine
    00117 {
    00118         char            text[256];                              // Holds Our String
    00119         va_list         ap;                                     // Pointer To List Of Arguments
    00120
    00121         if (fmt == NULL)                                        // If There's No Text
    00122                 return;                                         // Do Nothing
    00123
    00124         va_start(ap, fmt);                                      // Parses The String For Variables
    00125             vsprintf(text, fmt, ap);                            // And Converts Symbols To Actual Numbers
    00126         va_end(ap);                                             // Results Are Stored In Text
    00127
    00128         glPushAttrib(GL_LIST_BIT);                              // Pushes The Display List Bits         ( NEW )
    00129         glListBase(base - 32);                                  // Sets The Base Character to 32        ( NEW )
    00130
    00131         glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);      // Draws The Display List Text  ( NEW )
    00132         glPopAttrib();                                          // Pops The Display List Bits   ( NEW )
    00133 }
    00134
    00135
    00136
    00137
    00138 GLvoid ReSizeGLScene(GLsizei width, GLsizei height)                             // Resize And Initialize The GL Window
    00139 {
    00140         if (height==0)                                                          // Prevent A Divide By Zero By
    00141         {
    00142                 height=1;                                                       // Making Height Equal One
    00143         }
    00144
    00145         glViewport(0, 0, width, height);                                        // Reset The Current Viewport
    00146
    00147         glMatrixMode(GL_PROJECTION);                                            // Select The Projection Matrix
    00148         glLoadIdentity();                                                       // Reset The Projection Matrix
    00149
    00150         // Calculate The Aspect Ratio Of The Window
    00151         gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    00152
    00153         glMatrixMode(GL_MODELVIEW);                                             // Select The Modelview Matrix
    00154         glLoadIdentity();                                                       // Reset The Modelview Matrix
    00155 }
    00156
    00157
    00158
    00159 int InitGL(GLvoid)                                                              // All Setup For OpenGL Goes Here
    00160 {
    00161         glShadeModel(GL_SMOOTH);                                                // Enables Smooth Shading
    00162         glClearColor(0.3f, 0.3f, 1.0f, 1.0f);                   // Black Background
    00163
    00164         glClearDepth(1.0f);                                                             // Depth Buffer Setup
    00165         glEnable(GL_DEPTH_TEST);                                                // Enables Depth Testing
    00166         glDepthFunc(GL_LEQUAL);                                                 // The Type Of Depth Test To Do
    00167
    00168         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);                      // Really Nice Perspective Calculations
    00169
    00170         glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);                         // Setup The Ambient Light
    00171         glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);                         // Setup The Diffuse Light
    00172         glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);                        // Position The Light
    00173         glEnable(GL_LIGHT0);                                                    // Enable Light One
    00174         glEnable(GL_LIGHTING);
    00175         glEnable( GL_COLOR_MATERIAL );
    00176         glEnable(GL_NORMALIZE);
    00177         glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    00178         glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0f );
    00179
    00180         BuildFont();                                            // Build The Font
    00181
    00182         return TRUE;                                                                    // Initialization Went OK
    00183 }
    00184
    00185
    00186
    00187
    00188
    00189 int DrawGLScene(datas *d)//, graphicDatas *gd)//(datas *d, seaDatas *sd, boatDatas *bd)                                                         // Here's Where We Do All The Drawing
    00190 {
    00191         double sign;
    00192         float X, Y, Z, R; //joystick axes
    00193
    00194         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                     // Clear The Screen And The Depth Buffer
    00195
    00196
    00197         if (abs((1.0f/time_elapsed))<300.0f) fps=0.99*fps+0.01*(1.0f/time_elapsed); //frames per second
    00198
    00199 //On Screen Display informations (text and instruments):
    00200         glLoadIdentity();
    00201
    00202         glDisable(GL_LIGHTING);
    00203
    00204         glTranslatef(0.0f,0.0f,-1.0f);  //translate for text display
    00205         glColor3f(0.85,0.05,0.05);                      //font color
    00206         glRasterPos2f(-0.5, -0.35);             //font position
    00207         glPrint("Speed %3.2f km/h    Course %3.0f deg",
    00208                         3.6f*sqrt(d->dx*d->dx + d->dy*d->dy),
    00209                         -57.2958f*atan(d->dy/d->dx));   // Print GL Text To The Screen
    00210         glRasterPos2f(-0.5, -0.375);            //font position
    00211         glPrint("Wind Speed %3.2f km/h,    %3.1f fps", 3.6f*d->Wind, fps);
    00212         glRasterPos2f(-0.5, -0.4);              //font position
    00213         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)));
    00214
    00215         glLoadIdentity();
    00216         glTranslatef(-0.45f,0.4f,-1.0f);
    00217         glScalef(0.08f,0.08f,0.08f);
    00218         Girouette_draw(d);
    00219
    00220         if (joystick_exist)
    00221         {
    00222                 joystick_getXYZR(&d->target_x, &d->target_y, &d->Vslider_x, &d->Hslider_x, doX, doY, doZ, doR);
    00223                 if (doX) d->target_x *= dirX;
    00224                 if (doY) d->target_y *= dirY;
    00225                 if (doZ) d->Vslider_x *= dirZ;
    00226                 if (doR) d->Hslider_x *= dirR;
    00227         }
    00228
    00229         angles_update(d); //update cal angles from target/sliders position
    00230
    00231         glLoadIdentity();
    00232         glTranslatef(0.48f,-0.35f,-1.0f);
    00233         glScalef(0.05f,0.05f,0.04f);
    00234         Target_draw(d->target_x, d->target_y); //draw target instrument
    00235
    00236         glLoadIdentity();
    00237         glTranslatef(0.35f,-0.35f,-1.0f);
    00238         glScalef(0.1f,0.1f,0.1f);
    00239         HSlider_draw(d->Hslider_x);  //draw horizontal slider instrument
    00240
    00241         glLoadIdentity();
    00242         glTranslatef(0.25f,-0.35f,-1.0f);
    00243         glScalef(0.1f,0.1f,0.1f);
    00244         VSlider_draw(d->Vslider_x);  //draw vertical slider instrument
    00245
    00246         glEnable(GL_LIGHTING);
    00247
    00248 //3D graphics:
    00249         glLoadIdentity();                               //back to identity
    00250
    00251         if (cos(viewAngle2) >= 0) sign=1.0; //avoid looking up-side-down when going over 90deg of vertical view angle
    00252         else sign=-1.0;
    00253
    00254         gluLookAt(    d->x - cos(viewAngle1)*cos(viewAngle2)*viewDist,   //look at boat
    00255                                   d->y + sin(viewAngle1)*cos(viewAngle2)*viewDist,
    00256                                   d->z + sin(viewAngle2)*viewDist,
    00257
    00258                                   d->x,d->y,d->z+12.0,
    00259
    00260                                   sin(viewAngle2)*cos(viewAngle1) * sign,
    00261                                   -sin(viewAngle2)*sin(viewAngle1) * sign,
    00262                                   cos(viewAngle2) * sign);
    00263
    00264         glTranslatef(d->x,d->y,0.0);
    00265
    00266         wave_z_compute(d);//, gd);
    00267         Draw_sea();//gd);
    00268
    00269         glTranslatef(0.0,0.0,d->z);
    00270         glRotatef(57.2958*d->phi,1.0f,0.0f,0.0f);
    00271         glRotatef(57.2958*d->theta,0.0f,1.0f,0.0f);
    00272         glRotatef(57.2958*d->psi,0.0f,0.0f,1.0f);
    00273
    00274         Boat_draw();
    00275         Sail_create_and_draw(d);
    00276         RelativeWind_draw(d);
    00277
    00278         glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); //?ici?
    00279
    00280
    00281         return TRUE;                                                            // Everything Went OK
    00282 }
    00283
    00284
    00285
    00286
    00287 GLvoid KillGLWindow(GLvoid)                                                     // Properly Kill The Window
    00288 {
    00289         if (fullscreen)                                                         // Are We In Fullscreen Mode?
    00290         {
    00291                 ChangeDisplaySettings(NULL,0);                                  // If So Switch Back To The Desktop
    00292                 ShowCursor(TRUE);                                               // Show Mouse Pointer
    00293         }
    00294
    00295         if (hRC)                                                                // Do We Have A Rendering Context?
    00296         {
    00297                 if (!wglMakeCurrent(NULL,NULL))                                 // Are We Able To Release The DC And RC Contexts?
    00298                 {
    00299                         MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
    00300                 }
    00301
    00302                 if (!wglDeleteContext(hRC))                                     // Are We Able To Delete The RC?
    00303                 {
    00304                         MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
    00305                 }
    00306                 hRC=NULL;                                                       // Set RC To NULL
    00307         }
    00308
    00309         if (hDC && !ReleaseDC(hWnd,hDC))                                        // Are We Able To Release The DC
    00310         {
    00311                 MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
    00312                 hDC=NULL;                                                       // Set DC To NULL
    00313         }
    00314
    00315         if (hWnd && !DestroyWindow(hWnd))                                       // Are We Able To Destroy The Window?
    00316         {
    00317                 MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
    00318                 hWnd=NULL;                                                      // Set hWnd To NULL
    00319         }
    00320
    00321         if (!UnregisterClass("OpenGL",hInstance))                               // Are We Able To Unregister Class
    00322         {
    00323                 MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
    00324                 hInstance=NULL;                                                 // Set hInstance To NULL
    00325         }
    00326 }
    00327
    00328
    00329
    00330
    00331 BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
    00332 {
    00333         GLuint          PixelFormat;                                            // Holds The Results After Searching For A Match
    00334
    00335         WNDCLASS        wc;                                                     // Windows Class Structure
    00336
    00337         DWORD           dwExStyle;                                              // Window Extended Style
    00338         DWORD           dwStyle;                                                // Window Style
    00339
    00340         RECT WindowRect;                                                        // Grabs Rectangle Upper Left / Lower Right Values
    00341         WindowRect.left=(long)0;                                                // Set Left Value To 0
    00342         WindowRect.right=(long)width;                                           // Set Right Value To Requested Width
    00343         WindowRect.top=(long)0;                                                 // Set Top Value To 0
    00344         WindowRect.bottom=(long)height;                                         // Set Bottom Value To Requested Height
    00345
    00346         fullscreen=fullscreenflag;                                              // Set The Global Fullscreen Flag
    00347
    00348         hInstance               = GetModuleHandle(NULL);                        // Grab An Instance For Our Window
    00349         wc.style                = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;           // Redraw On Move, And Own DC For Window
    00350         wc.lpfnWndProc          = (WNDPROC) WndProc;                            // WndProc Handles Messages
    00351         wc.cbClsExtra           = 0;                                            // No Extra Window Data
    00352         wc.cbWndExtra           = 0;                                            // No Extra Window Data
    00353         wc.hInstance            = hInstance;                                    // Set The Instance
    00354         wc.hIcon                = LoadIcon(NULL, IDI_WINLOGO);                  // Load The Default Icon
    00355         wc.hCursor              = LoadCursor(NULL, IDC_ARROW);                  // Load The Arrow Pointer
    00356         wc.hbrBackground        = NULL;                                         // No Background Required For GL
    00357         wc.lpszMenuName         = NULL;                                         // We Don't Want A Menu
    00358         wc.lpszClassName        = "OpenGL";                                     // Set The Class Name
    00359
    00360         if (!RegisterClass(&wc))                                                // Attempt To Register The Window Class
    00361         {
    00362                 MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00363                 return FALSE;                                                   // Exit And Return FALSE
    00364         }
    00365
    00366         if (fullscreen)                                                         // Attempt Fullscreen Mode?
    00367         {
    00368                 DEVMODE dmScreenSettings;                                       // Device Mode
    00369                 memset(&dmScreenSettings,0,sizeof(dmScreenSettings));           // Makes Sure Memory's Cleared
    00370                 dmScreenSettings.dmSize=sizeof(dmScreenSettings);               // Size Of The Devmode Structure
    00371                 dmScreenSettings.dmPelsWidth    = width;                        // Selected Screen Width
    00372                 dmScreenSettings.dmPelsHeight   = height;                       // Selected Screen Height
    00373                 dmScreenSettings.dmBitsPerPel   = bits;                         // Selected Bits Per Pixel
    00374                 dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    00375
    00376                 // Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
    00377                 if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
    00378                 {
    00379                         // If The Mode Fails, Offer Two Options.  Quit Or Run In A Window.
    00380                         if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
    00381                         {
    00382                                 fullscreen=FALSE;                               // Select Windowed Mode (Fullscreen=FALSE)
    00383                         }
    00384                         else
    00385                         {
    00386                                 // Pop Up A Message Box Letting User Know The Program Is Closing.
    00387                                 MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
    00388                                 return FALSE;                                   // Exit And Return FALSE
    00389                         }
    00390                 }
    00391         }
    00392
    00393         if (fullscreen)                                                         // Are We Still In Fullscreen Mode?
    00394         {
    00395                 dwExStyle=WS_EX_APPWINDOW;                                      // Window Extended Style
    00396                 dwStyle=WS_POPUP;                                               // Windows Style
    00397                 ShowCursor(FALSE);                                              // Hide Mouse Pointer
    00398         }
    00399         else
    00400         {
    00401                 dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;                   // Window Extended Style
    00402                 dwStyle=WS_OVERLAPPEDWINDOW;                                    // Windows Style
    00403         }
    00404
    00405         AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);             // Adjust Window To True Requested Size
    00406
    00407         if (!(hWnd=CreateWindowEx(      dwExStyle,                              // Extended Style For The Window
    00408                                         "OpenGL",                               // Class Name
    00409                                         title,                                  // Window Title
    00410                                         WS_CLIPSIBLINGS |                       // Required Window Style
    00411                                         WS_CLIPCHILDREN |                       // Required Window Style
    00412                                         dwStyle,                                // Selected Window Style
    00413                                         0, 0,                                   // Window Position
    00414                                         WindowRect.right-WindowRect.left,       // Calculate Adjusted Window Width
    00415                                         WindowRect.bottom-WindowRect.top,       // Calculate Adjusted Window Height
    00416                                         NULL,                                   // No Parent Window
    00417                                         NULL,                                   // No Menu
    00418                                         hInstance,                              // Instance
    00419                                         NULL)))                                 // Don't Pass Anything To WM_CREATE
    00420         {
    00421                 KillGLWindow();                                                 // Reset The Display
    00422                 MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00423                 return FALSE;                                                   // Return FALSE
    00424         }
    00425
    00426
    00427         static  PIXELFORMATDESCRIPTOR pfd=                                      // pfd Tells Windows How We Want Things To Be
    00428         {
    00429                 sizeof(PIXELFORMATDESCRIPTOR),                                  // Size Of This Pixel Format Descriptor
    00430                 1,                                                              // Version Number
    00431                 PFD_DRAW_TO_WINDOW |                                            // Format Must Support Window
    00432                 PFD_SUPPORT_OPENGL |                                            // Format Must Support OpenGL
    00433                 PFD_DOUBLEBUFFER,                                               // Must Support Double Buffering
    00434                 PFD_TYPE_RGBA,                                                  // Request An RGBA Format
    00435                 bits,                                                           // Select Our Color Depth
    00436                 0, 0, 0, 0, 0, 0,                                               // Color Bits Ignored
    00437                 0,                                                              // No Alpha Buffer
    00438                 0,                                                              // Shift Bit Ignored
    00439                 0,                                                              // No Accumulation Buffer
    00440                 0, 0, 0, 0,                                                     // Accumulation Bits Ignored
    00441                 16,                                                             // 16Bit Z-Buffer (Depth Buffer)
    00442                 0,                                                              // No Stencil Buffer
    00443                 0,                                                              // No Auxiliary Buffer
    00444                 PFD_MAIN_PLANE,                                                 // Main Drawing Layer
    00445                 0,                                                              // Reserved
    00446                 0, 0, 0                                                         // Layer Masks Ignored
    00447         };
    00448
    00449
    00450         if (!(hDC=GetDC(hWnd)))                                                 // Did We Get A Device Context?
    00451         {
    00452                 KillGLWindow();                                                 // Reset The Display
    00453                 MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00454                 return FALSE;                                                   // Return FALSE
    00455         }
    00456
    00457
    00458         if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))                         // Did Windows Find A Matching Pixel Format?
    00459         {
    00460                 KillGLWindow();                                                 // Reset The Display
    00461                 MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00462                 return FALSE;                                                   // Return FALSE
    00463         }
    00464
    00465
    00466         if(!SetPixelFormat(hDC,PixelFormat,&pfd))                               // Are We Able To Set The Pixel Format?
    00467         {
    00468                 KillGLWindow();                                                 // Reset The Display
    00469                 MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00470                 return FALSE;                                                   // Return FALSE
    00471         }
    00472
    00473
    00474         if (!(hRC=wglCreateContext(hDC)))                                       // Are We Able To Get A Rendering Context?
    00475         {
    00476                 KillGLWindow();                                                 // Reset The Display
    00477                 MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00478                 return FALSE;                                                   // Return FALSE
    00479         }
    00480
    00481
    00482         if(!wglMakeCurrent(hDC,hRC))                                            // Try To Activate The Rendering Context
    00483         {
    00484                 KillGLWindow();                                                 // Reset The Display
    00485                 MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00486                 return FALSE;                                                   // Return FALSE
    00487         }
    00488
    00489
    00490         ShowWindow(hWnd,SW_SHOW);                                               // Show The Window
    00491         SetForegroundWindow(hWnd);                                              // Slightly Higher Priority
    00492         SetFocus(hWnd);                                                         // Sets Keyboard Focus To The Window
    00493         ReSizeGLScene(width, height);                                           // Set Up Our Perspective GL Screen
    00494
    00495
    00496         if (!InitGL())                                                          // Initialize Our Newly Created GL Window
    00497         {
    00498                 KillGLWindow();                                                 // Reset The Display
    00499                 MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
    00500                 return FALSE;                                                   // Return FALSE
    00501         }
    00502
    00503
    00504         return TRUE;                                                            // Success
    00505 }
    00506
    00507
    00508
    00510 LRESULT CALLBACK WndProc(       HWND    hWnd,                                   // Handle For This Window
    00511                                 UINT    uMsg,                                   // Message For This Window
    00512                                 WPARAM  wParam,                                 // Additional Message Information
    00513                                 LPARAM  lParam)                                 // Additional Message Information
    00514 {
    00515         switch (uMsg)                                                           // Check For Windows Messages
    00516         {
    00517                 case WM_ACTIVATE:                                               // Watch For Window Activate Message
    00518                 {
    00519                         if (!HIWORD(wParam))                                    // Check Minimization State
    00520                         {
    00521                                 active=TRUE;                                    // Program Is Active
    00522                         }
    00523                         else
    00524                         {
    00525                                 active=FALSE;                                   // Program Is No Longer Active
    00526                         }
    00527
    00528                         return 0;                                               // Return To The Message Loop
    00529                 }
    00530                 case WM_SYSCOMMAND:                                             // Intercept System Commands
    00531                 {
    00532                         switch (wParam)                                         // Check System Calls
    00533                         {
    00534                                 case SC_SCREENSAVE:                             // Screensaver Trying To Start?
    00535                                 case SC_MONITORPOWER:                           // Monitor Trying To Enter Powersave?
    00536                                 return 0;                                       // Prevent From Happening
    00537                         }
    00538                         break;                                                  // Exit
    00539                 }
    00540                 case WM_CLOSE:                                                  // Did We Receive A Close Message?
    00541                 {
    00542                         PostQuitMessage(0);                                     // Send A Quit Message
    00543                         return 0;                                               // Jump Back
    00544                 }
    00545                 case WM_KEYDOWN:                                                // Is A Key Being Held Down?
    00546                 {
    00547                         keys[wParam] = TRUE;                                    // If So, Mark It As TRUE
    00548                         return 0;                                               // Jump Back
    00549                 }
    00550                 case WM_KEYUP:                                                  // Has A Key Been Released?
    00551                 {
    00552                         keys[wParam] = FALSE;                                   // If So, Mark It As FALSE
    00553                         return 0;                                               // Jump Back
    00554                 }
    00555                 case WM_SIZE:                                                   // Resize The OpenGL Window
    00556                 {
    00557                         ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));           // LoWord=Width, HiWord=Height
    00558                         return 0;                                               // Jump Back
    00559                 }
    00560         }
    00561
    00562         // Pass All Unhandled Messages To DefWindowProc
    00563         return DefWindowProc(hWnd,uMsg,wParam,lParam);
    00564 }
    00565
    00566
    00567
    00568 int draw_graphics_init(datas *d)//, graphicDatas *gd)
    00569 {
    00570
    00571         // Ask The User Which Screen Mode They Prefer
    00572         if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
    00573         {
    00574                 fullscreen=FALSE;                                               // Windowed Mode
    00575         }
    00576
    00577         // Create Our OpenGL Window
    00578         if (!CreateGLWindow("Hydroptere",640,480,16,fullscreen))
    00579         {
    00580                 return 0;                                                       // Quit If Window Was Not Created
    00581         }
    00582
    00583
    00584         //joystick:
    00585         if (joystick_init()!=1)
    00586         {
    00587                 printf("Can't initialize joystick");
    00588                 joystick_exist = 0;
    00589         }
    00590         else
    00591         {
    00592                 joystick_exist = 1;
    00593         }
    00594
    00595         return 1; //OK...
    00596 }
    00597
    00598
    00599 bool draw_graphics(datas *d)//, graphicDatas *gd)
    00600 {
    00601         float tempAngle;
    00602
    00603         if(!done)                                                               // Loop That Runs Until done=TRUE
    00604         {
    00605                 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))                       // Is There A Message Waiting?
    00606                 {
    00607                         if (msg.message==WM_QUIT)                               // Have We Received A Quit Message?
    00608                         {
    00609                                 done=TRUE;                                      // If So done=TRUE
    00610                         }
    00611                         else                                                    // If Not, Deal With Window Messages
    00612                         {
    00613                                 TranslateMessage(&msg);                         // Translate The Message
    00614                                 DispatchMessage(&msg);                          // Dispatch The Message
    00615                         }
    00616                 }
    00617                 else                                                            // If There Are No Messages
    00618                 {
    00619                         // Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
    00620                         if (active)                                             // Program Active?
    00621                         {
    00622                                 if (keys[VK_ESCAPE])                            // Was ESC Pressed?
    00623                                 {
    00624                                         done=TRUE;                              // ESC Signalled A Quit
    00625                                 }
    00626                                 else                                            // Not Time To Quit, Update Screen
    00627                                 {
    00628                                         //DrawGLScene(d, sd, bd);                               // Draw The Scene
    00629                                         DrawGLScene(d);//,gd);
    00630                                         SwapBuffers(hDC);                       // Swap Buffers (Double Buffering)
    00631                                 }
    00632                         }
    00633                         if (keys[VK_F1])                                        // Is F1 Being Pressed?
    00634                         {
    00635                                 keys[VK_F1]=FALSE;                              // If So Make Key FALSE
    00636                                 KillGLWindow();                                 // Kill Our Current Window
    00637                                 fullscreen=!fullscreen;                         // Toggle Fullscreen / Windowed Mode
    00638                                 // Recreate Our OpenGL Window
    00639                                 if (!CreateGLWindow("Hydroptere",640,480,16,fullscreen))
    00640                                 {
    00641                                         return FALSE;                           // Quit If Window Was Not Created
    00642                                 }
    00643                         }
    00644                 }
    00645         }
    00646         //Change viewpoint from keyboard:
    00647         if (keys['Q'])
    00648         {
    00649                 viewAngle1 += dviewAngle;
    00650         }
    00651         if (keys['W'])
    00652         {
    00653                 viewAngle1 -= dviewAngle;
    00654         }
    00655         if (keys['E'])
    00656         {
    00657                 viewAngle2 += dviewAngle;
    00658         }
    00659         if (keys['R'])
    00660         {
    00661                 viewAngle2 -= dviewAngle;
    00662         }
    00663         if (keys['T'])
    00664         {
    00665                 viewDist += dviewDist;
    00666         }
    00667         if (keys['Z'])
    00668         {
    00669                 viewDist -= dviewDist;
    00670         }
    00671         //Joystick directions changes:
    00672         if (keys['X'] && allowChangeDirX) {dirX*=-1.0f; allowChangeDirX = FALSE;}
    00673         else if (!keys['X']) allowChangeDirX = TRUE;
    00674
    00675         if (keys['Y'] && allowChangeDirY) {dirY*=-1.0f; allowChangeDirY = FALSE;}
    00676         else if (!keys['Y']) allowChangeDirY = TRUE;
    00677
    00678         if (keys['V'] && allowChangeDirZ) {dirZ*=-1.0f; allowChangeDirZ = FALSE;}
    00679         else if (!keys['V']) allowChangeDirZ = TRUE;
    00680
    00681         if (keys['C'] && allowChangeDirR) {dirR*=-1.0f; allowChangeDirR = FALSE;}
    00682         else if (!keys['C']) allowChangeDirR = TRUE;
    00683
    00684         //use keyboard or joystick for each axes
    00685         if (keys['1'] && allowChangeDoX) {doX=!doX; allowChangeDoX = FALSE;}
    00686         else if (!keys['1']) allowChangeDoX = TRUE;
    00687
    00688         if (keys['2'] && allowChangeDoY) {doY=!doY; allowChangeDoY = FALSE;}
    00689         else if (!keys['2']) allowChangeDoY = TRUE;
    00690
    00691         if (keys['3'] && allowChangeDoZ) {doZ=!doZ; allowChangeDoZ = FALSE;}
    00692         else if (!keys['3']) allowChangeDoZ = TRUE;
    00693
    00694         if (keys['4'] && allowChangeDoR) {doR=!doR; allowChangeDoR = FALSE;}
    00695         else if (!keys['4']) allowChangeDoR = TRUE;
    00696
    00697
    00698         //keyboard controll:
    00699         time_elapsed = d->t - time_before;
    00700
    00701         if (keys[VK_UP]         &&      d->target_y<1.0f)        d->target_y += 1.5f*time_elapsed;
    00702         if (keys[VK_DOWN]       &&      d->target_y>-1.0f) d->target_y -= 1.5f*time_elapsed;
    00703
    00704         if (keys[VK_RIGHT]      &&      d->target_x<1.0f) d->target_x += 1.5f*time_elapsed;
    00705         if (keys[VK_LEFT]       &&      d->target_x>-1.0f) d->target_x -= 1.5f*time_elapsed;
    00706
    00707         if (keys['M']   &&      d->Hslider_x<1.0f) d->Hslider_x += 1.5f*time_elapsed;
    00708         if (keys['N']   &&      d->Hslider_x>-1.0f) d->Hslider_x -= 1.5f*time_elapsed;
    00709
    00710         if (keys['H']   &&      d->Vslider_x<1.0f) d->Vslider_x += 1.5f*time_elapsed;
    00711         if (keys['B']   &&      d->Vslider_x>-1.0f) d->Vslider_x -= 1.5f*time_elapsed;
    00712
    00713         if (keys[VK_SPACE]) {d->target_x=0.0f; d->target_y=0.0; d->Hslider_x=0.0f; d->Vslider_x=0.0f;} //recenter all commands
    00714
    00715         //sail:
    00716         if (keys['K'] && d->max_abs_angle_baume<1.4835f) { //max 85 degree
    00717                 d->max_abs_angle_baume += 0.1745*time_elapsed; // 10 degrees/second
    00718         }
    00719         if (keys['L'] && d->max_abs_angle_baume>0.0f) { //min 0.0 degree
    00720                 d->max_abs_angle_baume -= 0.1745*time_elapsed; // 10 degrees/second
    00721         }
    00722         //Sail goes with the wind and is constrained by +- d->max_abs_angle_baume:
    00723         d->tanAngle_sail = tan(d->angle_girouette);
    00724         if (atan(d->tanAngle_sail)>d->max_abs_angle_baume) d->tanAngle_sail=tan(d->max_abs_angle_baume);
    00725         if (atan(d->tanAngle_sail)< -d->max_abs_angle_baume) d->tanAngle_sail=tan(-d->max_abs_angle_baume);
    00726 /*      if (keys['K'] && atan(d->tanAngle_sail)<1.4835f) { //max 85 degree
    00727                 tempAngle = atan(d->tanAngle_sail);
    00728                 tempAngle += 0.1745*time_elapsed; // 10 degrees/second
    00729                 d->tanAngle_sail = tan(tempAngle);
    00730         }
    00731         if (keys['L'] && atan(d->tanAngle_sail)>-1.4835f) { //min -85 degree
    00732                 tempAngle = atan(d->tanAngle_sail);
    00733                 tempAngle -= 0.1745*time_elapsed; // 10 degrees/second
    00734                 d->tanAngle_sail = tan(tempAngle);
    00735         }
    00736 */
    00737
    00738         //Wind:
    00739         if (keys['9']){ //more wind
    00740                 d->Wind += 2.0f*time_elapsed;
    00741                 d->Wind_x = -d->Wind*cos(d->Wind_angle);
    00742                 d->Wind_y = -d->Wind*sin(d->Wind_angle);
    00743         }
    00744         if (keys['8'] && d->Wind>0){ //less wind
    00745                 d->Wind -= 2.0f*time_elapsed;
    00746                 d->Wind_x = -d->Wind*cos(d->Wind_angle);
    00747                 d->Wind_y = -d->Wind*sin(d->Wind_angle);
    00748         }
    00749
    00750         //Wave amplitude:
    00751         if (keys['7']) {
    00752                 d->Wave_amp0 += 0.15f*time_elapsed;
    00753                 d->Wave_amp[0] = d->Wave_amp0/2.0;
    00754                 d->Wave_amp[1] = d->Wave_amp0/4.0;
    00755                 d->Wave_amp1 = d->Wave_amp[0];
    00756                 d->Wave_amp2 = d->Wave_amp[1];
    00757         }
    00758         if (keys['6'] && d->Wave_amp0>0.0f) {
    00759                 d->Wave_amp0 -= 0.15f*time_elapsed;
    00760                 d->Wave_amp[0] = d->Wave_amp0/2.0;
    00761                 d->Wave_amp[1] = d->Wave_amp0/4.0;
    00762                 d->Wave_amp1 = d->Wave_amp[0];
    00763                 d->Wave_amp2 = d->Wave_amp[1];
    00764         }
    00765
    00766
    00767         time_before = d->t;
    00768
    00769
    00770         return (!done);
    00771 }
    00772
    00773
    00774 int draw_graphics_kill()
    00775 {
    00776         // Shutdown
    00777         KillGLWindow();                                                         // Kill The Window
    00778         done=FALSE; //so that windows do not close imediatelly if MEX is relaunched in same MATLAB session
    00779
    00780         joystick_close();
    00781
    00782         return (msg.wParam);                                                    // Exit The Program
    00783 }
    00784
    00785
    00786
    00787
    00788 void pass_joy_parameters(joy_parameters *jP) //pass parameters here (comming from MATLAB)
    00789 {
    00790         doX = jP->doX;
    00791         doY = jP->doY;
    00792         doZ = jP->doZ;
    00793         doR = jP->doR;
    00794
    00795         dirX = jP->dirX;
    00796         dirY = jP->dirY;
    00797         dirZ = jP->dirZ;
    00798         dirR = jP->dirR;
    00799 }