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

Windows Discussion :

afficher des donnees argb avec direct3d


Sujet :

Windows

  1. #1
    Membre habitué
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 340
    Points : 177
    Points
    177
    Par défaut afficher des donnees argb avec direct3d
    Bonjour,

    j'aimerais afficher un rectangle dont j'ai determine les pixels dans une fenetre, en utilisant direct3d.

    En gros, ma fonction qui affiche a cette tete:

    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
    int
    data_heartbeat (Data *d)
    {
      static int foo = 0;
      LPVOID  surf;
      unsigned char *tmp;
      int w, h, depth, pitch;
      int r, g;
      RECT rect;
      LPDIRECT3DTEXTURE9 texture;
    
      d->device->Clear (0, NULL,
                        D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                        D3DCOLOR_ARGB (0, 0, 0, 0),
                        1.0f, 0);
    
      d->device->BeginScene ();
    
    
      w = 64;
      h = 64;
      depth = 4;
      pitch = depth * w;
      surf = malloc (depth * w * h);
      if (!surf) return 0;
      ZeroMemory (surf, depth * w * h);
    
      tmp = (unsigned char *)surf;
      for (r = 0; r < w; r++) {
        for (g = 0; g < h; g++, tmp += depth) {
          tmp[0] = (r * 2 + foo) % 256;
          tmp[1] = (g + foo) % 256;
          tmp[2] = (w - 1 - g) * 4;
        }
      }
      foo++;
    
      rect.left = 0;
      rect.top = 0;
      rect.right = w;
      rect.bottom = h;
    
      d->sprite->Begin (0);
    
      d->device->CreateTexture (w, h, 0, 0,
                                D3DFMT_A8R8G8B8,
                                D3DPOOL_DEFAULT,
                                &texture, NULL);
    
      // Il faudrait ecrire les donnees de surf dans la texture
    
      d->sprite->Draw (texture, &rect, NULL, NULL, D3DCOLOR_ARGB (0, 0, 0, 0));
      d->sprite->End ();
    
      d->device->EndScene ();
      d->device->Present (NULL, NULL, NULL, NULL);
    
      return 1;
    }
    ne faites pas attention a la qualite de l'ecriture, ce code est un test, qui me servira pour un programme futur.

    Comme indique dans le code ci-dessus, j'aimerais ecrire les donnees de surf dans texture.

    J'ai pas mal cherche avec google ou msdn sans vraiment trouver de code precis.

    Quelqu'un aurait-il une idee ?

    Sinon, dans ce thread il est fait mention de la rapidite des vertices compares aux sprites. Comme je recherche principalement la rapidite, si c'est le cas, que dois-je faire pour utiliser ces vertices ?

    merci

  2. #2
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 396
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 396
    Points : 20 507
    Points
    20 507
    Par défaut
    Oui mais il n'ya aucune donnée dans surf ! ou j'ai la berlue....

  3. #3
    Membre habitué
    Homme Profil pro
    Inscrit en
    Avril 2004
    Messages
    340
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 340
    Points : 177
    Points
    177
    Par défaut
    il y a des donnees. J'ai juste utilise l'arithmetique des pointeurs : tmp pointe sur surf et je modifie tmp en lui ajoutant 4 (la longueur d'un pixel en octet).

    bon, etant loin de mon ordi perso et n'ayant pas pris le soin de prendre avec moi le code, j'ai essaye de le re-ecrire, et ca ne marche pas.

    voila le code complet:

    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
    // g++ -g -Wall -o d3d d3d.cpp -ld3d9 -ld3dx9d
    
    #include <cstdlib>
    #include <cstdio>
    
    #include <windows.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    
    typedef struct Data Data;
    
    struct Data
    {
      LPDIRECT3D9        object;
      LPDIRECT3DDEVICE9  device;
      LPD3DXSPRITE       sprite;
      LPDIRECT3DTEXTURE9 texture;
    };
    
    
    static LRESULT CALLBACK
    MainWndProc(HWND   hwnd,
    	    UINT   uMsg,
    	    WPARAM wParam,
    	    LPARAM lParam)
    {
      switch (uMsg)
        {
        case WM_CREATE:
          return 0;
        case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
        case WM_CLOSE:
          PostQuitMessage(0);
          return 0;
        default:
          return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
    }
    
    Data *
    data_init (HWND window,
    	   int  width,
    	   int  height)
    {
      D3DPRESENT_PARAMETERS pp;
      D3DDISPLAYMODE        dm;
      Data                 *d;
    
      d = (Data *)malloc (sizeof (Data));
      if (!d)
        goto no_data;
    
      d->object = Direct3DCreate9 (D3D_SDK_VERSION);
      if (!d->object)
        goto no_object;
    
      if (FAILED (d->object->GetAdapterDisplayMode (D3DADAPTER_DEFAULT, &dm)))
        goto no_device;
    
      ZeroMemory(&pp, sizeof(pp));
      pp.BackBufferFormat = dm.Format;
      pp.BackBufferCount = 1;
      pp.SwapEffect = D3DSWAPEFFECT_FLIP;
      pp.hDeviceWindow = window;
      pp.Windowed  = TRUE;
    
      if (FAILED(d->object->CreateDevice (D3DADAPTER_DEFAULT,
    				      D3DDEVTYPE_HAL,
    				      window,
    				      D3DCREATE_HARDWARE_VERTEXPROCESSING,
    				      &pp,
    				      &d->device)))
        goto no_device;
    
      if (FAILED (D3DXCreateSprite (d->device, &d->sprite)))
        goto no_sprite;
    
      if (FAILED (d->device->CreateTexture (width, height, 1,
    					D3DUSAGE_DYNAMIC,
    					dm.Format,
    					D3DPOOL_DEFAULT,
    					&d->texture, NULL)))
        goto no_texture;
    
      return d;
    
     no_texture:
      d->sprite->Release ();
     no_sprite:
      d->device->Release ();
     no_device:
      d->object->Release ();
     no_object:
      free (d);
     no_data:
      return NULL;
    }
    
    void
    data_shutdown (Data *d)
    {
      if (!d)
        return;
    
      d->texture->Release ();
      d->sprite->Release ();
      d->device->Release ();
      d->object->Release ();
    }
    
    void
    data_paint (Data *d)
    {
      D3DLOCKED_RECT d3d_rect;
      RECT rect;
      int w;
      int h;
      int depth;
      int pitch;
      unsigned char *tmp;
      unsigned char *d3d_tmp;
      static int foo = 0;
    
      void *surf;
    
      w = 64;
      h = 64;
      depth = 4;
      pitch = depth * w;
    
      surf = calloc (w * h * depth, 1);
      if (!surf) {
        printf ("pas bon \n");
        return;
      }
      tmp = (unsigned char *)surf;
      for (int r = 0; r < w; r++) {
        for (int g = 0; g < h; g++, tmp += depth) {
          tmp[0] = (r * 2 + foo) % 256;
          tmp[1] = (g + foo) % 256;
          tmp[2] = (w - 1 - g) * 4;
        }
      }
      foo++;
    
      rect.left = 0;
      rect.top = 0;
      rect.right = w;
      rect.bottom = h;
    
      if (FAILED (d->device->BeginScene ()))
        return;
    
      if (FAILED (d->sprite->Begin (0)))
        return;
      if (FAILED (d->texture->LockRect (0, &d3d_rect, NULL, D3DLOCK_DISCARD))) {
        d->sprite->End ();
        d->device->EndScene ();
        return;
      }
    
      tmp = (unsigned char *)surf;
      d3d_tmp = (unsigned char *)d3d_rect.pBits;
      for (int j = 0; j < h; j++, tmp += pitch, d3d_tmp += d3d_rect.Pitch)
        memcpy (d3d_tmp, tmp, pitch);
    
      d->texture->UnlockRect(0);
    
      d->sprite->Draw (d->texture, &rect, NULL, NULL,
    		   D3DCOLOR_ARGB (0, 0, 0, 0));
      d->sprite->End ();
    
      d->device->EndScene ();
      d->device->Present (NULL, NULL, NULL, NULL);
    
      free (surf);
    }
    
    int WINAPI
    WinMain(HINSTANCE hinstance,
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
    	int nCmdShow)
    #if 0
    main ()
    #endif
    {
      WNDCLASS  wc;
      RECT      rect;
      MSG       msg;
      HINSTANCE instance;
      HWND      window;
      Data     *d;
      int       width;
      int       height;
    
      instance = GetModuleHandle(0);
      if (!instance)
        goto no_instance;
    
      ZeroMemory(&wc, sizeof(wc));
      wc.lpfnWndProc = MainWndProc;
      wc.hInstance = instance;
      wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
      wc.hCursor = LoadCursor (NULL, IDC_ARROW);
      wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
      wc.lpszClassName = "Direct3D_test";
    
      if(!RegisterClass(&wc))
        goto no_window_class;
    
      width = 320;
      height = 200;
    
      rect.left = 0;
      rect.top = 0;
      rect.right = width;
      rect.bottom = height;
      AdjustWindowRect (&rect, WS_OVERLAPPEDWINDOW | WS_SIZEBOX, FALSE);
    
      window = CreateWindow("Direct3D_test",
    			"Direct3D_test",
    			WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
    			CW_USEDEFAULT, CW_USEDEFAULT,
    			rect.right - rect.left, rect.bottom - rect.top,
    			NULL, NULL, instance, NULL);
      if (!window)
        goto no_window;
    
      d = data_init (window, width, height);
      if (!d)
        goto no_data;
    
      ShowWindow(window, SW_SHOWDEFAULT);
      UpdateWindow(window);
    
      while (GetMessage (&msg, NULL, 0, 0))
        {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
          data_paint (d);
        }
    
      data_shutdown (d);
      DestroyWindow (window);
      UnregisterClass ("Direct3D_test", instance);
      FreeLibrary (instance);
    
      return EXIT_SUCCESS;
    
     no_data:
      DestroyWindow (window);
     no_window:
      UnregisterClass ("Direct3D_test", instance);
     no_window_class:
      FreeLibrary (instance);
     no_instance:
      return EXIT_FAILURE;
    }
    il devrait afficher, dans la fenetre, une petite animation dans un carre de 64x64 pixels en haut a gauche

    bien sur, ca ne marche pas (ca marchait chez moi) C'est peut-etre une mauvaise initialisation de direct3d (fonction data_init)

    quelqu'un a-t-il une idee ?
    merci

Discussions similaires

  1. afficher des donnees argb avec direct3d
    Par d'Oursse dans le forum DirectX
    Réponses: 3
    Dernier message: 15/08/2007, 09h56
  2. Réponses: 6
    Dernier message: 06/02/2007, 16h31
  3. [Debutant] Afficher des chaines unicodes avec println
    Par MichaelB dans le forum Langage
    Réponses: 2
    Dernier message: 10/01/2007, 19h39
  4. Réponses: 4
    Dernier message: 16/11/2006, 18h54
  5. Réponses: 1
    Dernier message: 19/07/2006, 12h47

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