From 0d42af920074ee7847e50fab0988f078dd8e7b31 Mon Sep 17 00:00:00 2001 From: wuyize Date: Mon, 13 Mar 2023 13:46:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BD=A9=E7=BB=98=E7=BA=B9?= =?UTF-8?q?=E7=90=86=E9=83=A8=E5=88=86=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res/Shaders/painting.comp | 2 +- .../src/Renderer/Mesh.h | 9 ++- .../src/Renderer/Model.cpp | 68 +++++++++++-------- .../src/Renderer/Model.h | 6 +- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/ArchitectureColoredPainting/res/Shaders/painting.comp b/ArchitectureColoredPainting/res/Shaders/painting.comp index ffd0e38..e7280a0 100644 --- a/ArchitectureColoredPainting/res/Shaders/painting.comp +++ b/ArchitectureColoredPainting/res/Shaders/painting.comp @@ -1190,7 +1190,7 @@ void main() //imageStore(gBaseColor, pixelLocation, vec4(uv,1,1)); //imageStore(gMetallicRoughness, pixelLocation, vec4(uv,1,1)); //return; - uv = vec2(1)-uv*2; + uv = uv*2-vec2(1); //vec2 uv = imageLoad(gPaintingTexCoord, pixelLocation).rg; vec3 debugBVH = vec3(0); diff --git a/ArchitectureColoredPainting/src/Renderer/Mesh.h b/ArchitectureColoredPainting/src/Renderer/Mesh.h index dd1d705..ec452fd 100644 --- a/ArchitectureColoredPainting/src/Renderer/Mesh.h +++ b/ArchitectureColoredPainting/src/Renderer/Mesh.h @@ -2,23 +2,22 @@ #include #include #include -#include -#include #include #include #include #include #include #include +#include #include "Drawable.h" namespace Renderer { struct Vertex { - QVector3D Position; - QVector3D Normal; - QVector2D TexCoords; + glm::vec3 Position; + glm::vec3 Normal; + glm::vec2 TexCoords; Vertex(const aiVector3D& position, const aiVector3D& Normal, const aiVector3D& TexCoords); }; diff --git a/ArchitectureColoredPainting/src/Renderer/Model.cpp b/ArchitectureColoredPainting/src/Renderer/Model.cpp index a22be70..644863d 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Model.cpp @@ -50,7 +50,7 @@ void Renderer::Model::loadModel(QString path) directory = modelFile.dir(); Assimp::Importer importer; - const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate | aiProcess_FlipUVs); + const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate /*| aiProcess_FlipUVs*/); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString() << endl; @@ -117,12 +117,8 @@ void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4) std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model) { - aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; QMatrix4x4 modelQ((float*)&model); - aiString str; - material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); - std::vector vertices; for (unsigned int i = 0; i < mesh->mNumVertices; i++) { @@ -139,45 +135,59 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, maxZ = std::max(maxZ, worldPos.z); } } + std::vector indices; for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++) indices.insert(indices.end(), face->mIndices, face->mIndices + face->mNumIndices); - - if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end()) + aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; + + if (auto iter = paintingMap.find([&] { + aiString str; + material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); + return std::string(str.C_Str()); + }()); paintingProgram != nullptr && iter != paintingMap.end()) { + qDebug() << iter->first.c_str() << "Replaced"; + + auto mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); + auto& [paintingPath, leftBottom, rightTop] = iter->second; - qDebug() << str.C_Str() << "Replaced"; - auto m_mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); - m_mesh->vertices = vertices; - m_mesh->indices = indices; - - m_mesh->paintingId = loadPainting(paintingPath); - auto& handle = vtManager->getPaintingHandle(m_mesh->paintingId); - m_mesh->textureBasecolor = handle.baseColor; - m_mesh->textureMetallicRoughness = handle.metallicRoughness; - m_mesh->setupMesh(); - return m_mesh; + for (auto& v : vertices) + { + //qDebug() << v.TexCoords.x << v.TexCoords.y; + v.TexCoords = (v.TexCoords - leftBottom) / (rightTop - leftBottom); + qDebug() << v.TexCoords.x << v.TexCoords.y; + } + + mesh->vertices = vertices; + mesh->indices = indices; + + mesh->paintingId = loadPainting(paintingPath); + auto& handle = vtManager->getPaintingHandle(mesh->paintingId); + mesh->textureBasecolor = handle.baseColor; + mesh->textureMetallicRoughness = handle.metallicRoughness; + mesh->setupMesh(); + return mesh; } else { - auto m_mesh = std::make_unique(glFunc, shaderProgram, shadowProgram, modelQ); - m_mesh->vertices = vertices; - m_mesh->indices = indices; + auto mesh = std::make_unique(glFunc, shaderProgram, shadowProgram, modelQ); + mesh->vertices = vertices; + mesh->indices = indices; - // 处理材质 - if (!(m_mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) + if (!(mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) qWarning() << "Basecolor Texture Loading Failed!"; - if (!(m_mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) + if (!(mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) qWarning() << "MetallicRoughness Texture Loading Failed!"; - if (!(m_mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) + if (!(mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) qWarning() << "Normal Texture Loading Failed!"; - if (m_mesh->textureBasecolor && m_mesh->textureMetallicRoughness && m_mesh->textureNormal) + if (mesh->textureBasecolor && mesh->textureMetallicRoughness && mesh->textureNormal) { - m_mesh->setupMesh(); - return m_mesh; + mesh->setupMesh(); + return mesh; } else return nullptr; @@ -208,7 +218,7 @@ GLuint Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type) texture.setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat); texture.setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat); texture.setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::Linear); - texture.setData(data); + texture.setData(data.mirrored()); return texture.textureId(); } diff --git a/ArchitectureColoredPainting/src/Renderer/Model.h b/ArchitectureColoredPainting/src/Renderer/Model.h index c19fdf3..d9a6bea 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.h +++ b/ArchitectureColoredPainting/src/Renderer/Model.h @@ -24,7 +24,11 @@ namespace Renderer QOpenGLShaderProgram* shadowProgram = nullptr; VirtualTextureManager* vtManager = nullptr; - std::unordered_map> paintingMap; + /** + * @param key BaseColor路径 + * @param value json路径, 纹理坐标 + */ + std::unordered_map> paintingMap; std::unordered_map paintingLoaded; std::unordered_map texturesLoaded; std::vector> meshes;