How to draw a model in XNA using BasicEffect

I want to draw a model in XNA. I moved forward and released it in Blender and exported it to the fbx file format so that the content pipeline could work with it. What code should be added to the Draw () method of my WindowsGame ()? I tried the following, but all I get is a gray screen (gray, not blue, which is a clear color, mind you). The model is imported with content. Download, and it does not cause errors, and I called it "Bowl".

Can someone tell me why this will not work here?

protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); BasicEffect b = new BasicEffect (graphics.GraphicsDevice, new EffectPool ( )); foreach (ModelMesh m in Bowl.Meshes) { b.View = Cam.mView; b.Projection = Cam.mProj; b.World = mWorld; b.EnableDefaultLighting ( ); b.Begin ( ); m.Draw ( ); b.End ( ); } base.Draw(gameTime); } 

I just noticed that this is equivalent to killing in terms of efficiency, but I tried so many things, I just need it to work before I optimize it.

+4
source share
1 answer

In fact, a common problem when you first try to do something is that the camera does not look at what you think it is looking at. Another possible problem is that the model is not at the scale you expect. So, for example, if the camera has 5 units back to z, but the model has a width of 10 units, your camera is effectively inside the model.

Regarding the rendering issue, Microsoft has some pretty good docs about this: http://msdn.microsoft.com/en-us/library/bb203933.aspx

This snippet can be used as an assistant:

 private void DrawModel(Model m) { Matrix[] transforms = new Matrix[m.Bones.Count]; float aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height; m.CopyAbsoluteBoneTransformsTo(transforms); Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom), Vector3.Zero, Vector3.Up); foreach (ModelMesh mesh in m.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.View = view; effect.Projection = projection; effect.World = gameWorldRotation * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Position); } mesh.Draw(); } } 
+5
source

Source: https://habr.com/ru/post/1285974/


All Articles