简体   繁体   English

将高度图转换为网格以在WP7的XNA中渲染

[英]Converting a heightmap to mesh for rendering in XNA for WP7

As I'm sure most of you know WP7 doesn't support custom shader files which to me unless I'm missing something means that I can't draw anything from just vertex and indices data like I can on Windows, I need to use a Model either loaded in or parsed from other data types. 如我所知,大多数人都知道WP7不支持自定义着色器文件,除非我丢失了某些东西,否则我无法像Windows那样仅从顶点和索引数据中绘制任何内容,因此我需要使用从其他数据类型加载或解析的模型。

What I'm looking for is a method for taking a Model variable and heightmap bitmap (presumably loaded as a Texture2D) and create a vertex for each pixel based on the grey-scale value, it would also be nice to set the vertical scale from a function constructor variable as well. 我正在寻找的是一种用于获取Model变量和heightmap位图(大概作为Texture2D加载)并基于灰度值为每个像素创建一个顶点的方法,将垂直比例设置为一个函数构造函数变量。

For reference here is my function that draws the models in my game: 供参考的是我在游戏中绘制模型的函数:

protected void DrawMesh(Model myMesh, Vector3 meshPos, float meshRot)
{
    // Copy any parent transforms
    Matrix[] transforms = new Matrix[myMesh.Bones.Count];
    myMesh.CopyAbsoluteBoneTransformsTo(transforms);

    // Draw the model. A model can have multiple meshes, so loop.
    foreach (ModelMesh mesh in myMesh.Meshes)
    {
        // This is where the mesh orientation is set, as well 
        // as our camera and projection.
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();
            effect.View = camera.View;
            effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(MathHelper.ToRadians(meshRot)) * Matrix.CreateTranslation(meshPos);
            effect.Projection = camera.Projection;
        }
        // Draw the mesh, using the effects set above.
        mesh.Draw();

    }
}

Does anyone know how I can do this fairly simply? 有谁知道我可以很简单地做到这一点? I'd prefer not to over complicate things with multiple files yet before I understand the process. 在理解该过程之前,我不希望过多地使多个文件复杂化。

Thanks! 谢谢!

-Ryan -瑞安

First of all, a lack of custom shaders does not imply that you cannot draw with raw vertices/indices. 首先,缺少自定义着色器并不意味着您无法使用原始顶点/索引进行绘制。 In fact you can on WP7. 实际上您可以在WP7上使用。 What it implies is that you cannot control raw pixel output (pixel shader), or write code that changes each individual vertex's projection mapping to the screen (vertex shader). 这意味着您无法控制原始像素输出(像素着色器),也无法编写将每个顶点的投影映射更改到屏幕的代码(顶点着色器)。 There are other kinds of shaders too at various stages of the entire pipeline, but none of them have anything to do with telling the graphics card which vertices & indices to use. 在整个流水线的各个阶段也有其他种类的着色器,但是它们与告诉图形卡要使用的顶点和索引没有任何关系。 You do that yourself in your game code. 您可以自己在游戏代码中进行操作。 In fact they (or rather, data taken from them down the chain) are used as inputs to shaders (among other things). 实际上,它们(或者更确切地说是从链中获取的数据)被用作着色器的输入 (除其他外)。

That said, you can still generate vertices yourself, in game code. 也就是说,您仍然可以自己在游戏代码中生成顶点。 And even manipulate them too if you wish, to a certain degree. 甚至可以在一定程度上操纵它们。 Doesn't require any shaders to generate vertices & indices and draw them raw, even on WP7. 即使在WP7上,也不需要任何着色器即可生成顶点和索引并以原始方式绘制它们。 Do not use Model for this; 请勿为此使用Model that's overkill. 那太过分了。

You can use GetData on your texture to get its color values, and simply generate an array of vertices ( VertexPositionColor , or texture, or any other format you wish) based on the color value of each pixel. 您可以在纹理上使用GetData来获取其颜色值,并根据每个像素的颜色值简单地生成一个顶点数组( VertexPositionColor或texture或您希望的任何其他格式)。 There is a tutorial laying out everything you need to do here . 有一个教程铺设,你需要做的一切在这里 Almost everything there will work as-is on WP7 except the effect parameters; 除了效果参数外,几乎所有的东西都可以在WP7上按原样工作。 map those instead to a BasicEffect or the like. 将它们映射到BasicEffect等。 There are no custom shaders in it. 没有自定义着色器。

Post-script: You should read up on the basics of 3D graphics, in terms of what shaders do vs. what vertices are meant for, and really the whole pipeline, to get a better understanding. 脚本后:您应该了解3D图形的基础知识,以了解着色器的用途,顶点的含义以及整个流程,以便更好地理解。 There are plenty of good articles, blogs, and books out there. 那里有很多不错的文章,博客和书籍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM