Generated Mesh Texturing

Started by
0 comments, last by Tarazu 16 years, 1 month ago
Hey guys, new poster here :) Anyway, I'm working on creating a height map by creating a mesh with 100*100 squares. The basics are working but my texture doesn't show and I'm pretty sure it's because I don't set any texture coordinates (u,v). The main question is then: How do I do that? :) I was thinking extending the TESTVERTEX with texture coords, but assigning a texture coordinate to vertex i,j isn't obvious as the vertex will be creating 4 squares and thus be in all 4 texture corners. I could of course step back and use tringlestrip and draw each square separately or something, but that feels much more inefficient and there should be a better solution. Here's my code atm, the init and the draw function. I've moved declarations to global scope for readability.

LPDIRECT3DDEVICE9 d3ddev;
ID3DXMesh * mesh;
DWORD numSubsets;
LPDIRECT3DTEXTURE9 texture;
D3DMATERIAL9 material;
const int x = SIZEX; //100
const int y = SIZEY; //100

// Vertex declaration
D3DVERTEXELEMENT9 TESTVERTEX[] =
{
    { 0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,  D3DDECLUSAGE_POSITION, 0},
    D3DDECL_END()
};


typedef struct TestVertexData
{
	TestVertexData() {};
	TestVertexData(D3DXVECTOR3 pos) : position(pos) { }
	D3DXVECTOR3 position;
} TestVertexData;

void init()
{
	int nVertices = (1+x)*(1+y);
	int nFaces = 2*x*y;
	
	D3DXCreateMesh( nFaces, nVertices, D3DXMESH_32BIT | D3DXMESH_DYNAMIC, TESTVERTEX, d3ddev, &mesh );

	TestVertexData vertices[(SIZEX+1)*(SIZEY+1)];
	for(int i = 0; i <= y; i++)
		for(int j = 0; j <= x; j++)
			vertices[i*(x+1) + j] = D3DXVECTOR3(j, 0, i);

	int indices[6*SIZEX*SIZEY];
	for(int i = 0; i < y; i++)
		for(int j = 0; j < x; j++) {
			// triangle 1
			indices[(i*x + j)*6]     = i*(x+1) + j + 1;
			indices[(i*x + j)*6 + 1] = i*(x+1) + j;
			indices[(i*x + j)*6 + 2] = (i+1)*(x+1) + j;
			// triangle 2
			indices[(i*x + j)*6 + 3] = (i+1)*(x+1) + j;
			indices[(i*x + j)*6 + 4] = (i+1)*(x+1) + j + 1;
			indices[(i*x + j)*6 + 5] = i*(x+1) + j + 1;
		}

	LPVOID buffer;
	DWORD * adjacency;

	mesh->LockVertexBuffer( D3DLOCK_DISCARD, &buffer );
	memcpy( buffer, vertices, sizeof( vertices ) );
	mesh->UnlockVertexBuffer();

	mesh->LockIndexBuffer( D3DLOCK_DISCARD, &buffer );
	memcpy( buffer, indices, sizeof( indices ) );
	mesh->UnlockIndexBuffer();

	adjacency = new DWORD[ mesh->GetNumFaces() * 3 ];
	mesh->GenerateAdjacency( 0.01f, adjacency );
	mesh->OptimizeInplace( D3DXMESHOPT_VERTEXCACHE, adjacency, NULL, NULL, NULL );
	DWORD pAttribTableSize;
	mesh->GetAttributeTable( NULL, &pAttribTableSize );
	numSubsets = pAttribTableSize;

	delete[] adjacency;

	// texture
	D3DXCreateTextureFromFile(d3ddev, "Concrete1.png", &texture);    

	// material
	ZeroMemory( &material, sizeof(D3DMATERIAL9) );
	material.Diffuse.r = material.Ambient.r = 1.0f;
	material.Diffuse.g = material.Ambient.g = 1.0f;
	material.Diffuse.b = material.Ambient.b = 1.0f;
	material.Diffuse.a = material.Ambient.a = 1.0f;

}

void draw()
{
	d3ddev->SetTexture(0, texture);
	d3ddev->SetMaterial(&material);
	for(DWORD  i = 0; i < numSubsets; i++)
		mesh->DrawSubset(i);
}

And a second question, I've been trying without success to declare indices and vertices dynamically like adjacency but it keeps telling me to provide a const buffer length (hence I now use the SIZEX and SIZEY along with x and y which is abundant). Code would be something like: int* indices; indices = new int[2*x*y]; Is it poor design to even try this or is it possible? I tried to allocate a much larger buffer than needed for scalability but also failed since that generated errors in the mesh for some reason I can't say I understand.
Advertisement
Alright I solved the main problem. My solution is to extend the vertices array with one vertex + the four u,v combinations for each vertex and then pick the appropriate one for the indices. I'm not sure I like the solution though since the vertices array now got 4 times as big! And I have no idea what the effect on the optimization is, since each vertex entry is only used once now.

Atleast I have pretty textures xD

This topic is closed to new replies.

Advertisement