public void Resolve(BitmapChunk bitmapChunk) { if (_vertexBuffer != null) { return; //already resolved } int vertCount = 0; List <VertexPositionTexture> allVerts = new List <VertexPositionTexture>(); foreach (Polygon poly in _polys) { if (poly.TextureName != null) { poly.ResolveTexture(bitmapChunk.FindByName(poly.TextureName)); } poly.VertexBufferIndex = vertCount; vertCount += poly.VertexCount; allVerts.AddRange(poly.GetVertices()); } _vertexBuffer = new VertexBuffer(Engine.Instance.Device, typeof(VertexPositionTexture), vertCount, BufferUsage.WriteOnly); _vertexBuffer.SetData <VertexPositionTexture>(allVerts.ToArray()); }
public Mesh(MeshChunk meshChunk, BitmapChunk bmpChunk) { _polys = meshChunk.Polygons; Identifier = meshChunk.Identifier; Resolve(bmpChunk); BoundingBox = GetBoundingBox(); }
/// <summary> /// Gets a reverse enumerator over the compacted integer list. /// </summary> /// <returns>An <see cref="T:IEnumerator{int}"/> that can be used to iterate over the list.</returns> public IEnumerator <int> GetReverseEnumerator() { if (null == _bitmapChunks) { yield break; } // Loop over each chunk. for (int i = _bitmapChunks.Length - 1; i > -1; i--) { // loop over bits in chunk and yield integer values represented by set bits. BitmapChunk chunk = _bitmapChunks[i]; // For speed we first test for non-zero uints within the chunk's bitmap array. for (int j = chunk._bitmap.Length - 1; j > -1; j--) { // Skip uints with no bits set. if (0u == chunk._bitmap[j]) { continue; } // Loop over bits in the current uint and yield the set bits. uint block = chunk._bitmap[j]; for (int k = 0, l = chunk._baseValue + (j << 5) + 31; k < 32; k++, l--) { if ((block & 0x80000000) != 0) { yield return(l); } block <<= 1; } } } }
/// <summary> /// Builds the bitmap chunks given the integer list to compact and the bitmap chunk size /// (number of bits in each chunk). /// </summary> /// <param name="intList">A collection of integers to populate with.</param> /// <param name="chunkSize">The chunk size.</param> /// <returns>A <see cref="T:BitmapChunk[]"/> containing the <paramref name="intList"/>.</returns> private BitmapChunk[] BuildChunks(List <int> intList, int chunkSize) { List <BitmapChunk> chunkList = new List <BitmapChunk>(20); // Index into indexList. int idx = 0; int count = intList.Count; // Build chunks until we exhaust intList. while (idx < count) { int val = intList[idx]; BitmapChunk chunk = new BitmapChunk(val, chunkSize); chunkList.Add(chunk); int chunkBound = val + chunkSize; // Iterate through all values that are represented by the current chunk. do { chunk.SetBitForValue(val); }while(++idx < count && (val = intList[idx]) < chunkBound); } // Return the chunks as an array. return(chunkList.ToArray()); }
static Color BrakeOnColor = new Color(158, 110, 6); //color of brake-off light color... public CarMesh(MeshChunk meshChunk, BitmapChunk bmpChunk, Color brakeColor) : base(meshChunk, bmpChunk) { foreach (var poly in _polys) { switch (poly.Label) { case "rt_rear": _rightRearWheel = poly; break; case "lt_rear": _leftRearWheel = poly; break; case "rt_frnt": _rightFrontWheel = poly; break; case "lt_frnt": _leftFrontWheel = poly; break; case "bkll": _leftBrakeLight = poly; break; case "bklr": _rightBrakeLight = poly; break; } } var tyreEntry = bmpChunk.FindByName("tyr1"); if (tyreEntry != null) { _wheelTexture = tyreEntry.Texture; } // This seems like it should be done in a shader but I couldn't get it to work well enough // (dealing with original paletted colors doesn't work so well in a texture stretched over a polygon) var rsidPoly = _polys.FirstOrDefault(a => a.TextureName == "rsid"); if (rsidPoly != null) { // Generate a new texture for brake lights on. Color[] pixels = new Color[rsidPoly.Texture.Width * rsidPoly.Texture.Height]; rsidPoly.Texture.GetData <Color>(pixels); for (int i = 0; i < pixels.Length; i++) { if (pixels[i] == brakeColor) { pixels[i] = BrakeOnColor; } } _brakeOnTexture = new Texture2D(Engine.Instance.Device, rsidPoly.Texture.Width, rsidPoly.Texture.Height); _brakeOnTexture.SetData <Color>(pixels); // Generate a new texture for brake lights off. for (int i = 0; i < pixels.Length; i++) { if (pixels[i] == BrakeOnColor) { pixels[i] = BrakeOffColor; } } _brakeOffTexture = new Texture2D(Engine.Instance.Device, _leftBrakeLight.Texture.Width, _leftBrakeLight.Texture.Height); _brakeOffTexture.SetData <Color>(pixels); } }