我想为我的地形网格创建一个普通的地图纹理,我用我的cpu生成,我不是要你为我编写我的代码。我要求你发给我一个独立的教程,因为我得到的每个教程都有一个倒退系列,我不想回去......请帮助我。或者给我一些我应该做的事情......
My Terrain.h
#ifndef TERRAIN_H
#define TERRAIN_H
#endif
#include "d3dUtil.h"
#include <string>
const int NUM_VERTS = 528;
const int NUM_INDICES = 792;
class Terrain{
public:
//Constructor
Terrain(int x, int y, int z, std::wstring fileName, char* heightmap);
Terrain(int x, int y, int z, std::wstring fileName, float terrainData[64][64][4]);
//Destructor
~Terrain();
//Methods
void Initialize(IDirect3DDevice9* device);
void Update(float dt);
void Draw(IDirect3DDevice9* device);
void Release();
void raiseVertex(int X, int Z, IDirect3DDevice9* device);
//Publc attributes
int m_iXPos;
int m_iYPos;
int m_iZPos;
std::wstring texName;
float m_faTerrainData[64][64][4];
private:
//Attributes
int m_iWidth;
int m_iHeight;
int m_iVertexCount;
int m_iIndeciseCount;
D3DXMATRIX world;
IDirect3DVertexBuffer9* vBuffer;
IDirect3DIndexBuffer9* iBuffer;
IDirect3DTexture9* texture;
D3DXMATRIX GetWorld();
D3DMATERIAL9 material;
};
My Terrain.cpp
#include "Terrain.h"
#include <math.h>
static const int NUMV;
Terrain::Terrain(int x, int y, int z, std::wstring fileName, float terrainData[64][64][4]){
float test[64][64][4];
float val = 10;
for(int X = 0; X < 64; X ++){
for(int Y = 0; Y < 64; Y ++){
for(int Z = 0; Z < 4; Z ++){
val = terrainData[X][Y][Z];
m_faTerrainData[X][Y][Z] = val;
}
}
}
m_iXPos = x;
m_iYPos = y;
m_iZPos = z;
m_iVertexCount = 16384;
m_iIndeciseCount = 24576;
texName = fileName;
material.Ambient = (D3DXCOLOR)d3dColors::WHITE;
material.Diffuse = (D3DXCOLOR)d3dColors::WHITE;
material.Emissive = (D3DXCOLOR)d3dColors::BLACK;
material.Power = 5.0f;
material.Specular = (D3DXCOLOR)d3dColors::WHITE;
}
Terrain::Terrain(int x, int y, int z, std::wstring fileName, char* heightmap){
m_iXPos = x;
m_iYPos = y;
m_iZPos = z;
m_iVertexCount = 16384;
m_iIndeciseCount = 24576;
BYTE R = 0;
BYTE G = 0;
BYTE B = 0;
texName = fileName;
material.Ambient = (D3DXCOLOR)d3dColors::WHITE;
material.Diffuse = (D3DXCOLOR)d3dColors::WHITE;
material.Emissive = (D3DXCOLOR)d3dColors::BLACK;
material.Power = 5.0f;
material.Specular = (D3DXCOLOR)d3dColors::WHITE;
}
Terrain::~Terrain(){
}
void Terrain::Initialize(IDirect3DDevice9* device){
D3DVertex::VertexPositionNormalTexture verts[16384];
WORD indices[24576];
float j = sqrt((float)(m_iVertexCount / 4));
float n = 0;
float g = 0;
float X = 32;
float Z = 32;
float textRep = 1.0f;
float textRepo = 0.0f;
int iVertex = 0;
int iIndex = 0;
for(int x = 0; x < j; x ++){
for(int z = 0; z < j; z ++){
if(iVertex < m_iVertexCount){
verts[iVertex] = D3DVertex::VertexPositionNormalTexture(x, m_faTerrainData[(int)x][(int)z][0], z+1, n, 1.0f, g, textRepo, textRepo);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, m_faTerrainData[(int)x][(int)z][1], z+1, n, 1.0f, g, textRep, textRepo);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, m_faTerrainData[(int)x][(int)z][2], z, n, 1.0f, g, textRepo, textRep);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, m_faTerrainData[(int)x][(int)z][3], z, n, 1.0f, g, textRep, textRep);
textRep += 0.5f;
textRepo += 0.5f;
if(textRep > 1){
textRep = 1;
textRepo = 0;
}
indices[iIndex ] = iVertex;
indices[iIndex+1] = iVertex+1;
indices[iIndex+2] = iVertex+2;
indices[iIndex+3] = iVertex+2;
indices[iIndex+4] = iVertex+1;
indices[iIndex+5] = iVertex+3;
iVertex += 4;
iIndex += 6;
}
}
}
device->CreateVertexBuffer(m_iVertexCount * sizeof(D3DVertex::VertexPositionNormalTexture),
NULL,
D3DVertex::VertexPositionNormalTexture::FVF,
D3DPOOL_MANAGED,
&vBuffer,
NULL);
VOID* pVerts;
vBuffer->Lock(0, sizeof(verts), (void**)&pVerts, 0);
memcpy_s(pVerts, sizeof(verts), verts, sizeof(verts));
vBuffer->Unlock();
device->CreateIndexBuffer(m_iIndeciseCount * sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&iBuffer,
NULL);
VOID* pIndices;
iBuffer->Lock(0, sizeof(indices), (void**)&pIndices, 0);
memcpy_s(pIndices, sizeof(indices), indices, sizeof(indices));
iBuffer->Unlock();
//Load texture
D3DXCreateTextureFromFile(device, texName.c_str(), &texture);
if(!texture){
MessageBox(NULL, L"Could not load texture", NULL, NULL);
return;
}
}
void Terrain::Update(float dt){
// rotation.y += dt;
//rotation.x += dt;
//rotation.z += dt;
}
void Terrain::Draw(IDirect3DDevice9* device){
device->SetTexture(0, texture);
device->SetMaterial(&material);
device->SetTransform(D3DTS_WORLD, &GetWorld());
device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
device->SetStreamSource(0, vBuffer, 0, sizeof(D3DVertex::VertexPositionNormalTexture));
device->SetIndices(iBuffer);
device->SetFVF(D3DVertex::VertexPositionNormalTexture::FVF);
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_iIndeciseCount, 0, m_iIndeciseCount/3);
}
D3DXMATRIX Terrain::GetWorld(){
D3DXMATRIX _scale, rX, rY, rZ, tRanslate;
D3DXVECTOR3 scale = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
D3DXVECTOR3 rotation = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 position = D3DXVECTOR3(m_iXPos, m_iYPos, m_iZPos);
D3DXMatrixScaling(&_scale, scale.x, scale.y, scale.z);
D3DXMatrixRotationX(&rX, rotation.x);
D3DXMatrixRotationY(&rY, rotation.y);
D3DXMatrixRotationZ(&rZ, rotation.z);
D3DXMatrixTranslation(&tRanslate, position.x, position.y, position.z);
world = _scale * rX * rY* rZ* tRanslate;
return world;
}
void Terrain::Release(){
delete(iBuffer);
delete(vBuffer);
delete(texture);
iBuffer = 0;
vBuffer = 0;
texture = 0;
}
void Terrain::raiseVertex(int X, int Z, IDirect3DDevice9* device){
iBuffer = 0;
vBuffer = 0;
texture = 0;
D3DVertex::VertexPositionNormalTexture verts[16384];
WORD indices[24576];
float j = sqrt((float)(m_iVertexCount / 4));
float n = 0;
float g = 0;
int iVertex = 0;
int iIndex = 0;
for(int x = 0; x < j; x ++){
for(int z = 0; z < j; z ++){
if(iVertex < m_iVertexCount){
if(x == X)
if(z == Z){
verts[iVertex] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, 0.5f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z, n, 1.0f, g, 1.0f, 1.0f);
}else
if(x == X)
if(z-1 == Z){
verts[iVertex] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, 0.5f, z, n, 1.0f, g, 1.0f, 1.0f);
}else
if(x-1 == X)
if(z == Z){
verts[iVertex] = D3DVertex::VertexPositionNormalTexture(x, 0.5f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z, n, 1.0f, g, 1.0f, 1.0f);
}else
if(x-1 == X)
if(z-1 == Z){
verts[iVertex] = D3DVertex::VertexPositionNormalTexture(x, 0.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, 0.5f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, 0.0f, z, n, 1.0f, g, 1.0f, 1.0f);
}
indices[iIndex ] = iVertex;
indices[iIndex+1] = iVertex+1;
indices[iIndex+2] = iVertex+2;
indices[iIndex+3] = iVertex+2;
indices[iIndex+4] = iVertex+1;
indices[iIndex+5] = iVertex+3;
iVertex += 4;
iIndex += 6;
}
}
}
device->CreateVertexBuffer(m_iVertexCount * sizeof(D3DVertex::VertexPositionNormalTexture),
NULL,
D3DVertex::VertexPositionNormalTexture::FVF,
D3DPOOL_MANAGED,
&vBuffer,
NULL);
VOID* pVerts;
vBuffer->Lock(0, sizeof(verts), (void**)&pVerts, 0);
memcpy_s(pVerts, sizeof(verts), verts, sizeof(verts));
vBuffer->Unlock();
device->CreateIndexBuffer(m_iIndeciseCount * sizeof(WORD), D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&iBuffer,
NULL);
VOID* pIndices;
iBuffer->Lock(0, sizeof(indices), (void**)&pIndices, 0);
memcpy_s(pIndices, sizeof(indices), indices, sizeof(indices));
iBuffer->Unlock();
//Load texture
D3DXCreateTextureFromFile(device, texName.c_str(), &texture);
if(!texture){
MessageBox(NULL, L"Could not load texture", NULL, NULL);
return;
}
}
我要问的是一个允许我保留代码的教程。如果你能帮助我实现一个普通的地图,并解释那将是多么伟大但不是必要的。我想学习directX而不仅仅是从别人那里复制代码。 DIRECTX版本= 2010年6月