Salut à tous !

Bon j'essaye de "traduire" le turorial 4 (Espace 3D) de C++/Directx10 a C#/SlimDX

Normalement, les conversions de fonction sont bonnes ... J'ai aucune erreur de compilation / éxécution

Donc voici mon fichier principal. J'ai tenté de découper en plusieurs fonctions pour un peu plus de lisibilité. Le découpage fonctionne (Déja testé avant de modifier le code pour adapter au tuto)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Drawing;
using SlimDX;
using SlimDX.Direct3D10;
using SlimDX.DXGI;
using SlimDX.Windows;
using SlimDX.D3DCompiler;
using Device = SlimDX.Direct3D10.Device;
 
namespace IcicleGraph
{
    class MainWindow : RenderForm
    {
        private Device device;
        private SwapChain swapChain;
        private RenderTargetView renderView;
        private Effect effect;
        private EffectTechnique technique; 
        private EffectPass pass;
        private InputLayout layout;
        private SlimDX.Direct3D10.Buffer vertexBuffer, indexBuffer;
        private EffectMatrixVariable worldVariable = null;
        private EffectMatrixVariable viewVariable = null;
        private EffectMatrixVariable projectionVariable = null;
        private Matrix world;
        private Matrix view;
        private Matrix projection;
        private Vector3 eye;
        private Vector3 at;
        private Vector3 up;
 
        public MainWindow(string windowName) : base(windowName)
        {
        }
 
        public void initForm()
        {
            var desc = new SwapChainDescription()
            {
                BufferCount = 1,
                ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed = true,
                OutputHandle = Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };
 
 
            Device.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain);
 
            //Stops Alt+enter from causing fullscreen skrewiness.
            Factory factory = swapChain.GetParent<Factory>();
            factory.SetWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);
 
            Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
            renderView = new RenderTargetView(device, backBuffer);
 
            device.OutputMerger.SetTargets(renderView);
            device.Rasterizer.SetViewports(new Viewport(0, 0, ClientSize.Width, ClientSize.Height, 0.0f, 1.0f));
 
