如何使用 BasicEffect 在 XNA 中绘制模型

发布于 2024-07-22 22:31:43 字数 881 浏览 5 评论 0原文

我想在 XNA 中画一个模型。 我已经在 Blender 中生成了它,并将其导出为 fbx 文件格式,以便内容管道可以使用它。 我应该将哪些代码添加到 WindowsGame() 的 Draw() 方法中? 我已经尝试过以下方法,但我得到的只是一个灰色屏幕(请注意,灰色不是蓝色,这是清晰的颜色)模型是通过 content.Load 导入的,这不会引发任何错误,我将其称为“碗” 。

谁能告诉我为什么这在这里不起作用?

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 want to draw a Model in XNA. I've gona ahead and produced it in Blender and Exported it to fbx File Format so the Content Pipeline can work with it. What code should I add to the Draw() method of my WindowsGame() ? I've tried the Following but All I get is a gray screen (Gray not Blue, which is the clear color, mind you) The Model is Imported with content.Load, and this throws no error, and I called it "Bowl".

Can Anyone tell my why this here won't work?

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've just noticed that this is equivalent to murder in terms of efficiency but I've tried so many things, I just need it to work before I optimize it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2024-07-29 22:31:43

第一次尝试渲染某些内容时,一个非常常见的问题是相机没有看到您认为它看到的东西。 另一个可能的问题是模型的规模不符合您的预期。 例如,如果相机在 z 轴上向后 5 个单位,但模型宽 10 个单位,则相机实际上位于模型内部。

就渲染问题而言,微软对此有很好的文档: http: //msdn.microsoft.com/en-us/library/bb203933.aspx

您可以使用此代码段作为帮助器:

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();
    }
}

A really common issue when first trying to render something is that the camera isn't looking at what you think it's looking at. Another possible issue is that the model is not at a scale that you'd expect. So for example, if the camera is 5 units back on the z, but the model is 10 units wide, your camera is effectively inside the model.

As far as the rendering question goes, Microsoft has pretty good docs on this: http://msdn.microsoft.com/en-us/library/bb203933.aspx

You can use this snippet as a helper:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文