Bonsoir tous le monde,
je touche a xna depuis un moment et après avoir jouer avec la 2D et fait quelque proto de jeux de plateforme et de rpg, j'ai enfin décider de passer a la 3d.
Pour accompagner mon apprentissage et en même temps étudier un sujet qui me tenter j'ai décider de faire un petit moteur 3d a base de voxel.
Mais voila je rencontre un problème a un niveau vraiment basique et l'erreur doit elle même être toute bête car je ne la voie pas.
J'ai un game component qui attention . . . est a camera !! Et une classe Cube qui me permet de construire et afficher un cube (). L'affichage d 'un cube se passe bien je controle sa position sa taille et sa couleur.
Ma camera peut se déplacer en avant en arriéré et pivoter ( yaw pitch et roll)
J'en suis donc au point ou il me faut organiser mes voxel en chunk mais avant ça je souhaite afficher une matrice tridimensionnel de voxel histoire de verifier que cela fonctionne au poile et bien sur sa fonctionne pas du tout !
Je n'est qu'un seul cube qui s'affiche.
Voila mon code :
Camera.cs
Cube.cs
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 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace VoxelLib { /// <summary> /// Le GameComponent servant a instancier la camera. /// </summary> public class Camera : Microsoft.Xna.Framework.GameComponent { /// <summary> /// La matrice contenant les details de positionnement /// et de direction de la camera. /// </summary> private Matrix viewMat; /// <summary> /// La matrice contenant les detail de la vue capturée par la camera. /// </summary> private Matrix projection; private MouseState prevMouseState; private Vector3 cameraPos, cameraDirection, cameraUpVector; private float speed = 1; public Matrix ViewMat { get { return viewMat; } set { viewMat = value; } } public Matrix Projection { get { return projection; } set { projection = value; } } public Camera(Game game, Vector3 cameraPos, Vector3 cameraTarget, Vector3 cameraUpVector) : base(game) { this.cameraPos = cameraPos; this.cameraDirection = cameraTarget - cameraPos; this.cameraDirection.Normalize(); this.cameraUpVector = cameraUpVector; CreateLookAt(); projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height, 1, 10000); } private void CreateLookAt() { this.viewMat = Matrix.CreateLookAt(cameraPos, cameraPos + cameraDirection, cameraUpVector); } /// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public override void Initialize() { //set mouse position and do initial get state Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2); prevMouseState = Mouse.GetState(); base.Initialize(); } /// <summary> /// Allows the game component to update itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> public override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Z)) { cameraPos += new Vector3(cameraDirection.X, 0 , cameraDirection.Z) * speed; } else if (Keyboard.GetState().IsKeyDown(Keys.S)) { cameraPos -= /*cameraDirection*/new Vector3(cameraDirection.X, 0, cameraDirection.Z) * speed; } else if (Keyboard.GetState().IsKeyDown(Keys.Q)) { cameraPos += Vector3.Cross(cameraUpVector, cameraDirection) * speed; } else if (Keyboard.GetState().IsKeyDown(Keys.D)) { cameraPos -= Vector3.Cross(cameraUpVector, cameraDirection) * speed; } //Yaw cameraDirection = Vector3.Transform(cameraDirection, Matrix.CreateFromAxisAngle(cameraUpVector, (-MathHelper.PiOver4 / 150) * (Mouse.GetState().X - prevMouseState.X))); //Roll if (Mouse.GetState().LeftButton == ButtonState.Pressed) { cameraUpVector = Vector3.Transform(cameraUpVector, Matrix.CreateFromAxisAngle(cameraDirection, MathHelper.PiOver4 / 45)); } if (Mouse.GetState().RightButton == ButtonState.Pressed) { cameraUpVector = Vector3.Transform(cameraUpVector, Matrix.CreateFromAxisAngle(cameraDirection, -MathHelper.PiOver4 / 45)); } //Pitch cameraDirection = Vector3.Transform(cameraDirection, Matrix.CreateFromAxisAngle(Vector3.Cross(cameraUpVector, cameraDirection), (MathHelper.PiOver4 / 100) * (Mouse.GetState().Y - prevMouseState.Y))); /*cameraUpVector = Vector3.Transform(cameraUpVector, Matrix.CreateFromAxisAngle(Vector3.Cross(cameraUpVector, cameraDirection), (MathHelper.PiOver4 / 100) * (Mouse.GetState().Y - prevMouseState.Y)));*/ //reset prevMouseState prevMouseState = Mouse.GetState(); CreateLookAt(); base.Update(gameTime); } } }
Game1.cs
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace VoxelLib { public class Cube { const short TAILLE = 2; /*private Vector3 moveTrans;*/ private Matrix world; private Boolean drawned = false; Color color; private VertexPositionColor[] vertices; private int[] indices; private VertexBuffer vb; private IndexBuffer ib; private GraphicsDevice gd; private BasicEffect effect; public Cube(Color color) { this.color = color; } private void CreateIndexedCubeColored(Vector3 position) { this.vertices = new VertexPositionColor[] { new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE) ) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE) , - (position.Y * TAILLE) , - (position.Z * TAILLE)) ,color), /* new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , - (position.Y * TAILLE) *10, - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE) *10, - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , - (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3( (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , - (position.Z * TAILLE)) ,color), new VertexPositionColor( new Vector3(- (position.X * TAILLE)*10 , (position.Y * TAILLE)*10 , (position.Z * TAILLE)) ,color)*/ }; this.indices = new int[] { 0, 3, 2, 0, 2, 1, 4, 7, 6, 4,6, 5, 0, 1, 7,0,7, 9, 1, 2, 6, 1, 6, 3, 2, 3, 5,2,5, 6, 4, 5, 3,4,3, 0 }; //0 = 8 ; 21 ; //1 = 11; 12 ; //2 = 15 ; 16 ; //3 = 19 ; 22 //4 = 9 ; 4 ; 20 ; //5 = 18; 23 //6 = 14 ; 17 ; //7 = 10 ; 13 ; this.vb = new VertexBuffer(this.gd, typeof(VertexPositionColor), this.vertices.Length, BufferUsage.WriteOnly); vb.SetData(this.vertices); this.gd.SetVertexBuffer(this.vb); this.ib = new IndexBuffer(this.gd, IndexElementSize.ThirtyTwoBits, this.indices.Length, BufferUsage.WriteOnly); this.ib.SetData(this.indices); this.gd.Indices = this.ib; } public void Initialize(GraphicsDevice gd, Vector3 position) { this.gd = gd; this.CreateIndexedCubeColored(position); this.world = Matrix.Identity; } public void LoadContent() { this.effect = new BasicEffect(this.gd); } public void Update() { /* if (Keyboard.GetState().IsKeyDown(Keys.Z)) { this.moveTrans += new Vector3(0, (float)0.1, 0); } else if (Keyboard.GetState().IsKeyDown(Keys.S)){ this.moveTrans += new Vector3(0, (float)-0.1, 0); } else if (Keyboard.GetState().IsKeyDown(Keys.Q)){ this.moveTrans += new Vector3((float)-0.1, 0, 0); } else if (Keyboard.GetState().IsKeyDown(Keys.D)){ this.moveTrans += new Vector3((float)0.1, 0, 0); }*/ } public void Draw(Camera camera) { effect.View = camera.ViewMat; effect.Projection = camera.Projection; effect.World = world /* * Matrix.CreateTranslation(moveTrans)*/; effect.VertexColorEnabled = true; foreach (EffectPass pass in effect.CurrentTechnique.Passes) { pass.Apply(); this.gd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, this.vertices.Length, 0, this.indices.Length / 3); } drawned = true; } } }
Merci d'avance de toute aide apporté.
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 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using VoxelLib; namespace testVoxel { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Camera camera; Vector3 cameraPos = new Vector3(0, 0, 20); Vector3 cameraTarget = Vector3.Zero; Vector3 cameraUpVector = Vector3.Up; static short nb = 4; Cube[, ,] cube = new Cube[nb, nb, nb]; /*Cube cube1, cube2;*/ Color color = Color.Blue; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // initialisation de la camera camera = new Camera(this, cameraPos, cameraTarget, cameraUpVector); Components.Add(camera); int x, y, z; x = 0; y = 0; z = 0; for ( x = 0; x < nb; x++) { if (color == Color.Blue) { color = Color.Red; } else if (color == Color.Red) { color = Color.Blue; } for ( y = 0; y < nb; y++) { if (color == Color.Blue) { color = Color.Red; } else if (color == Color.Red) { color = Color.Blue; } for ( z = 0; z < nb; z++) { if (color == Color.Blue) { color = Color.Red; } else if (color == Color.Red) { color = Color.Blue; } cube[x,y,z] = new Cube(color); cube[x,y,z].Initialize(GraphicsDevice, new Vector3(x, y, z)); } } } /*cube1 = new Cube(Color.Blue); cube2 = new Cube(Color.Red); cube1.Initialize(GraphicsDevice, new Vector3(0, 0, 0)); cube2.Initialize(GraphicsDevice, new Vector3(1, 0, 0));*/ base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); for (int x = 0; x < nb; x++) { for (int y = 0; y < nb; y++) { for (int z = 0; z < nb; z++) { cube[x, y, z].LoadContent(); } } } /*cube1.LoadContent(); cube2.LoadContent();*/ } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } //cube.Update(); // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.White); for (int x = 0; x < nb; x++) { for (int y = 0; y < nb; y++) { for (int z = 0; z < nb; z++) { cube[x, y, z].Draw(camera); } } } /*cube1.Draw(camera); cube2.Draw(camera);*/ base.Draw(gameTime); } } }
Partager