【SoftwareRender三部曲】一.网格

一.简介

在这个系列讲述绘制图元主要是以三角形为主,我们通过顶点构成三角形,三角形构成整个模型的网格。如下:

每个顶点可以携带一些信息,比如位置、法向量、颜色、UV坐标等等。后面我会在Shader着色器篇章讲述这些信息的用处。
如何用顶点来组织一个网格呢?这里我们是使用的索引顶点。也就是顶点数据只存一份,网格的图元顶点仅仅使用顶点的索引来表示。这里有篇Ogl中网格顶点组织结构的介绍


二.顶点结构

根据上面所述的顶点属性,我们可以简单的将顶点结构定义为如下:

//顶点信息 包括坐标,颜色,纹理坐标,法线等等
class VertexIn
{
public:

    Vector4 pos;    //顶点位置
    Vector4 color;  //顶点颜色
    Vector2 tex;    //纹理坐标
    Vector3 normal; //法线

    VertexIn() = default;
    VertexIn(Vector4 _pos, Vector4 _color, Vector2 _tex, Vector3 _normal)
        :pos(_pos), color(_color), tex(_tex), normal(_normal) {}
    VertexIn(const VertexIn& rhs) :pos(rhs.pos), color(rhs.color), tex(rhs.tex), normal(rhs.normal) {}
};


三.网格结构

根据上面的所述,我们知道网格结构一定有两个数组,一个是存储所有的顶点,一个是用于存储图元的顶点索引。代码如下:

class Mesh
{
public:
    Mesh() {}

    Mesh(const Mesh& mesh):vertices(mesh.vertices), indices(mesh.indices){}

    Mesh(Mesh&& mesh)
    {
        swap(vertices, std::move(mesh.vertices));
        swap(indices, std::move(mesh.indices));
    }

    Mesh& operator=(const Mesh& mesh)
    {
        if (&mesh == this)
        {
            return *this;
        }
        vertices = mesh.vertices;
        indices = mesh.indices;
        return *this;
    }

    Mesh& operator=(Mesh&& mesh)
    {
        swap(vertices, std::move(mesh.vertices));
        swap(indices, std::move(mesh.indices));
        return *this;
    }

    void SetVertexs(VertexIn* _vs, int count)
    {
        vertices.resize(count);
        new(&vertices[0])std::vector<VertexIn>(_vs, _vs + count);  //vertexs.std::vector<VertexIn>::vector(_vs, _vs + count);
    }

    void SetIndices(int* _es, int count)
    {
        indices.resize(count);
        new(&indices)std::vector<UINT>(_es, _es + count); //indices.std::vector<int>::vector(_es, _es + count);
    }

    std::vector<VertexIn> vertices;
    std::vector<UINT>  indices;
};


四.模型生成器

有了上面的基本结构,我们能够很好的组织我们需要绘制的模型数据,接下来我们需要提供一个工具用于生成一些常用的模型顶点数据。当然,对于哪些复杂的模型数据,一般都是由美术通过建模软件制作出来导出成模型文件,由程序读取模型文件解析得到顶点、贴图数据。不过这里我们只是使用一些简单的模型,比如正方形;所以我们直接在代码中设置顶点数据。具体代码如下:

class MeshGenerator
{
private:
    MeshGenerator();
    MeshGenerator(const MeshGenerator&);
    ~MeshGenerator() {}
public:
    static  Mesh CreateBox(real width, real height, real depth);
};

这里只贴出了头文件,具体的函数实现由于过长就不贴出来了,目前由于没有讲解Shader,所以可能对CreateBox这个方法中创建模型顶点数据的过程不是很能理解。等下一节,我们讲述了Shader(着色器),大家就会对这个方法中顶点属性的设置有所理解。



该章节涉及到代码:
Mesh.hMeshGenerator.hMeshGenerator.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值