main
wuyize 2023-04-04 15:51:38 +08:00
parent dfcad45b8c
commit 262eb0e19f
9 changed files with 58 additions and 46 deletions

View File

@ -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);
gMetallicRoughness = metallicRoughness;
gPaintingIndex = uvec2(0);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<float>::max();
maxX = std::numeric_limits<float>::min();
minY = std::numeric_limits<float>::max();
maxY = std::numeric_limits<float>::min();
minZ = std::numeric_limits<float>::max();
maxZ = std::numeric_limits<float>::min();
minPos = glm::vec3(std::numeric_limits<float>::max());
maxPos = glm::vec3(std::numeric_limits<float>::min());
}
void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4)
@ -143,12 +139,8 @@ std::unique_ptr<Drawable> 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));
}
}
@ -193,20 +185,25 @@ std::unique_ptr<Drawable> 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)
{
aiColor3D color;
material->Get(AI_MATKEY_BASE_COLOR, color);
mesh->baseColor = QVector3D(color.r, color.g, color.b);
}
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;
}
else
return nullptr;
}
}
GLuint Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type)

View File

@ -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<float>::max());
glm::vec3 maxPos = glm::vec3(std::numeric_limits<float>::min());
/// 递归遍历结点
void processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4 = aiMatrix4x4());

View File

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

1
svg/线段.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100.45 1"><defs><style>.cls-1{fill:none;stroke:#231815;stroke-miterlimit:10;}</style></defs><g id="图层_2" data-name="图层 2"><g id="图层_1-2" data-name="图层 1"><line class="cls-1" y1="0.5" x2="100.45" y2="0.5"/></g></g></svg>

After

Width:  |  Height:  |  Size: 288 B