Skip to content

Commit

Permalink
Sample5_10: 每顶点计算光照
Browse files Browse the repository at this point in the history
  • Loading branch information
JYLongKong committed May 4, 2022
1 parent 612184a commit ce6fc7d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
Binary file added apk/Sample5_10.apk
Binary file not shown.
31 changes: 31 additions & 0 deletions app/src/main/assets/shader/sample5_10.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#version 400

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (location = 0) in vec4 inLightQD;
layout (location = 1) in vec3 vposition;
layout (location = 0) out vec4 outColor;

vec4 genBoardColor(vec3 position) {
const float height = 4.0;
const float width = 6.0;
vec4 color;
float n = 8.0;
float spanh = height / n;
float spanw = width / n;
int i = int((position.x + 10.0) / spanw);
int j = int((position.y + 10.0) / spanh);
int whichColor = int(mod(float(i + j), 2.0));
if (whichColor == 1) {
color = vec4(0.678, 0.231, 0.129, 1.0);
} else {
color = vec4(1.0, 1.0, 1.0, 1.0);
}

return color;
}

void main() {
outColor = inLightQD * genBoardColor(vposition);
}
4 changes: 2 additions & 2 deletions app/src/main/cpp/bndev/MyVulkanManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,9 +941,9 @@ void MyVulkanManager::initMatrixAndLight() {
// MatrixState3D::setCamera(5000.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
/// Sample4_13 *************************************************** end

/// Sample5_5、Sample5_6、Sample5_9
/// Sample5_5~Sample5_10
// LightManager::setLightPosition(0, 0, -13); // 设置定位光光源位置
LightManager::setLightPosition(0, 0, -14.5f); // Sample5_9-每片元计算光照
LightManager::setLightPosition(0, 0, -14.5f); // Sample5_9、Sample5_10-光照的每顶点计算与每片元计算
LightManager::setLightDirection(-0.0f, 0.0f, 1.0f); // 设置定向光光源方向
LightManager::setLightAmbient(0.1f, 0.1f, 0.1f, 0.1f); // 设置环境光强度
LightManager::setLightDiffuse(0.6f, 0.6f, 0.6f, 0.6f); // 设置散射光强度
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/cpp/bndev/ShaderQueueSuit_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ void ShaderQueueSuit_Common::create_pipeline_layout(VkDevice &device) {
layout_bindings[0].binding = 0; // 此绑定的绑定点编号(需要与着色器中给定的对应绑定点编号一致)
layout_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; // 描述类型(此绑定对应类型为一致变量缓冲)
layout_bindings[0].descriptorCount = 1; // 描述数量
// layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 目标着色器阶段(此绑定对应的是顶点着色器)
layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; // Sample5_1、Sample5_9-目标着色器阶段(此绑定对应的是片元着色器)
layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; // 目标着色器阶段(此绑定对应的是顶点着色器)
// layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; // Sample5_1、Sample5_9-目标着色器阶段(此绑定对应的是片元着色器)
layout_bindings[0].pImmutableSamplers = nullptr;

VkDescriptorSetLayoutCreateInfo descriptor_layout = {}; // 构建描述集布局创建信息结构体实例
Expand Down Expand Up @@ -207,10 +207,11 @@ void ShaderQueueSuit_Common::create_shader(VkDevice &device) {
// std::string fragStr = FileUtil::loadAssetStr("shader/sample5_2.frag"); // Sample5_2
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_3.vert"); // Sample5_3
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_4.vert"); // Sample5_4
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_5.vert"); // Sample5_5、Sample5_7
std::string vertStr = FileUtil::loadAssetStr("shader/sample5_5.vert"); // Sample5_5、Sample5_7、Sample5_10
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_6.vert"); // Sample5_6
std::string vertStr = FileUtil::loadAssetStr("shader/sample5_9.vert"); // Sample5_9
std::string fragStr = FileUtil::loadAssetStr("shader/sample5_9.frag"); // Sample5_9
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_9.vert"); // Sample5_9
// std::string fragStr = FileUtil::loadAssetStr("shader/sample5_9.frag"); // Sample5_9
std::string fragStr = FileUtil::loadAssetStr("shader/sample5_10.frag"); // Sample5_10

// 给出顶点着色器对应的管线着色器阶段创建信息结构体实例的各项所需属性
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Expand Down

0 comments on commit ce6fc7d

Please sign in to comment.