/// <summary> /// Builds the vertex buffer using vertices with position and normal data. /// </summary> public void BuildVertices_PositionNormal() { GraphicsStream stream; DataCore.VertexFormats.PositionNormal[] tri = new DataCore.VertexFormats.PositionNormal[_page.TerrainPatch.NumVertices]; for (int i = 0; i < tri.Length; i++) { tri[i].Position = _page.TerrainPatch.Vertices[i].Position; tri[i].Normal = _page.TerrainPatch.Vertices[i].Normal; } if (_vb != null) { if (!_vb.Disposed) { _vb.Dispose(); } _vb = null; } _vb = new D3D.VertexBuffer(typeof(DataCore.VertexFormats.PositionNormal), tri.Length, _viewport.Device, D3D.Usage.WriteOnly, DataCore.VertexFormats.PositionNormal.Format, D3D.Pool.Managed); stream = _vb.Lock(0, 0, D3D.LockFlags.None); stream.Write(tri); _vb.Unlock(); _vbSize = tri.Length; }
public static void GetBoundingBox(D3D.Mesh mesh, float scale, out DX.Vector3 min, out DX.Vector3 max) { D3D.VertexBuffer verts = mesh.VertexBuffer; DX.GraphicsStream stream = verts.Lock(0, 0, D3D.LockFlags.None); D3D.Geometry.ComputeBoundingBox(stream, mesh.NumberVertices, mesh.VertexFormat, out min, out max); verts.Unlock(); stream = null; verts = null; }
public static float GetBoundingRadius(D3D.Mesh mesh, float scale) { D3D.VertexBuffer verts = mesh.VertexBuffer; DX.GraphicsStream stream = verts.Lock(0, 0, D3D.LockFlags.None); DX.Vector3 meshCenter; float radius = D3D.Geometry.ComputeBoundingSphere(stream, mesh.NumberVertices, mesh.VertexFormat, out meshCenter) * scale; verts.Unlock(); stream = null; verts = null; return(radius); }
public Array Lock(bool read) { D3d.LockFlags d3dLockFlags = D3d.LockFlags.None; if (!read) { d3dLockFlags |= D3d.LockFlags.Discard; } Array lockedArray = d3dVertexBuffer.Lock(0, d3dLockFlags); d3dLock = true; return(lockedArray); }
/// <summary> /// Builds the vertex buffer using vertices with position, normal, and eight sets of texture data. /// </summary> public void BuildVertices_PositionNormalTextured8() { GraphicsStream stream; DataCore.VertexFormats.PositionNormalTextured8[] tri = new DataCore.VertexFormats.PositionNormalTextured8[_page.TerrainPatch.NumVertices]; for (int i = 0; i < tri.Length; i++) { tri[i].Position = _page.TerrainPatch.Vertices[i].Position; tri[i].Normal = _page.TerrainPatch.Vertices[i].Normal; if (_page.TerrainPatch.NumTextures > 0) { for (int j = 0; j < _page.TerrainPatch.NumTextures && j < 8; j++) { tri[i].SetTextureCoordinates(_page.TerrainPatch.GetTextureCoordinateList(j)[i], j); } } else { tri[i].SetTextureCoordinates(new Vector2(0f, 0f), 0); } } if (_vb != null) { if (!_vb.Disposed) { _vb.Dispose(); } _vb = null; } _vb = new D3D.VertexBuffer(typeof(DataCore.VertexFormats.PositionNormalTextured8), tri.Length, _viewport.Device, D3D.Usage.WriteOnly, DataCore.VertexFormats.PositionNormalTextured8.Format, D3D.Pool.Managed); stream = _vb.Lock(0, 0, D3D.LockFlags.None); stream.Write(tri); _vb.Unlock(); _vbSize = tri.Length; }
// cells are setup as x, y index of col, row, and 1, 1 wide,high, and a total rows and cols they are of. // okay so this computes from that, public void SetupCell(D3DState state, float x, float y, float width, float height, float rows, float cols) { x_ofs = (int)x; y_ofs = (int)y; this.rows = (int)rows; this.cols = (int)cols; if (state == null) { return; } cell_verts = new Direct3D.VertexBuffer( typeof(Direct3D.CustomVertex.PositionTextured), 4, state.graphics, 0, Direct3D.CustomVertex.PositionTextured.Format, Direct3D.Pool.Default); Direct3D.CustomVertex.PositionTextured[] verts = (Direct3D.CustomVertex.PositionTextured[])cell_verts.Lock(0, 0); verts[0].X = -1.0f; verts[0].Y = -1.0f; verts[0].Z = 0.0f; verts[0].Tu = (x * width) / cols; verts[0].Tv = (y * height) / rows; verts[1].X = -1.0f; verts[1].Y = 1.0f; verts[1].Z = 0.0f; verts[1].Tu = ((x + 1) * width) / cols; verts[1].Tv = (y * height) / rows; verts[2].X = 1.0f; verts[2].Y = -1.0f; verts[2].Z = 0.0f; verts[2].Tu = (x * width) / cols; verts[2].Tv = ((y + 1) * height) / rows; verts[3].X = 1.0f; verts[3].Y = 1.0f; verts[3].Z = 0.0f; verts[3].Tu = ((x + 1) * width) / cols; verts[3].Tv = ((y + 1) * height) / rows; cell_verts.Unlock(); }
/// <summary> /// Renders 3D text /// </summary> public void Render3DText(string text, RenderFlags flags) { if (device == null) { throw new System.ArgumentNullException(); } // Set up renderstate savedStateBlock.Capture(); drawTextStateBlock.Apply(); device.VertexFormat = CustomVertex.PositionNormalTextured.Format; device.PixelShader = null; device.SetStreamSource(0, vertexBuffer, 0, VertexInformation.GetFormatSize(CustomVertex.PositionNormalTextured.Format)); // Set filter states if ((flags & RenderFlags.Filtered) != 0) { samplerState0.MinFilter = TextureFilter.Linear; samplerState0.MagFilter = TextureFilter.Linear; } // Position for each text element float x = 0.0f; float y = 0.0f; // Center the text block at the origin if ((flags & RenderFlags.Centered) != 0) { System.Drawing.SizeF sz = GetTextExtent(text); x = -(((float)sz.Width) / 10.0f) / 2.0f; y = -(((float)sz.Height) / 10.0f) / 2.0f; } // Turn off culling for two-sided text if ((flags & RenderFlags.TwoSided) != 0) { renderState.CullMode = Cull.None; } // Adjust for character spacing x -= spacingPerChar / 10.0f; float fStartX = x; // Fill vertex buffer GraphicsStream strm = vertexBuffer.Lock(0, 0, LockFlags.Discard); int numTriangles = 0; foreach (char c in text) { if (c == '\n') { x = fStartX; y -= (textureCoords[0, 3] - textureCoords[0, 1]) * textureHeight / 10.0f; } if ((c - 32) < 0 || (c - 32) >= 128 - 32) { continue; } float tx1 = textureCoords[c - 32, 0]; float ty1 = textureCoords[c - 32, 1]; float tx2 = textureCoords[c - 32, 2]; float ty2 = textureCoords[c - 32, 3]; float w = (tx2 - tx1) * textureWidth / (10.0f * textureScale); float h = (ty2 - ty1) * textureHeight / (10.0f * textureScale); if (c != ' ') { strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + 0, y + 0, 0), new Vector3(0, 0, -1), tx1, ty2)); strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + 0, y + h, 0), new Vector3(0, 0, -1), tx1, ty1)); strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + w, y + 0, 0), new Vector3(0, 0, -1), tx2, ty2)); strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + w, y + h, 0), new Vector3(0, 0, -1), tx2, ty1)); strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + w, y + 0, 0), new Vector3(0, 0, -1), tx2, ty2)); strm.Write(new CustomVertex.PositionNormalTextured(new Vector3(x + 0, y + h, 0), new Vector3(0, 0, -1), tx1, ty1)); numTriangles += 2; if (numTriangles * 3 > (MaxNumfontVertices - 6)) { // Unlock, render, and relock the vertex buffer vertexBuffer.Unlock(); device.DrawPrimitives(PrimitiveType.TriangleList, 0, numTriangles); strm = vertexBuffer.Lock(0, 0, LockFlags.Discard); numTriangles = 0; } } x += w - (2 * spacingPerChar) / 10.0f; } // Unlock and render the vertex buffer vertexBuffer.Unlock(); if (numTriangles > 0) { device.DrawPrimitives(PrimitiveType.TriangleList, 0, numTriangles); } // Restore the modified renderstates savedStateBlock.Apply(); }
/// <summary> /// Reloads the VertexBuffer for the TerrainPage for displaying vertices. /// </summary> public void RefreshVertexBuffer_Vertices() { if (_page != null) { GraphicsStream stream; D3D.CustomVertex.PositionColored[] vertices = new D3D.CustomVertex.PositionColored[_page.TerrainPatch.NumVertices * 4]; D3D.CustomVertex.PositionNormal[] tri = _page.TerrainPatch.Vertices; int color = Color.Black.ToArgb(); int red, blue; float zoomFactor = _viewport.Camera.FollowDistance / _originalZoomFactor; Vector3 position; float distance; float vDist = _page.TerrainPatch.NearestVertices * _vertexSize; for (int i = 0; i < tri.Length; i++) { if (_showSelectedVertices && _page.TerrainPatch.SelectedVertices[i]) { color = Color.Red.ToArgb(); } else if (_showSelectedVertices && _softSelection && _page.TerrainPatch.AreVerticesSelected) { position = _page.TerrainPatch.Vertices[i].Position; distance = _page.TerrainPatch.FindShortestDistanceToSelectedVertex(position); if (distance <= _softDistanceSquared) { if (_falloff) { red = Convert.ToInt32(Color.Red.R * (1 - distance / _softDistanceSquared)); blue = Convert.ToInt32(Color.Blue.B * (distance / _softDistanceSquared)); color = Color.FromArgb(red, 0, blue).ToArgb(); } else { color = Color.Red.ToArgb(); } } else { color = Color.Blue.ToArgb(); } } else { color = Color.Blue.ToArgb(); } vertices[i * 4].Position = new Vector3(tri[i].Position.X - (vDist * zoomFactor), tri[i].Position.Y, tri[i].Position.Z - (vDist * zoomFactor)); vertices[i * 4].Color = color; vertices[i * 4 + 1].Position = new Vector3(tri[i].Position.X - (vDist * zoomFactor), tri[i].Position.Y, tri[i].Position.Z + (vDist * zoomFactor)); vertices[i * 4 + 1].Color = color; vertices[i * 4 + 2].Position = new Vector3(tri[i].Position.X + (vDist * zoomFactor), tri[i].Position.Y, tri[i].Position.Z + (vDist * zoomFactor)); vertices[i * 4 + 2].Color = color; vertices[i * 4 + 3].Position = new Vector3(tri[i].Position.X + (vDist * zoomFactor), tri[i].Position.Y, tri[i].Position.Z - (vDist * zoomFactor)); vertices[i * 4 + 3].Color = color; } if (_vbVerts != null) { if (!_vbVerts.Disposed) { _vbVerts.Dispose(); } _vbVerts = null; } _vbVerts = new D3D.VertexBuffer(typeof(D3D.CustomVertex.PositionColored), vertices.Length, _viewport.Device, D3D.Usage.WriteOnly, D3D.CustomVertex.PositionColored.Format, D3D.Pool.Managed); stream = _vbVerts.Lock(0, 0, D3D.LockFlags.None); stream.Write(vertices); _vbVerts.Unlock(); _vbVertSize = vertices.Length; _page.TerrainPatch.RefreshVertices = false; } else if (_vbVerts != null) { if (!_vbVerts.Disposed) { _vbVerts.Dispose(); } _vbVerts = null; _vbVertSize = 0; } }
/// <summary> /// Reloads the VertexBuffer for the TerrainPage for displaying vertices. /// </summary> public void RefreshVertexBuffer_Vertices() { if ( _page != null ) { GraphicsStream stream; D3D.CustomVertex.PositionColored[] vertices = new D3D.CustomVertex.PositionColored[_page.TerrainPatch.NumVertices * 4]; D3D.CustomVertex.PositionNormal[] tri = _page.TerrainPatch.Vertices; int color = Color.Black.ToArgb(); int red, blue; float zoomFactor = _viewport.Camera.FollowDistance / _originalZoomFactor; Vector3 position; float distance; float vDist = _page.TerrainPatch.NearestVertices * _vertexSize; for ( int i = 0; i < tri.Length; i++ ) { if ( _showSelectedVertices && _page.TerrainPatch.SelectedVertices[i] ) color = Color.Red.ToArgb(); else if ( _showSelectedVertices && _softSelection && _page.TerrainPatch.AreVerticesSelected ) { position = _page.TerrainPatch.Vertices[i].Position; distance = _page.TerrainPatch.FindShortestDistanceToSelectedVertex( position ); if ( distance <= _softDistanceSquared ) { if ( _falloff ) { red = Convert.ToInt32( Color.Red.R * ( 1 - distance / _softDistanceSquared ) ); blue = Convert.ToInt32( Color.Blue.B * ( distance / _softDistanceSquared ) ); color = Color.FromArgb( red, 0, blue ).ToArgb(); } else color = Color.Red.ToArgb(); } else { color = Color.Blue.ToArgb(); } } else { color = Color.Blue.ToArgb(); } vertices[i * 4].Position = new Vector3( tri[i].Position.X - ( vDist * zoomFactor ), tri[i].Position.Y, tri[i].Position.Z - ( vDist * zoomFactor ) ); vertices[i * 4].Color = color; vertices[i * 4 + 1].Position = new Vector3( tri[i].Position.X - ( vDist * zoomFactor ), tri[i].Position.Y, tri[i].Position.Z + ( vDist * zoomFactor ) ); vertices[i * 4 + 1].Color = color; vertices[i * 4 + 2].Position = new Vector3( tri[i].Position.X + ( vDist * zoomFactor ), tri[i].Position.Y, tri[i].Position.Z + ( vDist * zoomFactor ) ); vertices[i * 4 + 2].Color = color; vertices[i * 4 + 3].Position = new Vector3( tri[i].Position.X + ( vDist * zoomFactor ), tri[i].Position.Y, tri[i].Position.Z - ( vDist * zoomFactor ) ); vertices[i * 4 + 3].Color = color; } if ( _vbVerts != null ) { if ( !_vbVerts.Disposed ) _vbVerts.Dispose(); _vbVerts = null; } _vbVerts = new D3D.VertexBuffer( typeof( D3D.CustomVertex.PositionColored ), vertices.Length, _viewport.Device, D3D.Usage.WriteOnly, D3D.CustomVertex.PositionColored.Format, D3D.Pool.Managed ); stream = _vbVerts.Lock( 0, 0, D3D.LockFlags.None ); stream.Write( vertices ); _vbVerts.Unlock(); _vbVertSize = vertices.Length; _page.TerrainPatch.RefreshVertices = false; } else if ( _vbVerts != null ) { if ( !_vbVerts.Disposed ) _vbVerts.Dispose(); _vbVerts = null; _vbVertSize = 0; } }
/// <summary> /// Builds the vertex buffer using vertices with position, normal, and eight sets of texture data. /// </summary> public void BuildVertices_PositionNormalTextured8() { GraphicsStream stream; DataCore.VertexFormats.PositionNormalTextured8[] tri = new DataCore.VertexFormats.PositionNormalTextured8[_page.TerrainPatch.NumVertices]; for ( int i = 0; i < tri.Length; i++ ) { tri[i].Position = _page.TerrainPatch.Vertices[i].Position; tri[i].Normal = _page.TerrainPatch.Vertices[i].Normal; if ( _page.TerrainPatch.NumTextures > 0 ) { for ( int j = 0; j < _page.TerrainPatch.NumTextures && j < 8; j++ ) tri[i].SetTextureCoordinates( _page.TerrainPatch.GetTextureCoordinateList( j )[i], j ); } else tri[i].SetTextureCoordinates( new Vector2( 0f, 0f ), 0 ); } if ( _vb != null ) { if ( !_vb.Disposed ) _vb.Dispose(); _vb = null; } _vb = new D3D.VertexBuffer( typeof( DataCore.VertexFormats.PositionNormalTextured8 ), tri.Length, _viewport.Device, D3D.Usage.WriteOnly, DataCore.VertexFormats.PositionNormalTextured8.Format, D3D.Pool.Managed ); stream = _vb.Lock( 0, 0, D3D.LockFlags.None ); stream.Write( tri ); _vb.Unlock(); _vbSize = tri.Length; }
/// <summary> /// Builds the vertex buffer using vertices with position and normal data. /// </summary> public void BuildVertices_PositionNormal() { GraphicsStream stream; DataCore.VertexFormats.PositionNormal[] tri = new DataCore.VertexFormats.PositionNormal[_page.TerrainPatch.NumVertices]; for ( int i = 0; i < tri.Length; i++ ) { tri[i].Position = _page.TerrainPatch.Vertices[i].Position; tri[i].Normal = _page.TerrainPatch.Vertices[i].Normal; } if ( _vb != null ) { if ( !_vb.Disposed ) _vb.Dispose(); _vb = null; } _vb = new D3D.VertexBuffer( typeof( DataCore.VertexFormats.PositionNormal ), tri.Length, _viewport.Device, D3D.Usage.WriteOnly, DataCore.VertexFormats.PositionNormal.Format, D3D.Pool.Managed ); stream = _vb.Lock( 0, 0, D3D.LockFlags.None ); stream.Write( tri ); _vb.Unlock(); _vbSize = tri.Length; }
/// <summary> /// /// </summary> /// <param name="offset"></param> /// <param name="length"></param> /// <param name="locking"></param> /// <returns></returns> /// DOC protected override IntPtr LockImpl(int offset, int length, BufferLocking locking) { D3D.LockFlags d3dLocking = D3DHelper.ConvertEnum(locking, usage); Microsoft.DirectX.GraphicsStream s = d3dBuffer.Lock(offset, length, d3dLocking); return(s.InternalData); }