gpt4 book ai didi

c++ - 在 directx 9 c++ 中应用纹理时出现巨大问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:12 26 4
gpt4 key购买 nike

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

这是我的代码

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

更新

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

最佳答案

您的问题是您根本不使用声明的 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 结构也是如此。

关于c++ - 在 directx 9 c++ 中应用纹理时出现巨大问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807859/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com