C# XNA: draw multiple 3D cubes

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello
Basically what I wanna do is to draw multiple cubes in XNA. I read the geometry information from database using SQL, and convert them to vectors for the cubes size and position, and then plot them in "protected override void Draw" however to some reason
it does not work.
The codes work when I show only one cube.

Here is the codes in draw method: (in my codes, I call "cube" as "module")
"Modulesize" here is a list of vector3, and "ModuleLocation" is also a list of vector3.

protected override void Draw(GameTime gameTime)<br/>
{<br/>
GraphicsDevice.Clear(Color.CornflowerBlue);<br/>
<br/>
//**********************************ModuleDrawing*******************************<br/>
<br/>
// Set the View matrix which defines the camera and what its looking at<br/>
cubeEffect.View = Matrix.CreateLookAt(cameraPosition, modelPosition, Vector3.Up);<br/>
<br/>
<br/>
//Draw the modules<br/>
for (int i = 0; i<ModuleSize.Count; i++)<br/>
{<br/>
// Set the World matrix which defines the position of the cube<br/>
cubeEffect.World = Matrix.CreateRotationY(MathHelper.ToRadians(rotation)) *<br/>
Matrix.CreateRotationX(MathHelper.ToRadians(rotation)) * Matrix.CreateTranslation(ModuleLocation);<br/>
<br/>
// Set the Projection matrix which defines how we see the scene (Field of view)<br/>
cubeEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 3f, 1.0f, 1000.0f);<br/>
// Enable textures on the Cube Effect. this is necessary to texture the model<br/>
cubeEffect.TextureEnabled = false;<br/>
//cubeEffect.Texture = cubeTexture (optional)<br/>
// Enable some pretty lights<br/>
cubeEffect.EnableDefaultLighting();<br/>
MyCube = new Cube(ModuleSize, ModuleLocation);<br/>
// apply the effect and render the cube<br/>
foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)<br/>
{<br/>
pass.Apply();<br/>
MyCube.RenderShape(GraphicsDevice);<br/>
}<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
base.Draw(gameTime);<br/>
}




View the full article
 
Back
Top