From e39acc9f254231c8beb778d930adf44854cda5bb Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Fri, 16 Nov 2018 13:55:52 +0800 Subject: [PATCH] Add normal map preview --- src/exportpreviewwidget.cpp | 33 +++++++++++++++++++++++++-------- src/texturegenerator.cpp | 8 ++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/exportpreviewwidget.cpp b/src/exportpreviewwidget.cpp index 3422a9d6..83cca35e 100644 --- a/src/exportpreviewwidget.cpp +++ b/src/exportpreviewwidget.cpp @@ -63,14 +63,14 @@ ExportPreviewWidget::ExportPreviewWidget(Document *document, QWidget *parent) : containerLayout->setSpacing(0); containerLayout->setContentsMargins(0, 0, 0, 0); containerLayout->addWidget(m_colorPreviewLabel, 0, 0); - //containerLayout->addWidget(m_normalPreviewLabel, 1, 0); - //containerLayout->addWidget(m_metalnessRoughnessAmbientOcclusionPreviewLabel, 2, 0); - containerLayout->setRowStretch(0, 1); - containerLayout->setColumnStretch(0, 1); + containerLayout->addWidget(m_normalPreviewLabel, 0, 1); + containerLayout->addWidget(m_metalnessRoughnessAmbientOcclusionPreviewLabel, 0, 2); + //containerLayout->setRowStretch(0, 1); + //containerLayout->setColumnStretch(0, 1); m_textureRenderWidget = new ModelWidget; m_textureRenderWidget->setMinimumSize(128, 128); - m_textureRenderWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + //m_textureRenderWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QVBoxLayout *renderLayout = new QVBoxLayout; renderLayout->setSpacing(0); @@ -113,17 +113,28 @@ void ExportPreviewWidget::resizeEvent(QResizeEvent *event) void ExportPreviewWidget::resizePreviewImages() { - { + if (m_colorImage.isNull()) { + m_colorPreviewLabel->hide(); + } else { QPixmap pixmap = QPixmap::fromImage(m_colorImage); m_colorPreviewLabel->setPixmap(pixmap.scaled(m_colorPreviewLabel->width(), m_colorPreviewLabel->height(), Qt::KeepAspectRatio)); + m_colorPreviewLabel->show(); } - { + + if (m_normalImage.isNull()) { + m_normalPreviewLabel->hide(); + } else { QPixmap pixmap = QPixmap::fromImage(m_normalImage); m_normalPreviewLabel->setPixmap(pixmap.scaled(m_normalPreviewLabel->width(), m_normalPreviewLabel->height(), Qt::KeepAspectRatio)); + m_normalPreviewLabel->show(); } - { + + if (m_metalnessRoughnessAmbientOcclusionImage.isNull()) { + m_metalnessRoughnessAmbientOcclusionPreviewLabel->hide(); + } else { QPixmap pixmap = QPixmap::fromImage(m_metalnessRoughnessAmbientOcclusionImage); m_metalnessRoughnessAmbientOcclusionPreviewLabel->setPixmap(pixmap.scaled(m_metalnessRoughnessAmbientOcclusionPreviewLabel->width(), m_metalnessRoughnessAmbientOcclusionPreviewLabel->height(), Qt::KeepAspectRatio)); + m_metalnessRoughnessAmbientOcclusionPreviewLabel->show(); } } @@ -161,10 +172,16 @@ void ExportPreviewWidget::updateTexturePreview() { if (m_document->textureGuideImage) m_colorImage = *m_document->textureGuideImage; + else + m_colorImage = QImage(); if (m_document->textureNormalImage) m_normalImage = *m_document->textureNormalImage; + else + m_normalImage = QImage(); if (m_document->textureMetalnessRoughnessAmbientOcclusionImage) m_metalnessRoughnessAmbientOcclusionImage = *m_document->textureMetalnessRoughnessAmbientOcclusionImage; + else + m_metalnessRoughnessAmbientOcclusionImage = QImage(); m_textureRenderWidget->updateMesh(m_document->takeResultTextureMesh()); resizePreviewImages(); checkSpinner(); diff --git a/src/texturegenerator.cpp b/src/texturegenerator.cpp index 5a2f58bd..e057a0e7 100644 --- a/src/texturegenerator.cpp +++ b/src/texturegenerator.cpp @@ -202,6 +202,7 @@ void TextureGenerator::generate() const auto &triangleVertexUvs = *m_outcome->triangleVertexUvs(); const auto &triangleSourceNodes = *m_outcome->triangleSourceNodes(); + const auto &triangleNormals = m_outcome->triangleNormals; std::map, const OutcomeNode *> nodeMap; for (const auto &item: m_outcome->nodes) { @@ -304,6 +305,13 @@ void TextureGenerator::generate() textureNormalPainter.drawImage(0, 0, findNormalTextureResult->second); textureNormalPainter.setClipping(false); hasNormalMap = true; + } else { + const auto &triangleNormal = triangleNormals[i]; + QColor brushColor; + brushColor.setRedF((triangleNormal.x() + 1) / 2); + brushColor.setGreenF((triangleNormal.y() + 1) / 2); + brushColor.setBlueF((triangleNormal.z() + 1) / 2); + textureNormalPainter.fillPath(path, brushColor); } // Copy metalness texture if there is one auto findMetalnessTextureResult = m_partMetalnessTextureMap.find(source.first);