| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 
 |  
 
// ============================================================================
// declarations
// ============================================================================
 
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
 
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
 
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
 
#if !wxUSE_GLCANVAS
    #error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
#endif
 
#include "wx/glcanvas.h"
#include "simple.h"
#include <GL/glu.h>
 
// the application icon (under Windows and OS/2 it is in resources and even
// though we could still include the XPM here it would be unused)
#ifndef wxHAS_IMAGES_IN_RESOURCES
    #include "sample.xpm"
#endif
 
// ============================================================================
// implementation
// ============================================================================
 
// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------
 
// Création de l'application
wxIMPLEMENT_APP(MyApp);
 
// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;
 
    // create the main application window
    MyFrame* CurrentFrame = new MyFrame("wxWidgets Simple OpenGL");
 
    //Exit if the required visual attributes or OGL context couldn't be created
    if ( ! CurrentFrame->OGLAvailable() )
        return false;
 
    // As of October 2015 GTK+ needs the frame to be shown before we call SetCurrent()
    CurrentFrame->Show(true);
 
    return true;
}
 
// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------
 
// frame constructor
MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500, 400))
{
    // set the frame icon
 //   SetIcon(wxICON(sample));
    SetIcon(wxICON(aaaa)); // To Set App Icon
 
    // Test des attributs d'OpenGL
    wxGLAttributes vAttrs;
    // Defaults should be accepted
    vAttrs.PlatformDefaults().Defaults().EndList();
    bool accepted = wxGLCanvas::IsDisplaySupported(vAttrs) ;
 
    if ( ! accepted )
    {
        // Try again without sample buffers
        vAttrs.Reset();
        vAttrs.PlatformDefaults().RGBA().DoubleBuffer().Depth(16).EndList();
        accepted = wxGLCanvas::IsDisplaySupported(vAttrs) ;
 
        if ( !accepted )
        {
            wxMessageBox("Visual attributes for OpenGL are not accepted.\nThe app will exit now.",
                         "Error with OpenGL", wxOK | wxICON_ERROR);
        }
    }
 
    // Les attributs sont bons, on peut créer le canvas
    if ( accepted )
        Fra_Canvas = new MyGLCanvas(this, vAttrs);
 
    // On défini une taille minimale pour la fenêtre
    // (y se passe des trucs bizarres sinon quand on réduit totalement la fenêtre !)
    SetMinSize(wxSize(250, 200));
}
 
MyFrame::~MyFrame()
{
    if ( Fra_Canvas )
        delete Fra_Canvas;
}
 
// event handlers
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
    // true is to force the frame to close
    Close(true);
}
 
bool MyFrame::OGLAvailable()
{
    if ( ! Fra_Canvas )
        return false;
 
    //Test if OGL context could be created.
    return Fra_Canvas->OglCtxAvailable();
}
 
// ----------------------------------------------------------------------------
// The canvas inside the frame. Our OpenGL connection
// ----------------------------------------------------------------------------
 
//We create a wxGLContext in this constructor.
//We do OGL initialization at OnSize().
MyGLCanvas::MyGLCanvas(MyFrame* parent, const wxGLAttributes& canvasAttrs)
                       : wxGLCanvas(parent, canvasAttrs)
{
    // Le parent (frame) du canvas
    Can_Parent = parent;
 
    // Explicitly create a new rendering context instance for this canvas.
    wxGLContextAttrs ctxAttrs;
    ctxAttrs.PlatformDefaults().CoreProfile().OGLVersion(3, 2).EndList();
    Can_OglContext = new wxGLContext(this, NULL, &ctxAttrs);
 
    // La création du contexte a foiré
    if ( !Can_OglContext->IsOK() )
    {
        wxMessageBox("This sample needs an OpenGL 3.2 capable driver.\nThe app will end now.",
                     "OpenGL version error", wxOK | wxICON_INFORMATION, this);
        delete Can_OglContext;
        Can_OglContext = NULL;
    }
}
 
MyGLCanvas::~MyGLCanvas()
{
    if ( Can_OglContext )
    {
        SetCurrent(*Can_OglContext);
        delete Can_OglContext;
        Can_OglContext = NULL;
    }
}
 
