Add basic lighting
This commit is contained in:
parent
e28adb0086
commit
d92c009fba
5 changed files with 29 additions and 5 deletions
|
@ -6,7 +6,7 @@
|
||||||
#include <vulkan/vulkan.h>
|
#include <vulkan/vulkan.h>
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
glm::vec3 position;
|
glm::vec3 position, normal;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Mesh {
|
class Mesh {
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 inFragPos;
|
||||||
|
layout(location = 1) in vec3 inNormal;
|
||||||
|
|
||||||
layout(location = 0) out vec4 outColor;
|
layout(location = 0) out vec4 outColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
outColor = vec4(1.0);
|
const vec3 norm = normalize(inNormal);
|
||||||
|
const vec3 lightDir = normalize(vec3(5) - inFragPos);
|
||||||
|
|
||||||
|
const float diff = max(dot(norm, lightDir), 0.0);
|
||||||
|
outColor = vec4(vec3(0.1) + vec3(diff), 1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 inPosition;
|
layout(location = 0) in vec3 inPosition;
|
||||||
|
layout(location = 1) in vec3 inNormal;
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 outFragPos;
|
||||||
|
layout(location = 1) out vec3 outNormal;
|
||||||
|
|
||||||
layout(push_constant) uniform PushConstants {
|
layout(push_constant) uniform PushConstants {
|
||||||
mat4 mvp;
|
mat4 mvp;
|
||||||
|
@ -8,4 +12,6 @@ layout(push_constant) uniform PushConstants {
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = pushConstants.mvp * vec4(inPosition, 1.0);
|
gl_Position = pushConstants.mvp * vec4(inPosition, 1.0);
|
||||||
|
outFragPos = inPosition;
|
||||||
|
outNormal = inNormal;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,8 @@ int main(int, char*[]) {
|
||||||
for(unsigned int i = 0; i < m->mNumVertices; i++) {
|
for(unsigned int i = 0; i < m->mNumVertices; i++) {
|
||||||
Vertex vertex;
|
Vertex vertex;
|
||||||
vertex.position = glm::vec3(m->mVertices[i].x, m->mVertices[i].y, m->mVertices[i].z);
|
vertex.position = glm::vec3(m->mVertices[i].x, m->mVertices[i].y, m->mVertices[i].z);
|
||||||
|
vertex.normal = glm::vec3(m->mNormals[i].x, m->mNormals[i].y, m->mNormals[i].z);
|
||||||
|
|
||||||
mesh->vertices.push_back(vertex);
|
mesh->vertices.push_back(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,12 +58,22 @@ void WorldPass::createPipeline() {
|
||||||
VkVertexInputAttributeDescription positionAttributeDescription = {};
|
VkVertexInputAttributeDescription positionAttributeDescription = {};
|
||||||
positionAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT;
|
positionAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||||
|
|
||||||
|
VkVertexInputAttributeDescription normalAttributeDescription = {};
|
||||||
|
normalAttributeDescription.location = 1;
|
||||||
|
normalAttributeDescription.offset = offsetof(Vertex, normal);
|
||||||
|
normalAttributeDescription.format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||||
|
|
||||||
|
const std::array<VkVertexInputAttributeDescription, 2> attributes = {
|
||||||
|
positionAttributeDescription,
|
||||||
|
normalAttributeDescription
|
||||||
|
};
|
||||||
|
|
||||||
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
|
||||||
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||||
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
vertexInputInfo.vertexBindingDescriptionCount = 1;
|
||||||
vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDescription;
|
vertexInputInfo.pVertexBindingDescriptions = &vertexBindingDescription;
|
||||||
vertexInputInfo.vertexAttributeDescriptionCount = 1;
|
vertexInputInfo.vertexAttributeDescriptionCount = attributes.size();
|
||||||
vertexInputInfo.pVertexAttributeDescriptions = &positionAttributeDescription;
|
vertexInputInfo.pVertexAttributeDescriptions = attributes.data();
|
||||||
|
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
|
||||||
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||||
|
|
Reference in a new issue