2010-09-27 75 views
1

我在编译我的游戏时没有遇到任何错误。唯一的问题是它不会应用纹理,而是应用一种颜色(我知道它很奇怪)。如果纹理有白色和红色两种颜色,则会应用白色。在directx中应用纹理时存在巨大问题9 C++

这里是我的代码

// 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 
} 

回答

1

你的问题是你根本就不使用声明textured_vertex和使用CUSTOMVERTEX代替。由于CUSTOMVERTEX只包含颜色信息,这是很明显,你会得到什么结果:)我建议扩大CUSTOMVERTEX结构是这样的:

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 
}; 

也改变了定义CUSTOMFVF的匹配结构:

#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) 


此外,全球阵列CUSTOMVERTEX vertices[500]似乎完全没有使用,删除它,以便它在未来不会导致不需要的行为。对于未使用的textured_vertex结构也是如此。

+0

嗯,我做了你说的,但我仍然有相同的结果 – Ramilol 2010-09-27 22:01:05

+0

我想你还添加了纹理坐标到顶点数组定义?现在只有位置和颜色,你应该把另外两个数字作为纹理坐标:'{-3.0f,3.0f,-3.0f,D3DCOLOR_XRGB(0,0,255),1.0f,1.0f}' – 2010-09-27 22:08:09

+0

我做了对于每一个但仍然是相同的结果检查新代码 – Ramilol 2010-09-27 22:19:30