Skip to content

Commit

Permalink
Sample5_9: 每片元计算光照
Browse files Browse the repository at this point in the history
  • Loading branch information
JYLongKong committed May 1, 2022
1 parent 1aab267 commit 612184a
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 20 deletions.
Binary file added apk/Sample5_9.apk
Binary file not shown.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_library(
src/main/cpp/bndev/ColorRect.cpp
src/main/cpp/bndev/Cube.cpp
src/main/cpp/bndev/BallData.cpp
src/main/cpp/bndev/FlatData.cpp
)


Expand Down
77 changes: 77 additions & 0 deletions app/src/main/assets/shader/sample5_9.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (std140, set = 0, binding = 0) uniform bufferVals {
vec4 uCamera;
vec4 lightPosition;
vec4 lightAmbient;
vec4 lightDiffuse;
vec4 lightSpecular;
} myBufferVals;

layout (location = 1) in vec3 vposition;// 传入的世界坐标系顶点位置
layout (location = 2) in vec3 vNormal;// 传入的世界坐标系法向量
layout (location = 3) in vec3 objPos;// 传入的物体坐标系顶点位置
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;
}

vec4 pointLight(
in vec3 uCamera,
in vec3 lightLocation,
in vec4 lightAmbient,
in vec4 lightDiffuse,
in vec4 lightSpecular,
in vec3 normal,
in vec3 aPosition
) {
vec4 ambient;
vec4 diffuse;
vec4 specular;
ambient = lightAmbient;
vec3 eye = normalize(uCamera - aPosition);
vec3 vp = normalize(lightLocation.xyz - aPosition);
vp = normalize(vp);
vec3 halfVector = normalize(vp + eye);
float shininess = 10.0;
float nDotViewPosition = max(0.0, dot(normal, vp));
diffuse = lightDiffuse * nDotViewPosition;
float nDotViewHalfVector = dot(normal, halfVector);
float powerFactor = max(0.0, pow(nDotViewHalfVector, shininess));
specular = lightSpecular * powerFactor;

return ambient + diffuse + specular;
}

void main() {
vec4 lightQD = pointLight(
myBufferVals.uCamera.xyz,
myBufferVals.lightPosition.xyz,
myBufferVals.lightAmbient,
myBufferVals.lightDiffuse,
myBufferVals.lightSpecular,
vNormal,
vposition
);

outColor = genBoardColor(objPos) * lightQD;
}
38 changes: 38 additions & 0 deletions app/src/main/assets/shader/sample5_9.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (push_constant) uniform constantVals {
mat4 mvp;
mat4 mm;
} myConstantVals;

layout (location = 0) in vec3 pos;// 传入的顶点坐标
layout (location = 1) in vec3 inNormal;// 传入的顶点法向量
layout (location = 1) out vec3 vposition;// 传出的顶点坐标
layout (location = 2) out vec3 vNormal;// 传出的世界坐标系法向量
layout (location = 3) out vec3 objPos;// 传出的物体坐标系顶点坐标

out gl_PerVertex {
vec4 gl_Position;
};

// 将物体坐标系的法向量变换到世界坐标系的方法
vec3 normalFromObjectToWorld(
in mat4 uMMatrix, // 基本变换矩阵
in vec3 normal, // 要变换的法向量
in vec3 position// 顶点位置
) {
vec3 normalTarget = position + normal;// 计算变换后的法向量
vec3 newNormal = (uMMatrix * vec4(normalTarget, 1)).xyz - (uMMatrix * vec4(position, 1)).xyz;
newNormal = normalize(newNormal);
return newNormal;
}

void main() {
gl_Position = myConstantVals.mvp * vec4(pos, 1.0);// 计算顶点最终位置
vposition = (myConstantVals.mm * vec4(pos, 1)).xyz;// 计算世界坐标系顶点位置
vNormal = normalFromObjectToWorld(myConstantVals.mm, inNormal, pos);// 计算世界坐标系法向量
objPos = pos;// 传送到片元着色器的物体坐标系顶点位置
}
21 changes: 21 additions & 0 deletions app/src/main/cpp/bndev/FlatData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "FlatData.h"
#include <vector>
#include <cmath>
#include <string>

float *FlatData::vdata;
int FlatData::dataByteCount;
int FlatData::vCount;

void FlatData::genData() {
vCount = 6;
dataByteCount = vCount * 6 * sizeof(float);
vdata = new float[vCount * 6]{ // 顶点位置+法向量
3, 2, 0, 0, 0, 1,
-3, 2, 0, 0, 0, 1,
-3, -2, 0, 0, 0, 1,
3, -2, 0, 0, 0, 1,
3, 2, 0, 0, 0, 1,
-3, -2, 0, 0, 0, 1
};
}
13 changes: 13 additions & 0 deletions app/src/main/cpp/bndev/FlatData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DEEPERVULKAN_FLATDATA_H
#define DEEPERVULKAN_FLATDATA_H

