打开模型时卸载现有模型

dev-LayerStyle
wuyize 2023-03-18 12:17:04 +08:00
parent c15e8c3a5b
commit 31f2c1be8f
7 changed files with 74 additions and 24 deletions

View File

@ -84,10 +84,26 @@
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0"> <layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum> <enum>QLayout::SetDefaultConstraint</enum>
</property> </property>
<item>
<widget class="QListWidget" name="textureListWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>0</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item> <item>
<widget class="Renderer::RendererGLWidget" name="openGLWidget"> <widget class="Renderer::RendererGLWidget" name="openGLWidget">
<property name="sizePolicy"> <property name="sizePolicy">

View File

@ -18,7 +18,7 @@ namespace Renderer
glm::vec3 Position; glm::vec3 Position;
glm::vec3 Normal; glm::vec3 Normal;
glm::vec2 TexCoords; glm::vec2 TexCoords;
Vertex(const aiVector3D& position, const aiVector3D& Normal, const aiVector3D& TexCoords); Vertex(const aiVector3D& position, const aiVector3D& normal, const aiVector3D& texCoords);
}; };
struct Texture struct Texture

View File

@ -32,14 +32,12 @@ Model::Model(QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram,
} }
void Model::draw() { void Model::draw() {
//shaderProgram->bind();
for (auto& mesh : meshes) { for (auto& mesh : meshes) {
mesh->draw(); mesh->draw();
} }
} }
void Model::drawShadow() { void Model::drawShadow() {
//shaderProgram->bind();
for (auto& mesh : meshes) { for (auto& mesh : meshes) {
mesh->drawShadow(); mesh->drawShadow();
} }
@ -55,7 +53,7 @@ void Renderer::Model::loadModel(QString path)
const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate); const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{ {
qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString() << endl; qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString();
return; return;
} }
qDebug() << modelFile.absoluteFilePath() << "Loaded Successfully"; qDebug() << modelFile.absoluteFilePath() << "Loaded Successfully";
@ -63,9 +61,11 @@ void Renderer::Model::loadModel(QString path)
qDebug() << "NumMaterials: " << scene->mNumMaterials; qDebug() << "NumMaterials: " << scene->mNumMaterials;
qDebug() << "NumTextures: " << scene->mNumTextures; qDebug() << "NumTextures: " << scene->mNumTextures;
unloadModel();
if (QFile paintingConfigFile(directory.filePath(name + ".txt")); paintingConfigFile.open(QFile::ReadOnly | QIODevice::Text)) if (QFile paintingConfigFile(directory.filePath(name + ".txt")); paintingConfigFile.open(QFile::ReadOnly | QIODevice::Text))
{ {
paintingMap.clear();
QTextStream stream(&paintingConfigFile); QTextStream stream(&paintingConfigFile);
while (!stream.atEnd()) while (!stream.atEnd())
{ {
@ -82,23 +82,36 @@ void Renderer::Model::loadModel(QString path)
qWarning() << "Painting Config Not Found!"; qWarning() << "Painting Config Not Found!";
} }
aiMatrix4x4 transform;
aiMatrix4x4::Scaling(aiVector3D(1 / 0.008), transform);
processNode(scene->mRootNode, scene, transform * scene->mRootNode->mTransformation);
AABB.clear();
AABB.emplace_back(minX, minY, minZ);
AABB.emplace_back(minX, minY, maxZ);
AABB.emplace_back(minX, maxY, minZ);
AABB.emplace_back(minX, maxY, maxZ);
AABB.emplace_back(maxX, minY, minZ);
AABB.emplace_back(maxX, minY, maxZ);
AABB.emplace_back(maxX, maxY, minZ);
AABB.emplace_back(maxX, maxY, maxZ);
}
void Renderer::Model::unloadModel()
{
paintingMap.clear();
for(auto& i: paintingLoaded)
vtManager->deleteVirtualTexture(i.second);
paintingLoaded.clear();
texturesLoaded.clear();
meshes.clear();
minX = std::numeric_limits<float>::max(); minX = std::numeric_limits<float>::max();
maxX = std::numeric_limits<float>::min(); maxX = std::numeric_limits<float>::min();
minY = std::numeric_limits<float>::max(); minY = std::numeric_limits<float>::max();
maxY = std::numeric_limits<float>::min(); maxY = std::numeric_limits<float>::min();
minZ = std::numeric_limits<float>::max(); minZ = std::numeric_limits<float>::max();
maxZ = std::numeric_limits<float>::min(); maxZ = std::numeric_limits<float>::min();
aiMatrix4x4 transform;
aiMatrix4x4::Scaling(aiVector3D(1 / 0.008), transform);
processNode(scene->mRootNode, scene, transform * scene->mRootNode->mTransformation);
AABB.push_back(QVector3D(minX, minY, minZ));
AABB.push_back(QVector3D(minX, minY, maxZ));
AABB.push_back(QVector3D(minX, maxY, minZ));
AABB.push_back(QVector3D(minX, maxY, maxZ));
AABB.push_back(QVector3D(maxX, minY, minZ));
AABB.push_back(QVector3D(maxX, minY, maxZ));
AABB.push_back(QVector3D(maxX, maxY, minZ));
AABB.push_back(QVector3D(maxX, maxY, maxZ));
} }
void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4) void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4)

