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+
619Model 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