summaryrefslogtreecommitdiff
path: root/src/modules/evas/model_loaders
diff options
context:
space:
mode:
authorperepelits.m <perepelits.m@samsung.com>2015-11-23 08:28:24 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-11-23 08:28:27 +0100
commitbcf38c58174406299b02d94e49c29e05f1bc58b1 (patch)
tree780683e57e90678d9431530233323ccb28ff1afd /src/modules/evas/model_loaders
parentd871ff8d11510f21c707d5431454489a9fa34f1c (diff)
evas: fix PLY loader and saver.
Summary: Use less memory by indexation vertices and not keeping more than one copy of it (from task T2713). [Fix] Reviewers: cedric, raster, Hermet Subscribers: artem.popov Differential Revision: https://phab.enlightenment.org/D3355 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/modules/evas/model_loaders')
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c125
1 files changed, 65 insertions, 60 deletions
diff --git a/src/modules/evas/model_loaders/ply/evas_model_load_ply.c b/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
index ad56abc..78b2918 100644
--- a/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
+++ b/src/modules/evas/model_loaders/ply/evas_model_load_ply.c
@@ -103,6 +103,7 @@ _read_header(char *map)//Check properties of mesh in .ply file.
103 103
104 sscanf(helping_pointer[1], "%d", &header.vertices_count); 104 sscanf(helping_pointer[1], "%d", &header.vertices_count);
105 105
106ERR("\n HEADER %d", header.vertices_count);
106 free(helping_pointer); 107 free(helping_pointer);
107 helping_pointer = eina_str_split(map, "end_header\n", 0); 108 helping_pointer = eina_str_split(map, "end_header\n", 0);
108 109
@@ -181,7 +182,7 @@ void
181evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file) 182evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
182{ 183{
183 Evas_Canvas3D_Mesh_Data *pd; 184 Evas_Canvas3D_Mesh_Data *pd;
184 int i = 0, j = 0, k = 0, count_of_triangles_in_line = 0; 185 int i = 0, j = 0, count_of_triangles_in_line = 0;
185 float *pos, *nor, *tex, *col; 186 float *pos, *nor, *tex, *col;
186 int stride_pos, stride_nor, stride_tex, stride_col; 187 int stride_pos, stride_nor, stride_tex, stride_col;
187 char *current, *map; 188 char *current, *map;
@@ -223,20 +224,20 @@ evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
223 _tex_coords_ply = malloc(header.vertices_count * 2 * sizeof(float)); 224 _tex_coords_ply = malloc(header.vertices_count * 2 * sizeof(float));
224 if (header.existence_of_colors) 225 if (header.existence_of_colors)
225 _colors_ply = malloc(header.vertices_count * 3 * sizeof(float)); 226 _colors_ply = malloc(header.vertices_count * 3 * sizeof(float));
226 int *_triangles = malloc(header.triangles_count * 3 * sizeof(int)); 227 unsigned short *_indices = malloc(header.triangles_count * 3 * sizeof(unsigned short));
227 228
228 if ((header.existence_of_geometries && (_vertices_ply == NULL)) || 229 if ((header.existence_of_geometries && (_vertices_ply == NULL)) ||
229 (header.existence_of_normals && (_normals_ply == NULL)) || 230 (header.existence_of_normals && (_normals_ply == NULL)) ||
230 (header.existence_of_texcoords && (_tex_coords_ply == NULL)) || 231 (header.existence_of_texcoords && (_tex_coords_ply == NULL)) ||
231 (header.existence_of_colors && (_colors_ply == NULL)) || 232 (header.existence_of_colors && (_colors_ply == NULL)) ||
232 (_triangles == NULL)) 233 (_indices == NULL))
233 { 234 {
234 ERR("Allocate memory is failed."); 235 ERR("Allocate memory is failed.");
235 free(_vertices_ply); 236 free(_vertices_ply);
236 free(_normals_ply); 237 free(_normals_ply);
237 free(_tex_coords_ply); 238 free(_tex_coords_ply);
238 free(_colors_ply); 239 free(_colors_ply);
239 free(_triangles); 240 free(_indices);
240 return; 241 return;
241 } 242 }
242 243
@@ -252,30 +253,30 @@ evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
252 current = _to_begin_of_line(current); 253 current = _to_begin_of_line(current);
253 } 254 }
254 255
255 for (i = 0; i < header.triangles_count;) 256 for (i = 0; i < header.triangles_count * 3;)
256 { 257 {
257 sscanf (current,"%d", &count_of_triangles_in_line); 258 sscanf (current,"%d", &count_of_triangles_in_line);
258 count_of_triangles_in_line -= 2; 259 count_of_triangles_in_line -= 2;
259 current = _to_next_number(current, 1); 260 current = _to_next_number(current, 1);
260 261
261 sscanf (current,"%i", &ARRAY_2D(_triangles, i, 0, 3)); 262 sscanf (current,"%hu", _indices + i);
262 263
263 for (j = 0; j < count_of_triangles_in_line; j++) 264 for (j = 0; j < count_of_triangles_in_line; j++)
264 { 265 {
265 if (j > 0) 266 if (j > 0)
266 ARRAY_2D(_triangles, i, 0, 3) = ARRAY_2D(_triangles, (i - 1), 0, 3); 267 _indices[i] = _indices[i - 3];
267 current = _to_next_number(current, 1); 268 current = _to_next_number(current, 1);
268 sscanf (current,"%i %i", 269 sscanf (current,"%hu %hu",
269 &ARRAY_2D(_triangles, i, 1, 3), 270 _indices + i + 1,
270 &ARRAY_2D(_triangles, i, 2, 3)); 271 _indices + i + 2);
271 i++; 272 i+=3;
272 } 273 }
273 current = _to_next_line(current); 274 current = _to_next_line(current);
274 } 275 }
275 276
276 /* prepare of mesh and take pointers to data which must be read */ 277 /* prepare of mesh and take pointers to data which must be read */
277 eo_do(mesh, 278 eo_do(mesh,
278 evas_canvas3d_mesh_vertex_count_set(header.triangles_count * 3), 279 evas_canvas3d_mesh_vertex_count_set(header.vertices_count),
279 evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES), 280 evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES),
280 evas_canvas3d_mesh_frame_add(0), 281 evas_canvas3d_mesh_frame_add(0),
281 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 0, NULL), 282 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 0, NULL),
@@ -298,62 +299,66 @@ evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
298 if (stride_tex == 0) stride_tex = sizeof(float) * 2; 299 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
299 if (stride_col == 0) stride_col = sizeof(float) * 4; 300 if (stride_col == 0) stride_col = sizeof(float) * 4;
300 301
301 for (j = 0; j < header.triangles_count; j++) 302
303 for (j = 0; j < header.vertices_count; j++)
302 { 304 {
303 for (k = 0; k < 3; k++) 305 float *p, *n, *t, *c;
304 {
305 float *p, *n, *t, *c;
306 306
307 p = (float *)((char *)pos + stride_pos * (j * 3 + k)); 307 p = (float *)((char *)pos + stride_pos * j);
308 n = (float *)((char *)nor + stride_nor * (j * 3 + k)); 308 n = (float *)((char *)nor + stride_nor * j);
309 t = (float *)((char *)tex + stride_tex * (j * 3 + k)); 309 t = (float *)((char *)tex + stride_tex * j);
310 c = (float *)((char *)col + stride_col * (j * 3 + k)); 310 c = (float *)((char *)col + stride_col * j);
311 311
312 p[0] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3);
313 p[1] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3);
314 p[2] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3);
315 312
316 if (header.existence_of_normals) 313 p[0] = ARRAY_2D(_vertices_ply, j, 0, 3);
317 { 314 p[1] = ARRAY_2D(_vertices_ply, j, 1, 3);
318 n[0] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3); 315 p[2] = ARRAY_2D(_vertices_ply, j, 2, 3);
319 n[1] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3);
320 n[2] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3);
321 }
322 else
323 {
324 n[0] = 0.0;
325 n[1] = 0.0;
326 n[2] = 0.0;
327 }
328 316
329 if (header.existence_of_texcoords) 317 if (header.existence_of_normals)
330 { 318 {
331 t[0] = ARRAY_2D(_tex_coords_ply, ARRAY_2D(_triangles, j, k, 3), 0, 2); 319 n[0] = ARRAY_2D(_normals_ply, j, 0, 3);
332 t[1] = ARRAY_2D(_tex_coords_ply, ARRAY_2D(_triangles, j, k, 3), 1, 2); 320 n[1] = ARRAY_2D(_normals_ply, j, 1, 3);
333 } 321 n[2] = ARRAY_2D(_normals_ply, j, 2, 3);
334 else 322 }
335 { 323 else
336 t[0] = 0.0; 324 {
337 t[1] = 0.0; 325 n[0] = 0.0;
338 } 326 n[1] = 0.0;
327 n[2] = 0.0;
328 }
339 329
340 if (header.existence_of_colors) 330 if (header.existence_of_texcoords)
341 { 331 {
342 c[0] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3); 332 t[0] = ARRAY_2D(_tex_coords_ply, j, 0, 2);
343 c[1] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3); 333 t[1] = ARRAY_2D(_tex_coords_ply, j, 1, 2);
344 c[2] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3); 334 }
345 c[3] = 1.0; 335 else
346 } 336 {
347 else 337 t[0] = 0.0;
348 { 338 t[1] = 0.0;
349 c[0] = 0.0; 339 }
350 c[1] = 0.0; 340
351 c[2] = 0.0; 341 if (header.existence_of_colors)
352 c[3] = 1.0; 342 {
353 } 343 c[0] = ARRAY_2D(_colors_ply, j, 0, 3);
344 c[1] = ARRAY_2D(_colors_ply, j, 1, 3);
345 c[2] = ARRAY_2D(_colors_ply, j, 2, 3);
346 c[3] = 1.0;
347 }
348 else
349 {
350 c[0] = 0.0;
351 c[1] = 0.0;
352 c[2] = 0.0;
353 c[3] = 1.0;
354 } 354 }
355 } 355 }
356 356
357 eo_do(mesh,
358 evas_canvas3d_mesh_index_data_copy_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT,
359 header.triangles_count * 3,
360 _indices));
361
357 free(helping_pointer); 362 free(helping_pointer);
358 free(_vertices_ply); 363 free(_vertices_ply);
359 if (header.existence_of_normals) 364 if (header.existence_of_normals)
@@ -362,7 +367,7 @@ evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
362 free(_tex_coords_ply); 367 free(_tex_coords_ply);
363 if (header.existence_of_colors) 368 if (header.existence_of_colors)
364 free(_colors_ply); 369 free(_colors_ply);
365 free(_triangles); 370 free(_indices);
366 371
367 /* Unmap vertex buffer. */ 372 /* Unmap vertex buffer. */
368 eo_do(mesh, 373 eo_do(mesh,