-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1aab267
commit 612184a
Showing
9 changed files
with
190 additions
and
20 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;// 传送到片元着色器的物体坐标系顶点位置 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters