实现彩绘纹理部分替换

TaoZhang-Branch
wuyize 2023-03-13 13:46:17 +08:00
parent acd25b4964
commit 0d42af9200
4 changed files with 49 additions and 36 deletions

View File

@ -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);

View File

@ -2,23 +2,22 @@
#include <QOpenGLFunctions_4_5_Core>
#include <QString>
#include <vector>
#include <QVector2D>
#include <QVector3D>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLTexture>
#include <QOpenGLWidget>
#include <assimp/vector3.h>
#include <glm/glm.hpp>
#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);
};

View File

@ -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<Drawable> 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<Vertex> vertices;
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
{
@ -139,45 +135,59 @@ std::unique_ptr<Drawable> Model::processMesh(aiMesh* mesh, const aiScene* scene,
maxZ = std::max(maxZ, worldPos.z);
}
}
std::vector<unsigned int> indices;
for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++)
indices.insert(indices.end(), face->mIndices, face->mIndices + face->mNumIndices);
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end())
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<PaintingMesh>(glFunc, paintingProgram, shadowProgram, modelQ);
auto& [paintingPath, leftBottom, rightTop] = iter->second;
qDebug() << str.C_Str() << "Replaced";
auto m_mesh = std::make_unique<PaintingMesh>(glFunc, paintingProgram, shadowProgram, modelQ);
m_mesh->vertices = vertices;
m_mesh->indices = indices;
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;
}
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;
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<Mesh>(glFunc, shaderProgram, shadowProgram, modelQ);
m_mesh->vertices = vertices;
m_mesh->indices = indices;
auto mesh = std::make_unique<Mesh>(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();
}

View File

@ -24,6 +24,10 @@ namespace Renderer
QOpenGLShaderProgram* shadowProgram = nullptr;
VirtualTextureManager* vtManager = nullptr;
/**
* @param key BaseColor
* @param value json,
*/
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, QOpenGLTexture> texturesLoaded;