summaryrefslogtreecommitdiff
path: root/src/modules/evas/model_loaders
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2015-12-10 18:20:43 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2015-12-10 18:21:09 +0900
commit4bcd2fa0f080ad8d2bb0a78250d01ec14954538d (patch)
tree7a92c34f9de09b8f2154706d61df5b707df2b5af /src/modules/evas/model_loaders
parent58bfa4b3283bfc21276faa2cb09d37656a9912ec (diff)
Revert "Evas: Refactor model's savers and loaders."
This reverts commit 01a32f64c0ffbe2ce048f8eb016d685913558cf5. This broke make check with the following error: evas_test_mesh.c:123:F:Meshes:evas_object_mesh_loader_saver:0: Failure 'res == 1' occurred Reopen https://phab.enlightenment.org/D3420
Diffstat (limited to 'src/modules/evas/model_loaders')
-rw-r--r--src/modules/evas/model_loaders/eet/evas_model_load_eet.c2
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c319
2 files changed, 242 insertions, 79 deletions
diff --git a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
index e049e2c..f5b1b6b 100644
--- a/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
+++ b/src/modules/evas/model_loaders/eet/evas_model_load_eet.c
@@ -127,3 +127,5 @@ evas_model_load_file_eet(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
127 127
128 _evas_canvas3d_eet_file_free(eet_file); 128 _evas_canvas3d_eet_file_free(eet_file);
129} 129}
130
131
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 971e951..1beef18 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
@@ -1,4 +1,44 @@
1#include "evas_model_load_save_common.h" 1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <stdlib.h>
6#include "stdio.h"
7#include "evas_common_private.h"
8#include "evas_private.h"
9#include <Eina.h>
10
11/* set value to position [x][y] to array name which have. */
12#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
13
14/* Structures for reading data from file. */
15typedef struct _PLY_Header PLY_Header;
16
17struct _PLY_Header
18{
19 int vertices_count;
20 int triangles_count;
21 Eina_Bool existence_of_geometries;
22 Eina_Bool existence_of_normals;
23 Eina_Bool existence_of_texcoords;
24 Eina_Bool existence_of_colors;
25};
26
27/* create new header */
28static inline PLY_Header
29_new_ply_header()
30{
31 PLY_Header header;
32
33 header.vertices_count = 0;
34 header.triangles_count = 0;
35 header.existence_of_geometries = EINA_FALSE;
36 header.existence_of_normals = EINA_FALSE;
37 header.existence_of_texcoords = EINA_FALSE;
38 header.existence_of_colors = EINA_FALSE;
39
40 return header;
41}
2 42
3static inline char * 43static inline char *
4_to_next_line(char *current) 44_to_next_line(char *current)
@@ -41,8 +81,8 @@ _read_data(float *array, int place, int count, char *current, float divider)
41 return current; 81 return current;
42} 82}
43 83
44static inline Eina_Bool 84static inline PLY_Header
45_read_ply_header(char *map, Evas_Model_Load_Save_Header *header) 85_read_header(char *map)//Check properties of mesh in .ply file.
46{ 86{
47 eina_init(); 87 eina_init();
48 88
@@ -50,29 +90,30 @@ _read_ply_header(char *map, Evas_Model_Load_Save_Header *header)
50 int vertex_lines, triangles = 0, vertices_in_current_face = 0; 90 int vertex_lines, triangles = 0, vertices_in_current_face = 0;
51 char **helping_pointer; 91 char **helping_pointer;
52 char *current; 92 char *current;
93 PLY_Header header;
53 94
95 header = _new_ply_header();
54 helping_pointer = eina_str_split(map, "vertex ", 0); 96 helping_pointer = eina_str_split(map, "vertex ", 0);
55 97
56 if (helping_pointer == NULL) 98 if (helping_pointer == NULL)
57 { 99 {
58 ERR("File have not kayword vertex. It is necessary."); 100 ERR("File have not kayword vertex. It is necessary.");
59 return EINA_FALSE; 101 return header;
60 } 102 }
61 103
62 sscanf(helping_pointer[1], "%d", &header->vertices_count); 104 sscanf(helping_pointer[1], "%d", &header.vertices_count);
63
64 free(helping_pointer); 105 free(helping_pointer);
65 helping_pointer = eina_str_split(map, "end_header\n", 0); 106 helping_pointer = eina_str_split(map, "end_header\n", 0);
66 107
67 if (helping_pointer == NULL) 108 if (helping_pointer == NULL)
68 { 109 {
69 ERR("File have not kayword end_header. It is necessary."); 110 ERR("File have not kayword end_header. It is necessary.");
70 return EINA_FALSE; 111 return header;
71 } 112 }
72 113
73 current = helping_pointer[1]; 114 current = helping_pointer[1];
74 115
75 vertex_lines = header->vertices_count; 116 vertex_lines = header.vertices_count;
76 while (*current != '\0') 117 while (*current != '\0')
77 { 118 {
78 if (vertex_lines == 1) 119 if (vertex_lines == 1)
@@ -98,7 +139,8 @@ _read_ply_header(char *map, Evas_Model_Load_Save_Header *header)
98 current++; 139 current++;
99 } 140 }
100 141
101 header->indices_count = 3 * triangles; 142 header.triangles_count = triangles;
143 free(helping_pointer);
102 144
103 /* analyse flags used when file was saved in blender */ 145 /* analyse flags used when file was saved in blender */
104 helping_pointer = eina_str_split(map, "property float ", 0); 146 helping_pointer = eina_str_split(map, "property float ", 0);
@@ -106,124 +148,243 @@ _read_ply_header(char *map, Evas_Model_Load_Save_Header *header)
106 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') && 148 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') &&
107 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') && 149 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') &&
108 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'z')) 150 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'z'))
109 header->existence_of_positions = EINA_TRUE; 151 header.existence_of_geometries = EINA_TRUE;
110 else return EINA_FALSE; 152 else return header;
111 153
112 if ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 'n') && 154 if ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 'n') &&
113 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 'n') && 155 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 'n') &&
114 (helping_pointer[6] != NULL) && (*helping_pointer[6] == 'n')) 156 (helping_pointer[6] != NULL) && (*helping_pointer[6] == 'n'))
115 header->existence_of_normals = EINA_TRUE; 157 header.existence_of_normals = EINA_TRUE;
116 158
117 if ((header->existence_of_normals && 159 if ((header.existence_of_normals &&
118 ((helping_pointer[7] != NULL) && (*helping_pointer[7] == 's') && 160 ((helping_pointer[7] != NULL) && (*helping_pointer[7] == 's') &&
119 (helping_pointer[8] != NULL) && (*helping_pointer[8] == 't'))) || 161 (helping_pointer[8] != NULL) && (*helping_pointer[8] == 't'))) ||
120 (!header->existence_of_normals && 162 (!header.existence_of_normals &&
121 ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 's') && 163 ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 's') &&
122 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't')))) 164 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't'))))
123 header->existence_of_tex_coords = EINA_TRUE; 165 header.existence_of_texcoords = EINA_TRUE;
124 166
125 helping_pointer = eina_str_split(map, "property uchar ", 0); 167 helping_pointer = eina_str_split(map, "property uchar ", 0);
126 168
127 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') && 169 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') &&
128 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') && 170 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') &&
129 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'b')) 171 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'b'))
130 header->existence_of_colors = EINA_TRUE; 172 header.existence_of_colors = EINA_TRUE;
173
174 free(helping_pointer);
175
176 return header;
177}
178
179void
180evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
181{
182 Evas_Canvas3D_Mesh_Data *pd;
183 int i = 0, j = 0, count_of_triangles_in_line = 0;
184 float *pos, *nor, *tex, *col;
185 int stride_pos, stride_nor, stride_tex, stride_col;
186 char *current, *map;
187 PLY_Header header;
188 float *_vertices_ply = NULL, *_normals_ply = NULL;
189 float *_tex_coords_ply = NULL, *_colors_ply = NULL;
190 char **helping_pointer;
191
192 map = eina_file_map_all(file, EINA_FILE_SEQUENTIAL);
131 193
132 if (!header->existence_of_positions) 194 if (map == NULL)
195 {
196 ERR("Failed to create map from file %s\n", eina_file_filename_get(file));
197 return;
198 }
199
200 header = _read_header(map);
201
202 if (!header.existence_of_geometries)
133 { 203 {
134 ERR("File have not x, y, or z field as the first 3 float fields. They are necessary."); 204 ERR("File have not x, y, or z field as the first 3 float fields. They are necessary.");
135 return EINA_FALSE; 205 return;
136 } 206 }
137 207
138 free(helping_pointer); 208 helping_pointer = eina_str_split(map, "end_header\n", 0);
139 return EINA_TRUE; 209
140} 210 if (helping_pointer == NULL)
211 {
212 ERR("File have not kayword end_header. It is necessary.");
213 return;
214 }
215
216 current = helping_pointer[1];
217
218 _vertices_ply = malloc(header.vertices_count * 3 * sizeof(float));
219 if (header.existence_of_normals)
220 _normals_ply = malloc(header.vertices_count * 3 * sizeof(float));
221 if (header.existence_of_texcoords)
222 _tex_coords_ply = malloc(header.vertices_count * 2 * sizeof(float));
223 if (header.existence_of_colors)
224 _colors_ply = malloc(header.vertices_count * 3 * sizeof(float));
225 unsigned short *_indices = malloc(header.triangles_count * 3 * sizeof(unsigned short));
226
227 if ((header.existence_of_geometries && (_vertices_ply == NULL)) ||
228 (header.existence_of_normals && (_normals_ply == NULL)) ||
229 (header.existence_of_texcoords && (_tex_coords_ply == NULL)) ||
230 (header.existence_of_colors && (_colors_ply == NULL)) ||
231 (_indices == NULL))
232 {
233 ERR("Allocate memory is failed.");
234 free(_vertices_ply);
235 free(_normals_ply);
236 free(_tex_coords_ply);
237 free(_colors_ply);
238 free(_indices);
239 return;
240 }
141 241
142static inline void
143_read_ply_vertex_data(Evas_Model_Load_Save_Header header,
144 char **current,
145 Evas_Model_Load_Save_Data data)
146{
147 int i;
148 for (i = 0; i < header.vertices_count; i++) 242 for (i = 0; i < header.vertices_count; i++)
149 { 243 {
150 *current = _read_data(data.positions, i, 3, *current, 1.0); 244 current = _read_data(_vertices_ply, i, 3, current, 1.0);
151 if (header.existence_of_normals) 245 if (header.existence_of_normals)
152 *current = _read_data(data.normals, i, 3, *current, 1.0); 246 current = _read_data(_normals_ply, i, 3, current, 1.0);
153 if (header.existence_of_tex_coords) 247 if (header.existence_of_texcoords)
154 *current = _read_data(data.tex_coords, i, 2, *current, 1.0); 248 current = _read_data(_tex_coords_ply, i, 2, current, 1.0);
155 if (header.existence_of_colors) 249 if (header.existence_of_colors)
156 *current = _read_data(data.colors, i, 3, *current, 255.0); 250 current = _read_data(_colors_ply, i, 3, current, 255.0);
157 *current = _to_begin_of_line(*current); 251 current = _to_begin_of_line(current);
158 } 252 }
159}
160 253
161static inline void 254 for (i = 0; i < header.triangles_count * 3;)
162_read_ply_indices_data(Evas_Model_Load_Save_Header header,
163 char **current,
164 Evas_Model_Load_Save_Data data)
165{
166 int i, j, count_of_triangles_in_line = 0;
167
168 for (i = 0; i < header.indices_count;)
169 { 255 {
170 sscanf (*current,"%d", &count_of_triangles_in_line); 256 sscanf (current,"%d", &count_of_triangles_in_line);
171 count_of_triangles_in_line -= 2; 257 count_of_triangles_in_line -= 2;
172 *current = _to_next_number(*current, 1); 258 current = _to_next_number(current, 1);
173 259
174 sscanf (*current,"%hu", data.indices + i); 260 sscanf (current,"%hu", _indices + i);
175 261
176 for (j = 0; j < count_of_triangles_in_line; j++) 262 for (j = 0; j < count_of_triangles_in_line; j++)
177 { 263 {
178 if (j > 0) 264 if (j > 0)
179 data.indices[i] = data.indices[i - 3]; 265 _indices[i] = _indices[i - 3];
180 *current = _to_next_number(*current, 1); 266 current = _to_next_number(current, 1);
181 sscanf (*current,"%hu %hu", 267 sscanf (current,"%hu %hu",
182 data.indices + i + 1, 268 _indices + i + 1,
183 data.indices + i + 2); 269 _indices + i + 2);
184 i+=3; 270 i+=3;
185 } 271 }
186 *current = _to_next_line(*current); 272 current = _to_next_line(current);
187 } 273 }
188}
189 274
190void 275 /* prepare of mesh and take pointers to data which must be read */
191evas_model_load_file_ply(Evas_Canvas3D_Mesh *mesh, Eina_File *file) 276 eo_do(mesh,
192{ 277 evas_canvas3d_mesh_vertex_count_set(header.vertices_count),
193 char *current = NULL, *map = NULL; 278 evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES),
194 Evas_Model_Load_Save_Header header; 279 evas_canvas3d_mesh_frame_add(0),
195 Evas_Model_Load_Save_Data data; 280 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 0, NULL),
196 Evas_Model_Load_Save_Stride stride; 281 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, 0, NULL),
197 282 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, 0, NULL),
198 map = eina_file_map_all(file, EINA_FILE_SEQUENTIAL); 283 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, 0, NULL),
199 if (map == NULL) 284
285 pos = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
286 nor = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
287 tex = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
288 col = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR),
289
290 stride_pos = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
291 stride_nor = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
292 stride_tex = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
293 stride_col = evas_canvas3d_mesh_frame_vertex_stride_get(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR));
294
295 if (stride_pos == 0) stride_pos = sizeof(float) * 3;
296 if (stride_nor == 0) stride_nor = sizeof(float) * 3;
297 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
298 if (stride_col == 0) stride_col = sizeof(float) * 4;
299
300
301 for (j = 0; j < header.vertices_count; j++)
200 { 302 {
201 ERR("Failed to create map from file %s\n", eina_file_filename_get(file)); 303 float *p, *n, *t, *c;
202 return; 304
305 p = (float *)((char *)pos + stride_pos * j);
306 n = (float *)((char *)nor + stride_nor * j);
307 t = (float *)((char *)tex + stride_tex * j);
308 c = (float *)((char *)col + stride_col * j);
309
310
311 p[0] = ARRAY_2D(_vertices_ply, j, 0, 3);
312 p[1] = ARRAY_2D(_vertices_ply, j, 1, 3);
313 p[2] = ARRAY_2D(_vertices_ply, j, 2, 3);
314
315 if (header.existence_of_normals)
316 {
317 n[0] = ARRAY_2D(_normals_ply, j, 0, 3);
318 n[1] = ARRAY_2D(_normals_ply, j, 1, 3);
319 n[2] = ARRAY_2D(_normals_ply, j, 2, 3);
320 }
321 else
322 {
323 n[0] = 0.0;
324 n[1] = 0.0;
325 n[2] = 0.0;
326 }
327
328 if (header.existence_of_texcoords)
329 {
330 t[0] = ARRAY_2D(_tex_coords_ply, j, 0, 2);
331 t[1] = ARRAY_2D(_tex_coords_ply, j, 1, 2);
332 }
333 else
334 {
335 t[0] = 0.0;
336 t[1] = 0.0;
337 }
338
339 if (header.existence_of_colors)
340 {
341 c[0] = ARRAY_2D(_colors_ply, j, 0, 3);
342 c[1] = ARRAY_2D(_colors_ply, j, 1, 3);
343 c[2] = ARRAY_2D(_colors_ply, j, 2, 3);
344 c[3] = 1.0;
345 }
346 else
347 {
348 c[0] = 0.0;
349 c[1] = 0.0;
350 c[2] = 0.0;
351 c[3] = 1.0;
352 }
203 } 353 }
204 354
205 header = evas_model_load_save_header_new(); 355 eo_do(mesh,
206 if(!_read_ply_header(map, &header)) return; 356 evas_canvas3d_mesh_index_data_copy_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT,
357 header.triangles_count * 3,
358 _indices));
207 359
208 if (!evas_model_load_allocate_data_due_to_header(header, &data)) 360 free(helping_pointer);
361 free(_vertices_ply);
362 if (header.existence_of_normals)
363 free(_normals_ply);
364 if (header.existence_of_texcoords)
365 free(_tex_coords_ply);
366 if (header.existence_of_colors)
367 free(_colors_ply);
368 free(_indices);
369
370 /* Unmap vertex buffer. */
371 eo_do(mesh,
372 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION),
373 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL),
374 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD),
375 evas_canvas3d_mesh_frame_vertex_data_unmap(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR));
376
377 pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS);
378
379 if (!evas_canvas3d_mesh_aabb_add_to_frame(pd, 0, stride_pos))
209 { 380 {
210 ERR("Memory allocation is failed."); 381 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
211 return;
212 } 382 }
213 383
214 current = eina_str_split(map, "end_header\n", 0)[1];
215 _read_ply_vertex_data(header, &current, data);
216 _read_ply_indices_data(header, &current, data);
217 evas_model_load_vertex_data_to_mesh(mesh, header, data, &stride);
218 evas_model_load_indices_data_to_mesh(mesh, header, data);
219 evas_model_load_vertex_data_unmap(mesh, 0, header);
220 evas_model_load_aabb_add_to_frame(mesh, 0, stride);
221
222 if (map) 384 if (map)
223 { 385 {
224 eina_file_map_free(file, map); 386 eina_file_map_free(file, map);
225 map = NULL; 387 map = NULL;
226 } 388 }
227
228 evas_model_load_save_data_free(header, &data);
229} 389}
390