            effect = Effect.FromFile(device, "IcicleGraph.fx", "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);
            technique = effect.GetTechniqueByIndex(0);
            pass = technique.GetPassByIndex(0);
 
            worldVariable = effect.GetVariableByName("World").AsMatrix();
            viewVariable = effect.GetVariableByName("View").AsMatrix();
            projectionVariable = effect.GetVariableByName("Projection").AsMatrix();
 
            layout = new InputLayout(device, pass.Description.Signature, new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) 
            });
 
            world = Matrix.Identity;
 
             // Initialize the view matrix
            eye = new Vector3( 0.0f, 1.0f, -5.0f );
            at = new Vector3( 0.0f, 1.0f, 0.0f );
            up = new Vector3(0.0f, 1.0f, 0.0f);
 
            view = Matrix.LookAtLH(eye, at, up);
 
            // Initialize the projection matrix
            projection = Matrix.PerspectiveFovLH( (float)(Math.PI * 0.5f), ClientSize.Width / ClientSize.Height, 0.1f, 100.0f);
 
            backBuffer.Dispose();
 
 
        }
 
        public void initData()
        {
 
            var stream = new DataStream(8 * 32, true, true);
            var index = new DataStream(36 * sizeof(int), true, true);
 
            stream.WriteRange(new[]
	        { 
                new Vector4( -1.0f, 1.0f, -1.0f , 1.0f ), new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( 1.0f, 1.0f, -1.0f , 1.0f ),  new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( 1.0f, 1.0f, 1.0f , 1.0f ),   new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( -1.0f, 1.0f, 1.0f , 1.0f ),  new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( -1.0f, -1.0f, -1.0f , 1.0f ),new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( 1.0f, -1.0f, -1.0f , 1.0f ), new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( 1.0f, -1.0f, 1.0f , 1.0f ),  new Vector4( 255.0f, 0.0f, 0.0f , 1.0f),
                new Vector4( -1.0f, -1.0f, 1.0f , 1.0f ), new Vector4( 255.0f, 0.0f, 0.0f , 1.0f)
            });
 
            index.WriteRange(new[]
            {
	            3,1,0,
	            2,1,3,
	            0,5,4,
	            1,5,0,
	            3,4,7,
	            0,4,3,
	            1,6,5,
	            2,6,1,
	            2,7,6,
	            3,7,2,
	            6,4,5,
	            7,4,6
            });
 
            stream.Position = 0;
            index.Position = 0;
 
            vertexBuffer = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription()
            {
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = 8 * 32,
                Usage = ResourceUsage.Default
            });
 
            indexBuffer = new SlimDX.Direct3D10.Buffer(device, index, new BufferDescription()
            {
                BindFlags = BindFlags.IndexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                SizeInBytes = 36 * sizeof(int) ,
                Usage = ResourceUsage.Default
            });
 
            index.Dispose();
            stream.Dispose();
 
        }
 
        public void run()
        {
           MessagePump.Run(this, () =>
           {
               device.ClearRenderTargetView(renderView, Color.LightGray);
 
               worldVariable.SetMatrix(world);
               viewVariable.SetMatrix(view);
               projectionVariable.SetMatrix(projection);
 
               device.InputAssembler.SetInputLayout(layout);
               device.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0);
               device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
               device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 32, 0));
 
               for (int i = 0; i < technique.Description.PassCount; ++i)
               {
                   pass.Apply();
                   device.Draw(3, 0);
               }
 
               swapChain.Present(0, PresentFlags.None);
           });
        }
 
        public void clear()
        {
            indexBuffer.Dispose();
            vertexBuffer.Dispose();
            layout.Dispose();
            effect.Dispose();
            renderView.Dispose();
            device.Dispose();
            swapChain.Dispose();
        }
 
 
        protected override void OnResize(EventArgs e)
        {
 
        }
 
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MainWindow
            // 
            this.ClientSize = new System.Drawing.Size(600, 600);
            this.Name = "MainWindow";
            this.ResumeLayout(false);
 
        }
    }
}
Ici mon fichier fx. J'pense qu'il est correct ...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
matrix World;
matrix View;
matrix Projection;
 
struct VS_IN
{
	float4 pos : POSITION;
	float4 col : COLOR;
};
 
struct VS_OUTPUT
{
	float4 pos : SV_POSITION;
	float4 col : COLOR;
};
 
VS_OUTPUT VS( VS_IN input )
{
	VS_OUTPUT output = (VS_OUTPUT)0;
	output.pos = mul( input.pos, World );
	output.pos = mul( output.pos, View );
	output.pos = mul( output.pos, Projection );
	output.col = input.col;
	return output;
}
 
float4 PS( VS_OUTPUT input ) : SV_Target
{
	return input.col;
}
 
technique10 Render
{
	pass P0
	{
		SetGeometryShader( 0 );
		SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetPixelShader( CompileShader( ps_4_0, PS() ) );
	}
}
D'après moi, le probleme est au niveau du stream et index, que je passe respectivement au vertexBuffer et a l'indexBuffer ...

Dans le tutoriel, le VertexBuffer est un tableau de couple (tableaux) <Vector3 (position), Vector4(color)> ... Que je traduis juste par un tableau de Vector4 ...
C'est la méthode utilisée sur les tutorials SlimDX, un tableau contenant tous les points, constitué successivement de Vector4 Position, Vector 4 Color, etc.

Du coup, est ce que je dois passer 6 dans le device.draw(x.0); pour passer 3 positions et 3 couleurs ?

Est-ce que c'est le fait d'avoir des Vector4 (et non Vector3) qui poserait problème ? J'ai vu sur un tutoriel qu'ils conseillaient des vector4 plutot que vector3 ...

Bref, comme vous pouvez le voir d'après mes explications, je suis un peu perdu !

Merci d'avance à ceux qui pourront m'aider