IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenGL Discussion :

[OpenGL] Problème de Vitesse d'éxécution


Sujet :

OpenGL

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut [OpenGL] Problème de Vitesse d'éxécution
    Bonjour à tous !
    Ceci est mon premier post ! Soyez indulgent

    Voila, j'ai un petit probleme, je m'explique :
    Je fait mes début en opengl, j'ai donc programmé une tite appli.
    Jusque là, pas de problème, c'est juste un cube avec une sphere à l'intérieur, que l'on peut faire tourner plus ou moins vite en appuyant sur les touche up , down.
    Chez moi, ça tourne niquel, par contre, j'embarque l'appli sur un autre PC, là aussi ça tourne, mais plus lentement ! Il faut appuyer 2 heures sur la touche up pour que ça commence à bien tourner, alors que chez moi, il faut un appui de une seconde sur cette même touche pour obtenir le même résultat !
    Je me suis dit, pb de proc ou de carte vidéo.
    Pour tester, je prend une démo programmé en delphi, je la lance chez moi, no problème, je la lance sur l'autre pc, no problème non plus ! Je ne comprend plus rien !
    Voici le code de mon appli :

    ----------- CUT HERE -----------

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    program OpenGLApp;
     
    uses
    Windows,
    Messages,
    dglOpenGL,
    Textures,
    glut,
    OpenGL;
     
    const
    WND_TITLE = 'OpenGL Template using OpenGL 1.5 by McCLaw';
    FPS_TIMER = 1; // Timer to calculate FPS
    FPS_INTERVAL = 1000; // Calculate FPS every 1000 ms
     
    // User Constants
     
    var
    h_Wnd : HWND; // Global window handle
    h_DC : HDC; // Global device context
    h_RC : HGLRC; // OpenGL rendering context
    keys : Array[0..255] of Boolean; // Holds keystrokes
    FPSCount : Integer = 0; // Counter for FPS
    ElapsedTime : Integer; // Elapsed time between frames
     
    // User vaiables
    xSpeed, ySpeed, transx, transy, transz : glFloat;
    division : integer;
    boucle, vitesse_CPU : real;
    // Mouse Variables
    MouseMove : boolean = false; // Is the mouse moving?
    MoveXcoord, MoveYcoord : GLfloat; // Current X and Y position of the cursor
     
    MouseButton : Integer = -1; // mouse button down
    Xcoord, Ycoord : Integer; // Current X and Y position where the mouse was clicked
     
    OpenGLInitialized : Boolean;
     
    i,j : real;
    LightPosition : Array[0..3] Of GLfloat = (20, 20,0,1);
    LightAmbiant : Array[0..3] Of GLfloat = (1,1,1,1);
    MateriauAmbiantW : Array[0..3] Of GLfloat = (1,1,1,1);
    MateriauAmbiantB : Array[0..3] Of GLfloat = (0,0,0,1);
    MateriauAmbiantR : Array[0..3] Of GLfloat = (1,0,0,1);
    MateriauAmbiantG : Array[0..3] Of GLfloat = (0,1,0,1);
    MateriauAmbiantU : Array[0..3] Of GLfloat = (0,0,1,1);
    {$R *.RES}
     
    function get_cpu_speed:dword;
    var Speed : dword;
    perform: int64;
    begin
    perform:=0;
    SetpriorityClass(getcurrentprocess,REALTIME_PRIORITY_CLASS);
    SetThreadPriority(getcurrentThread,THREAD_PRIORITY_TIME_CRITICAL);
    asm
    push eax
    push edx
    push ecx
    push 1000
    call sleep // appel de sleep(1000)
    dw $310F // rdtsc (red internal cpu timer in cycle)
    add eax,edx
    mov speed,eax // stokez les cycles machine
    push edx
    xor eax,eax
    push 1000
    call sleep // appel de sleep(1000)
    pop edx
    dw $310F // relire le timer cpu rdsc
    add eax,edx
    sub eax,speed // garder 1000 millisecondes (1 seconde)
    mov speed,eax // le stocké dans speed;
    pop ecx
    pop edx
    pop eax
    end;
    result:=round((speed / 1000000) / 3)*3; // div 3 * 3 a cose de busfeq
    end;
     
     
    {------------------------------------------------------------------}
    { Function to convert int to string. (No sysutils = smaller EXE) }
    {------------------------------------------------------------------}
    function IntToStr(Num : Integer) : String; // using SysUtils increase file size by 100K
    begin
    Str(Num, result);
    end;
     
    {------------------------------------------------------------------}
    { Function to draw the actual scene }
    {------------------------------------------------------------------}
    procedure glDraw();
    begin
     
    glLoadIdentity();
    glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
    glulookat(0,0,-10, // Position de l'oeil
    0,0,0, // Point de visée
    0,1,0); // direction du vecteur vertical de la camera
    glColor3f (1.0, 1.0, 1.0);
    glOrtho(-1,1,-1,1,0,10);
    // gltranslate(2*sin(k/1024),sin(k/512),2*sin(k/256));
    gltranslate(transx,transy,transz);
    glRotate(i/16,1,0,0);
    glRotate(j/8,0,1,0);
    glLineWidth(1);
    glmaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE ,@MateriauAmbiantR);
    glutwirecube(2);
    glLineWidth(1);
    glmaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE ,@MateriauAmbiantW);
    glutwiresphere(1.01,division,division);
    glFlush();
    i := i+xspeed;
    j := j+yspeed;
     
    end;
     
     
    {------------------------------------------------------------------}
    { Initialise OpenGL }
    {------------------------------------------------------------------}
    procedure glInit();
    begin
    glClearDepth(1.0);
    // glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glutinit(MS_LIB);
    glShadeModel(GL_SMOOTH);
    glLightfv( GL_LIGHT0, GL_POSITION, @LightPosition );
    glLightfv( GL_LIGHT0, GL_AMBIENT, @LightAmbiant );
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glEnable( GL_DEPTH_TEST );
    vitesse_CPU := 0.01*(200/get_cpu_speed);
    end;
     
     
    {------------------------------------------------------------------}
    { Handle window resize }
    {------------------------------------------------------------------}
    procedure glResizeWnd(Width, Height : Integer);
    begin
    if (Height = 0) then // prevent divide by zero exception
    Height := 1;
    glViewport(0, 0, Width, Height); // Set the viewport for the OpenGL window
    glMatrixMode(GL_PROJECTION); // Change Matrix Mode to Projection
    glLoadIdentity(); // Reset View
    gluPerspective(45.0, Width/Height, 1.0, 100.0); // Do the perspective calculations. Last value = max clipping depth
    glMatrixMode(GL_MODELVIEW); // Return to the modelview matrix
    glLoadIdentity(); // Reset View
    end;
     
    {------------------------------------------------------------------}
    { Processes all the mouse move }
    {------------------------------------------------------------------}
    procedure ProcessMouseMove();
    begin
    if MouseMove then
    begin
    // Add Code here to execute on mouse move
    MouseMove := False;
    end;
    end;
     
    {------------------------------------------------------------------}
    { Processes all the mouse clicks }
    {------------------------------------------------------------------}
    procedure ProcessMouseClick;
    begin
    case MouseButton of
    1: // Left Mouse Button
    begin
    // Add code to execute on mouse click
    MouseButton := 0; // Cancel our mouse click (To use this procedure as a mouse down event remove this line)
    end;
    2: // Right Mouse Button
    begin
    // Add code to execute on mouse click
    MouseButton := 0; // Cancel our mouse click (To use this procedure as a mouse down event remove this line)
    end;
    3: // Middle Mouse Button
    begin
    // Add code to execute on mouse click
    MouseButton := 0; // Cancel our mouse click (To use this procedure as a mouse down event remove this line)
    end;
    end;
    end;
     
    {------------------------------------------------------------------}
    { Processes all the keystrokes }
    {------------------------------------------------------------------}
    procedure ProcessKeys;
    begin
    if (keys[VK_UP]) then xspeed := xspeed - vitesse_CPU;//0.001; // Augmente la vitesse de rotation en X
    if (keys[VK_DOWN]) then xspeed := xspeed + vitesse_CPU;//0.001; // Diminue la vitesse de rotation en X
    if (keys[VK_RIGHT]) then yspeed := yspeed + vitesse_CPU;//0.001; // Augmente la vitesse de rotation en y
    if (keys[VK_LEFT]) then yspeed := yspeed - vitesse_CPU;//0.001; // Diminue la vitesse de rotation en Y
    if (keys[VK_NUMPAD5]) then
    begin
    xspeed := 0;
    yspeed := 0;
    transx := 0;
    transy := 0;
    transz := 0;
    end;
    if (keys[VK_ADD]) then boucle := boucle + vitesse_CPU*8;//0.01; // Augmente le nbre de subdivision de la sphere
    if (keys[VK_SUBTRACT]) then boucle := boucle -vitesse_CPU*8;//0.01; // diminue le nbre de subdivision de la sphere
    division := round (boucle);
    if (keys[VK_NUMPAD2]) then transy := transy - vitesse_CPU;//0.002;
    if (keys[VK_NUMPAD8]) then transy := transy + vitesse_CPU;//0.002;
    if (keys[VK_NUMPAD4]) then transx := transx + vitesse_CPU;//0.002;
    if (keys[VK_NUMPAD6]) then transx := transx - vitesse_CPU;//0.002;
    if (keys[VK_MULTIPLY]) then transz := transz + vitesse_CPU*16;//0.05;
    if (keys[VK_DIVIDE]) then transz := transz - vitesse_CPU*16;//0.05;
     
    end;
     
     
    {------------------------------------------------------------------}
    { Determines the application’s response to the messages received }
    {------------------------------------------------------------------}
    function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
    begin
    case (Msg) of
    WM_CREATE:
    begin
    // Insert stuff you want executed when the program starts
    end;
    WM_CLOSE:
    begin
    PostQuitMessage(0);
    Result := 0
    end;
    WM_KEYDOWN: // Set the pressed key (wparam) to equal true so we can check if its pressed
    begin
    keys[wParam] := True;
    Result := 0;
    end;
    WM_KEYUP: // Set the released key (wparam) to equal false so we can check if its pressed
    begin
    keys[wParam] := False;
    Result := 0;
    end;
    WM_MOUSEMOVE:
    begin
    MouseMove := True;
    MoveXcoord := LOWORD(lParam);
    MoveYcoord := HIWORD(lParam);
    Result := 0;
    end;
    WM_LBUTTONDOWN:
    begin
    ReleaseCapture(); // need them here, because if mouse moves off
    SetCapture(h_Wnd); // window and returns, it needs to reset status
    MouseButton := 1;
    Xcoord := LOWORD(lParam);
    Ycoord := HIWORD(lParam);
    Result := 0;
    end;
    WM_RBUTTONDOWN:
    begin
    ReleaseCapture(); // need them here, because if mouse moves off
    SetCapture(h_Wnd); // window and returns, it needs to reset status
    MouseButton := 2;
    Xcoord := LOWORD(lParam);
    Ycoord := HIWORD(lParam);
    Result := 0;
    end;
    WM_MBUTTONDOWN:
    begin
    ReleaseCapture(); // need them here, because if mouse moves off
    SetCapture(h_Wnd); // window and returns, it needs to reset status
    MouseButton := 3;
    Xcoord := LOWORD(lParam);
    Ycoord := HIWORD(lParam);
    Result := 0;
    end;
    WM_LBUTTONUP,WM_RBUTTONUP,WM_MBUTTONUP:
    begin
    ReleaseCapture(); // above
    MouseButton := 0;
    XCoord := 0;
    YCoord := 0;
    Result := 0;
    end;
    WM_SIZE: // Resize the window with the new width and height
    begin
    if OpenGLInitialized then
    glResizeWnd(LOWORD(lParam),HIWORD(lParam));
    Result := 0;
    end;
    WM_TIMER : // Add code here for all timers to be used.
    begin
    if wParam = FPS_TIMER then
    begin
    FPSCount :=Round(FPSCount * 1000/FPS_INTERVAL); // calculate to get per Second incase intercal is less or greater than 1 second
    SetWindowText(h_Wnd, PChar(WND_TITLE + ' [' + intToStr(FPSCount) + ' FPS]'));
    FPSCount := 0;
    Result := 0;
    end;
    end;
    else
    begin
     
    Result := DefWindowProc(hWnd, Msg, wParam, lParam); // Default result if nothing happens
    end;
    end;
    end;
     
     
    {---------------------------------------------------------------------}
    { Properly destroys the window created at startup (no memory leaks) }
    {---------------------------------------------------------------------}
    procedure glKillWnd(Fullscreen : Boolean);
    begin
    if Fullscreen then // Change back to non fullscreen
    begin
    ChangeDisplaySettings(devmode(nil^), 0);
    ShowCursor(True);
    end;
     
    // Makes current rendering context not current, and releases the device
    // context that is used by the rendering context.
    if (not wglMakeCurrent(h_DC, 0)) then
    MessageBox(0, 'Release of DC and RC failed!', 'Error', MB_OK or MB_ICONERROR);
     
    // Attempts to delete the rendering context
    if (not wglDeleteContext(h_RC)) then
    begin
    MessageBox(0, 'Release of rendering context failed!', 'Error', MB_OK or MB_ICONERROR);
    h_RC := 0;
    end;
     
    // Attemps to release the device context
    if ((h_DC > 0) and (ReleaseDC(h_Wnd, h_DC) = 0)) then
    begin
    MessageBox(0, 'Release of device context failed!', 'Error', MB_OK or MB_ICONERROR);
    h_DC := 0;
    end;
     
    // Attempts to destroy the window
    if ((h_Wnd <> 0) and (not DestroyWindow(h_Wnd))) then
    begin
    MessageBox(0, 'Unable to destroy window!', 'Error', MB_OK or MB_ICONERROR);
    h_Wnd := 0;
    end;
     
    // Attempts to unregister the window class
    if (not UnRegisterClass('OpenGL', hInstance)) then
    begin
    MessageBox(0, 'Unable to unregister window class!', 'Error', MB_OK or MB_ICONERROR);
    hInstance := 0;
    end;
    end;
     
     
    {--------------------------------------------------------------------}
    { Creates the window and attaches a OpenGL rendering context to it }
    {--------------------------------------------------------------------}
    function glCreateWnd(Width, Height : Integer; Fullscreen : Boolean; PixelDepth : Integer) : Boolean;
    var
    wndClass : TWndClass; // Window class
    dwStyle : DWORD; // Window styles
    dwExStyle : DWORD; // Extended window styles
    dmScreenSettings : DEVMODE; // Screen settings (fullscreen, etc...)
    PixelFormat : GLuint; // Settings for the OpenGL rendering
    h_Instance : HINST; // Current instance
    pfd : TPIXELFORMATDESCRIPTOR; // Settings for the OpenGL window
    begin
    InitOpenGL; // New call to initialize and bind the OpenGL dll
     
    h_Instance := GetModuleHandle(nil); //Grab An Instance For Our Window
    ZeroMemory(@wndClass, SizeOf(wndClass)); // Clear the window class structure
     
    with wndClass do // Set up the window class
    begin
    style := CS_HREDRAW or // Redraws entire window if length changes
    CS_VREDRAW or // Redraws entire window if height changes
    CS_OWNDC; // Unique device context for the window
    lpfnWndProc := @WndProc; // Set the window procedure to our func WndProc
    hInstance := h_Instance;
    hCursor := LoadCursor(0, IDC_ARROW);
    lpszClassName := 'OpenGL';
    end;
     
    if (RegisterClass(wndClass) = 0) then // Attemp to register the window class
    begin
    MessageBox(0, 'Failed to register the window class!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit
    end;
     
    // Change to fullscreen if so desired
    if Fullscreen then
    begin
    ZeroMemory(@dmScreenSettings, SizeOf(dmScreenSettings));
    with dmScreenSettings do begin // Set parameters for the screen setting
    dmSize := SizeOf(dmScreenSettings);
    dmPelsWidth := Width; // Window width
    dmPelsHeight := Height; // Window height
    dmBitsPerPel := PixelDepth; // Window color depth
    dmFields := DM_PELSWIDTH or DM_PELSHEIGHT or DM_BITSPERPEL;
    end;
     
    // Try to change screen mode to fullscreen
    if (ChangeDisplaySettings(dmScreenSettings, CDS_FULLSCREEN) = DISP_CHANGE_FAILED) then
    begin
    MessageBox(0, 'Unable to switch to fullscreen!', 'Error', MB_OK or MB_ICONERROR);
    Fullscreen := False;
    end;
    end;
     
    // If we are still in fullscreen then
    if (Fullscreen) then
    begin
    dwStyle := WS_POPUP or // Creates a popup window
    WS_CLIPCHILDREN // Doesn't draw within child windows
    or WS_CLIPSIBLINGS; // Doesn't draw within sibling windows
    dwExStyle := WS_EX_APPWINDOW; // Top level window
    ShowCursor(False); // Turn of the cursor (gets in the way)
    end
    else
    begin
    dwStyle := WS_OVERLAPPEDWINDOW or // Creates an overlapping window
    WS_CLIPCHILDREN or // Doesn't draw within child windows
    WS_CLIPSIBLINGS; // Doesn't draw within sibling windows
    dwExStyle := WS_EX_APPWINDOW or // Top level window
    WS_EX_WINDOWEDGE; // Border with a raised edge
    end;
     
    // Attempt to create the actual window
    h_Wnd := CreateWindowEx(dwExStyle, // Extended window styles
    'OpenGL', // Class name
    WND_TITLE, // Window title (caption)
    dwStyle, // Window styles
    0, 0, // Window position
    Width, Height, // Size of window
    0, // No parent window
    0, // No menu
    h_Instance, // Instance
    nil); // Pass nothing to WM_CREATE
     
    if h_Wnd = 0 then
    begin
    glKillWnd(Fullscreen); // Undo all the settings we've changed
    MessageBox(0, 'Unable to create window!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
     
    // Try to get a device context
    h_DC := GetDC(h_Wnd);
    if (h_DC = 0) then
    begin
    glKillWnd(Fullscreen);
    MessageBox(0, 'Unable to get a device context!', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
     
    // Settings for the OpenGL window
    with pfd do
    begin
    nSize := SizeOf(TPIXELFORMATDESCRIPTOR); // Size Of This Pixel Format Descriptor
    nVersion := 1; // The version of this data structure
    dwFlags := PFD_DRAW_TO_WINDOW // Buffer supports drawing to window
    or PFD_SUPPORT_OPENGL // Buffer supports OpenGL drawing
    or PFD_DOUBLEBUFFER; // Supports double buffering
    iPixelType := PFD_TYPE_RGBA; // RGBA color format
    cColorBits := PixelDepth; // OpenGL color depth
    cRedBits := 0; // Number of red bitplanes
    cRedShift := 0; // Shift count for red bitplanes
    cGreenBits := 0; // Number of green bitplanes
    cGreenShift := 0; // Shift count for green bitplanes
    cBlueBits := 0; // Number of blue bitplanes
    cBlueShift := 0; // Shift count for blue bitplanes
    cAlphaBits := 0; // Not supported
    cAlphaShift := 0; // Not supported
    cAccumBits := 0; // No accumulation buffer
    cAccumRedBits := 0; // Number of red bits in a-buffer
    cAccumGreenBits := 0; // Number of green bits in a-buffer
    cAccumBlueBits := 0; // Number of blue bits in a-buffer
    cAccumAlphaBits := 0; // Number of alpha bits in a-buffer
    cDepthBits := 16; // Specifies the depth of the depth buffer
    cStencilBits := 0; // Turn off stencil buffer
    cAuxBuffers := 0; // Not supported
    iLayerType := PFD_MAIN_PLANE; // Ignored
    bReserved := 0; // Number of overlay and underlay planes
    dwLayerMask := 0; // Ignored
    dwVisibleMask := 0; // Transparent color of underlay plane
    dwDamageMask := 0; // Ignored
    end;
     
    // Attempts to find the pixel format supported by a device context that is the best match to a given pixel format specification.
    PixelFormat := ChoosePixelFormat(h_DC, @pfd);
    if (PixelFormat = 0) then
    begin
    glKillWnd(Fullscreen);
    MessageBox(0, 'Unable to find a suitable pixel format', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
     
    // Sets the specified device context's pixel format to the format specified by the PixelFormat.
    if (not SetPixelFormat(h_DC, PixelFormat, @pfd)) then
    begin
    glKillWnd(Fullscreen);
    MessageBox(0, 'Unable to set the pixel format', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
     
    // Create a OpenGL rendering context
    h_RC := wglCreateContext(h_DC);
    if (h_RC = 0) then
    begin
    glKillWnd(Fullscreen);
    MessageBox(0, 'Unable to create an OpenGL rendering context', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
     
    // Makes the specified OpenGL rendering context the calling thread's current rendering context
    if (not wglMakeCurrent(h_DC, h_RC)) then
    begin
    glKillWnd(Fullscreen);
    MessageBox(0, 'Unable to activate OpenGL rendering context', 'Error', MB_OK or MB_ICONERROR);
    Result := False;
    Exit;
    end;
    // Read And Assign Extentions
    ReadExtensions;
    ReadImplementationProperties;
    OpenGLInitialized := True;
    // Initializes the timer used to calculate the FPS
    SetTimer(h_Wnd, FPS_TIMER, FPS_INTERVAL, nil);
     
    // Settings to ensure that the window is the topmost window
    ShowWindow(h_Wnd, SW_SHOW);
    SetForegroundWindow(h_Wnd);
    SetFocus(h_Wnd);
     
    // Ensure the OpenGL window is resized properly
    glResizeWnd(Width, Height);
    glInit();
     
    Result := True;
    end;
     
     
    {--------------------------------------------------------------------}
    { Main message loop for the application }
    {--------------------------------------------------------------------}
    function WinMain(hInstance : HINST; hPrevInstance : HINST;
    lpCmdLine : PChar; nCmdShow : Integer) : Integer; stdcall;
    var
    msg : TMsg;
    finished : Boolean;
    DemoStart, LastTime : DWord;
    begin
    finished := False;
     
    // Perform application initialization:
    if not glCreateWnd(800, 600, TRUE, 32) then
    begin
    Result := 0;
    Exit;
    end;
     
    DemoStart := GetTickCount(); // Get Time when demo started
     
    // Main message loop:
    while not finished do
    begin
    if (PeekMessage(msg, 0, 0, 0, PM_REMOVE)) then // Check if there is a message for this window
    begin
    if (msg.message = WM_QUIT) then // If WM_QUIT message received then we are done
    finished := True
    else
    begin // Else translate and dispatch the message to this window
    TranslateMessage(msg);
    DispatchMessage(msg);
    end;
    end
    else
    begin
    Inc(FPSCount); // Increment FPS Counter
     
    LastTime :=ElapsedTime;
    ElapsedTime :=GetTickCount() - DemoStart; // Calculate Elapsed Time
    ElapsedTime :=(LastTime + ElapsedTime) DIV 2; // Average it out for smoother movement
     
    glDraw(); // Draw the scene
    SwapBuffers(h_DC); // Display the scene
     
    if (keys[VK_ESCAPE]) then // If user pressed ESC then set finised TRUE
    finished := True
    else
    begin
    ProcessKeys; // Check for any other key Pressed
    ProcessMouseMove; // Check for mouse move
    ProcessMouseClick; // Check for Mouse Click
    end;
    end;
    end;
    glKillWnd(FALSE);
    Result := msg.wParam;
    end;
     
     
    begin
    WinMain( hInstance, hPrevInst, CmdLine, CmdShow );
    end.
    ----------- CUT HERE -----------

    Je vais essayer de mieux m'expliquer :
    J'ai un pc chez moi ou je créer des application avec Delphi.
    J'ai un pc au boulot pour usage professionel.
    J'ai fait une appli avec Delphi qui utilise OpenGL. Cette application fait apparaitre un cube et sphère (à l'intérieur de ce cube).
    On peut si on le désire faire tourner le cube et la sphère dans l'espace 3D en utilisant les touches de direction (up down left right)
    L'appuie sur la touche up par exemple fait tourner de plus en plus vite le cube et la sphère autour de l'axe x.
    Chez moi, quand je reste appuyé sur la touche up, le cube se met à tourner, et ce, de plus en plus vite, j'ai donc pris mon chronomètre, j'ai lancé l'application, et j'ai appuyé continuellement sur la touche up, le cube s'est mit à tourner selon l'axe x, de plus en plus vite, le cube à mit 9 secondes pour faire un tour.
    Une fois ceci fait, j'ai pris mon appli à mon boulot, et j'ai fait les même manip, là, au bout des 9 secondes, mon cube ne bouge toujours pas, enfin, à peine, je pense que pour avoir un tour de ce cube, je devrais resté appuyer sur la touche up plus de 5 minutes, je n'arrive pas à comprendre pourquoi.
    Le code que j'ai joint, et la source complète Delphi.

    Si quelqu'un à une solution, je suis preneur ! Merci !!

    Laurent...

  2. #2
    Membre habitué

    Profil pro
    Inscrit en
    Avril 2002
    Messages
    99
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2002
    Messages : 99
    Points : 126
    Points
    126
    Par défaut
    Pourquoi tu mets vitesse_CPU := 0.01*(200/get_cpu_speed); ? C'est surement ça qui cafouille... Tu as débogué voir quel est la valeur que tu sort chez toi et à ton boulot ?
    A+---------------> Nat <-------------------

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut
    Merci pour la réponse.
    En fait, j'ai mit

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    vitesse_CPU := 0.01*(200/get_cpu_speed);
    car je croyais que le ralentissement était dû à la vitesse du processeur, donc je me suis dit :
    Quand on appuie sur une touche, au lieu d'incrémenter sur toute les bécannes de 0.01, je vais faire une tite formule qui change la valeur d'incrémentation en fonction de la vitesse du processeur de la machine sur laquel le prog est éxécuté.
    La vitesse du processeur, on l'obtient grace à cette petite fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
    function get_cpu_speed:dword; 
    var Speed : dword; 
    perform: int64; 
    begin 
    perform:=0; 
    SetpriorityClass(getcurrentprocess,REALTIME_PRIORITY_CLASS); 
    SetThreadPriority(getcurrentThread,THREAD_PRIORITY_TIME_CRITICAL); 
    asm 
    push eax 
    push edx 
    push ecx 
    push 1000 
    call sleep // appel de sleep(1000) 
    dw $310F // rdtsc (red internal cpu timer in cycle) 
    add eax,edx 
    mov speed,eax // stokez les cycles machine 
    push edx 
    xor eax,eax 
    push 1000 
    call sleep // appel de sleep(1000) 
    pop edx 
    dw $310F // relire le timer cpu rdsc 
    add eax,edx 
    sub eax,speed // garder 1000 millisecondes (1 seconde) 
    mov speed,eax // le stocké dans speed; 
    pop ecx 
    pop edx 
    pop eax 
    end; 
    result:=round((speed / 1000000) / 3)*3; // div 3 * 3 a cose de busfeq 
    end;
    Mais ce qu'il y a encore plus étrange, c'est que si j'éxécute une démo trouvé sur le net, et programmée en Delphi, que je l'éxécute sur mon pc ou sur le pc du boulot, elle tourne à la même vitesse !
    Quand je regarde le nombre de fps de mon appli, chez moi je tourne à 1250 fps, au boulot, je tourne à 60 voir 70 fps, peut etre que le chemin de recherche serais de brider le programme pour qu'il tourne sur toute les machines a la même vitesse fps. Mais ça dépasse mes compétences !

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2002
    Messages : 108
    Points : 128
    Points
    128
    Par défaut
    Il vaudrait mieux syncrhoniser ton appli avec le temps plutôt qu'avec la vitesse du processeur.

    1 m/s aura donc toujours la même vitesse qu'elle que soit la machine mais tu n'aurra pas forcement le même nombre d'image par seconde.

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut
    En fait le vitesse_CPU := 0.01*(200/get_cpu_speed); à été écrit pour faire des test, à l'origine, il n'y avais pas ce bout de code et :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    procedure ProcessKeys; 
    begin 
    if (keys[VK_UP]) then xspeed := xspeed - vitesse_CPU;//0.001; // Augmente la vitesse de rotation en X 
    if (keys[VK_DOWN]) then xspeed := xspeed + vitesse_CPU;//0.001; // Diminue la vitesse de rotation en X 
    if (keys[VK_RIGHT]) then yspeed := yspeed + vitesse_CPU;//0.001; // Augmente la vitesse de rotation en y 
    if (keys[VK_LEFT]) then yspeed := yspeed - vitesse_CPU;//0.001; // Diminue la vitesse de rotation en Y 
    if (keys[VK_NUMPAD5]) then 
    begin 
    xspeed := 0; 
    yspeed := 0; 
    transx := 0; 
    transy := 0; 
    transz := 0; 
    end; 
    if (keys[VK_ADD]) then boucle := boucle + vitesse_CPU*8;//0.01; // Augmente le nbre de subdivision de la sphere 
    if (keys[VK_SUBTRACT]) then boucle := boucle -vitesse_CPU*8;//0.01; // diminue le nbre de subdivision de la sphere 
    division := round (boucle); 
    if (keys[VK_NUMPAD2]) then transy := transy - vitesse_CPU;//0.002; 
    if (keys[VK_NUMPAD8]) then transy := transy + vitesse_CPU;//0.002; 
    if (keys[VK_NUMPAD4]) then transx := transx + vitesse_CPU;//0.002; 
    if (keys[VK_NUMPAD6]) then transx := transx - vitesse_CPU;//0.002; 
    if (keys[VK_MULTIPLY]) then transz := transz + vitesse_CPU*16;//0.05; 
    if (keys[VK_DIVIDE]) then transz := transz - vitesse_CPU*16;//0.05; 
     
    end;
    avant la modif, ressemblais à :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    procedure ProcessKeys; 
    begin 
    if (keys[VK_UP]) then xspeed := xspeed - 0.001; // Augmente la vitesse de rotation en X 
    if (keys[VK_DOWN]) then xspeed := xspeed + 0.001; // Diminue la vitesse de rotation en X 
    if (keys[VK_RIGHT]) then yspeed := yspeed + 0.001; // Augmente la vitesse de rotation en y 
    if (keys[VK_LEFT]) then yspeed := yspeed - 0.001; // Diminue la vitesse de rotation en Y 
    if (keys[VK_NUMPAD5]) then 
    begin 
    xspeed := 0; 
    yspeed := 0; 
    transx := 0; 
    transy := 0; 
    transz := 0; 
    end; 
    if (keys[VK_ADD]) then boucle := boucle + 0.01; // Augmente le nbre de subdivision de la sphere 
    if (keys[VK_SUBTRACT]) then boucle := boucle -0.01; // diminue le nbre de subdivision de la sphere 
    division := round (boucle); 
    if (keys[VK_NUMPAD2]) then transy := transy - 0.002; 
    if (keys[VK_NUMPAD8]) then transy := transy + 0.002; 
    if (keys[VK_NUMPAD4]) then transx := transx + 0.002; 
    if (keys[VK_NUMPAD6]) then transx := transx - 0.002; 
    if (keys[VK_MULTIPLY]) then transz := transz + 0.05; 
    if (keys[VK_DIVIDE]) then transz := transz - 0.05; 
     
    end;
    En fait, j'ai oublié d'enlever la synchronisation cpu avant de poster.

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2002
    Messages : 108
    Points : 128
    Points
    128
    Par défaut
    C'était une très bonne idée de vouloir synchroniser avec le CPU. mais en faites, il faut aussi synchroniser avec la carte 3D la vitese de la mémoire, etc.... donc plus simplement le temps.

    Exemple :
    Si tu tourne à 60 images par secondes, tu vas passer (généralement) 60 fois dans ta boucle d'évènement clavier et tu vas donc incrémenter 60 fois la même valeur. Mais si tu es à 100 images par seconde, tu vas y passer 100 fois.

    En définitive, pour avoir toujours le même déplacement, il faut que le déplacement soit en fonction du temps. Pour syncroniser l'affichage il faut que ton animation (ton déplacement) soit proportionnel au temps qui a été mis pour réaliser l'affichage de l'image précédente.

    Cette valeur tu ne l'as pas directement puisque tu utilises un timer.
    Il va falloir que tu le supprimes, que tu l'adaptes ou que tu le remplaces.

    Pour en être sur, active le vsync sur la machine qui tourne à 1200 images par seconde. T'es images par seconde se syncroniseront avec ton affichage et tourneront à la fréquence de raffraichissement de ton écran. Ton animation ralentira donc en conséquence.

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut
    Merci pour les infos !
    C'est vrai que je ne me suis jamais interressé au VSync, peut etre que ça vient de là, car effectivement, il est désactivé, à la rigueur, pour qu'il qu'il tourne partout pareil que le vsync sois activé ou pas, il faudrais que le prog active le vsync au lancement, et le remet en l'état à la fin du prog.

    Sinon, hier soir, j'ai pensé à un truc, je n'ais qu'a changer la valeur d'incrémentation en fonction du fps, genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    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
     
    procedure ProcessKeys;
    begin
      if (keys[VK_UP])        then xspeed := xspeed + ((1250/FPSCount)*0.02));  // Augmente la vitesse de rotation en X
      if (keys[VK_DOWN])      then xspeed := xspeed - ((1250/FPSCount)*0.02));  // Diminue la vitesse de rotation en X
      if (keys[VK_RIGHT])     then yspeed := yspeed + ((1250/FPSCount)*0.02));  // Augmente la vitesse de rotation en y
      if (keys[VK_LEFT])      then yspeed := yspeed - ((1250/FPSCount)*0.02));  // Diminue la vitesse de rotation en Y
      if (keys[VK_NUMPAD5])     then
        begin
          xspeed := 0;
          yspeed := 0;
          transx := 0;
          transy := 0;
          transz := 0;
        end;
      if (keys[VK_ADD])       then boucle := boucle + ((1250/FPSCount)*0.5));   // Augmente le nbre de subdivision de la sphere
      if (keys[VK_SUBTRACT])  then boucle := boucle - ((1250/FPSCount)*0.5));    // diminue le nbre de subdivision de la sphere
      division := round (boucle);
      if (keys[VK_NUMPAD2])   then transy := transy - ((1250/FPSCount)*0.1));
      if (keys[VK_NUMPAD8])   then transy := transy + ((1250/FPSCount)*0.1));
      if (keys[VK_NUMPAD4])   then transx := transx - ((1250/FPSCount)*0.1));
      if (keys[VK_NUMPAD6])   then transx := transx + ((1250/FPSCount)*0.1));
      if (keys[VK_MULTIPLY])  then transz := transz + ((1250/FPSCount)*1.5));
      if (keys[VK_DIVIDE])    then transz := transz - ((1250/FPSCount)*1.5));
        i := i+xspeed;
      j := j+yspeed;
    end;
    Mais j'ai pas eu le temps de tester ça hier soir, mais j'ai l'impression d'être sur la bonne voie car :
    si le pc tourne à 60 FPS, ((1250/FPSCount)*0.02))=0.41
    tandis que si le pc tourne à 2000 FPS, ((1250/FPSCount)*0.02))=0.0125
    Plus le pc est rapide, plus la valeur d'incrément est petite, donc, logiquement, ça devrais fonctionner, enfin, je pense !

  8. #8
    Membre habitué
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    108
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2002
    Messages : 108
    Points : 128
    Points
    128
    Par défaut
    Je confirme, tu es sur la bonne voie.

  9. #9
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut
    Merci !!!!
    Une fois que j'aurais tout testé, je viendrais faire mon rapport !

  10. #10
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2004
    Messages : 19
    Points : 8
    Points
    8
    Par défaut
    :trouve:
    Bon, me revoila, à midi je suis rentré chez moi pour manger, et j'en ais profité:
    Alors le système avec la formule :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ((1250/FPSCount)*0.02));
    Fonctionne, mais ça saccade l'animation, car le FPS tombe à 0 régulièrement, un temps infime, mais suffisament longtemps pour saccader l'animation.
    L'autre solution (utilisation du vsync) est carrément meilleur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    type
      TVSyncMode = (vsmSync, vsmNoSync);
    En variable :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    VAR
    VSync : TVSyncMode;
    Au lancement de l'application ceci (dans la fonction winmain):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    if messagebox(0,'Activate V-Sync ?','Confirmation V-Sync',MB_YESNO) = 6 then
        VSync := vsmSync
      else
        VSync := vsmNoSync;
    En procedure ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    procedure VBL2(vsync : TVSyncMode);
    var
       i : Integer;
    begin
       if WGL_EXT_swap_control then
       begin
          i := wglGetSwapIntervalEXT;
          case VSync of
             vsmSync    : if i<>1 then wglSwapIntervalEXT(1);
             vsmNoSync  : if i<>0 then wglSwapIntervalEXT(0);
          else
             Assert(False);
          end;
       end;
     
    end;
    Puis dans la function glcreateWnd :

    A caser entre
    et
    Bon, tout ça n'est pas de moi voici la bonne adresse :
    http://sulaco.co.za/opengl/sinscroll.zip
    C'est un exemple d'activation et de désactivation du vsync via Delphi.

    Voila, donc mon problème est résolu, et je vous remercie bien tous pour m'avoir mit sur la bonne route.

    A bientôt.

    Laurent.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 10
    Dernier message: 08/06/2009, 11h30
  2. [OpenGL] problème de rotation 3D
    Par ReiVon dans le forum OpenGL
    Réponses: 3
    Dernier message: 11/04/2005, 09h47
  3. Réponses: 2
    Dernier message: 29/03/2005, 14h25
  4. [debutant][c++] Problème de vitesse de déplacement
    Par bornibus dans le forum OpenGL
    Réponses: 15
    Dernier message: 26/02/2005, 09h16
  5. [OpenGL] Problème d'éclairage
    Par Dev48 dans le forum OpenGL
    Réponses: 4
    Dernier message: 24/02/2005, 09h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo