-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.cpp
200 lines (163 loc) · 6.29 KB
/
asset.cpp
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
#include "asset.h"
#include "file_io.h"
void
Asset::PackModel(Render::model* Model)
{
uint64_t Base = (uint64_t)Model;
for(int i = 0; i < Model->MeshCount; i++)
{
Render::mesh* Mesh = Model->Meshes[i];
Mesh->Vertices = (Render::vertex*)((uint64_t)Mesh->Vertices - Base);
Mesh->Indices = (uint32_t*)((uint64_t)Mesh->Indices - Base);
Model->Meshes[i] = (Render::mesh*)((uint64_t)Model->Meshes[i] - Base);
}
if(Model->Skeleton)
{
Model->Skeleton = (Anim::skeleton*)((uint64_t)Model->Skeleton - Base);
}
Model->Meshes = (Render::mesh**)((uint64_t)Model->Meshes - Base);
}
void
Asset::UnpackModel(Render::model* Model)
{
uint64_t Base = (uint64_t)Model;
Model->Meshes = (Render::mesh**)((uint64_t)Model->Meshes + Base);
for(int i = 0; i < Model->MeshCount; i++)
{
Model->Meshes[i] = (Render::mesh*)((uint64_t)Model->Meshes[i] + Base);
Render::mesh* Mesh = Model->Meshes[i];
Mesh->Vertices = (Render::vertex*)((uint64_t)Mesh->Vertices + Base);
Mesh->Indices = (uint32_t*)((uint64_t)Mesh->Indices + Base);
}
if(Model->Skeleton)
{
Model->Skeleton = (Anim::skeleton*)((uint64_t)Model->Skeleton + Base);
}
}
void
Asset::PackAnimation(Anim::animation* Animation)
{
uint64_t Base = (uint64_t)Animation;
Animation->Transforms = (transform*)((uint64_t)Animation->Transforms - Base);
Animation->SampleTimes = (float*)((uint64_t)Animation->SampleTimes - Base);
}
void
Asset::UnpackAnimation(Anim::animation* Animation)
{
uint64_t Base = (uint64_t)Animation;
Animation->Transforms = (transform*)((uint64_t)Animation->Transforms + Base);
Animation->SampleTimes = (float*)((uint64_t)Animation->SampleTimes + Base);
}
void
Asset::PackAnimationGroup(Anim::animation_group* AnimationGroup)
{
uint64_t Base = (uint64_t)AnimationGroup;
for(int a = 0; a < AnimationGroup->AnimationCount; a++)
{
PackAnimation(AnimationGroup->Animations[a]);
AnimationGroup->Animations[a] =
(Anim::animation*)((uint64_t)AnimationGroup->Animations[a] - Base);
}
AnimationGroup->Animations = (Anim::animation**)((uint64_t)AnimationGroup->Animations - Base);
}
void
Asset::UnpackAnimationGroup(Anim::animation_group* AnimationGroup)
{
uint64_t Base = (uint64_t)AnimationGroup;
AnimationGroup->Animations = (Anim::animation**)((uint64_t)AnimationGroup->Animations + Base);
for(int a = 0; a < AnimationGroup->AnimationCount; a++)
{
AnimationGroup->Animations[a] =
(Anim::animation*)((uint64_t)AnimationGroup->Animations[a] + Base);
UnpackAnimation(AnimationGroup->Animations[a]);
}
}
void
Asset::PackMMController(mm_controller_data* Controller)
{
uint64_t Base = (uint64_t)Controller;
Controller->FrameInfos.Elements =
(mm_frame_info*)(((uint64_t)Controller->FrameInfos.Elements) - Base);
}
void
Asset::UnpackMMController(mm_controller_data* Controller)
{
uint64_t Base = (uint64_t)Controller;
Controller->FrameInfos.Elements =
(mm_frame_info*)(((uint64_t)Controller->FrameInfos.Elements) + Base);
}
void
Asset::ExportAnimationGroup(Memory::stack_allocator* Alloc,
const EditAnimation::animation_editor* AnimEditor, const char* FileName)
{
Memory::marker Marker = Alloc->GetMarker();
Anim::animation_group* AnimGroup = PushStruct(Alloc, Anim::animation_group);
AnimGroup->AnimationCount = 1;
AnimGroup->Animations = PushArray(Alloc, AnimGroup->AnimationCount, Anim::animation*);
for(int a = 0; a < AnimGroup->AnimationCount; a++)
{
AnimGroup->Animations[a] = PushStruct(Alloc, Anim::animation);
AnimGroup->Animations[a]->ChannelCount = AnimEditor->Skeleton->BoneCount;
AnimGroup->Animations[a]->KeyframeCount = AnimEditor->KeyframeCount;
AnimGroup->Animations[a]->SampleTimes =
PushArray(Alloc, AnimGroup->Animations[a]->KeyframeCount, float);
int32_t ChannelCount = AnimGroup->Animations[a]->ChannelCount;
int32_t KeyframeCount = AnimGroup->Animations[a]->KeyframeCount;
int32_t AnimationTransformCount = ChannelCount * KeyframeCount;
memcpy(AnimGroup->Animations[a]->SampleTimes, AnimEditor->SampleTimes,
KeyframeCount * sizeof(float));
for(int k = 0; k < KeyframeCount; k++)
{
AnimGroup->Animations[a]->SampleTimes[k] -= AnimEditor->SampleTimes[0];
}
AnimGroup->Animations[a]->Transforms = PushArray(Alloc, AnimationTransformCount, transform);
for(int k = 0; k < KeyframeCount; k++)
{
memcpy(&AnimGroup->Animations[a]->Transforms[k * ChannelCount],
AnimEditor->Keyframes[k].Transforms, ChannelCount * sizeof(transform));
}
}
int32_t TotalSize =
Memory::SafeTruncate_size_t_To_uint32_t(Alloc->GetByteCountAboveMarker(Marker));
PackAnimationGroup(AnimGroup);
Platform::WriteEntireFile(FileName, TotalSize, AnimGroup);
}
void
Asset::ExportMMParams(const mm_params* Params, const char* FileName)
{
Platform::WriteEntireFile(FileName, sizeof(mm_params), Params);
}
void
Asset::ImportMMParams(Memory::stack_allocator* Alloc, mm_params* OutParams, const char* FileName)
{
Memory::marker MemoryStart = Alloc->GetMarker();
debug_read_file_result ReadFile = Platform::ReadEntireFile(Alloc, FileName);
assert(ReadFile.Contents && ReadFile.ContentsSize == sizeof(mm_params));
memcpy(OutParams, ReadFile.Contents, sizeof(mm_params));
Alloc->FreeToMarker(MemoryStart);
}
#if 0
void
Asset::ImportAnimationGroup(Memory::stack_allocator* Alloc, Anim::animation_group** OutputAnimGroup,
char* FileName)
{
assert(OutputAnimGroup);
debug_read_file_result AssetReadResult = ReadEntireFile(Alloc, FileName);
assert(AssetReadResult.Contents);
Asset::asset_file_header* AssetHeader = (Asset::asset_file_header*)AssetReadResult.Contents;
UnpackAsset(AssetHeader);
*OutputAnimGroup = (Anim::animation_group*)AssetHeader->AnimationGroup;
assert(*OutputAnimGroup);
}
#endif
/*void
Asset::ImportAnimationGroup(Memory::stack_allocator* Alloc, Anim::animation_group** OutputAnimGroup,
const char* FileName)
{
assert(OutputAnimGroup);
debug_read_file_result AssetReadResult = Platform::ReadEntireFile(Alloc, FileName);
assert(AssetReadResult.Contents);
*OutputAnimGroup = (Anim::animation_group*)AssetReadResult.Contents;;
UnpackAnimationGroup(*OutputAnimGroup);
assert(*OutputAnimGroup);
}*/