2010-09-27 3 views
1

Je ne reçois aucune erreur lorsque je compile ma partie. Le seul problème est qu'il n'appliquera pas la texture, à la place il applique une couleur (je sais que c'est bizarre). Si la texture a deux couleurs blanc et rouge, alors le blanc sera appliqué.Énorme problème lors de l'application de texture dans directx 9 C++

Voici mon code

// Monopoly.cpp : Defines the entry point for the application. 
// 

#include "stdafx.h" 
#include "Monopoly.h" 

#define MAX_LOADSTRING 100 

// Global Variables: 
HINSTANCE hInst;        // current instance 
HWND hWnd;          // current window 
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 
LPDIRECT3D9 d3d;        // the pointer to our Direct3D interface 
LPDIRECT3DDEVICE9 d3ddev;      // the pointer to the device class 
LPDIRECT3DVERTEXBUFFER9 buffer= NULL; 
LPDIRECT3DINDEXBUFFER9 i_buffer; 
#define SCREEN_HEIGHT 600 
#define SCREEN_WIDTH 800 
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE) 
float z = 0.0f; 
float y = 0.0f; 
float x = 0.0f; 
float rz = 0.0f; 
float ry = 0.0f; 
float rx = 0.0f; 


// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

VOID InitGraphic(); 
VOID InitMatrices(); 

// FVF structure 
struct CUSTOMVERTEX 
{ 
    FLOAT x, y, z; // from the D3DFVF_XYZRHW flag 
    DWORD color; // from the D3DFVF_DIFFUSE flag 
}; 

struct textured_vertex{ 
    float x, y, z, rhw; // The transformed(screen space) position for the vertex. 
    float tu,tv;   // Texture coordinates 
}; 

//Transformed vertex with 1 set of texture coordinates 
const DWORD tri_fvf=D3DFVF_XYZRHW|D3DFVF_TEX1; 

CUSTOMVERTEX vertices[500]; 
IDirect3DTexture9 *g_texture=NULL; 

// Initilizing direct3D 9 
void InitD3D(HWND hWnd) 
{ 
    d3d = Direct3DCreate9(D3D_SDK_VERSION); 

    D3DPRESENT_PARAMETERS d3dpp; 

    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.Windowed = FALSE; 
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    d3dpp.hDeviceWindow = hWnd; 
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
    d3dpp.BackBufferWidth = SCREEN_WIDTH; 
    d3dpp.BackBufferHeight = SCREEN_HEIGHT; 
    d3dpp.EnableAutoDepthStencil = TRUE; 
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 

    d3d->CreateDevice(D3DADAPTER_DEFAULT, 
         D3DDEVTYPE_HAL, 
         hWnd, 
         D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
         &d3dpp, 
         &d3ddev); 

    InitGraphic(); 

    D3DXCreateTextureFromFile(d3ddev, //Direct3D Device 
           _T("image.png"),  //File Name 
           &g_texture); //Texture handle 

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting 
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // turn off culling 
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer 
    d3ddev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1); 
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); 
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); //Ignored 

    d3ddev->SetTexture(0,g_texture); 
} 


//render a single frame 
void RenderFrame(void) 
{ 
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 

    d3ddev->BeginScene(); 

    d3ddev->SetFVF(CUSTOMFVF); 

    InitMatrices(); 

    // set the world transform 

    D3DXMATRIX matTran; // a matrix to store the rotation for each triangle 
    D3DXMATRIX matRotz; 
    D3DXMATRIX matRoty; 
    D3DXMATRIX matRotx; 
    D3DXMatrixTranslation(&matTran, x, y, z); 
    D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz)); 
    D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry)); 
    D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx)); 

    d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx)); // set the world transform 

    // select the vertex buffer to display 
    d3ddev->SetStreamSource(0, buffer, 0, sizeof(CUSTOMVERTEX)); 
    d3ddev->SetIndices(i_buffer); 

    // draw the Hypercraft 
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); 

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL); 
} 

// cleans up Direct3D and COM 
void CleanD3D(void) 
{ 
    buffer->Release(); 
    i_buffer->Release(); 
    d3ddev->Release(); // close and release the 3D device 
    d3d->Release(); // close and release Direct3D 
    g_texture->Release(); 
    g_texture=NULL; 
} 

