protected void SetupBuffers() { SetupVertexDeclaration(); if (buffersNeedRecreating) { // Create the vertex buffer (always dynamic due to the camera adjust) HardwareVertexBuffer pBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( vertexData.vertexDeclaration.GetVertexSize(0), vertexData.vertexCount, BufferUsage.DynamicWriteOnlyDiscardable); // (re)Bind the buffer // Any existing buffer will lose its reference count and be destroyed vertexData.vertexBufferBinding.SetBinding(0, pBuffer); indexData.indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer( IndexType.Size16, numberOfChains * maxElementsPerChain * 6, // max we can use dynamic? BufferUsage.DynamicWriteOnly : BufferUsage.StaticWriteOnly); // NB we don't set the indexCount on IndexData here since we will // probably use less than the maximum number of indices buffersNeedRecreating = false; } }
void UpdateMeshVertices() { SubMesh subMesh = mesh.SubMeshes[0]; Vec2 cellSize = 1.0f / Type.GridSize.ToVec2(); HardwareVertexBuffer vertexBuffer = subMesh.VertexData.VertexBufferBinding.GetBuffer(0); unsafe { Vertex *buffer = (Vertex *)vertexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); subMesh.VertexData.VertexCount = (Type.GridSize.X + 1) * (Type.GridSize.Y + 1); for (int y = 0; y < Type.GridSize.Y + 1; y++) { for (int x = 0; x < Type.GridSize.X + 1; x++) { Vertex vertex = new Vertex(); vertex.position = new Vec3(0, (float)(x - Type.GridSize.X / 2) * cellSize.X, (float)(y - Type.GridSize.Y / 2) * cellSize.Y); vertex.normal = new Vec3(1, 0, 0); vertex.texCoord = new Vec2((float)x / (float)Type.GridSize.X, 1.0f - (float)y / (float)Type.GridSize.Y); *buffer = vertex; buffer++; } } vertexBuffer.Unlock(); } }
public void CalculateNormals() { #if !AXIOM_SAFE_ONLY unsafe #endif { Vector3 normal; HardwareVertexBuffer buffer = this.terrain.vertexBufferBinding.GetBuffer(NORMAL); var norm = buffer.Lock(BufferLocking.Discard); var normPtr = norm.ToFloatPointer(); int count = 0; for (int j = 0; j < this.size; j++) { for (int i = 0; i < this.size; i++) { GetNormalAt(GetVertex(i, j, 0), GetVertex(i, j, 2), out normal); normPtr[count++] = normal.x; normPtr[count++] = normal.y; normPtr[count++] = normal.z; } } buffer.Unlock(); } }
public RenderOperation CreateBillboardBuffer() { RenderOperation renderOp = new RenderOperation(); renderOp.operationType = OperationType.TriangleFan; renderOp.useIndices = false; VertexData vertexData = new VertexData(); vertexData.vertexCount = 4; vertexData.vertexStart = 0; // free the original vertex declaration to avoid a leak HardwareBufferManager.Instance.DestroyVertexDeclaration(vertexData.vertexDeclaration); // use common vertex declaration vertexData.vertexDeclaration = TreeGroup.BillboardVertexDeclaration; // create the hardware vertex buffer and set up the buffer binding HardwareVertexBuffer hvBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( vertexData.vertexDeclaration.GetVertexSize(0), vertexData.vertexCount, BufferUsage.DynamicWriteOnly, false); vertexData.vertexBufferBinding.SetBinding(0, hvBuffer); renderOp.vertexData = vertexData; return(renderOp); }
private void CreatePrefabPlane() { Mesh mesh = (Mesh)Create("Prefab_Plane"); SubMesh subMesh = mesh.CreateSubMesh("Prefab_Plane_Submesh"); float[] vertices = { -100, -100, 0, // pos 0, 0, 1, // normal 0, 1, // texcoord 100, -100, 0, 0, 0, 1, 1, 1, 100, 100, 0, 0, 0, 1, 1, 0, -100, 100, 0, 0, 0, 1, 0, 0 }; mesh.SharedVertexData = new VertexData(); mesh.SharedVertexData.vertexCount = 4; VertexDeclaration vertexDeclaration = mesh.SharedVertexData.vertexDeclaration; VertexBufferBinding binding = mesh.SharedVertexData.vertexBufferBinding; int offset = 0; vertexDeclaration.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.Position); offset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexDeclaration.AddElement(0, offset, VertexElementType.Float3, VertexElementSemantic.Normal); offset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexDeclaration.AddElement(0, offset, VertexElementType.Float2, VertexElementSemantic.TexCoords, 0); offset += VertexElement.GetTypeSize(VertexElementType.Float2); // allocate vertex buffer HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer(offset, 4, BufferUsage.StaticWriteOnly); // set up the binding, one source only binding.SetBinding(0, vertexBuffer); vertexBuffer.WriteData(0, vertexBuffer.Size, vertices, true); subMesh.useSharedVertices = true; HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, 6, BufferUsage.StaticWriteOnly); short[] faces = { 0, 1, 2, 0, 2, 3 }; subMesh.indexData.indexBuffer = indexBuffer; subMesh.indexData.indexCount = 6; subMesh.indexData.indexStart = 0; indexBuffer.WriteData(0, indexBuffer.Size, faces, true); mesh.BoundingBox = new AxisAlignedBox(new Vector3(-100, -100, 0), new Vector3(100, 100, 0)); mesh.BoundingSphereRadius = MathUtil.Sqrt(100 * 100 + 100 * 100); resourceList.Add(mesh.Name, mesh); }
VertexData CreateVertexData(Vec3[] vertices) { VertexData vertexData = new VertexData(); VertexDeclaration declaration = vertexData.VertexDeclaration; declaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); VertexBufferBinding bufferBinding = vertexData.VertexBufferBinding; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( 12, vertices.Length, HardwareBuffer.Usage.StaticWriteOnly); bufferBinding.SetBinding(0, vertexBuffer, true); vertexData.VertexCount = vertices.Length; unsafe { Vec3 *buffer = (Vec3 *)vertexBuffer.Lock(HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (Vec3 position in vertices) { *buffer = position; buffer++; } vertexBuffer.Unlock(); } return(vertexData); }
protected unsafe AxisAlignedBox CalculateBounds(VertexData vertexData, Vector3 position, Quaternion orientation, Vector3 scale) { VertexElement posElem = vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.Position); HardwareVertexBuffer vbuf = vertexData.vertexBufferBinding.GetBuffer(posElem.Source); IntPtr src = vbuf.Lock(BufferLocking.ReadOnly); byte * vertex = (byte *)src.ToPointer(); Vector3 min = Vector3.Zero; Vector3 max = Vector3.Zero; bool first = true; for (int j = 0; j < vertexData.vertexCount; ++j, vertex += vbuf.VertexSize) { float * pFloat = (float *)(vertex + posElem.Offset); Vector3 pt = new Vector3(*pFloat++, *pFloat++, *pFloat++); // Transform to world (scale, rotate, translate) pt = (orientation * (pt * scale)) + position; if (first) { min = max = pt; first = false; } else { min.Floor(pt); max.Ceil(pt); } } vbuf.Unlock(); return(new AxisAlignedBox(min, max)); }
/// <summary>Get a hardware vertex buffer version of the vertex offsets.</summary> public HardwareVertexBuffer GetHardwareVertexBuffer(int numVertices) { if (vertexBuffer == null) { // Create buffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( VertexElement.GetTypeSize(VertexElementType.Float3), numVertices, BufferUsage.StaticWriteOnly, false); // lock the vertex buffer IntPtr ipBuf = vertexBuffer.Lock(BufferLocking.Discard); unsafe { float *buffer = (float *)ipBuf.ToPointer(); for (int i = 0; i < numVertices * 3; i++) { buffer[i] = 0f; } // Set each vertex foreach (KeyValuePair <int, Vector3> pair in vertexOffsetMap) { int offset = 3 * pair.Key; Vector3 v = pair.Value; buffer[offset++] = v.x; buffer[offset++] = v.y; buffer[offset] = v.z; } vertexBuffer.Unlock(); } } return(vertexBuffer); }
void UpdateBuffers() { unsafe { VertexData vertexData = staticMeshObject.VertexData; HardwareVertexBuffer vertexBuffer = vertexData.VertexBufferBinding.GetBuffer(0); Vertex *buffer = (Vertex *)vertexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (Vertex vertex in vertices) { *buffer = vertex; buffer++; } vertexBuffer.Unlock(); } unsafe { HardwareIndexBuffer indexBuffer = staticMeshObject.IndexData.IndexBuffer; ushort * buffer = (ushort *)indexBuffer.Lock( HardwareBuffer.LockOptions.Normal).ToPointer(); foreach (int index in indices) { *buffer = (ushort)index; buffer++; } indexBuffer.Unlock(); } }
public void InitAABB(AxisAlignedBox box) { SetupAABBVertices(box); // get a reference to the color buffer HardwareVertexBuffer buffer = vertexData.vertexBufferBinding.GetBuffer(COLOR); // lock the buffer IntPtr colPtr = buffer.Lock(BufferLocking.Discard); // load the color buffer with the specified color for each element unsafe { uint *pCol = (uint *)colPtr.ToPointer(); for (int i = 0; i < vertexData.vertexCount; i++) { pCol[i] = Root.Instance.ConvertColor(ColorEx.Red); } } // unlock the buffer buffer.Unlock(); // store the bounding box locally this.box = box; }
/// <summary> /// Sets the texture coordinates for the left edge of the border. /// </summary> /// <remarks> /// The border panel uses 8 panels for the border (9 including the center). /// Imagine a table with 3 rows and 3 columns. The corners are always the same size, /// but the edges stretch depending on how big the panel is. Those who have done /// resizable HTML tables will be familiar with this approach. /// <p/> /// We only require 2 sets of uv coordinates, one for the top-left and one for the /// bottom-right of the panel, since it is assumed the sections are aligned on the texture. /// </remarks> /// <param name="cell">Index of the cell to update.</param> /// <param name="u1">Top left u.</param> /// <param name="v1">Top left v.</param> /// <param name="u2">Bottom right u.</param> /// <param name="v2">Bottom right v.</param> public void SetCellUV(BorderCell cell, float u1, float v1, float u2, float v2) { int cellIndex = (int)cell; // no choice but to lock/unlock each time here, locking only what we want to modify HardwareVertexBuffer buffer = renderOp2.vertexData.vertexBufferBinding.GetBuffer(TEXCOORDS); // can't use discard, or it will discard the whole buffer, wiping out the positions too IntPtr data = buffer.Lock( cellIndex * 8 * Marshal.SizeOf(typeof(float)), Marshal.SizeOf(typeof(float)) * 8, BufferLocking.Normal); int index = 0; unsafe { float *texPtr = (float *)data.ToPointer(); texPtr[index++] = u1; texPtr[index++] = v1; texPtr[index++] = u1; texPtr[index++] = v2; texPtr[index++] = u2; texPtr[index++] = v1; texPtr[index++] = u2; texPtr[index++] = v2; } buffer.Unlock(); }
/// <summary> /// Fill a vertex buffer with the contents of a two dimensional /// float array /// </summary> /// <param name="vBuffer">HardwareVertexBuffer to populate</param> /// <param name="vertexCount">the number of vertices</param> /// <param name="vertexSize">the size of each vertex</param> /// <param name="vertexOffset">the offset (in bytes) of this element in the vertex buffer</param> /// <param name="vertexStride">the stride (in bytes) between vertices</param> /// <param name="data">the array of data to put in the buffer</param> internal static void FillBuffer(HardwareVertexBuffer vBuffer, int vertexCount, int vertexSize, int vertexOffset, int vertexStride, uint[ , ] data) { int count = data.GetLength(1); IntPtr bufData = vBuffer.Lock(BufferLocking.Discard); int uintStride = vertexStride / sizeof(uint); int uintOffset = vertexOffset / sizeof(uint); unsafe { uint *pUints = (uint *)bufData.ToPointer(); for (int i = 0; i < vertexCount; ++i) { for (int j = 0; j < count; ++j) { Debug.Assert(sizeof(uint) * (i * uintStride + uintOffset + j) < vertexCount * vertexSize, "Wrote off end of vertex buffer"); pUints[i * uintStride + uintOffset + j] = data[i, j]; } } } // unlock the buffer vBuffer.Unlock(); }
/// <summary> /// Hacked old version to add a transform /// </summary> /// <param name="vBuffer"></param> /// <param name="vertexOffset"></param> /// <param name="vertexStride"></param> /// <param name="vertexCount"></param> /// <param name="vertexSize"></param> /// <param name="data"></param> /// <param name="transform"></param> private void ReadBuffer(HardwareVertexBuffer vBuffer, int vertexOffset, int vertexStride, int vertexCount, int vertexSize, float[,] data, Matrix4 transform) { int count = data.GetLength(1); IntPtr bufData = vBuffer.Lock(BufferLocking.ReadOnly); Debug.Assert(count == 3); int floatOffset = vertexOffset / sizeof(float); int floatStride = vertexStride / sizeof(float); Debug.Assert(vertexOffset % sizeof(float) == 0); unsafe { float *pFloats = (float *)bufData.ToPointer(); for (int i = 0; i < vertexCount; ++i) { Vector3 tmpVec = Vector3.Zero; for (int j = 0; j < count; ++j) { int k = i * floatStride + floatOffset + j; Debug.Assert(sizeof(float) * k < vertexCount * vertexStride, "Read off end of vertex buffer"); tmpVec[j] = pFloats[k]; } tmpVec = transform * tmpVec; for (int j = 0; j < count; ++j) { data[i, j] = tmpVec[j]; } } } // unlock the buffer vBuffer.Unlock(); }
private VertexData InitVertexBuffer(int numVerts) { vertexData = new VertexData(); vertexData.vertexCount = numVerts; vertexData.vertexStart = 0; // set up the vertex declaration int vDecOffset = 0; vertexData.vertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float3, VertexElementSemantic.Position); vDecOffset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexData.vertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float3, VertexElementSemantic.Normal); vDecOffset += VertexElement.GetTypeSize(VertexElementType.Float3); vertexData.vertexDeclaration.AddElement(0, vDecOffset, VertexElementType.Float2, VertexElementSemantic.TexCoords); vDecOffset += VertexElement.GetTypeSize(VertexElementType.Float2); // create the hardware vertex buffer and set up the buffer binding HardwareVertexBuffer hvBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( vertexData.vertexDeclaration.GetVertexSize(0), vertexData.vertexCount, BufferUsage.StaticWriteOnly, false); vertexData.vertexBufferBinding.SetBinding(0, hvBuffer); return vertexData; }
/// <summary> /// Utility method to pull a bunch of floats out of a vertex buffer. /// </summary> /// <param name="data"></param> /// <param name="vBuffer"></param> /// <param name="elem"></param> public static void ReadBuffer(float[ , ] data, HardwareVertexBuffer vBuffer, VertexElement elem) { int count = data.GetLength(1); IntPtr bufData = vBuffer.Lock(BufferLocking.ReadOnly); Debug.Assert(vBuffer.VertexSize % sizeof(float) == 0); Debug.Assert(elem.Offset % sizeof(float) == 0); int vertexCount = vBuffer.VertexCount; int vertexSpan = vBuffer.VertexSize / sizeof(float); int offset = elem.Offset / sizeof(float); unsafe { float *pFloats = (float *)bufData.ToPointer(); for (int i = 0; i < vertexCount; ++i) { for (int j = 0; j < count; ++j) { Debug.Assert(((offset + i * vertexSpan + j) * sizeof(float)) < (vertexCount * vBuffer.VertexSize), "Read off end of vertex buffer"); data[i, j] = pFloats[offset + i * vertexSpan + j]; } } } // unlock the buffer vBuffer.Unlock(); }
private void CreateBuffers(ushort[] indices, short[] vertices) { var numIndices = indices.Length; var numVertices = vertices.Length; _vertexDeclaration = HardwareBufferManager.Instance.CreateVertexDeclaration(); _vertexDeclaration.AddElement(0, 0, VertexElementType.Short2, VertexElementSemantic.Position); _ib = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size16, numIndices, BufferUsage.WriteOnly); _vb = HardwareBufferManager.Instance.CreateVertexBuffer(_vertexDeclaration, numVertices, BufferUsage.WriteOnly, false); _ib.WriteData(0, numIndices * sizeof(ushort), indices, true); _vb.WriteData(0, numVertices * sizeof(ushort), vertices, true); var binding = new VertexBufferBinding(); binding.SetBinding(0, _vb); VertexData = new VertexData(); VertexData.vertexDeclaration = _vertexDeclaration; VertexData.vertexBufferBinding = binding; VertexData.vertexCount = numVertices; VertexData.vertexStart = 0; IndexData = new IndexData(); IndexData.indexBuffer = _ib; IndexData.indexCount = numIndices; IndexData.indexStart = 0; }
/// <summary> /// /// </summary> public override void Initialize() { // setup the vertex data renderOp.vertexData = new VertexData(); // Vertex declaration: 1 position, add texcoords later depending on #layers // Create as separate buffers so we can lock & discard separately VertexDeclaration decl = renderOp.vertexData.vertexDeclaration; decl.AddElement(POSITION, 0, VertexElementType.Float3, VertexElementSemantic.Position); renderOp.vertexData.vertexStart = 0; renderOp.vertexData.vertexCount = 4; // create the first vertex buffer, mostly static except during resizing HardwareVertexBuffer buffer = HardwareBufferManager.Instance.CreateVertexBuffer( decl.GetVertexSize(POSITION), renderOp.vertexData.vertexCount, BufferUsage.StaticWriteOnly); // bind the vertex buffer renderOp.vertexData.vertexBufferBinding.SetBinding(POSITION, buffer); // no indices, and issue as a tri strip renderOp.useIndices = false; renderOp.operationType = OperationType.TriangleStrip; }
public void _notifyVertexBufferDestroyed(HardwareVertexBuffer buf) { OgrePINVOKE.HardwareBufferManagerBase__notifyVertexBufferDestroyed(swigCPtr, HardwareVertexBuffer.getCPtr(buf)); if (OgrePINVOKE.SWIGPendingException.Pending) { throw OgrePINVOKE.SWIGPendingException.Retrieve(); } }
protected void DisposeVertexBuffer() { if (vertexBuffer != null) { vertexBuffer.Dispose(); vertexBuffer = null; } }
protected void DisposeVertexBuffer() { if (this.vertexBuffer != null) { this.vertexBuffer.Dispose(); this.vertexBuffer = null; } }
/// <summary> /// Internal method for setting up geometry, called by GuiElement.Update /// </summary> protected override void UpdatePositionGeometry() { /* * 0-----2 | /| | / | |/ | | 1-----3 */ float left, right, top, bottom; left = right = top = bottom = 0.0f; /* Convert positions into -1, 1 coordinate space (homogenous clip space). * - Left / right is simple range conversion * - Top / bottom also need inverting since y is upside down - this means * that top will end up greater than bottom and when computing texture * coordinates, we have to flip the v-axis (ie. subtract the value from * 1.0 to get the actual correct value). */ left = this.DerivedLeft * 2 - 1; right = left + (width * 2); top = -((this.DerivedTop * 2) - 1); bottom = top - (height * 2); // get a reference to the position buffer HardwareVertexBuffer buffer = renderOp.vertexData.vertexBufferBinding.GetBuffer(POSITION); // lock the buffer IntPtr data = buffer.Lock(BufferLocking.Discard); int index = 0; // modify the position data unsafe { float *posPtr = (float *)data.ToPointer(); posPtr[index++] = left; posPtr[index++] = top; posPtr[index++] = -1; posPtr[index++] = left; posPtr[index++] = bottom; posPtr[index++] = -1; posPtr[index++] = right; posPtr[index++] = top; posPtr[index++] = -1; posPtr[index++] = right; posPtr[index++] = bottom; posPtr[index++] = -1; } // unlock the position buffer buffer.Unlock(); }
public override void AllocateVertexBuffers(Terrain forTerrain, int numVertices, out HardwareVertexBuffer destPos, out HardwareVertexBuffer destDelta) { //destPos = this.GetVertexBuffer( ref FreePosBufList, forTerrain.PositionBufVertexSize, numVertices ); //destDelta = this.GetVertexBuffer( ref FreeDeltaBufList, forTerrain.DeltaBufVertexSize, numVertices ); destPos = GetVertexBuffer(ref this.FreePosBufList, forTerrain.PositionVertexDecl, numVertices); destDelta = GetVertexBuffer(ref this.FreeDeltaBufList, forTerrain.DeltaVertexDecl, numVertices); }
/// <summary> /// free all the billboard vertex buffers, and clear renderOps /// </summary> protected void FreeRenderOps() { foreach (TreeBillboardRenderOp op in renderOps) { HardwareVertexBuffer vb = op.renderOp.vertexData.vertexBufferBinding.GetBuffer(0); vb.Dispose(); } renderOps.Clear(); }
unsafe void MakeRoad() { if (end_make == false && HeightmapTerrain.Instances[0] != null) { Vertex[] vertices = new Vertex[vertex_count]; ushort[] indices = new ushort[index_count]; for (int i = 0; i < index_count; i++) { indices[i] = (ushort)(vertex_ind[i]); } for (int i = 0; i < vertex_count; i++) { Vertex vertex = new Vertex(); Vec3 p = ((vertex_pos[i] * attached_mesh.ScaleOffset) * Rotation) + (Position + attached_mesh.PositionOffset); Vec2 mesh_vertex_pos = new Vec2(p.X, p.Y); float terrain_height = HeightmapTerrain.Instances[0].Position.Z + HeightmapTerrain.Instances[0].GetHeight(mesh_vertex_pos, false); Vec3 terrain_norm = HeightmapTerrain.Instances[0].GetNormal(mesh_vertex_pos); vertex.position = new Vec3(vertex_pos[i].X, vertex_pos[i].Y, terrain_height); vertex.normal = terrain_norm; vertex.texCoord = vertex_tc[i]; vertices[i] = vertex; } SubMesh sub_mesh = mesh.SubMeshes[0]; { HardwareVertexBuffer vertex_buffer = sub_mesh.VertexData.VertexBufferBinding.GetBuffer(0); IntPtr buffer = vertex_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(Vertex *pvertices = vertices) { NativeUtils.CopyMemory(buffer, (IntPtr)pvertices, vertices.Length * sizeof(Vertex)); } vertex_buffer.Unlock(); } { HardwareIndexBuffer index_buffer = sub_mesh.IndexData.IndexBuffer; IntPtr buffer = index_buffer.Lock(HardwareBuffer.LockOptions.Discard); fixed(ushort *pindices = indices) { NativeUtils.CopyMemory(buffer, (IntPtr)pindices, indices.Length * sizeof(ushort)); } index_buffer.Unlock(); } if (EngineApp.Instance.ApplicationType == EngineApp.ApplicationTypes.Simulation) { end_make = true; first_init_mesh = true; } } }
public HardwareVertexBuffer __deref__() { global::System.IntPtr cPtr = OgrePINVOKE.HardwareVertexBufferPtr___deref__(swigCPtr); HardwareVertexBuffer ret = (cPtr == global::System.IntPtr.Zero) ? null : new HardwareVertexBuffer(cPtr, false); if (OgrePINVOKE.SWIGPendingException.Pending) { throw OgrePINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public static void CopyVertexData(VertexData dst, VertexData src, Dictionary <uint, uint> vertexIdMap) { dst.vertexStart = 0; dst.vertexCount = vertexIdMap.Count; // Copy vertex buffers in turn Dictionary <ushort, HardwareVertexBuffer> bindings = src.vertexBufferBinding.Bindings; foreach (ushort source in bindings.Keys) { HardwareVertexBuffer srcBuf = bindings[source]; // Create our new, more limited, buffer HardwareVertexBuffer dstBuf = HardwareBufferManager.Instance.CreateVertexBuffer( srcBuf.VertexSize, dst.vertexCount, srcBuf.Usage, srcBuf.HasShadowBuffer); // Copy elements for (int i = 0; i < src.vertexDeclaration.ElementCount; i++) { VertexElement element = src.vertexDeclaration.GetElement(i); dst.vertexDeclaration.AddElement( element.Source, element.Offset, element.Type, element.Semantic, element.Index); } // write the data to this buffer IntPtr srcData = srcBuf.Lock(BufferLocking.ReadOnly); IntPtr dstData = dstBuf.Lock(BufferLocking.Discard); unsafe { byte *srcPtr = (byte *)srcData.ToPointer(); byte *dstPtr = (byte *)dstData.ToPointer(); foreach (uint srcVertexId in vertexIdMap.Keys) { uint dstVertexId = vertexIdMap[srcVertexId]; int srcOffset = (int)(srcBuf.VertexSize * srcVertexId); int dstOffset = (int)(dstBuf.VertexSize * dstVertexId); for (int i = 0; i < srcBuf.VertexSize; ++i) { dstPtr[dstOffset + i] = srcPtr[srcOffset + i]; } } } dstBuf.Unlock(); srcBuf.Unlock(); dst.vertexBufferBinding.SetBinding(source, dstBuf); } }
/// <summary> /// /// </summary> /// <param name="vertexData"></param> /// <returns></returns> private unsafe float *NormalsGetCleared(VertexData vertexData) { VertexElement element = vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.Normal); HardwareVertexBuffer buffer = vertexData.vertexBufferBinding.GetBuffer(element.Source); IntPtr data = buffer.Lock(BufferLocking.Discard); float *normPtr = (float *)data.ToPointer(); for (int i = 0; i < buffer.VertexCount; i++) { normPtr[i] = 0.0f; } return(normPtr); }
private void buildVertexData() { if (vertexData != null) { vertexData.vertexBufferBinding.GetBuffer(0).Dispose(); } vertexData = InitVertexBuffer(width * height); HardwareVertexBuffer hvBuffer = vertexData.vertexBufferBinding.GetBuffer(0); // lock the vertex buffer IntPtr ipBuf = hvBuffer.Lock(BufferLocking.Discard); int bufferOff = 0; unsafe { float* buffer = (float*)ipBuf.ToPointer(); for (int zIndex = 0; zIndex < height; zIndex++) { for (int xIndex = 0; xIndex < width; xIndex++) { // Position buffer[bufferOff++] = xIndex * xzScale + offsetX; buffer[bufferOff++] = heightData[zIndex*width + xIndex] * yScale; buffer[bufferOff++] = zIndex * xzScale + offsetZ; // normals // XXX - pointing up for now Vector3 norm = Vector3.UnitY; buffer[bufferOff++] = norm.x; buffer[bufferOff++] = norm.y; buffer[bufferOff++] = norm.z; // Texture // XXX - assumes one unit of texture space is one page. // how does the vertex shader deal with texture coords? buffer[bufferOff++] = xIndex; buffer[bufferOff++] = zIndex; } } } hvBuffer.Unlock(); return; }
/// <summary> /// Returns the vertex coord for the given coordinates. /// </summary> /// <param name="x"></param> /// <param name="z"></param> /// <param name="n"></param> /// <returns></returns> public float GetVertex(int x, int z, int n) { HardwareVertexBuffer buffer = this.terrain.vertexBufferBinding.GetBuffer(POSITION); var vertex = new float[1]; var ptr = Memory.PinObject(vertex); int offset = (x * 3 + z * this.options.size * 3 + n) * 4; buffer.ReadData(offset, 4, ptr); Memory.UnpinObject(vertex); return(vertex[0]); }
private void CreateMesh() { DetachMeshFromEntity(); DestroyMesh(); int maxVertexCount = tesselation * tesselation; int maxIndexCount = (tesselation - 1) * (tesselation - 1) * 6; string meshName = MeshManager.Instance.GetUniqueName("DynamicSinusoidSurface"); mesh = MeshManager.Instance.CreateManual(meshName); SubMesh subMesh = mesh.CreateSubMesh(); subMesh.UseSharedVertices = false; //init vertexData VertexDeclaration declaration = subMesh.VertexData.VertexDeclaration; declaration.AddElement(0, 0, VertexElementType.Float3, VertexElementSemantic.Position); declaration.AddElement(0, 12, VertexElementType.Float3, VertexElementSemantic.Normal); declaration.AddElement(0, 24, VertexElementType.Float2, VertexElementSemantic.TextureCoordinates, 0); //declaration.AddElement( 0, 32, VertexElementType.Float4, VertexElementSemantic.Tangent, 0 ); HardwareBuffer.Usage usage = HardwareBuffer.Usage.DynamicWriteOnly;//HardwareBuffer.Usage.StaticWriteOnly; HardwareVertexBuffer vertexBuffer = HardwareBufferManager.Instance.CreateVertexBuffer( Marshal.SizeOf(typeof(Vertex)), maxVertexCount, usage); subMesh.VertexData.VertexBufferBinding.SetBinding(0, vertexBuffer, true); subMesh.VertexData.VertexCount = maxVertexCount; HardwareIndexBuffer indexBuffer = HardwareBufferManager.Instance.CreateIndexBuffer( HardwareIndexBuffer.IndexType._16Bit, maxIndexCount, usage); subMesh.IndexData.SetIndexBuffer(indexBuffer, true); subMesh.IndexData.IndexCount = maxIndexCount; //set material subMesh.MaterialName = "DynamicSinusoidSurface"; //set mesh gabarites Bounds bounds = new Bounds(-Scale / 2, Scale / 2); mesh.SetBoundsAndRadius(bounds, bounds.GetRadius()); }