Dust3D开源项目分析——渲染与材质部分 | 材质初始化

 2021SC@SDUSC

读取快照信息,并从快照中初始化材质纹理。

头文件 material.h

#ifndef DUST3D_MATERIAL_H
#define DUST3D_MATERIAL_H
#include <QImage>
#include <QUuid>
#include "texturetype.h"
#include "snapshot.h"

struct MaterialTextures
{
    const QImage *textureImages[(int)TextureType::Count - 1] = {nullptr};
};

void initializeMaterialTexturesFromSnapshot(const Snapshot &snapshot,
    const QUuid &materialId,
    MaterialTextures &materialTextures,
    float &tileScale);

#endif

使用了一个结构体数据MaterialTextures,用QImage类型数组存储材质贴图。

声明了一个从快照初始化材质纹理的函数initializeMaterialTexturesFromSnapshot()。

material.cpp

#include "material.h"
#include "imageforever.h"
#include "util.h"

void initializeMaterialTexturesFromSnapshot(const Snapshot &snapshot,
    const QUuid &materialId,
    MaterialTextures &materialTextures,
    float &tileScale)
{
    QString materialIdString = materialId.toString();
    for (const auto &materialItem: snapshot.materials) {
        if (materialIdString != valueOfKeyInMapOrEmpty(materialItem.first, "id"))
            continue;
        for (const auto &layer: materialItem.second) {
            //FIXME: Only support one layer currently
            auto findTileScale = layer.first.find("tileScale");
            if (findTileScale != layer.first.end())
                tileScale = findTileScale->second.toFloat();
            for (const auto &mapItem: layer.second) {
                auto textureType = TextureTypeFromString(valueOfKeyInMapOrEmpty(mapItem, "for").toUtf8().constData());
                if (textureType != TextureType::None) {
                    int index = (int)textureType - 1;
                    if (index >= 0 && index < (int)TextureType::Count - 1) {
                        if ("imageId" == valueOfKeyInMapOrEmpty(mapItem, "linkDataType")) {
                            auto imageIdString = valueOfKeyInMapOrEmpty(mapItem, "linkData");
                            materialTextures.textureImages[index] = ImageForever::get(QUuid(imageIdString));
                        }
                    }
                }
            }
            break;
        }
        break;
    }
}

这里是函数initializeMaterialTexturesFromSnapshot()的具体实现。遍历快照下的snapshot.materials的每一层,读取材质类型信息并把纹理拷贝到出来。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值