首页 > 解决方案 > Vulkan 漫反射着色不随对象变换而改变

问题描述

系统:GPU:Intel(R) UHD Graphics

这是我的着色器代码:

顶点着色器:

#version 450

layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec2 uv;

layout(location = 0) out vec3 fragCol;

layout(push_constant) uniform Push {
 mat4 transform; // projection * view * model
 mat4 modelMatrix;
} push;

vec3 DIRECTION_TO_LIGHT = normalize(vec3(1.0, -10.0, -1.0));

void main() {
gl_Position = push.transform * vec4(position, 1.0);

vec3 normalWorldSpace = normalize(mat3(push.modelMatrix) * normal);

float lightIntensity = max(dot(normalWorldSpace, DIRECTION_TO_LIGHT), 0);

fragCol = lightIntensity * vec3(1.0);
}

片段着色器:

#version 450 core

layout(location = 0) in vec3 fragCol;
layout(location = 0) out vec4 outColor;

layout(push_constant) uniform Push{
mat4 transform;
mat4 modelMatrix;
}push;

void main(){
outColor = vec4(fragCol, 1.0);
}

在此处输入图像描述

即使在 z 轴上旋转时,模型的黑暗部分仍然保持黑暗。就像颜色被烘焙到模型中一样,事实并非如此。

我尝试在片段着色器上计算颜色,但这也不起作用。

计算变换矩阵的代码:

auto projectionMatrix = camera.getProjection() * camera.getView();

        for (auto& obj : gameObjects) {
//rotating doesn't change the dark pixels.
            obj.transform.rotation.y += glm::radians(45.0f) * deltaTime;
            SimplePushConstantData push{};
            auto modelMatrix = obj.transform.mat4();
            push.transform = projectionMatrix * modelMatrix;
            push.modelMatrix = modelMatrix;
            vkCmdPushConstants(commandBuffer, pipelineLayout,
                VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
                0, sizeof(SimplePushConstantData), &push);
            obj.model->bind(commandBuffer);
            obj.model->draw(commandBuffer);
        }

标签: graphicsglslvulkanshadingdirectional-light

解决方案


推荐阅读