Bonjour à tous,
J'ai un petit problème avec le logiciel que je développe, qui je pense est liée à la résolution de mon panel ...
Déjà voila à quoi ça ressemble:
Le fond: C'est un carré, texturé avec une image jpg de 1024*1024 représentant un plan.
Le "truc" au dessus, c'est supposé être un tracé, j'y fais aucun traitement dans le Vertex Shader / Pixel Shader, ce qui explique que ce soit si moche, mais on peut voir que les pixels sont relativement gros ...
Maintenant mon code ... (Je précise, c'est SlimDX et DirectX10, en C#)
J'ai une classe GPUDevice qui instancie mon device, et qui contient une liste de Panels qui utiliseront ce device.
J'ai une classe MapView, qui donc elle hérite de Panel, créée mes différents modeles (M_XX), créé mes objets chargés du dessin (D_XX), et surtout, créé la swapChain relative à ce panel, le ViewPort et le RenderTargetView ...
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using SlimDX; using SlimDX.Direct3D10; using Device = SlimDX.Direct3D10.Device; namespace PrototypeV4.GraphicEngine { static class GPUDevice { private static SlimDX.Direct3D10.Device gpuDevice; private static List<PrototypeV4.GUI.ViewPanel> listOfPanels; static GPUDevice() { gpuDevice = new SlimDX.Direct3D10.Device(DriverType.Hardware, DeviceCreationFlags.Debug); listOfPanels = new List<GUI.ViewPanel>(); } public static SlimDX.Direct3D10.Device GpuDevice { get { return gpuDevice; } } public static List<GUI.ViewPanel> ListOfPanelsToRender { get { return listOfPanels; } } public static void addPanelToRender(GUI.ViewPanel vp) { listOfPanels.Add(vp); } public static void clear() { gpuDevice.Dispose(); } } }
Et ensuite ma class D_Map qui elle s'occupe de dessiner la map ...
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using SlimDX; using SlimDX.Direct3D10; using SlimDX.DXGI; using SlimDX.Windows; using SlimDX.D3DCompiler; using System.Windows.Forms; using System.Runtime.InteropServices; namespace PrototypeV4.GUI { class MapView : ViewPanel { SlimDX.Direct3D10.Device gpuDevice; private Modele.M_Map modeleMap; private Modele.M_Path modelePath; private Draw.D_Map drawMap; private Draw.D_Path drawPath; private MapViewContainer parent; private Color4 backColor = new Color4(new Vector3(1.0f, 1.0f, 1.0f)); public MapView(MapViewContainer parent) : base() { this.parent = parent; modeleMap = parent.CurrentMap; modelePath = new Modele.M_Path(); this.Dock = System.Windows.Forms.DockStyle.Fill; gpuDevice = GraphicEngine.GPUDevice.GpuDevice; initCamera(); initView(); drawMap = new Draw.D_Map(this, modeleMap); DateTime now = DateTime.Now; modelePath.add(new Modele.M_Point(0.2f, 0.2f, now)); modelePath.add(new Modele.M_Point(0.4f, 0.3f, now.AddSeconds(10))); modelePath.add(new Modele.M_Point(0.4f, 0.7f, now.AddSeconds(20))); modelePath.add(new Modele.M_Point(0.2f, 0.6f, now.AddSeconds(30))); modelePath.add(new Modele.M_Point(0.3f, 0.9f, now.AddSeconds(40))); modelePath.add(new Modele.M_Point(0.5f, 0.10f, now.AddSeconds(50))); modelePath.add(new Modele.M_Point(0.8f, 0.10f, now.AddSeconds(60))); modelePath.add(new Modele.M_Point(0.10f, 0.8f, now.AddSeconds(70))); modelePath.add(new Modele.M_Point(0.9f, 0.6f, now.AddSeconds(80))); modelePath.add(new Modele.M_Point(0.5f, 0.7f, now.AddSeconds(90))); modelePath.add(new Modele.M_Point(0.3f, 0.5f, now.AddSeconds(100))); drawPath = new Draw.D_Path(this, modelePath); drawMap.init(); drawPath.init(); } public void initView() { Factory factory; SlimDX.Direct3D10.Resource resource; var description = new SwapChainDescription() { BufferCount = 2, Usage = Usage.RenderTargetOutput, OutputHandle = Handle, IsWindowed = true, ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm), SampleDescription = new SampleDescription(1, 0), Flags = SwapChainFlags.AllowModeSwitch, SwapEffect = SwapEffect.Discard }; factory = new Factory(); factory.SetWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter); swapChain = new SwapChain(factory, gpuDevice, description); viewport = new Viewport(0, 0, this.Width, this.Height); resource = SlimDX.Direct3D10.Resource.FromSwapChain<Texture2D>(swapChain, 0); renderTarget = new RenderTargetView(gpuDevice, resource); gpuDevice.OutputMerger.SetTargets(renderTarget); gpuDevice.Rasterizer.SetViewports(viewport); factory.Dispose(); resource.Dispose(); } public void initCamera() { // Initialisation de la caméra camera = new Utilities.Camera( new Vector3(0.0f, 0.0f, -100.0f), // Location new Vector3(0.0f, 0.0f, 0.0f), // Target new Vector3(0.0f, 1.0f, 0.0f), // Up 0.1f, 100.0f, (float)(Math.PI * 0.5f), this.Width / this.Height); camera.build(); } public override void run() { gpuDevice.ClearRenderTargetView(renderTarget, backColor); gpuDevice.OutputMerger.SetTargets(renderTarget); drawMap.run(); drawPath.run(); swapChain.Present(0, PresentFlags.None); } public void reinit() { drawMap.reinit(); } public override void clear() { swapChain.Dispose(); renderTarget.Dispose(); } public MapViewContainer Parent { get { return Parent; } } public Modele.M_Map Map { set { modeleMap = value; reinit(); } } } }
Cette classe a pas grand chose de spécial je pense, c'est juste les "techniques" habituelle pour appliquer une texture ... Mais je dois faire une erreur quelque part ...
Pour termine,r mon fichier .fx, qui la aussi est classique:
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 Texture2D texture2d; matrix World; matrix View; matrix Projection; SamplerState linearSampler { Filter = MIN_MAG_MIP_LINEAR; AddressU = Wrap; AddressV = Wrap; }; struct VS_IN { float4 position : POSITION; float4 color : COLOR; float2 UV: TEXCOORD; }; struct PS_IN { float4 position : SV_POSITION; float4 color : COLOR; float2 UV: TEXCOORD; }; PS_IN VS( VS_IN vertexShaderIn ) { PS_IN vertexShaderOut = (PS_IN)0; vertexShaderOut.position = mul( vertexShaderIn.position, World ); vertexShaderOut.position = mul( vertexShaderOut.position, View ); vertexShaderOut.position = mul( vertexShaderOut.position, Projection ); vertexShaderOut.color = vertexShaderIn.color; vertexShaderOut.UV = vertexShaderIn.UV; return vertexShaderOut; } float4 PS( PS_IN pixelShaderIn ) : SV_Target { float4 finalColor = texture2d.Sample( linearSampler, pixelShaderIn.UV ); return finalColor; } technique10 Render { pass P0 { SetGeometryShader( 0 ); SetVertexShader( CompileShader( vs_4_0, VS() ) ); SetPixelShader( CompileShader( ps_4_0, PS() ) ); } }Voila, j'ai pas mis tout le code, seulement ce qui, je pense, peut être utile ... J'pense que j'ai fait une erreur de débutant assez grossière quelque part, mais je sais pas ou
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using PrototypeV4.Modele; using PrototypeV4.GUI; using SlimDX; using SlimDX.DXGI; using SlimDX.Windows; using SlimDX.D3DCompiler; using SlimDX.Direct3D10; using System.Runtime.InteropServices; namespace PrototypeV4.Draw { [StructLayout(LayoutKind.Sequential)] struct VertexMap { public Vector4 PositionRhw; public Vector4 Color; public Vector2 UV; } class D_Map { private ViewPanel target; private M_Map modele; private Effect effect; protected EffectTechnique technique; protected EffectPass pass; private List<Vector4> vertexPositions; private List<Vector2> textureCoords; private List<Vector4> vertexColors; private SlimDX.Direct3D10.Buffer vertices; private InputLayout layout; private float mapWidth; private float mapHeight; private EffectMatrixVariable worldVariable = null, viewVariable = null, projectionVariable = null; private Matrix world; private SlimDX.Direct3D10.Device gpuDevice; public D_Map(ViewPanel target, M_Map modele) { this.target = target; this.modele = modele; mapHeight = modele.Height; mapWidth = modele.Width; gpuDevice= PrototypeV4.GraphicEngine.GPUDevice.GpuDevice; world = Matrix.Identity; } public void init() { vertexPositions = new List<Vector4>(); textureCoords = new List<Vector2>(); vertexColors = new List<Vector4>(); effect = Effect.FromFile(gpuDevice, "fx/D_Map.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(); Texture2D texture = Texture2D.FromFile(gpuDevice, modele.FilePath); EffectResourceVariable shaderTexture = effect.GetVariableByName("texture2d").AsResource(); ShaderResourceView textureView = new ShaderResourceView(gpuDevice, texture); shaderTexture.SetResource(textureView); InputElement[] inputElements = new InputElement[] { new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 16, 0), new InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 32, 0) }; layout = new InputLayout(gpuDevice, pass.Description.Signature, inputElements); float ratio = 1 / (mapHeight / mapWidth); ratio = 100; vertexPositions.Add(new Vector4(-1.0f * ratio, -1.0f * ratio, 0.0f, 1.0f)); vertexPositions.Add(new Vector4(-1.0f * ratio, 1.0f * ratio, 0.0f, 1.0f)); vertexPositions.Add(new Vector4(1.0f * ratio, -1.0f * ratio, 0.0f, 1.0f)); vertexPositions.Add(new Vector4(1.0f * ratio, 1.0f * ratio, 0.0f, 1.0f)); textureCoords.Add(new Vector2(0.0f, 1.0f)); textureCoords.Add(new Vector2(0.0f, 0.0f)); textureCoords.Add(new Vector2(1.0f, 1.0f)); textureCoords.Add(new Vector2(1.0f, 0.0f)); vertexColors.Add(new Vector4(1.0f, 0.0f, 0.0f, 1.0f)); vertexColors.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f)); vertexColors.Add(new Vector4(0.0f, 0.0f, 1.0f, 1.0f)); vertexColors.Add(new Vector4(1.0f, 0.0f, 1.0f, 1.0f)); DataStream stream = new DataStream(vertexPositions.Count * Marshal.SizeOf(typeof(VertexMap)), true, true); for (int i = 0; i < vertexPositions.Count; i++) { stream.Write(vertexPositions[i]); stream.Write(vertexColors[i]); stream.Write(textureCoords[i]); } stream.Position = 0; BufferDescription bufferDescription = new BufferDescription(); bufferDescription.BindFlags = BindFlags.VertexBuffer; bufferDescription.CpuAccessFlags = CpuAccessFlags.None; bufferDescription.OptionFlags = ResourceOptionFlags.None; bufferDescription.SizeInBytes = vertexPositions.Count * Marshal.SizeOf(typeof(VertexMap)); bufferDescription.Usage = ResourceUsage.Default; vertices = new SlimDX.Direct3D10.Buffer(gpuDevice, stream, bufferDescription); stream.Close(); // create the vertex layout and buffer // configure the Input Assembler portion of the pipeline with the vertex data } public void run() { worldVariable.SetMatrix(world); viewVariable.SetMatrix(target.Camera.ViewMatrix); projectionVariable.SetMatrix(target.Camera.ProjectionMatrix); gpuDevice.InputAssembler.SetInputLayout(layout); gpuDevice.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip); gpuDevice.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Marshal.SizeOf(typeof(VertexMap)), 0)); for (int p = 0; p < technique.Description.PassCount; p++) { pass.Apply(); gpuDevice.Draw(4, 0); } } public void reinit() { Texture2D texture = Texture2D.FromFile(gpuDevice, modele.FilePath); EffectResourceVariable shaderTexture = effect.GetVariableByName("texture2d").AsResource(); ShaderResourceView textureView = new ShaderResourceView(gpuDevice, texture); shaderTexture.SetResource(textureView); } } }
Bref, toute aide me serait utileEt toute critique aussi ! Parce que j'ai très peu de connaissance théoriques sur le sujet, donc j'ai un peu construit mon logiciel au "feeling" sans appliquer les bonnes pratiques ...
Ps: Si quelqu'un a un titre plus approprié pour mon problème aussi je suis preneur![]()
Partager