class FlatData {
public:
static float *vdata;
static int dataByteCount;
static int vCount;

static void genData();
};

#endif // DEEPERVULKAN_FLATDATA_H
45 changes: 30 additions & 15 deletions app/src/main/cpp/bndev/MyVulkanManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ColorRect.h"
#include "BallData.h"
#include "LightManager.h"
#include "FlatData.h"

// 静态成员实现
android_app *MyVulkanManager::Android_application;
Expand Down Expand Up @@ -811,10 +812,16 @@ void MyVulkanManager::createDrawableObject() {
/// Sample5_1 **************************************************** end

/// Sample5_7 ************************************************** start
CubeData::genData();
objForDraw = new DrawableObjectCommon(
CubeData::vdata, CubeData::dataByteCount, CubeData::vCount, device, memoryroperties);
// CubeData::genData();
// objForDraw = new DrawableObjectCommon(
// CubeData::vdata, CubeData::dataByteCount, CubeData::vCount, device, memoryroperties);
/// Sample5_7 **************************************************** end

/// Sample5_9 ************************************************** start
FlatData::genData();
objForDraw = new DrawableObjectCommon(
FlatData::vdata, FlatData::dataByteCount, FlatData::vCount, device, memoryroperties);
/// Sample5_9 **************************************************** end
}

/**
Expand Down Expand Up @@ -934,8 +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
LightManager::setLightPosition(0, 0, -13); // 设置定位光光源位置
/// Sample5_5、Sample5_6、Sample5_9
// LightManager::setLightPosition(0, 0, -13); // 设置定位光光源位置
LightManager::setLightPosition(0, 0, -14.5f); // Sample5_9-每片元计算光照
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 Expand Up @@ -1227,19 +1235,26 @@ void MyVulkanManager::drawObject() {
/// Sample5_2 **************************************************** end

/// Sample5_7 ************************************************** start
// MatrixState3D::pushMatrix();
// MatrixState3D::translate(-1.5f, 0, -15);
// MatrixState3D::rotate(45, 1, 0, 0);
// MatrixState3D::rotate(45, 0, 0, 1);
// objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
// MatrixState3D::popMatrix();
// MatrixState3D::pushMatrix();
// MatrixState3D::translate(1.5f, 0, -15);
// MatrixState3D::rotate(45, 1, 0, 0);
// MatrixState3D::rotate(45, 0, 0, 1);
// objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
// MatrixState3D::popMatrix();
/// Sample5_7 **************************************************** end

/// Sample5_9 ************************************************** start
MatrixState3D::pushMatrix();
MatrixState3D::translate(-1.5f, 0, -15);
MatrixState3D::rotate(45, 1, 0, 0);
MatrixState3D::rotate(45, 0, 0, 1);
objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
MatrixState3D::popMatrix();
MatrixState3D::pushMatrix();
MatrixState3D::translate(1.5f, 0, -15);
MatrixState3D::rotate(45, 1, 0, 0);
MatrixState3D::rotate(45, 0, 0, 1);
MatrixState3D::translate(0, 0, -15);
objForDraw->drawSelf(cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
MatrixState3D::popMatrix();
/// Sample5_7 **************************************************** end
/// Sample5_9 **************************************************** end

// triForDraw->drawSelf( // 绘制三色三角形、Sample4_14-卷绕和背面剪裁
// cmdBuffer, sqsCL->pipelineLayout, sqsCL->pipeline, &(sqsCL->descSet[0]));
Expand Down
10 changes: 6 additions & 4 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-目标着色器阶段(此绑定对应的是片元着色器)
// 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 @@ -204,11 +204,13 @@ void ShaderQueueSuit_Common::create_shader(VkDevice &device) {
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_1.vert"); // Sample5_1
// std::string fragStr = FileUtil::loadAssetStr("shader/sample5_1.frag"); // Sample5_1
// std::string vertStr = FileUtil::loadAssetStr("shader/sample5_2.vert"); // Sample5_2
std::string fragStr = FileUtil::loadAssetStr("shader/sample5_2.frag"); // Sample5_2
// 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
// 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

// 给出顶点着色器对应的管线着色器阶段创建信息结构体实例的各项所需属性
shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/cpp/bndev/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ bool isClick = true;
//float TOUCH_SCALE_FACTOR = 180.0f / 320;

/// Sample5_3
float TOUCH_SCALE_FACTOR = 10.0f / 320; // 角度缩放比例
//float TOUCH_SCALE_FACTOR = 10.0f / 320; // 角度缩放比例

/// Sample5_9
float TOUCH_SCALE_FACTOR = 5.0f / 320;

/**
* 事件处理回调方法
Expand Down

0 comments on commit 612184a

Please sign in to comment.