1
Fork 0
mirror of https://github.com/redstrate/Novus.git synced 2025-04-27 14:17:45 +00:00

Use mdl normals to display some lighting

This commit is contained in:
Joshua Goins 2022-04-12 09:11:31 -04:00
parent a2688ca2dc
commit 0b01715824
5 changed files with 29 additions and 10 deletions

View file

@ -1,7 +1,17 @@
#version 450
layout(location = 0) in vec3 inNormal;
layout(location = 1) in vec3 inFragPos;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(0.0, 1.0, 0.0, 1.0);
const vec3 lightPos = vec3(3);
vec3 norm = normalize(inNormal);
vec3 lightDir = normalize(lightPos - inFragPos);
float diff = max(dot(norm, lightDir), 0.0);
outColor = vec4(0.0, 1.0, 0.0, 1.0) * diff;
}

Binary file not shown.

View file

@ -1,11 +1,17 @@
#version 450
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout( push_constant ) uniform PushConstant {
layout(location = 0) out vec3 outNormal;
layout(location = 1) out vec3 outFragPos;
layout(push_constant) uniform PushConstant {
mat4 mvp;
};
void main() {
gl_Position = mvp * vec4(inPosition, 1.0);
outNormal = inNormal;
outFragPos = inNormal;
}

Binary file not shown.

View file

@ -493,7 +493,7 @@ RenderModel Renderer::addModel(const Model& model) {
for(auto part : model.lods[0].parts) {
RenderPart renderPart;
size_t vertexSize = part.vertices.size() * sizeof(float) * 3;
size_t vertexSize = part.vertices.size() * sizeof(Vertex);
auto[vertexBuffer, vertexMemory] = createBuffer(vertexSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
size_t indexSize = part.indices.size() * sizeof(uint16_t);
@ -501,13 +501,10 @@ RenderModel Renderer::addModel(const Model& model) {
// copy vertex data
{
void *mapped_data = nullptr;
void* mapped_data = nullptr;
vkMapMemory(device, vertexMemory, 0, vertexSize, 0, &mapped_data);
for (int i = 0; i < part.vertices.size(); i++) {
memcpy((char *) mapped_data + ((sizeof(float) * 3) * i),
part.vertices[i].position.data(), sizeof(float) * 3);
}
memcpy(mapped_data, part.vertices.data(), vertexSize);
VkMappedMemoryRange range = {};
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
@ -520,7 +517,7 @@ RenderModel Renderer::addModel(const Model& model) {
// copy index data
{
void *mapped_data = nullptr;
void* mapped_data = nullptr;
vkMapMemory(device, indexMemory, 0, indexSize, 0, &mapped_data);
memcpy(mapped_data, part.indices.data(), indexSize);
@ -564,10 +561,16 @@ void Renderer::initPipeline() {
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages = {vertexShaderStageInfo, fragmentShaderStageInfo};
VkVertexInputBindingDescription binding = {};
binding.stride = sizeof(float) * 3;
binding.stride = sizeof(Vertex);
VkVertexInputAttributeDescription positionAttribute = {};
positionAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
positionAttribute.offset = offsetof(Vertex, position);
VkVertexInputAttributeDescription normalAttribute = {};
normalAttribute.format = VK_FORMAT_R32G32B32_SFLOAT;
normalAttribute.location = 1;
normalAttribute.offset = offsetof(Vertex, normal);
VkPipelineVertexInputStateCreateInfo vertexInputState = {};
vertexInputState.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;