Skip to content

Commit f0e52b8

Browse files
committed
Normal vectors
1 parent 93822bc commit f0e52b8

6 files changed

Lines changed: 47 additions & 8 deletions

File tree

shaders/model_frag.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#version 450 core
22

3+
layout(location = 0) in vec3 in_normal;
4+
35
layout(location = 0) out vec4 out_color;
46

57
void main() {
6-
out_color = vec4(1.0);
8+
vec3 normal = normalize(in_normal);
9+
out_color = vec4(normal * 0.5 + 0.5, 1.0);
710
}

shaders/model_frag.spv

344 Bytes
Binary file not shown.

shaders/model_vert.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#version 450 core
22

33
layout(location = 0) in vec3 in_position;
4+
layout(location = 1) in vec3 in_normal;
5+
6+
layout(location = 0) out vec3 out_normal;
47

58
void main() {
69
gl_Position = vec4(in_position.x, -in_position.y, in_position.z, 1.0);
10+
out_normal = in_normal;
711
}

shaders/model_vert.spv

156 Bytes
Binary file not shown.

src/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,19 @@ void initApplication(SDL_Window* window) {
196196
vertexAttributeDescriptions, ARRAY_COUNT(vertexAttributeDescriptions), &vertexInputBinding, 1, &descriptorLayout);
197197

198198

199-
VkVertexInputAttributeDescription modelAttributeDescriptions[1] = {};
199+
VkVertexInputAttributeDescription modelAttributeDescriptions[2] = {};
200200
modelAttributeDescriptions[0].binding = 0;
201201
modelAttributeDescriptions[0].location = 0;
202202
modelAttributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
203203
modelAttributeDescriptions[0].offset = 0;
204+
modelAttributeDescriptions[1].binding = 0;
205+
modelAttributeDescriptions[1].location = 1;
206+
modelAttributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
207+
modelAttributeDescriptions[1].offset = 0;
204208
VkVertexInputBindingDescription modelInputBinding = {};
205209
modelInputBinding.binding = 0;
206210
modelInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
207-
modelInputBinding.stride = sizeof(float) * 3;
211+
modelInputBinding.stride = sizeof(float) * 6;
208212
modelPipeline = createPipeline(context, "../shaders/model_vert.spv", "../shaders/model_frag.spv", renderPass, swapchain.width, swapchain.height,
209213
modelAttributeDescriptions, ARRAY_COUNT(modelAttributeDescriptions), &modelInputBinding, 0, 0);
210214

src/model.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
#define CGLTF_IMPLEMENTATION
44
#include <cgltf/cgltf.h>
55

6+
// Stride in bytes. Element size in bytes
7+
void fillBuffer(uint32_t inputStride, void* inputData, uint32_t outputStride, void* outputData, uint32_t numElements, uint32_t elementSize) {
8+
uint8_t* output = (uint8_t*)outputData;
9+
uint8_t* input = (uint8_t*)inputData;
10+
for(uint32_t i = 0; i < numElements; ++i) {
11+
for(uint32_t b = 0; b < elementSize; ++b) {
12+
output[b] = input[b];
13+
}
14+
output += outputStride;
15+
input += inputStride;
16+
}
17+
}
18+
619
Model createModel(VulkanContext* context, const char* filename) {
720
Model result = {};
821
cgltf_options options = {};
@@ -15,7 +28,6 @@ Model createModel(VulkanContext* context, const char* filename) {
1528
assert(data->meshes[0].primitives_count == 1);
1629
assert(data->meshes[0].primitives[0].attributes_count > 0);
1730
assert(data->meshes[0].primitives[0].attributes[0].type == cgltf_attribute_type_position);
18-
assert(data->meshes[0].primitives[0].attributes[0].data->stride == sizeof(float)*3);
1931
assert(data->meshes[0].primitives[0].indices->component_type == cgltf_component_type_r_16u);
2032
assert(data->meshes[0].primitives[0].indices->stride == sizeof(uint16_t));
2133

@@ -30,12 +42,28 @@ Model createModel(VulkanContext* context, const char* filename) {
3042

3143

3244
// Vertices
33-
bufferBase = (uint8_t*)data->meshes[0].primitives[0].attributes[0].data->buffer_view->buffer->data;
34-
uint64_t vertexDataSize = data->meshes[0].primitives[0].attributes[0].data->buffer_view->size;
35-
void* vertexData = bufferBase + data->meshes[0].primitives[0].attributes[0].data->buffer_view->offset;
36-
45+
uint64_t numVertices = data->meshes[0].primitives[0].attributes->data->count;
46+
uint64_t vertexDataSize = sizeof(float) * 6 * numVertices;
47+
uint8_t* vertexData = new uint8_t[vertexDataSize];
48+
for(uint64_t i = 0; i < data->meshes[0].primitives[0].attributes_count; ++i) {
49+
cgltf_attribute* attribute = data->meshes[0].primitives[0].attributes + i;
50+
if(attribute->type == cgltf_attribute_type_position) {
51+
assert(attribute->data->stride == sizeof(float)*3);
52+
bufferBase = (uint8_t*)attribute->data->buffer_view->buffer->data;
53+
uint64_t positionDataSize = attribute->data->buffer_view->size;
54+
void* positionData = bufferBase + attribute->data->buffer_view->offset;
55+
fillBuffer(sizeof(float)*3, positionData, sizeof(float) * 6, vertexData, numVertices, sizeof(float)*3);
56+
} else if(attribute->type == cgltf_attribute_type_normal) {
57+
assert(attribute->data->stride == sizeof(float)*3);
58+
bufferBase = (uint8_t*)attribute->data->buffer_view->buffer->data;
59+
uint64_t normalDataSize = attribute->data->buffer_view->size;
60+
void* normalData = bufferBase + attribute->data->buffer_view->offset;
61+
fillBuffer(sizeof(float)*3, normalData, sizeof(float) * 6, vertexData+(sizeof(float)*3), numVertices, sizeof(float)*3);
62+
}
63+
}
3764
createBuffer(context, &result.vertexBuffer, vertexDataSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
3865
uploadDataToBuffer(context, &result.vertexBuffer, vertexData, vertexDataSize);
66+
delete[] vertexData;
3967
}
4068
cgltf_free(data);
4169
}

0 commit comments

Comments
 (0)