   [SlimDX]Directx10C++ SDK Tutorials to c#
  Here my first post and my first contribution.You know dx9 and dx10 fear you ?? DX10 C# samples are impossible to find, like real &%@ pictures of britney;). I'm understanding...for the fear, not britneySo i started from last Feb2010 SlimDX Samples Triangle DX10 and try to Convert Directx10 SDK Tutorials and Samples to c#..........I Succeed:)Throw away DX9 now , i threw away xna for that, I'm a pc gamer, what the use to buy 300-500$ video cards and never exploited it.In my sample, i wrote much more explanation of the things i didnt understand first time or confused me. I'm a Beginner and speak french usually , i hope you'll be able to compile my language too :D.Like i said i'm a newbie, if i make mistakes or my explanations are not really what i understood or there are better ways to do that. Tell it please, i'm here to learn. Hope you ll appreciate it.First One Tutorial 04: 3D Spaces/** Copyright (c) 2007-2010 SlimDX Group* * Permission is hereby granted, free of charge, to any person obtaining a copy* of this software and associated documentation files (the "Software"), to deal* in the Software without restriction, including without limitation the rights* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell* copies of the Software, and to permit persons to whom the Software is* furnished to do so, subject to the following conditions:* * The above copyright notice and this permission notice shall be included in* all copies or substantial portions of the Software.* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN* THE SOFTWARE.*/using System.Drawing;using System.Runtime.InteropServices;using SlimDX;using SlimDX.Direct3D10;using SlimDX.SampleFramework;namespace Tutorial04_3DSpaces10{    /// <summary>    /// This application displays a 3D cube using Direct3D 10    /// </summary>    class Spaces10Sample : Sample    {   /// <summary>   /// In a derived class, implements logic to initialize the sample.   /// </summary>   protected override void OnInitialize()   {  DeviceSettings10 settings = new DeviceSettings10  { AdapterOrdinal = 0, CreationFlags = DeviceCreationFlags.None, Width = WindowWidth, Height = WindowHeight  };  InitializeDevice(settings);   }   protected override void OnResourceLoad()   {  using (Texture2D texture = Texture2D.FromSwapChain<Texture2D>(Context10.SwapChain, 0))  { renderTargetView = new RenderTargetView(Context10.Device, texture);  }    effect = Effect.FromFile(Context10.Device, "Spaces10.fx", "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);  technique = effect.GetTechniqueByName("Render"); //C++ Comparaison// technique = effect->GetTechniqueByName( "Render" );  pass = technique.GetPassByIndex(0);    worldVariable = effect.GetVariableByName("World").AsMatrix(); //C++ Comparaison// worldVariable = effect->GetVariableByName( "World" )->AsMatrix();  viewVariable = effect.GetVariableByName("View").AsMatrix();   projectionVariable = effect.GetVariableByName("Projection").AsMatrix();ShaderSignature signature = pass.Description.Signature;  inputLayout = new InputLayout(Context10.Device, signature, new[]   {    //C++...// { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0), // 8bits = 1 bytes, so 32bits = 4 bytes, then R32+G32+B32+A32 = 4+4+4+4 = 16 bytesnew InputElement("COLOR", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 16, 0) // 16 = total bytes from the element before, here it's "POSITION"});    // I have separate these for a better and clean code See just next  CreateVertexBuffer();  CreateIndexBuffer();  SetRasterizerParameters();    // Initialize the View matrix and Projection matrix  Vector3 Eye = new Vector3(0.0f, 2.0f, 6.0f); // Camera Position  Vector3 At = new Vector3(0.0f, 1.0f, 0.0f); // Camera Target / Direction  Vector3 Up = new Vector3(0.0f, 1.0f, 0.0f); // If your Eyes are the camera, Walk on your feet y = 1.0f(positive), Walk on your hand y = -1.0f(negative) view = Matrix.LookAtLH(Eye, At, Up);  projection = Matrix.PerspectiveFovLH((float)System.Math.PI * 0.5f, WindowWidth / WindowHeight, 0.1f, 100.0f);  /* //C++//  D3DXMatrixLookAtLH( &g_View, &Eye, &At, &Up );  D3DXMatrixPerspectiveFovLH( &g_Projection, ( float )D3DX_PI * 0.5f, width / ( FLOAT )height, 0.1f, 100.0f );  */ //C++//   }   // Create VertexBuffer   private void CreateVertexBuffer()   {  // 8 * 32 WHY ? : 1 Float = 4bytes, so Vector4(floatX,floatY,floatZ,floatW) = 4 float = 16 bytes.   // See above there are 8 vertices and each Vertex contain 2 * Vector4. Then 8 * (16 bytes + 16 bytes) = 8 * 32  vertexBuffer = new Buffer(Context10.Device, 8 * 32, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);    DataStream vertexStream = vertexBuffer.Map(MapMode.WriteDiscard, MapFlags.None);  vertexStream.WriteRange(new[]   { // Color ((R)ed, (G)reen, (B)lue, (A)lpha)   *Note Alpha used to blending (Transparency) // Position   X Y ZWand  Color   RGBA new Vector4(-1.0f,  1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // First Square  Vertex 1 (0) new Vector4( 1.0f,  1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), //Vertex 2 (1) new Vector4( 1.0f,  1.0f,  1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), //Vertex 3 (2) new Vector4(-1.0f,  1.0f,  1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), //Vertex 4 (3)  new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // Second Square Vertex 5 (4) new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), //Vertex 6 (5) new Vector4( 1.0f, -1.0f,  1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), //Vertex 7 (6) new Vector4(-1.0f, -1.0f,  1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f)  //Vertex 8 (7)});  vertexBuffer.Unmap();   }   // Create IndexBuffer   private void CreateIndexBuffer()   {  // Why 36 * sizeof(short) ? : See above there are 36 Indices * sizeof(short) : short = 16bits = 2 bytes  indexBuffer = new Buffer(Context10.Device, 36 * sizeof(short), ResourceUsage.Dynamic, BindFlags.IndexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None);    DataStream indexStream = indexBuffer.Map(MapMode.WriteDiscard, MapFlags.None);  indexStream.WriteRange(new short[]   {3,1,0, // Top   Face 1  Triangle  1 2,1,3, //Triangle  2  0,5,4, // Front Face 2  Triangle  3 1,5,0, //Triangle  4  3,4,7, // Left Side  Face 3  Triangle  5 0,4,3, //Triangle  6  1,6,5, // Right Side Face 4  Triangle  7 2,6,1, //Triangle  8  2,7,6, // Back  Face 5  Triangle  9 3,7,2, //Triangle 10  6,4,5, // BottomFace 6  Triangle 11 7,4,6, //Triangle 12  });  indexBuffer.Unmap();   }   // Set RasterizerParameters   private void SetRasterizerParameters()   {  RasterizerStateDescription rsd = new RasterizerStateDescription()  { CullMode = CullMode.Back, FillMode = FillMode.Solid,  };  RasterizerState rsdState = RasterizerState.FromDescription(Context10.Device, rsd);  Context10.Device.Rasterizer.State = rsdState;   }   protected override void OnResourceUnload()   {  effect.Dispose();  inputLayout.Dispose();  renderTargetView.Dispose();  vertexBuffer.Dispose();  indexBuffer.Dispose();   }   protected override void OnUpdate()   {  rotation += (float)(System.Math.PI / 4.0f * FrameDelta);    // Cube rotation transformation  Matrix rotationMatrix;  Matrix.RotationY(rotation, out rotationMatrix);  world = Matrix.Identity;  Matrix.Multiply(ref world, ref rotationMatrix, out world);   }   protected override void OnRenderBegin()   {  Context10.Device.OutputMerger.SetTargets(renderTargetView);  Context10.Device.Rasterizer.SetViewports(new Viewport(0, 0, WindowWidth, WindowHeight, 0.0f, 1.0f));  Context10.Device.ClearRenderTargetView(renderTargetView, new Color4(0.3f, 0.3f, 0.3f));   }   protected override void OnRender()   {  Context10.Device.InputAssembler.SetInputLayout(inputLayout);  Context10.Device.InputAssembler.SetPrimitiveTopology(SlimDX.Direct3D10.PrimitiveTopology.TriangleList);  Context10.Device.InputAssembler.SetIndexBuffer(indexBuffer, SlimDX.DXGI.Format.R16_UInt, 0); // R16_UInt = Size of one index , here is short = 16  Context10.Device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 32, 0)); // 32  = Size of one vertex;    // Update WorldViewProjection variable  worldVariable.SetMatrix(world);  viewVariable.SetMatrix(view);  projectionVariable.SetMatrix(projection);    // Draw Cube  pass.Apply();  Context10.Device.DrawIndexed(36, 0, 0);   }   protected override void OnRenderEnd()   {  Context10.SwapChain.Present(0, SlimDX.DXGI.PresentFlags.None);   }   #region Implementation Detail   private RenderTargetView renderTargetView;   private Effect effect;   private EffectTechnique technique;   private EffectPass pass;   private InputLayout inputLayout;   private Buffer vertexBuffer;   private Buffer indexBuffer;   private float rotation;   private EffectMatrixVariable worldVariable;   private EffectMatrixVariable viewVariable;   private EffectMatrixVariable projectionVariable;   private Matrix world;   private Matrix view;   private Matrix projection;   #endregion    }}Effect File//--------------------------------------------------------------------------------------// File: Tutorial04DX10.fx//// Copyright (c) Microsoft Corporation. All rights reserved.//--------------------------------------------------------------------------------------//--------------------------------------------------------------------------------------// Constant Buffer Variables//--------------------------------------------------------------------------------------matrix World;matrix View;matrix Projection;//--------------------------------------------------------------------------------------struct VS_OUTPUT{    float4 Pos : SV_POSITION;    float4 Color : COLOR0;};//--------------------------------------------------------------------------------------// Vertex Shader//--------------------------------------------------------------------------------------VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR ){    VS_OUTPUT output = (VS_OUTPUT)0;    output.Pos = mul( Pos, World );    output.Pos = mul( output.Pos, View );    output.Pos = mul( output.Pos, Projection );    output.Color = Color;    return output;}//--------------------------------------------------------------------------------------// Pixel Shader//--------------------------------------------------------------------------------------float4 PS( VS_OUTPUT input ) : SV_Target{    return input.Color;}//--------------------------------------------------------------------------------------technique10 Render{    pass P0    {   SetVertexShader( CompileShader( vs_4_0, VS() ) );   SetGeometryShader( 0 );   SetPixelShader( CompileShader( ps_4_0, PS() ) );    }}TroubleShooting : Be sure to set Effect properties Copy if newerand i dont why copy and paste triangle effect file and rename it and replace code with this. I read there is a bug sometime from effect file unicode or something like thatTutorial 05: 3D TransformationTo be continue... (hehe) i'll wait before to see reaction and commentsfor this one.   