View File

@ -15,6 +15,7 @@ namespace Renderer
void draw(); void draw();
void drawShadow(); void drawShadow();
void loadModel(QString path); void loadModel(QString path);
void unloadModel();
Model(QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram, QOpenGLShaderProgram* paintingProgram, QOpenGLShaderProgram* shadowProgram, VirtualTextureManager* vtManager); Model(QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram, QOpenGLShaderProgram* paintingProgram, QOpenGLShaderProgram* shadowProgram, VirtualTextureManager* vtManager);
private: private:
QOpenGLContext* context = nullptr; QOpenGLContext* context = nullptr;
@ -25,8 +26,8 @@ namespace Renderer
VirtualTextureManager* vtManager = nullptr; VirtualTextureManager* vtManager = nullptr;
/** /**
* @param key BaseColor * @brief key BaseColor \n
* @param value json, * value json,
*/ */
std::unordered_map<std::string, std::tuple<std::string, glm::vec2, glm::vec2>> paintingMap; std::unordered_map<std::string, std::tuple<std::string, glm::vec2, glm::vec2>> paintingMap;
std::unordered_map<std::string, GLuint> paintingLoaded; std::unordered_map<std::string, GLuint> paintingLoaded;

View File

@ -13,6 +13,7 @@ namespace Renderer
~RendererWidget(); ~RendererWidget();
public slots: public slots:
void currentTabChanged(int index); void currentTabChanged(int index);
private: private:
Ui::RendererWidgetClass ui; Ui::RendererWidgetClass ui;
}; };

View File

@ -152,8 +152,26 @@ std::uint16_t Renderer::VirtualTextureManager::createVirtualTexture(Painting pai
} }
} }
if (const auto it = std::find_if(paintings.begin(), paintings.end(), [](const PaintingHandle& i) { return i.baseColor == 0; });
it != paintings.end())
{
*it = { baseColor, metallicRoughness, painting.buffers };
return it - paintings.begin() + 1;
}
else
{
paintings.emplace_back(baseColor, metallicRoughness, painting.buffers); paintings.emplace_back(baseColor, metallicRoughness, painting.buffers);
return paintings.size(); return paintings.size();
}
}
void Renderer::VirtualTextureManager::deleteVirtualTexture(std::uint16_t id)
{
auto& painting = getPaintingHandle(id);
glMain->DeleteTextures(2, (GLuint*)&painting);
painting.baseColor = 0;
painting.metallicRoughness = 0;
} }
Renderer::PaintingHandle& Renderer::VirtualTextureManager::getPaintingHandle(std::uint16_t id) Renderer::PaintingHandle& Renderer::VirtualTextureManager::getPaintingHandle(std::uint16_t id)

View File

@ -24,11 +24,12 @@ namespace Renderer
VirtualTextureManager(GladGLContext* glMain); VirtualTextureManager(GladGLContext* glMain);
/** /**
* @brief * @brief
* @param painting * @param painting
* @return id * @return id
*/ */
std::uint16_t createVirtualTexture(Painting painting); std::uint16_t createVirtualTexture(Painting painting);
void deleteVirtualTexture(std::uint16_t id);
PaintingHandle& getPaintingHandle(std::uint16_t id); PaintingHandle& getPaintingHandle(std::uint16_t id);
void tryUpdatePages(const std::vector<glm::u16vec2>& pageIds); void tryUpdatePages(const std::vector<glm::u16vec2>& pageIds);