private void CalculateTangentBinormal(TempVertex vertex1, TempVertex vertex2, TempVertex vertex3, out Vector tangent, out Vector binormal) { // Calculate the two vectors for the this face. var vector1 = new[] { vertex2.x - vertex1.x, vertex2.y - vertex1.y, vertex2.z - vertex1.z }; var vector2 = new[] { vertex3.x - vertex1.x, vertex3.y - vertex1.y, vertex3.z - vertex1.z }; // Calculate the tu and tv texture space vectors. var tuVector = new[] { vertex2.tu - vertex1.tu, vertex3.tu - vertex1.tu }; var tvVector = new[] { vertex2.tv - vertex1.tv, vertex3.tv - vertex1.tv }; // Calculate the denominator of the tangent / binormal equation. var den = 1.0f / (tuVector[0] * tvVector[1] - tuVector[1] * tvVector[0]); // Calculate the cross products and multiply by the coefficient to get the tangent and binormal. tangent.x = (tvVector[1] * vector1[0] - tvVector[0] * vector2[0]) * den; tangent.y = (tvVector[1] * vector1[1] - tvVector[0] * vector2[1]) * den; tangent.z = (tvVector[1] * vector1[2] - tvVector[0] * vector2[2]) * den; binormal.x = (tuVector[0] * vector2[0] - tuVector[1] * vector1[0]) * den; binormal.y = (tuVector[0] * vector2[1] - tuVector[1] * vector1[1]) * den; binormal.z = (tuVector[0] * vector2[2] - tuVector[1] * vector1[2]) * den; // Calculate the length of this normal. var length = (float)Math.Sqrt(tangent.x * tangent.x + tangent.y * tangent.y + tangent.z * tangent.z); // Normalize the normal and the store it. tangent.x = tangent.x / length; tangent.y = tangent.y / length; tangent.z = tangent.z / length; // Calculate the length of this normal. length = (float)Math.Sqrt(binormal.x * binormal.x + binormal.y * binormal.y + binormal.z * binormal.z); // Normalize the normal and the store it. binormal.x = binormal.x / length; binormal.y = binormal.y / length; binormal.z = binormal.z / length; }
private bool UpdateBuffers(DeviceContext deviceContext, int positionX, int positionY) { // If the position we rendering this bitmap to has not changed then don't update the vertex buffer since it. if (positionX == PreviousPosX && positionY == PreviousPosY) return true; // If it has changed then update the position it is being rendered to. PreviousPosX = positionX; PreviousPosY = positionY; // Calculate the screen coordinates of the left side of the bitmap. var left = (-(ScreenWidth >> 1)) + (float)positionX; // Calculate the screen coordinates of the right side of the bitmap. var right = left + BitmapWidth; // Calculate the screen coordinates of the top of the bitmap. var top = (ScreenHeight >> 1) - (float)positionY; // Calculate the screen coordinates of the bottom of the bitmap. var bottom = top - BitmapHeight; // Create and load the vertex array. var vertices = new[] { new TextureShader.Vertex() { position = new Vector3(left, top, 0), texture = new Vector2(0, 0) }, new TextureShader.Vertex() { position = new Vector3(right, bottom, 0), texture = new Vector2(1, 1) }, new TextureShader.Vertex() { position = new Vector3(left, bottom, 0), texture = new Vector2(0, 1) }, new TextureShader.Vertex() { position = new Vector3(left, top, 0), texture = new Vector2(0, 0) }, new TextureShader.Vertex() { position = new Vector3(right, top, 0), texture = new Vector2(1, 0) }, new TextureShader.Vertex() { position = new Vector3(right, bottom, 0), texture = new Vector2(1, 1) } }; DataStream mappedResource; #region Vertex Buffer // Lock the vertex buffer so it can be written to. deviceContext.MapSubresource(VertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); // Copy the data into the vertex buffer. mappedResource.WriteRange<TextureShader.Vertex>(vertices); // Unlock the vertex buffer. deviceContext.UnmapSubresource(VertexBuffer, 0); #endregion return true; }