VOID InitGraphic() 
{ 
    CUSTOMVERTEX vertices[] = 
    { 
     { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), }, 
     { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0), }, 
     { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(255, 0, 0), }, 
     { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), }, 
     { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), }, 
     { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), }, 
     { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), }, 
     { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), }, 
    }; 

    // create a vertex buffer interface called v_buffer 
    d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX), 
           0, 
           CUSTOMFVF, 
           D3DPOOL_MANAGED, 
           &buffer, 
           NULL); 

    VOID* pVoid; // a void pointer 

    // lock v_buffer and load the vertices into it 
    buffer->Lock(0, 0, (void**)&pVoid, 0); 
    memcpy(pVoid, vertices, sizeof(vertices)); 
    buffer->Unlock(); 

    // create the indices using an int array 
    short indices[] = 
    { 
     0, 1, 2, // side 1 
     2, 1, 3, 
     4, 0, 6, // side 2 
     6, 0, 2, 
     7, 5, 6, // side 3 
     6, 5, 4, 
     3, 1, 7, // side 4 
     7, 1, 5, 
     4, 5, 0, // side 5 
     0, 5, 1, 
     3, 7, 2, // side 6 
     2, 7, 6, 
    }; 

    // create an index buffer interface called i_buffer 
    d3ddev->CreateIndexBuffer(36*sizeof(short), 
           0, 
           D3DFMT_INDEX16, 
           D3DPOOL_MANAGED, 
           &i_buffer, 
           NULL); 

    // lock i_buffer and load the indices into it 
    i_buffer->Lock(0, 0, (void**)&pVoid, 0); 
    memcpy(pVoid, indices, sizeof(indices)); 
    i_buffer->Unlock(); 
} 

// Create metrices 
VOID InitMatrices() 
{ 
    // set the view transform 
    D3DXMATRIX matView; // the view transform matrix 
    D3DXMatrixLookAtLH(&matView, 
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f), // the camera position 
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),  // the look-at position 
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction 
    d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView 

    // set the projection transform 
    D3DXMATRIX matProjection; // the projection transform matrix 
    D3DXMatrixPerspectiveFovLH(&matProjection, 
           D3DXToRadian(45), // the horizontal field of view 
           (FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, // aspect ratio 
           1.0f, // the near view-plane 
           100.0f); // the far view-plane 
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection 
} 

UDPATE

// Monopoly.cpp : Defines the entry point for the application. 
// 

#include "stdafx.h" 
#include "Monopoly.h" 

#define MAX_LOADSTRING 100 

// Global Variables: 
HINSTANCE hInst;        // current instance 
HWND hWnd;          // current window 
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 
LPDIRECT3D9 d3d;        // the pointer to our Direct3D interface 
LPDIRECT3DDEVICE9 d3ddev;      // the pointer to the device class 
LPDIRECT3DVERTEXBUFFER9 buffer= NULL; 
LPDIRECT3DINDEXBUFFER9 i_buffer; 
#define SCREEN_HEIGHT 600 
#define SCREEN_WIDTH 800 
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) 
float z = 0.0f; 
float y = 0.0f; 
float x = 0.0f; 
float rz = 0.0f; 
float ry = 0.0f; 
float rx = 0.0f; 


// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

VOID InitGraphic(); 
VOID InitMatrices(); 

// FVF structure 
struct CUSTOMVERTEX 
{ 
    FLOAT x, y, z; // from the D3DFVF_XYZRHW flag 
    DWORD color; // from the D3DFVF_DIFFUSE flag 
    FLOAT tx, ty; 
}; 

IDirect3DTexture9 *g_texture=NULL; 

// Initilizing direct3D 9 
void InitD3D(HWND hWnd) 
{ 
    d3d = Direct3DCreate9(D3D_SDK_VERSION); 

    D3DPRESENT_PARAMETERS d3dpp; 

    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.Windowed = FALSE; 
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    d3dpp.hDeviceWindow = hWnd; 
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
    d3dpp.BackBufferWidth = SCREEN_WIDTH; 
    d3dpp.BackBufferHeight = SCREEN_HEIGHT; 
    d3dpp.EnableAutoDepthStencil = TRUE; 
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 

    d3d->CreateDevice(D3DADAPTER_DEFAULT, 
         D3DDEVTYPE_HAL, 
         hWnd, 
         D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
         &d3dpp, 
         &d3ddev); 

    InitGraphic(); 

    D3DXCreateTextureFromFile(d3ddev, //Direct3D Device 
           _T("image.png"),  //File Name 
           &g_texture); //Texture handle 

    d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting 
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // turn off culling 
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer 
    d3ddev->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1); 
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE); 
    d3ddev->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE); //Ignored 

    d3ddev->SetTexture(0,g_texture); 
} 


//render a single frame 
void RenderFrame(void) 
{ 
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 

    d3ddev->BeginScene(); 

    d3ddev->SetFVF(CUSTOMFVF); 

    InitMatrices(); 

    // set the world transform 

    D3DXMATRIX matTran; // a matrix to store the rotation for each triangle 
    D3DXMATRIX matRotz; 
    D3DXMATRIX matRoty; 
    D3DXMATRIX matRotx; 
    D3DXMatrixTranslation(&matTran, x, y, z); 
    D3DXMatrixRotationZ(&matRotz, D3DXToRadian(rz)); 
    D3DXMatrixRotationY(&matRoty, D3DXToRadian(ry)); 
    D3DXMatrixRotationX(&matRotx, D3DXToRadian(rx)); 

    d3ddev->SetTransform(D3DTS_WORLD, &(matTran * matRotz * matRoty * matRotx)); // set the world transform 

    // select the vertex buffer to display 
    d3ddev->SetStreamSource(0, buffer, 0, sizeof(CUSTOMVERTEX)); 
    d3ddev->SetIndices(i_buffer); 

    // draw the Hypercraft 
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12); 

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL); 
} 

