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的每一层,读取材质类型信息并把纹理拷贝到出来。