summaryrefslogtreecommitdiff
path: root/src/blu_cat/gra/skeletal_mesh.cpp
blob: 1515ddaa75e8b54fcbf6f07cea942b3e050f7b53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright 2022-2025 Frederico de Oliveira Linhares
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "skeletal_mesh.hpp"

#include "../com/binary_reader.hpp"
#include "../com/command.hpp"
#include "../int/core.hpp"
#include "skeletal_mesh_vertex.hpp"

namespace
{

struct StaticMeshHeader
{
	char file_magic[8];
	UI32 version;
	UI32 num_vertexes;
	UI32 num_indexes;
	UI32 num_bones;
	UI32 num_animations;
	UI32 num_bone_transforms; // For all animations
	UI32 num_bone_positions; // For all animations
	UI32 num_bone_rotations; // For all animations
	UI32 num_bone_scales; // For all animations
};

constexpr UI64F SKELETAL_MESH_HEADER_SIZE{44};

// Data that is only needed for the command chain but not for the SkeletalMesh
// goes here.
struct MeshBuilder
{
  std::string mesh_path;
  BluCat::GRA::SkeletalMesh *mesh;

  MeshBuilder(BluCat::GRA::SkeletalMesh *m, std::string mp);
  MeshBuilder(BluCat::GRA::SkeletalMesh *m, const char* mp);
};

MeshBuilder::MeshBuilder(BluCat::GRA::SkeletalMesh *m, std::string mp):
  mesh{m},
  mesh_path{mp}
{
}

MeshBuilder::MeshBuilder(BluCat::GRA::SkeletalMesh *m, const char *mp):
  MeshBuilder{m, std::string(mp)}
{
}

void
load_mesh(void *obj)
{
  auto self = static_cast<MeshBuilder*>(obj);

	StaticMeshHeader header;
  BinaryReader input{self->mesh_path};

	{ // Read header
		input.read_chars(header.file_magic, 8);
		header.version = input.read_ui32();
		header.num_vertexes = input.read_ui32();
		header.num_indexes = input.read_ui32();
		header.num_bones = input.read_ui32();
		header.num_animations = input.read_ui32();
		header.num_bone_transforms = input.read_ui32();
		header.num_bone_positions = input.read_ui32();
		header.num_bone_rotations = input.read_ui32();
		header.num_bone_scales = input.read_ui32();

		size_t total_file_size{
			SKELETAL_MESH_HEADER_SIZE +
			header.num_vertexes * 16 * SIZE_32_BIT +
			header.num_indexes * SIZE_32_BIT +
			header.num_bones * 16 * SIZE_32_BIT +
			header.num_animations * (2 * SIZE_64_BIT + SIZE_32_BIT) +
			header.num_bone_transforms * 4 * SIZE_32_BIT +
			header.num_bone_positions * (3 * SIZE_32_BIT + SIZE_64_BIT) +
			header.num_bone_rotations * (4 * SIZE_32_BIT + SIZE_64_BIT) +
			header.num_bone_scales * (3 * SIZE_32_BIT + SIZE_64_BIT)};

		if(total_file_size != input.size())
		{
			std::string error{"File size does not match header information: "};
			error += self->mesh_path;
			throw CommandError{error};
		}
	}

  self->mesh->queue_family =
    BluCat::INT::core.vk_device_with_swapchain->
		get_queue_family_with_graphics();

  { // Load vertexes.
    std::vector<BluCat::GRA::SkeletalMeshVertex> vertexes{header.num_vertexes};

    for(auto i{0}; i < header.num_vertexes; i++)
    {
      vertexes[i].position = input.read_vec3();
      vertexes[i].normal = input.read_vec3();
      vertexes[i].texture_coord = input.read_vec2();

      for(auto ii{0};
					ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES;
					ii++)
				vertexes[i].bone_ids[ii] = input.read_ui32();

      for(auto ii{0};
					ii < BluCat::GRA::SKELETAL_MESH_MAX_NUM_OF_INFLUENCING_BONES;
					ii++)
				vertexes[i].bone_weights[ii] = input.read_f32();
    }

    void *vertexes_data{vertexes.data()};
    size_t vertexes_size = sizeof(vertexes[0]) * vertexes.size();
    self->mesh->source_vertex_buffer = new BluCat::GRA::SourceBuffer{
      self->mesh->queue_family->device, vertexes_data, vertexes_size};
    self->mesh->vertex_buffer = new BluCat::GRA::DestinationBuffer{
      self->mesh->queue_family, self->mesh->source_vertex_buffer,
      VK_BUFFER_USAGE_VERTEX_BUFFER_BIT};
  }

  { // Load indexes.
    self->mesh->index_count = header.num_indexes;
    std::vector<uint32_t> indexes(self->mesh->index_count);

    for(auto i{0}; i < self->mesh->index_count; i++)
      indexes[i] = input.read_ui32();

    void *indexes_data{indexes.data()};
    size_t indexes_size{sizeof(indexes[0]) * indexes.size()};
    BluCat::GRA::SourceBuffer source_index_buffer{
      self->mesh->queue_family->device, indexes_data, indexes_size};
    self->mesh->index_buffer = new BluCat::GRA::DestinationBuffer{
      self->mesh->queue_family, &source_index_buffer,
      VK_BUFFER_USAGE_INDEX_BUFFER_BIT};
  }

  { // Load bones
    self->mesh->bones.reserve(header.num_bones);
    for(int i{0}; i < header.num_bones; i++)
      self->mesh->bones.emplace_back(input.read_mat4());
  }

  { // Load animations
    self->mesh->animations.resize(header.num_animations);
    for(uint32_t i{0}; i < header.num_animations; i++)
    {
      auto duration{input.read_f64()};
      self->mesh->animations[i].final_time = (float)duration;

      auto ticks_per_second{input.read_f64()};

      auto num_bone_transforms{input.read_ui32()};
      std::vector<BluCat::GRA::BoneTransform> *bone_transforms =
				&(self->mesh->animations[i].bone_transforms);
      bone_transforms->resize(num_bone_transforms);
      for(uint32_t bone_transform_index{0};
					bone_transform_index < num_bone_transforms; bone_transform_index++)
      {
				auto bone_id{input.read_ui32()};

				auto num_positions{input.read_ui32()};
				BluCat::GRA::Channel<glm::vec3> *positions =
					&((*bone_transforms)[bone_transform_index].positions);
				for(auto position_key_index{0}; position_key_index < num_positions;
						position_key_index++)
				{
					auto vec3{input.read_vec3()};
					auto timestamp{input.read_f64()};
					positions->key_frames.emplace_back(
						vec3, static_cast<float>(timestamp));
				}

				auto num_rotations{input.read_ui32()};
				BluCat::GRA::Channel<glm::quat> *rotations =
					&((*bone_transforms)[bone_transform_index].rotations);
				for(auto rotation_key_index{0}; rotation_key_index < num_rotations;
						rotation_key_index++)
				{
					auto quat{input.read_quat()};
					auto timestamp{input.read_f64()};
					rotations->key_frames.emplace_back(
						quat, static_cast<float>(timestamp));
				}

				auto num_scales{input.read_ui32()};
				BluCat::GRA::Channel<glm::vec3> *scales =
					&((*bone_transforms)[bone_transform_index].scales);
				for(auto scaling_key_index{0}; scaling_key_index < num_scales;
						scaling_key_index++)
				{
					auto vec3{input.read_vec3()};
					auto timestamp{input.read_f64()};
					scales->key_frames.emplace_back(vec3, static_cast<float>(timestamp));
				}
      }
    }
  }
}

void
unload_mesh(void *obj)
{
  auto self = static_cast<MeshBuilder*>(obj);

  delete self->mesh->index_buffer;
  delete self->mesh->vertex_buffer;
  delete self->mesh->source_vertex_buffer;
}

static const CommandChain loader{
  {&load_mesh, &unload_mesh}
};

}

namespace BluCat::GRA
{

SkeletalMesh::SkeletalMesh(std::string mesh_path)
{
  MeshBuilder mesh_builder(this, mesh_path);
  loader.execute(&mesh_builder);
}

SkeletalMesh::SkeletalMesh(const char* mesh_path):
  SkeletalMesh{std::string(mesh_path)}
{
}

SkeletalMesh::~SkeletalMesh()
{
  MeshBuilder mesh_builder(this, "");
  loader.revert(&mesh_builder);
}

}