Archived

This topic is now archived and is closed to further replies.

SimDemon

Terrain Based On Two-Kings ... Doesn't display correctly??

Recommended Posts

SimDemon    122
Hey, thanks for reading this, and I hope you can help. I am currently using MSVC++ 6.0, Windows 98, and the DirectX 8 SDK. I was looking at the tutorial for terrain on the Two-Kings website, for my screensaver application. I am not sure why, but it doesn''t render the terrain correctly. I will post the loading (initialization, etc.) code for the terrain, but it looks okay to me. First, here''s the problem. BTW, It''s a .RAW file. Okay, even when I just place a simple white spot in the center of the .RAW file, for a simple hill on the terrain, it seems to randomly place height for the rest of the terrain, too. It doesn''t just make the single hill. Here''s the code:
	unsigned char  map[65][65];       // Terrain map data array

	TERRAINVERTEX *pVertex;         // Pointer to vertex structure

	short         *pIndex;          // Pointer to the index

	ifstream       File;            // Terrain map file

	m_nDimensions[0] = nWidth;      // Terrain width

	m_nDimensions[1] = nHeight;     // Terrain height


	// Try to load the texture

	if (m_pTexture->LoadTexture("terrain_grass.bmp", NULL, pDevice) != TDX_ERR_NONE) 
		return FALSE;    // Failed to load texture; return an error


	// First try to load the file

	File.open(szFile, ios::binary);
	if (File == NULL)        // Error opening file

		return FALSE;        // Return an error


	// Read terrain data

	for (unsigned y = 0; y < (nHeight + 1); ++y)      // Loop map file height

	{
		for (unsigned x = 0; x < (nWidth + 1); ++x)   // Loop map file width

		{
			File.get(map[x][y]);                  // Read terrain data

		}
	}

	File.close();     // Close the file


	// Setup vertex buffer

	if (FAILED(pDevice->CreateVertexBuffer(sizeof(TERRAINVERTEX) * ((nWidth + 1) * (nHeight + 1)),
		                                   D3DUSAGE_WRITEONLY, D3DFVF_TERRAIN, D3DPOOL_MANAGED,
										   &m_pVB))) 
	{
		return FALSE;    // Error creating vertex buffer; return an error

	}
	if (FAILED(m_pVB->Lock(0, 0, (BYTE**) &pVertex, 0)))     // Try to lock vertex buffer

		return FALSE;    // Error locking vertex buffer; return an error


	// Setup vertex buffer data

	for (y = 0; y < (nHeight + 1); ++y)      // Loop map file width

	{
		for (unsigned x = 0; x < (nWidth + 1); ++x)  // Loop map file height

		{
			// Setup vertex data

			pVertex[x + y * (nWidth + 1)].x         = (float) x;
			pVertex[x + y * (nWidth + 1)].z         = (float) y;
			pVertex[x + y * (nWidth + 1)].y         = (float) (map[x][y] / 15.0f);
			pVertex[x + y * (nWidth + 1)].m_dwColor = 0xffffff;
			pVertex[x + y * (nWidth + 1)].u         = x;
			pVertex[x + y * (nWidth + 1)].v         = y;
		}
	}
	m_pVB->Unlock();    // Unlock the vertex buffer


	// Setup index buffer

	int nTempPrim = (nWidth * nHeight * 2);
	if (FAILED(pDevice->CreateIndexBuffer(sizeof(short) * nTempPrim * 3,
		                                  D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED,
										  &m_pIB))) 
	{
		return FALSE;    // Error creating index buffer; return an error

	}

	if (FAILED(m_pIB->Lock(0, 0, (BYTE**) &pIndex, 0)))      // Try to lock index buffer

		return FALSE;    // Error locking index buffer; return an error


	// Setup index buffer data

	for (y = 0; y < nHeight; ++y)      // Loop map file width

	{
		for (unsigned x = 0; x < nWidth; ++x)  // Loop map file height

		{
			// Setup v1, v2, and v4

			*pIndex++ = x + y * (nWidth + 1);
			*pIndex++ = x + 1 + y * (nWidth + 1);
			*pIndex++ = x + 1 + (y + 1) * (nWidth + 1);

			// Setup v1, v4, and v3

			*pIndex++ = x + y * (nWidth + 1);
			*pIndex++ = x + 1 + (y + 1) * (nWidth + 1);
			*pIndex++ = x + (y + 1) * (nWidth + 1);
		}
	}
	m_pIB->Unlock();    // Unlock the index buffer

If you can, please help. I would really appreciate it. =) Thanks, Matt U.

Share this post


Link to post
Share on other sites