From 262eb0e19fd92e457a50aca77d4ae5ed3f84f1ae Mon Sep 17 00:00:00 2001 From: wuyize Date: Tue, 4 Apr 2023 15:51:38 +0800 Subject: [PATCH] bug fix --- .../res/Shaders/model.frag | 24 ++++---- .../res/Shaders/shadow_mapping.comp | 3 + .../src/Editor/util/PaintingUtil.cpp | 2 +- .../src/Renderer/Mesh.cpp | 6 ++ .../src/Renderer/Mesh.h | 2 + .../src/Renderer/Model.cpp | 61 +++++++++---------- .../src/Renderer/Model.h | 3 +- .../src/Renderer/RendererGLWidget.cpp | 2 +- svg/线段.svg | 1 + 9 files changed, 58 insertions(+), 46 deletions(-) create mode 100644 svg/线段.svg diff --git a/ArchitectureColoredPainting/res/Shaders/model.frag b/ArchitectureColoredPainting/res/Shaders/model.frag index 97e724a..420b814 100644 --- a/ArchitectureColoredPainting/res/Shaders/model.frag +++ b/ArchitectureColoredPainting/res/Shaders/model.frag @@ -1,10 +1,15 @@ #version 450 core - - uniform sampler2D texture_basecolor; uniform sampler2D texture_metallic_roughness; uniform sampler2D texture_normal; +uniform bool texture_basecolor_available = false; +uniform bool texture_metallic_roughness_available = false; +uniform bool texture_normal_available = false; + +uniform vec3 baseColor; +uniform vec2 metallicRoughness; + layout (location = 0) out vec4 gBaseColor; layout (location = 1) out vec3 gNormal; layout (location = 2) out vec3 gPosition; @@ -15,8 +20,6 @@ in vec2 TexCoords; in vec3 WorldPos; in vec3 Normal; - - vec3 getNormalFromMap() { vec3 tangentNormal = texture(texture_normal, TexCoords).xyz * 2.0 - 1.0; @@ -36,22 +39,21 @@ vec3 getNormalFromMap() void main() { - if(textureSize(texture_basecolor,0)!=vec2(0)) + if(texture_basecolor_available) gBaseColor = texture(texture_basecolor, TexCoords); else - gBaseColor = vec4(1); + gBaseColor = vec4(baseColor, 1); if(gBaseColor.a<0.4) discard; gPosition = WorldPos; - if(textureSize(texture_normal,0)!=vec2(0)) + if(texture_normal_available) gNormal = getNormalFromMap(); else gNormal = Normal; - if(textureSize(texture_metallic_roughness,0)!=vec2(0)) + if(texture_metallic_roughness_available) gMetallicRoughness = texture(texture_metallic_roughness, TexCoords).bg; else - gMetallicRoughness = vec2(0,1); - - gPaintingIndex = uvec2(0); + gMetallicRoughness = metallicRoughness; + gPaintingIndex = uvec2(0); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/res/Shaders/shadow_mapping.comp b/ArchitectureColoredPainting/res/Shaders/shadow_mapping.comp index f65c5f6..a7410c9 100644 --- a/ArchitectureColoredPainting/res/Shaders/shadow_mapping.comp +++ b/ArchitectureColoredPainting/res/Shaders/shadow_mapping.comp @@ -204,6 +204,9 @@ void main() vec3 V = normalize(camPos - worldPos); + if(dot(V, normal)<0) + normal = -normal; + vec3 F0 = vec3(0.04); F0 = mix(F0, albedo, metallic); diff --git a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp index 5cbd956..93cf184 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp +++ b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp @@ -90,7 +90,7 @@ void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transfo //qDebug() << leafLayer<<"------" << painterPath; // transform to -1£¬ 1 QTransform trans; - double maxLen = std::max(bound.width(), bound.height()) * 1.00001; + double maxLen = std::max(bound.width(), bound.height()) * 1.00001 * 2; //qDebug() << maxLen << bound; trans.scale(1 / maxLen, 1 / maxLen); trans.translate(-bound.center().x(), -bound.center().y()); diff --git a/ArchitectureColoredPainting/src/Renderer/Mesh.cpp b/ArchitectureColoredPainting/src/Renderer/Mesh.cpp index 4af7520..59c0042 100644 --- a/ArchitectureColoredPainting/src/Renderer/Mesh.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Mesh.cpp @@ -28,6 +28,12 @@ void Mesh::draw() shaderProgram->setUniformValue("texture_basecolor", 0); shaderProgram->setUniformValue("texture_metallic_roughness", 1); shaderProgram->setUniformValue("texture_normal", 2); + shaderProgram->setUniformValue("texture_basecolor_available", textureBasecolor != 0); + shaderProgram->setUniformValue("texture_metallic_roughness_available", textureMetallicRoughness != 0); + shaderProgram->setUniformValue("texture_normal_available", textureNormal != 0); + shaderProgram->setUniformValue("model", model); + shaderProgram->setUniformValue("baseColor", baseColor); + shaderProgram->setUniformValue("metallicRoughness", metallicRoughness); QOpenGLVertexArrayObject::Binder bind(&VAO); shaderProgram->setUniformValue("model", model); diff --git a/ArchitectureColoredPainting/src/Renderer/Mesh.h b/ArchitectureColoredPainting/src/Renderer/Mesh.h index 35348bc..97d6243 100644 --- a/ArchitectureColoredPainting/src/Renderer/Mesh.h +++ b/ArchitectureColoredPainting/src/Renderer/Mesh.h @@ -43,6 +43,8 @@ namespace Renderer GLuint textureBasecolor = 0; GLuint textureMetallicRoughness = 0; GLuint textureNormal = 0; + QVector3D baseColor; + QVector2D metallicRoughness; QMatrix4x4 model; QOpenGLFunctions_4_5_Core* glFunc; diff --git a/ArchitectureColoredPainting/src/Renderer/Model.cpp b/ArchitectureColoredPainting/src/Renderer/Model.cpp index 48382cb..87e2253 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Model.cpp @@ -88,14 +88,14 @@ void Renderer::Model::loadModel(QString path) 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); + AABB.emplace_back(minPos.x, minPos.y, minPos.z); + AABB.emplace_back(minPos.x, minPos.y, maxPos.z); + AABB.emplace_back(minPos.x, maxPos.y, minPos.z); + AABB.emplace_back(minPos.x, maxPos.y, maxPos.z); + AABB.emplace_back(maxPos.x, minPos.y, minPos.z); + AABB.emplace_back(maxPos.x, minPos.y, maxPos.z); + AABB.emplace_back(maxPos.x, maxPos.y, minPos.z); + AABB.emplace_back(maxPos.x, maxPos.y, maxPos.z); } void Renderer::Model::unloadModel() @@ -107,12 +107,8 @@ void Renderer::Model::unloadModel() texturesLoaded.clear(); meshes.clear(); - minX = std::numeric_limits::max(); - maxX = std::numeric_limits::min(); - minY = std::numeric_limits::max(); - maxY = std::numeric_limits::min(); - minZ = std::numeric_limits::max(); - maxZ = std::numeric_limits::min(); + minPos = glm::vec3(std::numeric_limits::max()); + maxPos = glm::vec3(std::numeric_limits::min()); } void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4) @@ -143,12 +139,8 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, auto pos = mesh->mVertices[i]; vertices.push_back(Vertex(pos, mesh->mNormals[i], mesh->mTextureCoords[0][i])); auto worldPos = model * pos; - minX = std::min(minX, worldPos.x); - maxX = std::max(maxX, worldPos.x); - minY = std::min(minY, worldPos.y); - maxY = std::max(maxY, worldPos.y); - minZ = std::min(minZ, worldPos.z); - maxZ = std::max(maxZ, worldPos.z); + minPos = glm::min(minPos, glm::vec3(worldPos.x, worldPos.y, worldPos.z)); + maxPos = glm::max(maxPos, glm::vec3(worldPos.x, worldPos.y, worldPos.z)); } } @@ -162,7 +154,7 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, aiString str; material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); return std::string(str.C_Str()); - }()); paintingProgram != nullptr && iter != paintingMap.end()) + }()); paintingProgram != nullptr && iter != paintingMap.end()) { qDebug() << iter->first.c_str() << "Replaced"; @@ -193,19 +185,24 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, mesh->indices = indices; if (!(mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) - qWarning() << "Basecolor Texture Loading Failed!"; - if (!(mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) - qWarning() << "MetallicRoughness Texture Loading Failed!"; - if (!(mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) - qWarning() << "Normal Texture Loading Failed!"; - - if (mesh->textureBasecolor && mesh->textureMetallicRoughness && mesh->textureNormal) { - mesh->setupMesh(); - return mesh; + aiColor3D color; + material->Get(AI_MATKEY_BASE_COLOR, color); + mesh->baseColor = QVector3D(color.r, color.g, color.b); } - else - return nullptr; + if (!(mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) + { + float metallic, roughness; + material->Get(AI_MATKEY_METALLIC_FACTOR, metallic); + material->Get(AI_MATKEY_ROUGHNESS_FACTOR, roughness); + mesh->metallicRoughness = QVector2D(metallic, roughness); + } + if (!(mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) + { + } + + mesh->setupMesh(); + return mesh; } } diff --git a/ArchitectureColoredPainting/src/Renderer/Model.h b/ArchitectureColoredPainting/src/Renderer/Model.h index d8112f9..21c5674 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.h +++ b/ArchitectureColoredPainting/src/Renderer/Model.h @@ -37,7 +37,8 @@ namespace Renderer QDir directory; /// Ä£ÐÍËùÔÚ·¾¶ QString name; /// Ä£ÐÍÎļþÃû - float minX, maxX, minY, maxY, minZ, maxZ; + glm::vec3 minPos = glm::vec3(std::numeric_limits::max()); + glm::vec3 maxPos = glm::vec3(std::numeric_limits::min()); /// µÝ¹é±éÀú½áµã void processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4 = aiMatrix4x4()); diff --git a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp index 679bfcb..8712313 100644 --- a/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp +++ b/ArchitectureColoredPainting/src/Renderer/RendererGLWidget.cpp @@ -293,7 +293,7 @@ void RendererGLWidget::paintGL() gl->BeginQuery(GL_TIME_ELAPSED, timeQuery); - gl->Enable(GL_CULL_FACE); + //gl->Enable(GL_CULL_FACE); light.lightDirection.setX(cos(qDegreesToRadians(sunPitch)) * cos(qDegreesToRadians(sunYaw))); light.lightDirection.setY(sin(qDegreesToRadians(sunPitch))); diff --git a/svg/线段.svg b/svg/线段.svg new file mode 100644 index 0000000..c743251 --- /dev/null +++ b/svg/线段.svg @@ -0,0 +1 @@ + \ No newline at end of file