22 #include <android/log.h>
23 #define LOG_TAG "Asset_Loader"
24 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
25 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
33 bool load_file(
const std::string& a_path,
char **a_buffer,
int &a_bytes_read,
const bool binary)
39 file.open( a_path.c_str(), std::ios::in );
43 LOGE(
"Error! opening file %s", a_path.c_str());
48 size_t file_size = file.tellg();
49 file.seekg(0, std::ios_base::beg);
53 LOGE(
"Error! reading file size %s ", a_path.c_str());
57 *a_buffer =
new char[file_size];
61 LOGE(
"Error! Out of memory allocating *a_buffer");
65 file.read(*a_buffer, file_size);
68 a_bytes_read =
static_cast<int>(file_size);
107 this->m_has_animation =
false;
108 this->m_has_materials =
false;
109 this->m_has_indices =
false;
110 this->m_has_normals =
false;
111 this->m_has_texture_coordinates0 =
false;
113 this->m_vertices_count = NULL;
114 this->m_indices_count = NULL;
115 this->m_bones_count = NULL;
116 this->m_keyframes_count = NULL;
117 this->m_materials_count = NULL;
119 this->m_positions = NULL;
120 this->m_texture_coordinates0 = NULL;
121 this->m_normals = NULL;
122 this->m_bone_ids = NULL;
123 this->m_weights = NULL;
124 this->m_materials = NULL;
125 this->m_indices = NULL;
126 this->m_keyframes = NULL;
128 this->m_geometry_buffer = NULL;
129 this->m_tangent_buffer = NULL;
130 this->m_animation_buffer = NULL;
132 this->m_bounding_box_minimum = NULL;
133 this->m_bounding_box_maximum = NULL;
138 if (this->m_has_materials)
140 for (
unsigned int i = 0; i < *this->m_materials_count; ++i)
142 delete this->m_materials[i];
144 delete[] this->m_materials;
147 if (this->m_has_animation)
149 for (
unsigned int i = 0; i < *this->m_keyframes_count; ++i)
151 delete this->m_keyframes[i];
153 delete[] this->m_keyframes;
156 this->m_vertices_count = NULL;
157 this->m_indices_count = NULL;
158 this->m_bones_count = NULL;
159 this->m_keyframes_count = NULL;
160 this->m_materials_count = NULL;
162 this->m_positions = NULL;
163 this->m_texture_coordinates0 = NULL;
164 this->m_normals = NULL;
165 this->m_bone_ids = NULL;
166 this->m_weights = NULL;
167 this->m_materials = NULL;
168 this->m_indices = NULL;
169 this->m_keyframes = NULL;
171 this->m_bounding_box_minimum = NULL;
172 this->m_bounding_box_maximum = NULL;
174 delete[] this->m_geometry_buffer;
175 delete[] this->m_tangent_buffer;
176 delete[] this->m_animation_buffer;
178 this->m_geometry_buffer = NULL;
179 this->m_tangent_buffer = NULL;
180 this->m_animation_buffer = NULL;
183 bool Model3D::load(
const std::string& a_path)
186 if (!
load_file(a_path, &this->m_geometry_buffer, bytes_read))
188 LOGE(
"Error! opening geometry file %s", a_path.c_str());
192 char *
buffer = this->m_geometry_buffer;
195 if (buffer[0] !=
'g' ||
200 LOGE(
"%s is not a valid 'geom' file", a_path.c_str() );
201 delete []this->m_geometry_buffer;
205 buffer +=
sizeof(
unsigned int);
207 this->m_has_indices = *((
unsigned int*)buffer) & 1;
208 this->m_has_texture_coordinates0 = *((
unsigned int*)buffer) & (1 << 8);
209 this->m_has_materials = *((
unsigned int*)buffer) & (1 << 12);
210 this->m_has_normals = *((
unsigned int*)buffer) & (1 << 16);
211 this->m_has_animation = *((
unsigned int*)buffer) & (1 << 24);
213 buffer +=
sizeof(
unsigned int);
215 buffer +=
sizeof(
float ) * 16 ;
218 this->m_bounding_box_minimum = (
float*)buffer;
219 buffer += (
sizeof(
float) * 3);
220 this->m_bounding_box_maximum = (
float*)buffer;
221 buffer += (
sizeof(
float) * 3);
224 this->m_vertices_count = (
unsigned int*) buffer;
225 buffer +=
sizeof(
unsigned int);
226 this->m_positions = (
float*)buffer;
227 buffer += (
sizeof(
float) * 3 * (*this->m_vertices_count));
229 LOGI(
"Vx count=%u", *this->m_vertices_count );
232 if (this->m_has_texture_coordinates0)
234 this->m_texture_coordinates0 = (
float*)buffer;
235 buffer += (
sizeof(
float) * 3 * (*this->m_vertices_count));
239 if (this->m_has_normals)
241 this->m_normals = (
float*)buffer;
242 buffer += (
sizeof(
float) * 3 * (*this->m_vertices_count));
246 if (this->m_has_animation)
249 this->m_bone_ids = (
unsigned int*)buffer;
250 buffer += (
sizeof(
unsigned int) * 4 * (*this->m_vertices_count));
253 this->m_weights = (
float*)buffer;
254 buffer += (
sizeof(
float) * 4 * (*this->m_vertices_count));
258 if (this->m_has_indices)
260 this->m_indices_count = (
unsigned int*)buffer;
261 buffer +=
sizeof(
unsigned int);
262 this->m_indices = (
unsigned int*)buffer;
263 buffer += (
sizeof(
unsigned int) * 3 * (*this->m_indices_count));
267 if (this->m_has_materials)
269 this->m_materials_count = (
unsigned int*)buffer;
270 buffer +=
sizeof(
unsigned int);
272 this->m_materials =
new Material*[*this->m_materials_count];
274 for (
unsigned int i = 0; i < *this->m_materials_count; ++i)
276 this->m_materials[i] =
new Material();
278 this->m_materials[i]->m_ambient = (
float*)buffer; buffer += (
sizeof(
float) * 4);
279 this->m_materials[i]->m_diffuse = (
float*)buffer; buffer += (
sizeof(
float) * 4);
280 this->m_materials[i]->m_specular = (
float*)buffer; buffer += (
sizeof(
float) * 4);
281 this->m_materials[i]->m_emmision = (
float*)buffer; buffer += (
sizeof(
float) * 4);
282 this->m_materials[i]->m_shine = (
float*)buffer; buffer +=
sizeof(
float);
283 this->m_materials[i]->m_transparency = (
float*)buffer; buffer +=
sizeof(
float);
288 if ( this->m_has_normals && this->m_has_texture_coordinates0 )
291 if ( !
load_file( a_path +
"tan", &this->m_tangent_buffer, bytes_read_tan ) )
293 LOGE(
"Error! opening tangent file %s", (a_path +
"tan").c_str() );
297 char *bufferTan = this->m_tangent_buffer;
300 if ( bufferTan[0] !=
'g' ||
301 bufferTan[1] !=
'e' ||
302 bufferTan[2] !=
'o' ||
303 bufferTan[3] !=
'm' ||
304 bufferTan[4] !=
't' ||
305 bufferTan[5] !=
'a' ||
306 bufferTan[6] !=
'n' )
308 LOGE(
"%s is not a valid 'geomtan' file", (a_path +
"tan").c_str() );
309 delete[]this->m_tangent_buffer;
314 this->m_tangents = (
float*) bufferTan;
316 LOGI(
"Finished loading");
319 if (this->m_has_animation)
322 std::string animation_filename = a_path.substr(0, a_path.length() - 5);
323 animation_filename = animation_filename.append(
".anim");
325 if (!
load_file(animation_filename, &this->m_animation_buffer, bytes_read))
327 LOGE(
"Error! opening animation file %s", animation_filename.c_str());
331 char *buffer = this->m_animation_buffer;
334 if (buffer[0] !=
'a' ||
339 LOGE(
"%s is not a valid 'anim' file", a_path.c_str());
340 delete []this->m_animation_buffer;
344 buffer +=
sizeof(
unsigned int);
347 this->m_bones_count = (
unsigned int*)buffer;
348 buffer +=
sizeof(
unsigned int);
351 this->m_keyframes_count = (
unsigned int*)buffer;
352 buffer +=
sizeof(
unsigned int);
354 this->m_keyframes =
new Keyframe*[*this->m_keyframes_count];
355 char *transforms_buffer = buffer + (*this->m_keyframes_count *
sizeof(
float));
357 for (
unsigned int i = 0; i < *this->m_keyframes_count; ++i)
359 this->m_keyframes[i] =
new Keyframe();
360 this->m_keyframes[i]->m_time = (
float*)buffer;
361 this->m_keyframes[i]->m_transforms = (
float*)transforms_buffer;
363 buffer +=
sizeof(
float);
364 transforms_buffer +=
sizeof(
float) * 16 * (*this->m_bones_count);
372 unsigned int Model3D::get_indices_count()
const
374 return *this->m_indices_count;
377 unsigned int Model3D::get_keyframes_count()
const
379 return *this->m_keyframes_count;
382 float* Model3D::get_positions()
const
384 return this->m_positions;
387 float* Model3D::get_normals()
const
389 return this->m_normals;
392 float* Model3D::get_texture_coordinates0()
const
394 return this->m_texture_coordinates0;
397 float* Model3D::get_tangents()
const
399 return this->m_tangents;
402 unsigned int* Model3D::get_indices()
const
404 return this->m_indices;
float * m_transforms
Transformations for all the bones in the current keyframe.
float * m_ambient
4D Ambient color of the material
GLsizei GLsizei GLenum void * binary
float * m_transparency
Transparency of the material.
bool load_file(const std::string &a_path, char **a_buffer, int &a_bytes_read, const bool binary)
float * m_shine
Shine exponent of the material.
float * m_diffuse
4D Diffuse color of the material
float * m_specular
4D Specular color of the material
float * m_time
Keyframe time in seconds for the all the bones.
float * m_emmision
4D Emissive color of the material