bool MyGLCanvas::oglInit()
{
    if ( !Can_OglContext )
        return false;
 
    // The current context must be set before we get OGL pointers
    SetCurrent(*Can_OglContext);
 
    // Non nécessaire, juste récupérer les infos sur le hardware
    // Get the GL version for the current OGL context
    wxString sglVer = "\nUsing OpenGL version: ";
    sglVer += wxString::FromUTF8(
                reinterpret_cast<const char *>(glGetString(GL_VERSION)) );
    // Also Vendor and Renderer
    sglVer += "\nVendor: ";
    sglVer += wxString::FromUTF8(
                reinterpret_cast<const char *>(glGetString(GL_VENDOR)) );
    sglVer += "\nRenderer: ";
    sglVer += wxString::FromUTF8(
                reinterpret_cast<const char *>(glGetString(GL_RENDERER)) );
    Can_Parent->SetOGLString(sglVer);
 
    return true;
}
 
void MyGLCanvas::prepare3DViewport(int topleft_x, int topleft_y, int bottomrigth_x, int bottomrigth_y)
{
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
    glClearDepth(1.0f);	// Depth Buffer Setup
    glEnable(GL_DEPTH_TEST); // Enables Depth Testing
    glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 
    glEnable(GL_COLOR_MATERIAL);
 
    glViewport(topleft_x, topleft_y, bottomrigth_x-topleft_x, bottomrigth_y-topleft_y);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
 
    float ratio_w_h = (float)(bottomrigth_x-topleft_x)/(float)(bottomrigth_y-topleft_y);
    gluPerspective(45 /*view angle*/, ratio_w_h, 0.1 /*clip close*/, 200 /*clip far*/);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 
void MyGLCanvas::OnPaint( wxPaintEvent& WXUNUSED(event) )
{
    // This is a dummy, to avoid an endless succession of paint messages.
    // OnPaint handlers must always create a wxPaintDC.
    wxPaintDC dc(this);  //wxClientDC
 
    // Avoid painting when we have not yet a size
    if ( Can_WinHeight < 1)
        return;
 
    // This should not be needed, while we have only one canvas
    SetCurrent(*Can_OglContext);
 
    // Do the paint of a simple figure
    switch (draw)
    {
        case draw_None: break;
        case draw_Axe: DoAxe(); break;
        case draw_Cube: DoCube(1.0); break;
        case draw_Sphere: DoSphere(2.0); break;
    }
 
    SwapBuffers();
}
 
//Note:
// You may wonder why OpenGL initialization was not done at wxGLCanvas ctor.
// The reason is due to GTK+/X11 working asynchronously, we can't call
// SetCurrent() before the window is shown on screen (GTK+ doc's say that the
// window must be realized first).
// In wxGTK, window creation and sizing requires several size-events. At least
// one of them happens after GTK+ has notified the realization. We use this
// circumstance and do initialization then.
 
void MyGLCanvas::OnSize(wxSizeEvent& event)
{
    event.Skip();
 
    // If this window is not fully initialized, dismiss this event
    if ( !IsShownOnScreen() )
        return;
 
    // On termine l'initialisation du GLCanvas
    if (Can_WinHeight == 0)
    {
        if ( !oglInit() )
            return;
        //Some GPUs need an additional forced paint event
        PostSizeEvent();
    }
 
    // This is normally only necessary if there is more than one wxGLCanvas
    // or more than one wxGLContext in the application.
    SetCurrent(*Can_OglContext);
 
    // It's up to the application code to update the OpenGL viewport settings.
  //  const wxSize size = event.GetSize() * GetContentScaleFactor();
//    Can_WinHeight = size.y;
//    m_oglManager->SetViewport(0, 0, size.x, Can_WinHeight);
//glViewport(0, 0, size.x, Can_WinHeight);
 
//    const wxSize ClientSize = GetClientSize() * GetContentScaleFactor();
 
 //   TestGLContext& canvas = wxGetApp().GetContext(this, m_useStereo);
 //   glViewport(0, 0, ClientSize.x, ClientSize.y);
 
     const wxSize size = event.GetSize() * GetContentScaleFactor();
  //  glViewport(0, 0, size.x, size.y);
    Can_WinHeight = size.y;
 
    prepare3DViewport(0, 0, size.x, size.y);
 
    // Generate paint event without erasing the background.
    Refresh(false);
}
 
void MyGLCanvas::OnMouse(wxMouseEvent& event)
{
    event.Skip();
 
    // GL 0 Y-coordinate is at bottom of the window
    int oglwinY = Can_WinHeight - event.GetY();
 
    if ( event.LeftIsDown() )
    {
        if ( ! event.Dragging() )
        {
            // Store positions
 //           m_oglManager->OnMouseButDown(event.GetX(), oglwinY);
        }
        else
        {
            // Rotation
//            m_oglManager->OnMouseRotDragging( event.GetX(), oglwinY );
 
            // Generate paint event without erasing the background.
            Refresh(false);
        }
    }
}
 
// Effectuer des dessins simples : Axe, Carré, Sphère
void MyGLCanvas::OnKeyDown(wxKeyEvent& event)
{
    int k = event.GetKeyCode();
    // caractère en minuscule
    if ( k>=97 ) k -= 32;
 
    switch ( k )
    {
        case 65: // A
            // dessiner un axe 3D
            draw = draw_Axe;
            Refresh();
            break;
        case 67: // C
            // dessiner un carré
            draw = draw_Cube;
            Refresh();
            break;
        case 83: // S
            // dessiner une sphère
            draw = draw_Sphere;
            Refresh();
            break;
        case 73: // I
            // Information sur OpenGL
            wxMessageBox(Can_Parent->OGLString, "Info OpenGL", wxOK | wxICON_INFORMATION);
            break;
 
        default:
            wxMessageBox(_T("Demande inconnue"), "OpenGL Canvas", wxOK | wxICON_ERROR);
            event.Skip();
            return;
    }
}
 
void MyGLCanvas::DoAxe(void)
{
 
}
 
void MyGLCanvas::DoCube(GLfloat cote)
{
    SetCurrent(*Can_OglContext);
 
 
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
 
//    // add slightly more light, the default lighting is rather dark
    GLfloat ambient[] = { 0.5, 0.5, 0.5, 1.0 };
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
 
    glLoadIdentity();
 
    glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen
 
    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
                                      // Top face (y = 1.0f)
                                      // Define vertices in counter-clockwise (CCW) order with normal pointing out
    glColor3f(0.0f, 1.0f, 0.0f);     // Green
    glVertex3f(cote, cote, -cote);
    glVertex3f(-cote, cote, -cote);
    glVertex3f(-cote, cote, cote);
    glVertex3f(cote, cote, cote);
 
    // Bottom face (y = -1.0f)
    glColor3f(1.0f, 0.5f, 0.0f);     // Orange
    glVertex3f(cote, -cote, cote);
    glVertex3f(-cote, -cote, cote);
    glVertex3f(-cote, -cote, -cote);
    glVertex3f(cote, -cote, -cote);
 
    // Front face  (z = 1.0f)
    glColor3f(1.0f, 0.0f, 0.0f);     // Red
    glVertex3f(cote, cote, cote);
    glVertex3f(-cote, cote, cote);
    glVertex3f(-cote, -cote, cote);
    glVertex3f(cote, -cote, cote);
 
    // Back face (z = -1.0f)
    glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
    glVertex3f(cote, -cote, -cote);
    glVertex3f(-cote, -cote, -cote);
    glVertex3f(-cote, cote, -cote);
    glVertex3f(cote, cote, -cote);
 
    // Left face (x = -1.0f)
    glColor3f(0.0f, 0.0f, 1.0f);     // Blue
    glVertex3f(-cote, cote, cote);
    glVertex3f(-cote, cote, -cote);
    glVertex3f(-cote, -cote, -cote);
    glVertex3f(-cote, -cote, cote);
 
    // Right face (x = 1.0f)
    glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
    glVertex3f(cote, cote, -cote);
    glVertex3f(cote, cote, cote);
    glVertex3f(cote, -cote, cote);
    glVertex3f(cote, -cote, -cote);
    glEnd();  // End of drawing color-cube
 
    glFlush();
 
 //   SwapBuffers();
}
 
void MyGLCanvas::DoSphere(GLfloat radius)
{
 
 
} | 
Partager