Skip to main content
gamedev.net
🔒 Locked

Texture issues on Index Buffer

Started by choffstein 2 replies 550+ views
Original Post
choffstein
choffstein
Hey. I am working on my terrain engine, and am getting some pretty wierd texture problems. The textures are loading correctly. Here is my rendering code:

pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
	   D3DXMatrixScaling(&matWorld, TERRAIN_X*15, TERRAIN_Y*15, TERRAIN_Y*15); //make the box scaled by the correct size, to fit the terrain
	   pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);

	   pd3dDevice->SetTexture(0, SkyTexture);
	   //set the source as the skybox vertex buffer for the index buffer
	   pd3dDevice->SetStreamSource(0, SkyBoxVB, 0, sizeof(CUSTOMVERTEX));
	   //set our index buffer to show what triangles to use.
	   pd3dDevice->SetIndices(SkyBoxIB);
	   //we draw triangles lists as blocks of 2 triangles at a time
	   pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12); //we have 8 vertices, and 12 primitives

	   D3DXMatrixScaling(&matWorld, 1.0f, 1.0f, 1.0f); //we dont need to scale anymore, so set the world
	                                                   //back to normal.
	   pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);

	   //apply the ground texture to the Index Buffer
	   pd3dDevice->SetTexture(0, GroundTexture);
	   pd3dDevice->SetStreamSource(0,TerrainVB,0,sizeof(CUSTOMVERTEX));
	   pd3dDevice->SetIndices(TerrainIB);
	   //draw the index buffer as triangle lists. (blocks of 2)
	   pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,TerrainVertices,0,TerrainPrimitives);
Here is my custom vertex:

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
	FLOAT fX, fY, fZ;  // The untransformed, 3D position for the vertex
	DWORD dwColor;
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1)
Here is what the ground texture looks like: Here is what it looks like when running Here is what the sky texture looks like: Here is what it looks like when running Any ideas why this looks so bad? Maybe it looks correct and I have some sort of misunderstanding with how the rendering works. I was skimming through my directx9 book and noticed that the custom vertex they use has a float u and v value, used in conjunction with the D3DFVF_CUSTOMVERTEX I have, which are used to repeat a texture across a polygon. Is this what I have done wrong? Sorry, im a huge directx noob. If someone could point me in the right direction as to why this is looking so crappy, I would really appreciate it. Thanks.
Supernat02
Supernat02
Your vertex structure doesn't match your FVF declaration. You declared your FVF as XYZ | TEX1, but your structure would require XYZ | DIFFUSE, because Diffuse is the dwColor property. Change the DWORD dwColor to float tu, tv and you'll be set. You just need to set tu and tv, your texture coordinates.

Good luck,
Chris
Chris ByersMicrosoft DirectX MVP - 2005
choffstein
choffstein
I figured that was the issue. Many thanks =D
choffstein
choffstein
Alright, im having a little trouble mapping these bad boys. Here is the mapping code I am using for my skybox:
       CUSTOMVERTEX skyBoxVerts[] =       {		   {-0.5f, 0.0f, -0.5f, 0.0f, 0.0f}, //0		   {-0.5f, 1.5f, -0.5f, 1.0f, 0.0f}, // 1		   {0.5f, 1.5f, -0.5f, 0.0f, 1.0f}, // 2		   {0.5f, 0.0f, -0.5f, 1.0f, 1.0f}, // 3		   {-0.5f, 0.0f, 0.5f, 0.0f, 0.0f}, // 4		   {0.5f, 0.0f, 0.5f, 1.0f, 0.0f}, // 5		   {0.5f, 1.5f, 0.5f, 0.0f, 1.0f}, // 6		   {-0.5f, 1.5f, 0.5f, 1.0f, 1.0f}, // 7       };       WORD skyBoxIndexData[] =        {			0,1,2, // 1			2,3,0, // 2			4,5,6, // 3			6,7,4, // 4			0,3,5, // 5			5,4,0, // 6			3,2,6, // 7			6,5,3, // 8			2,1,7, // 9			7,6,2, // 10			1,0,4, // 11			4,7,1  // 12       };


And here it is for my terrain verts
	for(unsigned y = 0; y < (TERRAIN_Y + 1); ++y)	{		for(unsigned x = 0; x < (TERRAIN_X + 1); ++x)		{			pVertexData[x + y * (TERRAIN_X + 1)].fX = ((float) x - TERRAIN_X/2)*15;			//the Y should be a float height map -- [x][z] with a [y] height.			pVertexData[x + y * (TERRAIN_X + 1)].fY = (float) (rand() % 100) / 15.0f;			pVertexData[x + y * (TERRAIN_X + 1)].fZ = ((float) y - TERRAIN_Y/2)*15;			if(y % 2 == 0 && x % 2 == 0)			{					pVertexData[x + y * (TERRAIN_X + 1)].fU = 0.0f;					pVertexData[x + y * (TERRAIN_X + 1)].fV = 0.0f;			}			else if(y % 2 == 1 && x % 2 == 0)			{					pVertexData[x + y * (TERRAIN_X + 1)].fU = 1.0f;					pVertexData[x + y * (TERRAIN_X + 1)].fV = 0.0f;			}			else if(y % 2 == 0 && x % 2 == 1)			{					pVertexData[x + y * (TERRAIN_X + 1)].fU = 0.0f;					pVertexData[x + y * (TERRAIN_X + 1)].fV = 1.0f;			}			else if(y % 2 == 1 && y % 2 == 1)			{					pVertexData[x + y * (TERRAIN_X + 1)].fU = 1.0f;					pVertexData[x + y * (TERRAIN_X + 1)].fV = 1.0f;			}		}	}


I render using index buffers, which all work correctly when I draw in WireFrame, so I dont think those are the problem...

Any idea what the issue could be here? Terrain_X and Terrain_Y are each set at 256.

Thoughts? I thought I got this one nailed, but only parts are texturing correctly, and others not so well, so I figure my algorithms must be a little wrong.

The issue is definitely that when reusing a vertex, the U and V get reused, and shouldn't be reused as the same values for the new side. Thoughts?

Thank ya much.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.