/// <summary> /// Converts design-time vertex position and channel data into a vertex buffer format that a graphics device can recognize. /// </summary> /// <returns>A packed vertex buffer.</returns> /// <exception cref="InvalidContentException"> /// One or more of the vertex channel types are invalid or an unrecognized name was passed to <see cref="VertexElementUsage"/>. /// </exception> public VertexBufferContent CreateVertexBuffer() { var vertexBuffer = new VertexBufferContent(positions.Count); var stride = SetupVertexDeclaration(vertexBuffer); // TODO: Verify enough elements in channels to match positions? // Write out data in an interleaved fashion each channel at a time, for example: // |------------------------------------------------------------| // |POSITION[0] | NORMAL[0] |TEX0[0] | POSITION[1]| NORMAL[1] | // |-----------------------------------------------|------------| // #0 |111111111111|____________|________|111111111111|____________| // #1 |111111111111|111111111111|________|111111111111|111111111111| // #2 |111111111111|111111111111|11111111|111111111111|111111111111| // #0: Write position vertices using stride to skip over the other channels: vertexBuffer.Write(0, stride, positions); var channelOffset = VertexBufferContent.SizeOf(typeof(Vector3)); foreach (var channel in Channels) { // #N: Fill in the channel within each vertex var channelType = channel.ElementType; vertexBuffer.Write(channelOffset, stride, channelType, channel); channelOffset += VertexBufferContent.SizeOf(channelType); } return(vertexBuffer); }
public PatchContent Build() { #region Local vertex buffer // create local vertex buffer for patch int numVertices = _patchSize * _patchSize; VertexBufferContent localVertexBuffer = new VertexBufferContent(numVertices); // fill vertex buffer VertexPositionNormalTexture2[] vertices = new VertexPositionNormalTexture2[numVertices]; int nStartX = _patchOffsetX * (_patchSize - 1); int nStartY = _patchOffsetY * (_patchSize - 1); int nEndX = nStartX + _patchSize; int nEndY = nStartY + _patchSize; float fMinZ = float.MaxValue, fMaxZ = float.MinValue; int index = 0; for (int y = nStartY; y < nEndY; y++) { for (int x = nStartX; x < nEndX; x++) { // write local data float fZ = _heightMap[x, y]; if (fZ < fMinZ) { fMinZ = fZ; } if (fZ > fMaxZ) { fMaxZ = fZ; } Vector2 texCoords1 = new Vector2(x / (float)(_heightMap.Width - 1), y / (float)(_heightMap.Height - 1)); Vector2 texCoords2 = texCoords1 * _detailTextureTiling; vertices[index++] = new VertexPositionNormalTexture2( new Vector3(x * _horizontalScale, fZ, y * _horizontalScale), new Vector3(0, 1, 0), texCoords1, texCoords2); } } localVertexBuffer.Write(0, VertexBufferContent.SizeOf(typeof(VertexPositionNormalTexture2)), vertices); localVertexBuffer.VertexDeclaration = new VertexDeclarationContent(); foreach (VertexElement vertexElement in VertexPositionNormalTexture2.VertexDeclaration.GetVertexElements()) { localVertexBuffer.VertexDeclaration.VertexElements.Add(vertexElement); } #endregion LevelContent[] levels = new LevelContent[_numLevels]; for (int i = 0; i < _numLevels; i++) { LevelContentBuilder levelContentBuilder = new LevelContentBuilder(_heightMap, _patchSize, _numLevels, i, nStartX, nEndX, nStartY, nEndY); levels[i] = levelContentBuilder.Build(); } #region Bounding box, centre, and offset BoundingBox boundingBox = new BoundingBox( new Vector3(nStartX * _horizontalScale, fMinZ, nStartY * _horizontalScale), new Vector3(nEndX * _horizontalScale, fMaxZ, nEndY * _horizontalScale)); float fAverageZ = (fMinZ + fMaxZ) / 2.0f; Vector3 center = new Vector3( (nStartX + ((_patchSize - 1) / 2.0f)) * _horizontalScale, fAverageZ, (nStartY + ((_patchSize - 1) / 2.0f)) * _horizontalScale); Vector2 offset = new Vector2( (_patchOffsetX * (_patchSize - 1)) * _horizontalScale, (_patchOffsetY * (_patchSize - 1)) * _horizontalScale); #endregion return(new PatchContent { VertexBuffer = localVertexBuffer, Levels = levels, BoundingBox = boundingBox, Center = center, Offset = offset }); }