// cleans up Direct3D and COM 
void CleanD3D(void) 
{ 
    buffer->Release(); 
    i_buffer->Release(); 
    d3ddev->Release(); // close and release the 3D device 
    d3d->Release(); // close and release Direct3D 
    g_texture->Release(); 
    g_texture=NULL; 
} 

VOID InitGraphic() 
{ 
    CUSTOMVERTEX vertices[] = 
    { 
     { -3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 1.0f, }, 
     { 3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f, }, 
     { -3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f, }, 
     { 3.0f, -3.0f, -3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, }, 
     { -3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 1.0f, }, 
     { 3.0f, 3.0f, 3.0f, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 1.0f, }, 
     { -3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 1.0f, }, 
     { 3.0f, -3.0f, 3.0f, D3DCOLOR_XRGB(0, 255, 255), 1.0f, 1.0f, }, 
    }; 

    // create a vertex buffer interface called v_buffer 
    d3ddev->CreateVertexBuffer(8*sizeof(CUSTOMVERTEX), 
           0, 
           CUSTOMFVF, 
           D3DPOOL_MANAGED, 
           &buffer, 
           NULL); 

    VOID* pVoid; // a void pointer 

    // lock v_buffer and load the vertices into it 
    buffer->Lock(0, 0, (void**)&pVoid, 0); 
    memcpy(pVoid, vertices, sizeof(vertices)); 
    buffer->Unlock(); 

    // create the indices using an int array 
    short indices[] = 
    { 
     0, 1, 2, // side 1 
     2, 1, 3, 
     4, 0, 6, // side 2 
     6, 0, 2, 
     7, 5, 6, // side 3 
     6, 5, 4, 
     3, 1, 7, // side 4 
     7, 1, 5, 
     4, 5, 0, // side 5 
     0, 5, 1, 
     3, 7, 2, // side 6 
     2, 7, 6, 
    }; 

    // create an index buffer interface called i_buffer 
    d3ddev->CreateIndexBuffer(36*sizeof(short), 
           0, 
           D3DFMT_INDEX16, 
           D3DPOOL_MANAGED, 
           &i_buffer, 
           NULL); 

    // lock i_buffer and load the indices into it 
    i_buffer->Lock(0, 0, (void**)&pVoid, 0); 
    memcpy(pVoid, indices, sizeof(indices)); 
    i_buffer->Unlock(); 
} 

// Create metrices 
VOID InitMatrices() 
{ 
    // set the view transform 
    D3DXMATRIX matView; // the view transform matrix 
    D3DXMatrixLookAtLH(&matView, 
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f), // the camera position 
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),  // the look-at position 
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction 
    d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView 

    // set the projection transform 
    D3DXMATRIX matProjection; // the projection transform matrix 
    D3DXMatrixPerspectiveFovLH(&matProjection, 
           D3DXToRadian(45), // the horizontal field of view 
           (FLOAT)SCREEN_WIDTH/(FLOAT)SCREEN_HEIGHT, // aspect ratio 
           1.0f, // the near view-plane 
           100.0f); // the far view-plane 
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection 
} 

Répondre

1

Votre problème est que vous n'utilisez pas le textured_vertex déclaré à tous et utilisez le CustomVertex à la place. Parce que CustomVertex ne contient que des informations de couleur, il est tout à fait évident quel résultat vous obtiendrez :) Je vous suggère élargir la struct CustomVertex comme ceci:

struct CUSTOMVERTEX 
{ 
    FLOAT x, y, z; // from the D3DFVF_XYZRHW flag 
    DWORD color; // from the D3DFVF_DIFFUSE flag 
    FLOAT tx, ty; // from the D3DFVF_TEX1 flag 
}; 

aussi changer la définition de CUSTOMFVF pour correspondre à la struct:


Également le tableau global CUSTOMVERTEX vertices[500] semble être complètement inutilisé, supprimez-le afin qu'il ne provoque pas un comportement indésirable à l'avenir. Il en va de même pour la structure textured_vertex non utilisée.

+0

bien que j'ai fait ce que vous avez dit mais j'ai toujours le même résultat – Ramilol

+0

Je suppose que vous avez également ajouté les coordonnées de texture à la définition du tableau vertices? En ce moment, il n'y a que la position et la couleur, vous devez mettre deux autres nombres comme coordonnées de texture: '{-3.0f, 3.0f, -3.0f, D3DCOLOR_XRGB (0, 0, 255), 1.0f, 1.0f}' –

+0

je l'ai fait pour chaque seul mais toujours le même résultat vérifier le nouveau code – Ramilol

Questions connexes