Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Creation of IB with patched meshes

This topic is 4389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to patch my terrain into chunks. I am using one VB/IB for the whole terrain and not sure if this is the correct way or not? I am confused as to how I am supposed to use the lock() for the IB when I use chunks. e.g. 513x513 with 33x33 patches = 16x16 patches or 256 patches total. So as of now I am using one VB and IB to hold all the data, but shouldn't there be someway to index the IB so call the indices I need per loop?
//create IB

bool Terrain::CreateIndexBufferData(void)
{
	unsigned long currentIndex = 0;
	unsigned long index = 0;
	unsigned long index1 = 0;
	unsigned long index2 = 0;
	unsigned long startx = 0;
	unsigned long startz = 0;
	unsigned long endx = 0;
	unsigned long endz = 0;
	unsigned long rows = ((mapData.map_Z - 1) / (PATCH_SIZE - 1));
	unsigned long columns = ((mapData.map_X - 1) / (PATCH_SIZE - 1));
	float maxX = 0.0f, maxY = 0.0f, maxZ = 0.0f;
	float minX = 0.0f, minY = 0.0f, minZ = 0.0f;

	CreatePatches();
	
	// Calculate the triangle index lists for each patch
	for(unsigned long z = 0; z < rows; ++z)
	{
		for(unsigned long x = 0; x < columns; ++x)
		{
			index = z * ((PATCH_SIZE - 1) * .5) + x;
			
			// Calculate the extents of the chunk.
			startx = x * (PATCH_SIZE - 1);
			startz = z * (PATCH_SIZE - 1);
			endx = startx + PATCH_SIZE;
			endz = startz + PATCH_SIZE;

			index1 = (startz * mapData.map_X + startx);
			maxX = float(startx);
			minX = float(startx);
			maxY = vertices[index1].pos.y;
			minY = vertices[index1].pos.y;
			maxZ = float(startz);
			minZ = float(startz);

			// Loop through the chunk extents and create the list.
			indexBuffer->Lock(0, 0, (void**)&patch[index].index_buffer, 0);
			for(unsigned long zz = startz; zz < endz - 1; ++zz)
			{
				for(unsigned long xx = startx; xx < endx; ++xx)
				{
					index1 = (zz * mapData.map_X + xx);
					// Update the min and max values.
					maxX = maxX > xx ? maxX : xx;
					minX = minX < xx ? minX : xx;
					maxY = maxY > vertices[index1].pos.y ? maxY : vertices[index1].pos.y;
					minY = minY < vertices[index1].pos.y ? minY : vertices[index1].pos.y;
					maxZ = maxZ > zz ? maxZ : zz;
					minZ = minZ < zz ? minZ : zz;
					maxZ = maxZ > (zz + 1) ? maxZ : (zz + 1);
					minZ = minZ < (zz + 1) ? minZ : (zz + 1);

					patch[index].index_buffer[index2]		= zz * mapData.map_X + xx + 1;
                    patch[index].index_buffer[index2 + 1]	= zz * mapData.map_X + xx;			
					patch[index].index_buffer[index2 + 2]	= (zz + 1) * mapData.map_X + xx + 1;

					patch[index].index_buffer[index2 + 3]	= (zz + 1) * mapData.map_X + xx + 1;
					patch[index].index_buffer[index2 + 4]	= zz * mapData.map_X + xx;			
					patch[index].index_buffer[index2 + 5]	= (zz + 1) * mapData.map_X + xx;
                    index2+=6;
				}
			}
			indexBuffer->Unlock();
						
			// Assign the min and max values found
			//scale the max min values to terrain scale
			maxX *= mapData.terrainScaleFactor;
			maxZ *= mapData.terrainScaleFactor;
			minX *= mapData.terrainScaleFactor;
			minZ *= mapData.terrainScaleFactor;
			
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxX = maxX;
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxY = maxY;
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxZ = maxZ;
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minX = minX;
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minY = minY;
			patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minZ = minZ;
		}
	}
	return true;
}

//render 
// Begin passes.
	UINT numPasses = 0;
	terrainFX.shader->Begin(&numPasses, 0);
	for(UINT i = 0; i < numPasses; ++i)
	{
		terrainFX.shader->BeginPass(i);
		
		for(unsigned long z = 0; z < (mapData.map_Z - 1) / (PATCH_SIZE - 1); ++z)
		{
			for(unsigned long x = 0; x < (mapData.map_X - 1) / (PATCH_SIZE - 1); ++x)
			{
				maxX = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxX;
				maxY = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxY;
				maxZ = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].maxZ;
				minX = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minX;
				minY = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minY;
				minZ = patch[z * ((mapData.map_X - 1) / (PATCH_SIZE - 1)) + x].minZ;

				// Check to see if the corners of the patches are within the frustum.
				if (!gCamera.pointInFrustum(maxX, maxY, maxZ) &&
					!gCamera.pointInFrustum(minX, maxY, minZ) &&
					!gCamera.pointInFrustum(minX, maxY, maxZ) &&
					!gCamera.pointInFrustum(maxX, maxY, minZ) &&
					!gCamera.pointInFrustum(maxX, minY, maxZ) &&
					!gCamera.pointInFrustum(minX, minY, minZ) &&
					!gCamera.pointInFrustum(minX, minY, maxZ) &&
					!gCamera.pointInFrustum(maxX, minY, minZ))
					continue;

				dxApp.d3dDevice->DrawIndexedPrimitive(type, 0, 0, INDEX_SUM_OFFSET, indexSum, NUM_POLYGONS);
				indexSum += INDEX_SUM_OFFSET;
			}
		}

	        terrainFX.shader->EndPass();
	}
	terrainFX.shader->End();
}

Any help would be great. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!