打开模型时卸载现有模型

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>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</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>
<widget class="Renderer::RendererGLWidget" name="openGLWidget">
<property name="sizePolicy">

View File

@ -18,7 +18,7 @@ namespace Renderer
glm::vec3 Position;
glm::vec3 Normal;
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

View File

@ -32,14 +32,12 @@ Model::Model(QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram,
}
void Model::draw() {
//shaderProgram->bind();
for (auto& mesh : meshes) {
mesh->draw();
}
}
void Model::drawShadow() {
//shaderProgram->bind();
for (auto& mesh : meshes) {
mesh->drawShadow();
}
@ -55,7 +53,7 @@ void Renderer::Model::loadModel(QString path)
const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString() << endl;
qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString();
return;
}
qDebug() << modelFile.absoluteFilePath() << "Loaded Successfully";
@ -63,9 +61,11 @@ void Renderer::Model::loadModel(QString path)
qDebug() << "NumMaterials: " << scene->mNumMaterials;
qDebug() << "NumTextures: " << scene->mNumTextures;
unloadModel();
if (QFile paintingConfigFile(directory.filePath(name + ".txt")); paintingConfigFile.open(QFile::ReadOnly | QIODevice::Text))
{
paintingMap.clear();
QTextStream stream(&paintingConfigFile);
while (!stream.atEnd())
{
@ -81,6 +81,30 @@ void Renderer::Model::loadModel(QString path)
{
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();
maxX = std::numeric_limits<float>::min();
@ -88,17 +112,6 @@ void Renderer::Model::loadModel(QString path)
maxY = std::numeric_limits<float>::min();
minZ = std::numeric_limits<float>::max();
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)

View File

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

View File

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

View File

@ -76,7 +76,7 @@ Renderer::VirtualTextureManager::VirtualTextureManager(GladGLContext* glMain)
pageIdBufferMutex.lock();
currentBuffer = 1 - currentBuffer;
pageIdBufferMutex.unlock();
gl->BeginQuery(GL_TIME_ELAPSED, pageLoadTimeQuery);
updatePages(pageIds);
gl->EndQuery(GL_TIME_ELAPSED);
@ -152,8 +152,26 @@ std::uint16_t Renderer::VirtualTextureManager::createVirtualTexture(Painting pai
}
}
paintings.emplace_back(baseColor, metallicRoughness, painting.buffers);
return paintings.size();
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);
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)

View File

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