C++、DirectX 9:渲染自制顶点结构
C++, DirecX 9: Render Self-made Vertex Struct
我是 Whosebug 的新手,这是我的第一个问题。
我是 C++ 和 DirectX9 的新手,所以它们可能是我代码中的很多问题!
我的问题:
我想渲染一个 "D3DXVECTOR3"s 的结构,用于从 .raw.
制作高度图
我有一个工作中的 FP 相机系统,想实现一个高度图。我遵循了教程 "Terrain in DirectX9 and C++" (https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/),但代码无法正常工作(教程是 "Bad" 德语,我使用它是因为我没有找到任何教程DirectX9 中的高度图。...我是德国人)。
它输出的都是这个(忽略Cylinder,它只是一个测试):
renderissue
以下是准则的一些相关部分:
Heightmap.cpp
BOOL LoadMapFromRAW(char* pcHMAP, UINT sizePerSide, BYTE **ppData)
{
BYTE *pData = NULL;
FILE *pFile = NULL;
// Open as Binary
pFile = fopen(pcHMAP, "rb");
if (!pFile)
return false;
// Memory
pData = new BYTE[sizePerSide*sizePerSide];
// Read
fread(&pData, 1, sizePerSide*sizePerSide, pFile);
*ppData = pData;
return true;
}
BOOL CreateVertices(UINT sizePerSide, BYTE *pVertexData, STerrainVector **ppVertices, UINT *uiTriangleCount)
{
UINT uiSizePerSide = sizePerSide - 1;
*uiTriangleCount = uiSizePerSide*uiSizePerSide*2;
// Memory for Vertices
STerrainVector *pVertices = new STerrainVector[(*uiTriangleCount) * 3];
// Buffer
int index = 0;
for (int x = 0; x < uiSizePerSide; x++)
for (int z = 0; z < uiSizePerSide; z++)
{
index += 6;
pVertices[index + 0].vPos = D3DXVECTOR3(x, pVertexData[z * sizePerSide + x], z);
pVertices[index + 1].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
pVertices[index + 2].vPos = D3DXVECTOR3(x + 1, pVertexData[z * sizePerSide + x + 1], z);
pVertices[index + 3].vPos = D3DXVECTOR3(x + 1, pVertexData[(z) * sizePerSide + x + 1], z);
pVertices[index + 4].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
pVertices[index + 5].vPos = D3DXVECTOR3(x + 1, pVertexData[(z + 1) * sizePerSide + x + 1], z + 1);
}
// Done
*ppVertices = pVertices;
return true;
}
main.cpp
//////////////UPDATE///////////////////
{
gDXdevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(160, 200, 255), 1.0f, 0);
gDXdevice->BeginScene();
// Get and set the view matrix
D3DXMATRIX viewMatrix;
gCamera->CalculateViewMatrix(&viewMatrix);
gDXdevice->SetTransform(D3DTS_VIEW, &viewMatrix)
// Draw Heightmap
gDXdevice->SetFVF(D3DFVF_XYZ);
gDXdevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, uiTriangleCount, (void*)&pVertices, sizeof(STerrainVector));
gDXdevice->EndScene();
gDXdevice->Present(0, 0, 0, 0);}
<pre> <code>
////////////////////////Part in Setup////////////////////////
bool SetupDirect3D(HWND hWnd)
{
// Standart Directx Init
gDX3dObject = Direct3DCreate9(D3D_SDK_VERSION);
if (!gDX3dObject)
return 0;
D3DPRESENT_PARAMETERS presParams;
ZeroMemory(&presParams, sizeof(presParams));
presParams.Windowed = TRUE;
presParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
presParams.BackBufferFormat = D3DFMT_UNKNOWN;
presParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
presParams.EnableAutoDepthStencil = TRUE;
presParams.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT hr = gDX3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &gDXdevice);
if (FAILED(hr))
return false;
// Z-Buffer
gDXdevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
// Create a Model to View
hr = D3DXCreateCylinder(gDXdevice, 0.1f, 0.1f, 1.0f, 10, 10, &gCylinderMesh, 0);
if (FAILED(hr))
return false;
hr = D3DXCreatePolygon(gDXdevice, 10.0f, 4, &gPlateMesh, 0);
if (FAILED(hr))
return false;
// Light for Model
gDXdevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(20, 20, 20));
gDXdevice->SetRenderState(D3DRS_LIGHTING, TRUE);
// A light-structure
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(D3DLIGHT9));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.a = 1.0f;
light.Diffuse.b = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.r = 1.0f;
light.Range = 1000.0f;
// Direction for Light
D3DXVECTOR3 vecDir;
vecDir = D3DXVECTOR3(0.0f, -0.3f, 0.7f);
D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &vecDir);
// Turn it on!
gDXdevice->SetLight(0, &light);
gDXdevice->LightEnable(0, TRUE);
// Create World
hr = LoadMapFromRAW("heightMap.raw", 1024, &pHeightData);
if (!hr)
return false;
hr = CreateVertices(1024, pHeightData, &pVertices, &uiTriangleCount);
if (!hr)
return false;
// Set up a Matrix
RECT rect;
GetClientRect(hWnd, &rect);
D3DXMATRIX matProj;
float aspect = (rect.right - rect.left) / (float)(rect.bottom - rect.top);
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, aspect, 1.0f, 100.0f);
gDXdevice->SetTransform(D3DTS_PROJECTION, &matProj);
return true;
}
.....使用代码有困难......
“*pHeightData; *pVertices; uiTriangleCount”在代码开头定义。
我希望你能帮助我!
我为糟糕的英语道歉:我是德国人,还在上学! :-)
提前致谢!
所以...我自己解决了。解决方案是:完全重写!对于遇到 same/similar 问题的任何人:http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut13.php 这是我找到的非常好的教程!试试吧!
我是 Whosebug 的新手,这是我的第一个问题。 我是 C++ 和 DirectX9 的新手,所以它们可能是我代码中的很多问题!
我的问题: 我想渲染一个 "D3DXVECTOR3"s 的结构,用于从 .raw.
制作高度图我有一个工作中的 FP 相机系统,想实现一个高度图。我遵循了教程 "Terrain in DirectX9 and C++" (https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/),但代码无法正常工作(教程是 "Bad" 德语,我使用它是因为我没有找到任何教程DirectX9 中的高度图。...我是德国人)。
它输出的都是这个(忽略Cylinder,它只是一个测试): renderissue
以下是准则的一些相关部分:
Heightmap.cpp
BOOL LoadMapFromRAW(char* pcHMAP, UINT sizePerSide, BYTE **ppData)
{
BYTE *pData = NULL;
FILE *pFile = NULL;
// Open as Binary
pFile = fopen(pcHMAP, "rb");
if (!pFile)
return false;
// Memory
pData = new BYTE[sizePerSide*sizePerSide];
// Read
fread(&pData, 1, sizePerSide*sizePerSide, pFile);
*ppData = pData;
return true;
}
BOOL CreateVertices(UINT sizePerSide, BYTE *pVertexData, STerrainVector **ppVertices, UINT *uiTriangleCount)
{
UINT uiSizePerSide = sizePerSide - 1;
*uiTriangleCount = uiSizePerSide*uiSizePerSide*2;
// Memory for Vertices
STerrainVector *pVertices = new STerrainVector[(*uiTriangleCount) * 3];
// Buffer
int index = 0;
for (int x = 0; x < uiSizePerSide; x++)
for (int z = 0; z < uiSizePerSide; z++)
{
index += 6;
pVertices[index + 0].vPos = D3DXVECTOR3(x, pVertexData[z * sizePerSide + x], z);
pVertices[index + 1].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
pVertices[index + 2].vPos = D3DXVECTOR3(x + 1, pVertexData[z * sizePerSide + x + 1], z);
pVertices[index + 3].vPos = D3DXVECTOR3(x + 1, pVertexData[(z) * sizePerSide + x + 1], z);
pVertices[index + 4].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
pVertices[index + 5].vPos = D3DXVECTOR3(x + 1, pVertexData[(z + 1) * sizePerSide + x + 1], z + 1);
}
// Done
*ppVertices = pVertices;
return true;
}
main.cpp
//////////////UPDATE///////////////////
{
gDXdevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(160, 200, 255), 1.0f, 0);
gDXdevice->BeginScene();
// Get and set the view matrix
D3DXMATRIX viewMatrix;
gCamera->CalculateViewMatrix(&viewMatrix);
gDXdevice->SetTransform(D3DTS_VIEW, &viewMatrix)
// Draw Heightmap
gDXdevice->SetFVF(D3DFVF_XYZ);
gDXdevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, uiTriangleCount, (void*)&pVertices, sizeof(STerrainVector));
gDXdevice->EndScene();
gDXdevice->Present(0, 0, 0, 0);}
<pre> <code>
////////////////////////Part in Setup////////////////////////
bool SetupDirect3D(HWND hWnd)
{
// Standart Directx Init
gDX3dObject = Direct3DCreate9(D3D_SDK_VERSION);
if (!gDX3dObject)
return 0;
D3DPRESENT_PARAMETERS presParams;
ZeroMemory(&presParams, sizeof(presParams));
presParams.Windowed = TRUE;
presParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
presParams.BackBufferFormat = D3DFMT_UNKNOWN;
presParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
presParams.EnableAutoDepthStencil = TRUE;
presParams.AutoDepthStencilFormat = D3DFMT_D16;
HRESULT hr = gDX3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &gDXdevice);
if (FAILED(hr))
return false;
// Z-Buffer
gDXdevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
// Create a Model to View
hr = D3DXCreateCylinder(gDXdevice, 0.1f, 0.1f, 1.0f, 10, 10, &gCylinderMesh, 0);
if (FAILED(hr))
return false;
hr = D3DXCreatePolygon(gDXdevice, 10.0f, 4, &gPlateMesh, 0);
if (FAILED(hr))
return false;
// Light for Model
gDXdevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(20, 20, 20));
gDXdevice->SetRenderState(D3DRS_LIGHTING, TRUE);
// A light-structure
D3DLIGHT9 light;
ZeroMemory(&light, sizeof(D3DLIGHT9));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.a = 1.0f;
light.Diffuse.b = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.r = 1.0f;
light.Range = 1000.0f;
// Direction for Light
D3DXVECTOR3 vecDir;
vecDir = D3DXVECTOR3(0.0f, -0.3f, 0.7f);
D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &vecDir);
// Turn it on!
gDXdevice->SetLight(0, &light);
gDXdevice->LightEnable(0, TRUE);
// Create World
hr = LoadMapFromRAW("heightMap.raw", 1024, &pHeightData);
if (!hr)
return false;
hr = CreateVertices(1024, pHeightData, &pVertices, &uiTriangleCount);
if (!hr)
return false;
// Set up a Matrix
RECT rect;
GetClientRect(hWnd, &rect);
D3DXMATRIX matProj;
float aspect = (rect.right - rect.left) / (float)(rect.bottom - rect.top);
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, aspect, 1.0f, 100.0f);
gDXdevice->SetTransform(D3DTS_PROJECTION, &matProj);
return true;
}
.....使用代码有困难......
“*pHeightData; *pVertices; uiTriangleCount”在代码开头定义。
我希望你能帮助我!
我为糟糕的英语道歉:我是德国人,还在上学! :-)
提前致谢!
所以...我自己解决了。解决方案是:完全重写!对于遇到 same/similar 问题的任何人:http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut13.php 这是我找到的非常好的教程!试试吧!