2010-08-04 7 views
1
#include <windows.h> 
#include <d3d9.h> 
#include <D3DX9Mesh.h> 

#define THROW_ERROR_AND_EXIT(x) { \ 
    MessageBox(0,x,0,0); \ 
    return -1; \ 
    } 

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    // Handle close event 
    switch(msg) 
    { 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     return 0; 
    } 
    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    // Registering class 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 
    wcex.style= CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc= (WNDPROC)WndProc; 
    wcex.cbClsExtra= 0; 
    wcex.cbWndExtra= 0; 
    wcex.hInstance= hInstance; 
    wcex.hIcon= 0; 
    wcex.hCursor= LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName= 0; 
    wcex.lpszClassName= "MyMeshViewer"; 
    wcex.hIconSm= 0; 

    RegisterClassEx(&wcex); 

    // Creating Window 
    HWND hWnd = CreateWindow("MyMeshViewer", "MyMeshViewer", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 

    // Creating Direct3D object 
    LPDIRECT3D9 d3dObject=NULL; 
    LPDIRECT3DDEVICE9 d3dDevice=NULL; 

    d3dObject=Direct3DCreate9(D3D_SDK_VERSION); 

    // Creating Direct3D device 
    if(NULL == d3dObject) 
     THROW_ERROR_AND_EXIT("NULL == d3dObject"); 
    D3DPRESENT_PARAMETERS presParams; 
    ZeroMemory(&presParams,sizeof(presParams)); 
    presParams.Windowed=TRUE; 
    presParams.SwapEffect=D3DSWAPEFFECT_DISCARD; 
    presParams.BackBufferFormat=D3DFMT_UNKNOWN; 
    presParams.PresentationInterval=D3DPRESENT_INTERVAL_ONE; 
    HRESULT hr=d3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &d3dDevice); 
    if(FAILED(hr)) 
     THROW_ERROR_AND_EXIT("d3dObject->CreateDevice"); 

    // Rendering 
    d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0),1.0f,0); 
    d3dDevice->BeginScene(); 

    // Loading the mesh 
    LPD3DXBUFFER materialBuffer = NULL; 
    DWORD numMaterials = 0; 
    LPD3DXMESH mesh = NULL; 
    hr=D3DXLoadMeshFromX("tiger.x", D3DXMESH_SYSTEMMEM, 
     d3dDevice, NULL, 
     &materialBuffer,NULL, &numMaterials, 
     &mesh); 
    if(FAILED(hr)) 
     THROW_ERROR_AND_EXIT("hr=D3DXLoadMeshFromX"); 

    // Loading the material buffer 
    D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)materialBuffer->GetBufferPointer(); 
    // Holding material and texture pointers 
    D3DMATERIAL9 *meshMaterials = new D3DMATERIAL9[numMaterials]; 
    LPDIRECT3DTEXTURE9 *meshTextures = new LPDIRECT3DTEXTURE9[numMaterials]; 
    // Filling material and texture arrays 
    for (DWORD i=0; i<numMaterials; i++) 
    { 
     // Copy the material 
     meshMaterials[i] = d3dxMaterials[i].MatD3D; 

     // Set the ambient color for the material (D3DX does not do this) 
     meshMaterials[i].Ambient = meshMaterials[i].Diffuse; 

     // Create the texture if it exists - it may not 
     meshTextures[i] = NULL; 
     if (d3dxMaterials[i].pTextureFilename) 
      D3DXCreateTextureFromFile(d3dDevice, d3dxMaterials[i].pTextureFilename,  &meshTextures[i]); 
    } 

    materialBuffer->Release(); 

    for (DWORD i=0; i<numMaterials; i++) 
    { 
     // Set the material and texture for this subset 
     d3dDevice->SetMaterial(&meshMaterials[i]); 
     d3dDevice->SetTexture(0,meshTextures[i]); 
     // Draw the mesh subset 
     mesh->DrawSubset(i); 
    } 

    d3dDevice->EndScene(); 
    d3dDevice->Present(NULL, NULL, NULL, NULL);  

    // Show Window 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    // Handle messages 
    MSG msg; 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    }  

    return (int)msg.wParam; 
} 

Ceci est mon programme pour charger un maillage tiger.x et l'afficher. Mais il ne s'affiche pas. La valeur de la variable numMaterials reste 1 tout le temps. Je suppose qu'il y a un problème avec mon programme. Quelqu'un s'il vous plaît aidez-moi à comprendre. Merci.Affichage du maillage en utilisant DirectX 9


Le fichier de maillage tiger.x ci-dessous

http://pastebin.com/DuvpS4mh

+0

Ouf long morceau de code homme – ckv

Répondre

1

Le problème est que vous êtes rendu à la fenêtre, puis en appelant UpdateWindow qui force une nouvelle peinture, effaçant ainsi le dessin.

Je vous suggère de télécharger le SDK DirectX et de regarder les exemples pour voir comment construire une boucle de rendu «correcte».

+0

Merci. J'aurais dû faire comme [ceci] (http://pastebin.com/005A5aYU) – bdhar

0
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    switch (message) 
    { 
    case WM_PAINT: 
    { 
     // Rendering 
     d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0); 
     d3dDevice->BeginScene(); 


     for (DWORD i = 0; i < numMaterials; i++) 
     { 
      // Set the material and texture for this subset 
      d3dDevice->SetMaterial(&meshMaterials[i]); 
      d3dDevice->SetTexture(0, meshTextures[i]); 
      // Draw the mesh subset 
      mesh->DrawSubset(i); 
     } 

     d3dDevice->EndScene(); 
     d3dDevice->Present(NULL, NULL, NULL, NULL); 

    } 
    break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

Vous devez gérer l'événement WM_PAINT.

Questions connexes