summaryrefslogtreecommitdiff
path: root/src/modules/evas/model_loaders
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2014-11-27 13:27:01 +0200
committerCedric BAIL <cedric@osg.samsung.com>2014-12-23 21:14:30 +0100
commit9b7e22f5a5a2ee734264265267e2186cbebce65c (patch)
treecc175159cdd5320b1e9ecbd0644a927c49eb4894 /src/modules/evas/model_loaders
parent8c09ad1b81b0139b6c97444b232950bd2a0df5e6 (diff)
evas: replace and rename savers/loaders.
Diffstat (limited to 'src/modules/evas/model_loaders')
-rw-r--r--src/modules/evas/model_loaders/eet/evas_model_load_eet.c132
-rw-r--r--src/modules/evas/model_loaders/md2/evas_model_load_md2.c447
-rw-r--r--src/modules/evas/model_loaders/obj/evas_model_load_obj.c402
-rw-r--r--src/modules/evas/model_loaders/ply/evas_model_load_ply.c402
4 files changed, 1379 insertions, 4 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 8940b65..26c3569 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
@@ -1 +1,131 @@
1//dummy of saver/loader 1#ifdef HAVE_CONFIG_H
2 #include "config.h"
3#endif
4
5#include <Eo.h>
6#include <stdio.h>
7#include "evas_common_private.h"
8#include "evas_private.h"
9
10extern Evas_3D_File_Eet* eet_file;
11extern const char EVAS_3D_FILE_CACHE_FILE_ENTRY[];
12extern Eet_Data_Descriptor *_file_descriptor;
13
14void
15_set_geometry_to_mesh_from_eet_file(Evas_3D_Mesh *mesh,
16 Evas_3D_Mesh_Eet *eet_mesh)
17{
18 float *pos, *nor, *tex;
19 int stride_pos, stride_nor, stride_tex, j;
20
21 eo_do(mesh,
22 evas_3d_mesh_vertex_count_set(eet_mesh->geometries->vertices_count),
23 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
24 evas_3d_mesh_frame_add(0),
25
26 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION, 0, NULL),
27 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL, 0, NULL),
28 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_TEXCOORD, 0, NULL),
29
30 pos = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_POSITION),
31 nor = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_NORMAL),
32 tex = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_TEXCOORD),
33
34 stride_pos = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_POSITION),
35 stride_nor = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_NORMAL),
36 stride_tex = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_TEXCOORD));
37
38 if (stride_pos == 0) stride_pos = sizeof(float) * 3;
39 if (stride_nor == 0) stride_nor = sizeof(float) * 3;
40 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
41
42 /* set data to vertex buffer */
43 for (j = 0; j < eet_mesh->geometries->vertices_count; j++)
44 {
45 float *p, *n, *t;
46
47 p = (float *)((char *)pos + stride_pos * j);
48 n = (float *)((char *)nor + stride_nor * j);
49 t = (float *)((char *)tex + stride_tex * j);
50
51 p[0] = eet_mesh->geometries->vertices[j].position.x;
52 p[1] = eet_mesh->geometries->vertices[j].position.y;
53 p[2] = eet_mesh->geometries->vertices[j].position.z;
54
55 n[0] = eet_mesh->geometries->vertices[j].normal.x;
56 n[1] = eet_mesh->geometries->vertices[j].normal.y;
57 n[2] = eet_mesh->geometries->vertices[j].normal.z;
58
59 t[0] = eet_mesh->geometries->vertices[j].texcoord.x;
60 t[1] = eet_mesh->geometries->vertices[j].texcoord.y;
61 }
62
63 /* unmap vertex buffer */
64 eo_do(mesh,
65 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_POSITION),
66 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_NORMAL),
67 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_TEXCOORD));
68}
69
70void
71_set_material_to_mesh_from_eet_file(Evas_3D_Mesh *mesh,
72 Evas_3D_Mesh_Eet *eet_mesh)
73{
74 Evas_3D_Object_Data *pd = eo_data_scope_get(mesh, EVAS_3D_OBJECT_CLASS);
75 Eo *material = NULL;
76 material = eo_add(EVAS_3D_MATERIAL_CLASS, pd->evas);
77
78 eo_do(material,
79 evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT,
80 !!(eet_mesh->materials->colors[0].a > 0)),
81 evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE,
82 !!(eet_mesh->materials->colors[1].a > 0)),
83 evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR,
84 !!(eet_mesh->materials->colors[2].a > 0)),
85 evas_3d_material_enable_set(EVAS_3D_MATERIAL_NORMAL, EINA_TRUE),
86 evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT,
87 eet_mesh->materials->colors[0].r,
88 eet_mesh->materials->colors[0].g,
89 eet_mesh->materials->colors[0].b,
90 eet_mesh->materials->colors[0].a),
91 evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE,
92 eet_mesh->materials->colors[1].r,
93 eet_mesh->materials->colors[1].g,
94 eet_mesh->materials->colors[1].b,
95 eet_mesh->materials->colors[1].a),
96 evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR,
97 eet_mesh->materials->colors[2].r,
98 eet_mesh->materials->colors[2].g,
99 eet_mesh->materials->colors[2].b,
100 eet_mesh->materials->colors[2].a),
101 evas_3d_material_shininess_set(eet_mesh->materials->shininess));
102
103 eo_do(mesh, evas_3d_mesh_frame_material_set(0, material));
104}
105
106void
107evas_3d_mesh_file_eet_set(Evas_3D_Mesh *mesh, const char *file)
108{
109 Eet_File *ef;
110
111 _evas_3d_eet_file_init();
112 ef = eet_open(file, EET_FILE_MODE_READ);
113 eet_file = eet_data_read(ef,
114 _file_descriptor,
115 EVAS_3D_FILE_CACHE_FILE_ENTRY);
116
117 eet_close(ef);
118
119 if ((eet_file->mesh == NULL) || (eet_file->header == NULL))
120 {
121 ERR("Reading of file is failed.");
122 _evas_3d_eet_file_free();
123 return;
124 }
125
126 _set_geometry_to_mesh_from_eet_file(mesh, eet_file->mesh);
127 _set_material_to_mesh_from_eet_file(mesh, eet_file->mesh);
128
129 _evas_3d_eet_file_free();
130}
131
diff --git a/src/modules/evas/model_loaders/md2/evas_model_load_md2.c b/src/modules/evas/model_loaders/md2/evas_model_load_md2.c
index 8940b65..4a356b7 100644
--- a/src/modules/evas/model_loaders/md2/evas_model_load_md2.c
+++ b/src/modules/evas/model_loaders/md2/evas_model_load_md2.c
@@ -1 +1,446 @@
1//dummy of saver/loader 1#include "evas_common_private.h"
2#include "evas_private.h"
3
4#define PACKED __attribute__((__packed__))
5
6#define MD2_MAGIC_NUMBER 844121161
7#define MD2_VERSION 8
8#define MD2_FRAME_SCALE 256
9
10/* Structures for reading data from file. */
11typedef struct _MD2_Header MD2_Header;
12typedef struct _MD2_Vertex MD2_Vertex;
13typedef struct _MD2_Frame MD2_Frame;
14typedef struct _MD2_Triangle MD2_Triangle;
15typedef struct _MD2_Texcoord MD2_Texcoord;
16
17struct PACKED _MD2_Header
18{
19 int magic;
20 int version;
21
22 int skin_width;
23 int skin_height;
24
25 int frame_size;
26
27 int skins_count;
28 int vertex_count;
29 int texcoord_count;
30 int triangle_count;
31 int glcmd_count;
32 int frame_count;
33
34 int offset_skins;
35 int offset_texcoords;
36 int offset_triangles;
37 int offset_frames;
38 int offset_glcmds;
39 int offset_end;
40};
41
42struct PACKED _MD2_Vertex
43{
44 unsigned char pos[3];
45 unsigned char normal_idx;
46};
47
48struct PACKED _MD2_Frame
49{
50 float scale[3];
51 float trans[3];
52 char name[16];
53 MD2_Vertex vertices[1];
54};
55
56struct PACKED _MD2_Triangle
57{
58 unsigned short vertex_idx[3];
59 unsigned short texcoord_idx[3];
60};
61
62struct PACKED _MD2_Texcoord
63{
64 short s;
65 short t;
66};
67
68typedef struct _MD2_Loader
69{
70 Eina_File *file;
71 char *map;
72 int size;
73
74 int skin_width;
75 int skin_height;
76
77 int frame_count;
78 int frame_size;
79 char *frames;
80
81 int vertex_count;
82 int triangle_count;
83 int texcoord_count;
84
85 MD2_Triangle *triangles;
86 MD2_Texcoord *texcoords;
87} MD2_Loader;
88
89static const float normal_table[162][3] =
90{
91 {-0.525731f, 0.000000f, 0.850651f},
92 {-0.442863f, 0.238856f, 0.864188f},
93 {-0.295242f, 0.000000f, 0.955423f},
94 {-0.309017f, 0.500000f, 0.809017f},
95 {-0.162460f, 0.262866f, 0.951056f},
96 { 0.000000f, 0.000000f, 1.000000f},
97 { 0.000000f, 0.850651f, 0.525731f},
98 {-0.147621f, 0.716567f, 0.681718f},
99 { 0.147621f, 0.716567f, 0.681718f},
100 { 0.000000f, 0.525731f, 0.850651f},
101 { 0.309017f, 0.500000f, 0.809017f},
102 { 0.525731f, 0.000000f, 0.850651f},
103 { 0.295242f, 0.000000f, 0.955423f},
104 { 0.442863f, 0.238856f, 0.864188f},
105 { 0.162460f, 0.262866f, 0.951056f},
106 {-0.681718f, 0.147621f, 0.716567f},
107 {-0.809017f, 0.309017f, 0.500000f},
108 {-0.587785f, 0.425325f, 0.688191f},
109 {-0.850651f, 0.525731f, 0.000000f},
110 {-0.864188f, 0.442863f, 0.238856f},
111 {-0.716567f, 0.681718f, 0.147621f},
112 {-0.688191f, 0.587785f, 0.425325f},
113 {-0.500000f, 0.809017f, 0.309017f},
114 {-0.238856f, 0.864188f, 0.442863f},
115 {-0.425325f, 0.688191f, 0.587785f},
116 {-0.716567f, 0.681718f, -0.147621f},
117 {-0.500000f, 0.809017f, -0.309017f},
118 {-0.525731f, 0.850651f, 0.000000f},
119 { 0.000000f, 0.850651f, -0.525731f},
120 {-0.238856f, 0.864188f, -0.442863f},
121 { 0.000000f, 0.955423f, -0.295242f},
122 {-0.262866f, 0.951056f, -0.162460f},
123 { 0.000000f, 1.000000f, 0.000000f},
124 { 0.000000f, 0.955423f, 0.295242f},
125 {-0.262866f, 0.951056f, 0.162460f},
126 { 0.238856f, 0.864188f, 0.442863f},
127 { 0.262866f, 0.951056f, 0.162460f},
128 { 0.500000f, 0.809017f, 0.309017f},
129 { 0.238856f, 0.864188f, -0.442863f},
130 { 0.262866f, 0.951056f, -0.162460f},
131 { 0.500000f, 0.809017f, -0.309017f},
132 { 0.850651f, 0.525731f, 0.000000f},
133 { 0.716567f, 0.681718f, 0.147621f},
134 { 0.716567f, 0.681718f, -0.147621f},
135 { 0.525731f, 0.850651f, 0.000000f},
136 { 0.425325f, 0.688191f, 0.587785f},
137 { 0.864188f, 0.442863f, 0.238856f},
138 { 0.688191f, 0.587785f, 0.425325f},
139 { 0.809017f, 0.309017f, 0.500000f},
140 { 0.681718f, 0.147621f, 0.716567f},
141 { 0.587785f, 0.425325f, 0.688191f},
142 { 0.955423f, 0.295242f, 0.000000f},
143 { 1.000000f, 0.000000f, 0.000000f},
144 { 0.951056f, 0.162460f, 0.262866f},
145 { 0.850651f, -0.525731f, 0.000000f},
146 { 0.955423f, -0.295242f, 0.000000f},
147 { 0.864188f, -0.442863f, 0.238856f},
148 { 0.951056f, -0.162460f, 0.262866f},
149 { 0.809017f, -0.309017f, 0.500000f},
150 { 0.681718f, -0.147621f, 0.716567f},
151 { 0.850651f, 0.000000f, 0.525731f},
152 { 0.864188f, 0.442863f, -0.238856f},
153 { 0.809017f, 0.309017f, -0.500000f},
154 { 0.951056f, 0.162460f, -0.262866f},
155 { 0.525731f, 0.000000f, -0.850651f},
156 { 0.681718f, 0.147621f, -0.716567f},
157 { 0.681718f, -0.147621f, -0.716567f},
158 { 0.850651f, 0.000000f, -0.525731f},
159 { 0.809017f, -0.309017f, -0.500000f},
160 { 0.864188f, -0.442863f, -0.238856f},
161 { 0.951056f, -0.162460f, -0.262866f},
162 { 0.147621f, 0.716567f, -0.681718f},
163 { 0.309017f, 0.500000f, -0.809017f},
164 { 0.425325f, 0.688191f, -0.587785f},
165 { 0.442863f, 0.238856f, -0.864188f},
166 { 0.587785f, 0.425325f, -0.688191f},
167 { 0.688191f, 0.587785f, -0.425325f},
168 {-0.147621f, 0.716567f, -0.681718f},
169 {-0.309017f, 0.500000f, -0.809017f},
170 { 0.000000f, 0.525731f, -0.850651f},
171 {-0.525731f, 0.000000f, -0.850651f},
172 {-0.442863f, 0.238856f, -0.864188f},
173 {-0.295242f, 0.000000f, -0.955423f},
174 {-0.162460f, 0.262866f, -0.951056f},
175 { 0.000000f, 0.000000f, -1.000000f},
176 { 0.295242f, 0.000000f, -0.955423f},
177 { 0.162460f, 0.262866f, -0.951056f},
178 {-0.442863f, -0.238856f, -0.864188f},
179 {-0.309017f, -0.500000f, -0.809017f},
180 {-0.162460f, -0.262866f, -0.951056f},
181 { 0.000000f, -0.850651f, -0.525731f},
182 {-0.147621f, -0.716567f, -0.681718f},
183 { 0.147621f, -0.716567f, -0.681718f},
184 { 0.000000f, -0.525731f, -0.850651f},
185 { 0.309017f, -0.500000f, -0.809017f},
186 { 0.442863f, -0.238856f, -0.864188f},
187 { 0.162460f, -0.262866f, -0.951056f},
188 { 0.238856f, -0.864188f, -0.442863f},
189 { 0.500000f, -0.809017f, -0.309017f},
190 { 0.425325f, -0.688191f, -0.587785f},
191 { 0.716567f, -0.681718f, -0.147621f},
192 { 0.688191f, -0.587785f, -0.425325f},
193 { 0.587785f, -0.425325f, -0.688191f},
194 { 0.000000f, -0.955423f, -0.295242f},
195 { 0.000000f, -1.000000f, 0.000000f},
196 { 0.262866f, -0.951056f, -0.162460f},
197 { 0.000000f, -0.850651f, 0.525731f},
198 { 0.000000f, -0.955423f, 0.295242f},
199 { 0.238856f, -0.864188f, 0.442863f},
200 { 0.262866f, -0.951056f, 0.162460f},
201 { 0.500000f, -0.809017f, 0.309017f},
202 { 0.716567f, -0.681718f, 0.147621f},
203 { 0.525731f, -0.850651f, 0.000000f},
204 {-0.238856f, -0.864188f, -0.442863f},
205 {-0.500000f, -0.809017f, -0.309017f},
206 {-0.262866f, -0.951056f, -0.162460f},
207 {-0.850651f, -0.525731f, 0.000000f},
208 {-0.716567f, -0.681718f, -0.147621f},
209 {-0.716567f, -0.681718f, 0.147621f},
210 {-0.525731f, -0.850651f, 0.000000f},
211 {-0.500000f, -0.809017f, 0.309017f},
212 {-0.238856f, -0.864188f, 0.442863f},
213 {-0.262866f, -0.951056f, 0.162460f},
214 {-0.864188f, -0.442863f, 0.238856f},
215 {-0.809017f, -0.309017f, 0.500000f},
216 {-0.688191f, -0.587785f, 0.425325f},
217 {-0.681718f, -0.147621f, 0.716567f},
218 {-0.442863f, -0.238856f, 0.864188f},
219 {-0.587785f, -0.425325f, 0.688191f},
220 {-0.309017f, -0.500000f, 0.809017f},
221 {-0.147621f, -0.716567f, 0.681718f},
222 {-0.425325f, -0.688191f, 0.587785f},
223 {-0.162460f, -0.262866f, 0.951056f},
224 { 0.442863f, -0.238856f, 0.864188f},
225 { 0.162460f, -0.262866f, 0.951056f},
226 { 0.309017f, -0.500000f, 0.809017f},
227 { 0.147621f, -0.716567f, 0.681718f},
228 { 0.000000f, -0.525731f, 0.850651f},
229 { 0.425325f, -0.688191f, 0.587785f},
230 { 0.587785f, -0.425325f, 0.688191f},
231 { 0.688191f, -0.587785f, 0.425325f},
232 {-0.955423f, 0.295242f, 0.000000f},
233 {-0.951056f, 0.162460f, 0.262866f},
234 {-1.000000f, 0.000000f, 0.000000f},
235 {-0.850651f, 0.000000f, 0.525731f},
236 {-0.955423f, -0.295242f, 0.000000f},
237 {-0.951056f, -0.162460f, 0.262866f},
238 {-0.864188f, 0.442863f, -0.238856f},
239 {-0.951056f, 0.162460f, -0.262866f},
240 {-0.809017f, 0.309017f, -0.500000f},
241 {-0.864188f, -0.442863f, -0.238856f},
242 {-0.951056f, -0.162460f, -0.262866f},
243 {-0.809017f, -0.309017f, -0.500000f},
244 {-0.681718f, 0.147621f, -0.716567f},
245 {-0.681718f, -0.147621f, -0.716567f},
246 {-0.850651f, 0.000000f, -0.525731f},
247 {-0.688191f, 0.587785f, -0.425325f},
248 {-0.587785f, 0.425325f, -0.688191f},
249 {-0.425325f, 0.688191f, -0.587785f},
250 {-0.425325f, -0.688191f, -0.587785f},
251 {-0.587785f, -0.425325f, -0.688191f},
252 {-0.688191f, -0.587785f, -0.425325f},
253};
254
255static inline void
256_md2_loader_fini(MD2_Loader *loader)
257{
258 if (loader->map)
259 {
260 eina_file_map_free(loader->file, loader->map);
261 loader->map = NULL;
262 }
263
264 if (loader->file)
265 {
266 eina_file_close(loader->file);
267 loader->file = NULL;
268 }
269}
270
271static inline Eina_Bool
272_md2_loader_init(MD2_Loader *loader, const char *file)
273{
274 MD2_Header header;
275
276 memset(loader, 0x00, sizeof(MD2_Loader));
277
278 /* Open given file. */
279 loader->file = eina_file_open(file, 0);
280
281 if (loader->file == NULL)
282 {
283 ERR("Failed to open file %s\n", file);
284 goto error;
285 }
286
287 /* Check file size. We require a file larger than MD2 header size. */
288 loader->size = eina_file_size_get(loader->file);
289
290 if (loader->size < (int)sizeof(MD2_Header))
291 goto error;
292
293 /* Map the file. */
294 loader->map = eina_file_map_all(loader->file, EINA_FILE_SEQUENTIAL);
295
296 if (loader->map == NULL)
297 goto error;
298
299 /* Read header. */
300 memcpy(&header, loader->map, sizeof(MD2_Header));
301
302 /* Check identity */
303 if (header.magic != MD2_MAGIC_NUMBER || header.version != MD2_VERSION)
304 goto error;
305
306 /* Check offsets */
307 if (header.offset_skins > header.offset_end)
308 goto error;
309
310 if (header.offset_texcoords > header.offset_end)
311 goto error;
312
313 if (header.offset_triangles > header.offset_end)
314 goto error;
315
316 if (header.offset_frames > header.offset_end)
317 goto error;
318
319 if (header.offset_glcmds > header.offset_end)
320 goto error;
321
322 if (header.offset_end > loader->size)
323 goto error;
324
325 loader->skin_width = header.skin_width;
326 loader->skin_height = header.skin_height;
327
328 loader->frame_count = header.frame_count;
329 loader->frame_size = header.frame_size;
330 loader->frames = loader->map + header.offset_frames;
331
332 loader->vertex_count = header.vertex_count;
333 loader->triangle_count = header.triangle_count;
334 loader->texcoord_count = header.texcoord_count;
335
336 loader->triangles = (MD2_Triangle *)(loader->map + header.offset_triangles);
337 loader->texcoords = (MD2_Texcoord *)(loader->map + header.offset_texcoords);
338 return EINA_TRUE;
339
340error:
341 _md2_loader_fini(loader);
342 return EINA_FALSE;
343}
344
345void
346evas_3d_mesh_file_md2_set(Evas_3D_Mesh *mesh, const char *file)
347{
348 MD2_Loader loader;
349 int i, j, k;
350 float *pos, *nor, *tex;
351 int stride_pos, stride_nor, stride_tex;
352 float s_scale, t_scale;
353 Evas_3D_Mesh_Data *pd;
354
355 /* Initialize MD2 loader (Open file and read MD2 head ant etc) */
356 if (!_md2_loader_init(&loader, file))
357 {
358 ERR("Failed to initialize MD2 loader.");
359 return;
360 }
361
362 s_scale = 1.0 / (float)(loader.skin_width - 1);
363 t_scale = 1.0 / (float)(loader.skin_height - 1);
364
365 eo_do(mesh,
366 evas_3d_mesh_vertex_count_set(loader.triangle_count * 3),
367 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES));
368
369 /* Load frames */
370 for (i = 0; i < loader.frame_count; i++)
371 {
372 const MD2_Frame *frame = (const MD2_Frame *)(loader.frames + loader.frame_size * i);
373 int f = i * MD2_FRAME_SCALE;
374
375 eo_do(mesh,
376 /* Add a mesh frame. */
377 evas_3d_mesh_frame_add(f),
378
379 /* Allocate vertex buffer for the frame. */
380 evas_3d_mesh_frame_vertex_data_copy_set(f, EVAS_3D_VERTEX_POSITION, 0, NULL),
381 evas_3d_mesh_frame_vertex_data_copy_set(f, EVAS_3D_VERTEX_NORMAL, 0, NULL),
382 evas_3d_mesh_frame_vertex_data_copy_set(f, EVAS_3D_VERTEX_TEXCOORD, 0, NULL),
383
384 /* Map vertex buffer. */
385 pos = (float *)evas_3d_mesh_frame_vertex_data_map(f, EVAS_3D_VERTEX_POSITION),
386 nor = (float *)evas_3d_mesh_frame_vertex_data_map(f, EVAS_3D_VERTEX_NORMAL),
387 tex = (float *)evas_3d_mesh_frame_vertex_data_map(f, EVAS_3D_VERTEX_TEXCOORD),
388
389 stride_pos = evas_3d_mesh_frame_vertex_stride_get(f, EVAS_3D_VERTEX_POSITION),
390 stride_nor = evas_3d_mesh_frame_vertex_stride_get(f, EVAS_3D_VERTEX_NORMAL),
391 stride_tex = evas_3d_mesh_frame_vertex_stride_get(f, EVAS_3D_VERTEX_TEXCOORD));
392
393 if (stride_pos == 0)
394 stride_pos = sizeof(float) * 3;
395
396 if (stride_nor == 0)
397 stride_nor = sizeof(float) * 3;
398
399 if (stride_tex == 0)
400 stride_tex = sizeof(float) * 2;
401
402 for (j = 0; j < loader.triangle_count; j++)
403 {
404 const MD2_Triangle *tri = &loader.triangles[j];
405
406 for (k = 0; k < 3; k++)
407 {
408 unsigned int tidx, vidx;
409 float *p, *n, *t;
410
411 tidx = tri->texcoord_idx[k];
412 vidx = tri->vertex_idx[k];
413
414 p = (float *)((char *)pos + stride_pos * (j * 3 + k));
415 n = (float *)((char *)nor + stride_nor * (j * 3 + k));
416 t = (float *)((char *)tex + stride_tex * (j * 3 + k));
417
418 p[0] = frame->vertices[vidx].pos[0] * frame->scale[0] + frame->trans[0];
419 p[1] = frame->vertices[vidx].pos[1] * frame->scale[1] + frame->trans[1];
420 p[2] = frame->vertices[vidx].pos[2] * frame->scale[2] + frame->trans[2];
421
422 n[0] = normal_table[frame->vertices[vidx].normal_idx][0];
423 n[1] = normal_table[frame->vertices[vidx].normal_idx][1];
424 n[2] = normal_table[frame->vertices[vidx].normal_idx][2];
425
426 t[0] = loader.texcoords[tidx].s * s_scale;
427 t[1] = 1.0 - loader.texcoords[tidx].t * t_scale;
428 }
429 }
430
431 /* Unmap vertex buffer. */
432 eo_do(mesh,
433 evas_3d_mesh_frame_vertex_data_unmap(f, EVAS_3D_VERTEX_POSITION),
434 evas_3d_mesh_frame_vertex_data_unmap(f, EVAS_3D_VERTEX_NORMAL),
435 evas_3d_mesh_frame_vertex_data_unmap(f, EVAS_3D_VERTEX_TEXCOORD));
436
437 pd = eo_data_scope_get(mesh, EVAS_3D_MESH_CLASS);
438
439 if (!evas_3d_mesh_aabb_add_to_frame(pd, f, stride_pos))
440 {
441 ERR("Axis-Aligned Bounding Box wasn't added in frame %d ", f);
442 }
443 }
444
445 _md2_loader_fini(&loader);
446}
diff --git a/src/modules/evas/model_loaders/obj/evas_model_load_obj.c b/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
index 8940b65..c6733a7 100644
--- a/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
+++ b/src/modules/evas/model_loaders/obj/evas_model_load_obj.c
@@ -1 +1,401 @@
1//dummy of saver/loader 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
10/* set value to position [x][y] to array name which have. */
11#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
12
13/* read 3 float values in string and put it in array */
14#define PUT_DATA_TO_ARRAY(array_name, name) \
15 sscanf (current,"%f %f %f", \
16 &ARRAY_2D(_##array_name##_obj, counts.current_##name##_counter, 0, 3), \
17 &ARRAY_2D(_##array_name##_obj, counts.current_##name##_counter, 1, 3), \
18 &ARRAY_2D(_##array_name##_obj, counts.current_##name##_counter, 2, 3)); \
19 counts.current_##name##_counter++;
20
21/* Structures for reading data from file. */
22typedef struct _OBJ_Counts OBJ_Counts;
23
24struct _OBJ_Counts
25{
26 int _vertex_counter;
27 int _normal_counter;
28 int _texture_point_counter;
29 int _triangles_counter;
30
31 int current_vertex_counter;
32 int current_normal_counter;
33 int current_texture_point_counter;
34 int current_triangles_counter;
35
36 Eina_Bool existence_of_normal;
37 Eina_Bool existence_of_tex_point;
38};
39
40static inline char*
41_file_to_buf(const char *file, long *length)//prepare text file for reading
42{
43 FILE *file_for_print;
44 char *buf;
45 int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread"
46
47 *length = 0;
48 file_for_print = fopen(file, "rb");
49 if (!file_for_print) return NULL;
50 fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file
51 *length = ftell(file_for_print);//set current position of file_for_print
52 if (*length < 0)
53 {
54 fclose(file_for_print);
55 return NULL;
56 }
57 buf = malloc(*length + 1);
58 fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file
59 unused = fread(buf, *length, 1, file_for_print);
60 fclose(file_for_print);
61 buf[*length] = '\0';
62 return buf;
63}
64
65/* create new counter */
66static inline OBJ_Counts
67_new_count_elements()
68{
69 OBJ_Counts counts;
70
71 counts._vertex_counter = 0;
72 counts._normal_counter = 0;
73 counts._texture_point_counter = 0;
74 counts._triangles_counter = 0;
75
76 counts.current_vertex_counter = 0;
77 counts.current_normal_counter = 0;
78 counts.current_texture_point_counter = 0;
79 counts.current_triangles_counter = 0;
80
81 counts.existence_of_normal = EINA_FALSE;
82 counts.existence_of_tex_point = EINA_FALSE;
83 return counts;
84}
85
86/* count triangles in face */
87static void
88_analyze_face_line(char * face_analyzer,
89 int * count_of_triangles_in_line)
90{
91 int polygon_checker = -2;
92 Eina_Bool previous_is_space = EINA_TRUE;
93 while ((*face_analyzer != '\n') && (*face_analyzer != '#'))
94 {
95 if (*face_analyzer == ' ')
96 {
97 previous_is_space = EINA_TRUE;
98 }
99 else if ((previous_is_space) && (*face_analyzer >= '0') && (*face_analyzer <= '9'))
100 {
101 polygon_checker++;
102 previous_is_space = EINA_FALSE;
103 }
104 face_analyzer++;
105 }
106 *count_of_triangles_in_line = polygon_checker;
107}
108
109static inline OBJ_Counts
110_count_elements(char *start, long length)//count elements of mesh in .obj
111{
112 OBJ_Counts counts = _new_count_elements();
113
114 char * current = start;
115 int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles
116 Eina_Bool will_check_next_char = EINA_FALSE;
117 Eina_Bool first_char_is_v = EINA_FALSE;
118 Eina_Bool first_char_is_f = EINA_FALSE;
119 Eina_Bool previous_is_space = EINA_FALSE;
120
121 long i = 0;
122 /* count elements of mesh in .obj */
123 for (; length > i; i++)
124 {
125 if (will_check_next_char)
126 {
127 if (first_char_is_v)
128 {
129 switch (*current)
130 {
131 case ' ':
132 counts._vertex_counter++;
133 break;
134 case 't':
135 counts._texture_point_counter++;
136 counts.existence_of_tex_point = EINA_TRUE;
137 break;
138 case 'n':
139 counts._normal_counter++;
140 counts.existence_of_normal = EINA_TRUE;
141 break;
142 default:
143 break;
144 }
145 first_char_is_v = EINA_FALSE;
146 will_check_next_char = EINA_FALSE;
147 }
148 else if (first_char_is_f)
149 {
150 switch (*current)
151 {
152 case '\n':
153 first_char_is_f = EINA_FALSE;
154 counts._triangles_counter += polygon_checker;
155 polygon_checker = -2;
156 previous_is_space = EINA_FALSE;
157 break;
158 case '#':
159 first_char_is_f = EINA_FALSE;
160 counts._triangles_counter += polygon_checker;
161 polygon_checker = -2;
162 previous_is_space = EINA_FALSE;
163 break;
164 case ' ':
165 previous_is_space = EINA_TRUE;
166 break;
167 default:
168 if ((previous_is_space) && (*current >= '0') && (*current <= '9'))
169 {
170 polygon_checker++;
171 previous_is_space = EINA_FALSE;
172 }
173 break;
174 }
175 }
176 else
177 {
178 switch (*current)
179 {
180 case 'v':
181 first_char_is_v = EINA_TRUE;
182 break;
183 case 'f':
184 first_char_is_f = EINA_TRUE;
185 break;
186 case 'm':
187 will_check_next_char = EINA_FALSE;
188 break;
189 default:
190 will_check_next_char = EINA_FALSE;
191 break;
192 }
193 }
194 }
195 else if (*current == '\n')
196 {
197 will_check_next_char = EINA_TRUE;
198 }
199 current++;
200 }
201 return counts;
202}
203
204void
205evas_3d_mesh_file_obj_set(Evas_3D_Mesh *mesh, const char *file)
206{
207 long length, i;
208 char * start = _file_to_buf(file, &length);
209 OBJ_Counts counts = _count_elements(start, length);//count elements of mesh in .obj
210 Eina_Bool will_check_next_char = EINA_FALSE;
211 Eina_Bool first_char_is_v = EINA_FALSE;
212 Eina_Bool first_char_is_f = EINA_FALSE;
213 float *pos, *nor, *tex;
214 int stride_pos, stride_nor, stride_tex;
215 int j, k;
216 char * current;
217
218 float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float));
219 float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float));
220 float *_tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float));
221 /* triangle has 3 points, every point has 3(vertix, texture and normal) coord */
222 int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int));
223
224 if ((start == NULL) || (_vertices_obj == NULL) ||
225 (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL))
226 {
227 ERR("Allocate memory is failed.");
228 free(start);
229 free(_vertices_obj);
230 free(_normales_obj);
231 free(_tex_coords_obj);
232 free(_triangles);
233 return;
234 }
235
236 current = start;
237 i = 0;
238
239 /* put data to arrays */
240 for (; length > i; i++)
241 {
242 if (will_check_next_char)
243 {
244 if (first_char_is_v)
245 {
246 switch (*current)
247 {
248 case ' ':
249 PUT_DATA_TO_ARRAY(vertices, vertex)
250 i--;
251 break;
252 case 't':
253 current++;
254 PUT_DATA_TO_ARRAY(tex_coords, texture_point)
255 break;
256 case 'n':
257 current++;
258 PUT_DATA_TO_ARRAY(normales, normal)
259 break;
260 default:
261 break;
262 }
263 first_char_is_v = EINA_FALSE;
264 will_check_next_char = EINA_FALSE;
265 }
266 else if (first_char_is_f)
267 {
268 char * auxiliary_pointer = current;
269 int count_of_triangles_in_line;
270
271 _analyze_face_line(auxiliary_pointer,
272 &count_of_triangles_in_line);
273 current++;
274 i++;
275 int first_pos, first_tex, first_norm;
276 sscanf (current,"%i/%i/%i",
277 &first_pos,
278 &first_tex,
279 &first_norm);
280
281 do
282 {
283 current++;
284 i++;
285 }
286 while (*current != ' ');
287
288 current++;
289 i++;
290
291 for (j = 0; j < count_of_triangles_in_line; j++)
292 {
293 auxiliary_pointer = current;
294 ARRAY_2D(_triangles, counts.current_triangles_counter, 0, 9) = first_pos;
295 ARRAY_2D(_triangles, counts.current_triangles_counter, 1, 9) = first_tex;
296 ARRAY_2D(_triangles, counts.current_triangles_counter, 2, 9) = first_norm;
297 sscanf (auxiliary_pointer,"%i/%i/%i %i/%i/%i",
298 &ARRAY_2D(_triangles, counts.current_triangles_counter, 3, 9),
299 &ARRAY_2D(_triangles, counts.current_triangles_counter, 4, 9),
300 &ARRAY_2D(_triangles, counts.current_triangles_counter, 5, 9),
301 &ARRAY_2D(_triangles, counts.current_triangles_counter, 6, 9),
302 &ARRAY_2D(_triangles, counts.current_triangles_counter, 7, 9),
303 &ARRAY_2D(_triangles, counts.current_triangles_counter, 8, 9));
304
305 while (*current != ' ')
306 {
307 current++;
308 i++;
309 }
310
311 counts.current_triangles_counter++;
312 }
313 first_char_is_f = EINA_FALSE;
314 }
315 else
316 {
317 switch (*current)
318 {
319 case 'v':
320 first_char_is_v = EINA_TRUE;
321 break;
322 case 'f':
323 first_char_is_f = EINA_TRUE;
324 break;
325 case 'm':
326 will_check_next_char = EINA_FALSE;
327 break;
328 default:
329 will_check_next_char = EINA_FALSE;
330 break;
331 }
332 }
333 }
334 else if (*current == '\n')
335 {
336 will_check_next_char = EINA_TRUE;
337 }
338 current++;
339 }
340 free(start);
341
342 /* prepare of mesh and take pointers to data which must be read */
343 eo_do(mesh,
344 evas_3d_mesh_vertex_count_set(counts._triangles_counter * 3),
345 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
346 evas_3d_mesh_frame_add(0),
347 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION, 0, NULL),
348 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL, 0, NULL),
349 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_TEXCOORD, 0, NULL),
350
351 pos = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_POSITION),
352 nor = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_NORMAL),
353 tex = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_TEXCOORD),
354
355 stride_pos = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_POSITION),
356 stride_nor = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_NORMAL),
357 stride_tex = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_TEXCOORD));
358
359 if (stride_pos == 0) stride_pos = sizeof(float) * 3;
360 if (stride_nor == 0) stride_nor = sizeof(float) * 3;
361 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
362
363 for (j = 0; j < counts._triangles_counter; j++)
364 {
365 for (k = 0; k < 3; k++)
366 {
367 float *p, *n, *t;
368
369 p = (float *)((char *)pos + stride_pos * (j * 3 + k));
370 n = (float *)((char *)nor + stride_nor * (j * 3 + k));
371 t = (float *)((char *)tex + stride_tex * (j * 3 + k));
372
373 p[0] = ARRAY_2D(_vertices_obj, (ARRAY_2D(_triangles, j, (3 * k), 9) - 1), 0, 3);
374 p[1] = ARRAY_2D(_vertices_obj, (ARRAY_2D(_triangles, j, (3 * k), 9) - 1), 1, 3);
375 p[2] = ARRAY_2D(_vertices_obj, (ARRAY_2D(_triangles, j, (3 * k), 9) - 1), 2, 3);
376
377 n[0] = ARRAY_2D(_normales_obj, (ARRAY_2D(_triangles, j, (3 * k + 2), 9) - 1), 0, 3);
378 n[1] = ARRAY_2D(_normales_obj, (ARRAY_2D(_triangles, j, (3 * k + 2), 9) - 1), 1, 3);
379 n[2] = ARRAY_2D(_normales_obj, (ARRAY_2D(_triangles, j, (3 * k + 2), 9) - 1), 2, 3);
380
381 t[0] = ARRAY_2D(_tex_coords_obj, (ARRAY_2D(_triangles, j, (3 * k + 1), 9) - 1), 0, 3);
382 t[1] = ARRAY_2D(_tex_coords_obj, (ARRAY_2D(_triangles, j, (3 * k + 1), 9) - 1), 1, 3);
383 }
384 }
385 free(_vertices_obj);
386 free(_normales_obj);
387 free(_tex_coords_obj);
388 free(_triangles);
389 /* Unmap vertex buffer. */
390 eo_do(mesh,
391 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_POSITION),
392 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_NORMAL),
393 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_TEXCOORD));
394
395 Evas_3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_3D_MESH_CLASS);
396
397 if (!evas_3d_mesh_aabb_add_to_frame(pd, 0, stride_pos))
398 {
399 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
400 }
401}
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 8940b65..b4bbe18 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 +1,401 @@
1//dummy of saver/loader 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
27static inline char*
28_file_to_buf(const char *file, long *length)//prepare text file for reading
29{
30 FILE *file_for_print;
31 char *buf;
32 int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread"
33
34 *length = 0;
35 file_for_print = fopen(file, "rb");
36 if (!file_for_print) return NULL;
37 fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file
38 *length = ftell(file_for_print);//set current position of file_for_print
39 buf = malloc(*length + 1);
40 fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file
41 unused = fread(buf, *length, 1, file_for_print);
42 fclose(file_for_print);
43 buf[*length] = '\0';
44 return buf;
45}
46
47/* create new header */
48static inline PLY_Header
49_new_ply_header()
50{
51 PLY_Header header;
52
53 header.vertices_count = 0;
54 header.triangles_count = 0;
55 header.existence_of_geometries = EINA_FALSE;
56 header.existence_of_normals = EINA_FALSE;
57 header.existence_of_texcoords = EINA_FALSE;
58 header.existence_of_colors = EINA_FALSE;
59
60 return header;
61}
62
63static inline char *
64_to_next_line(char *current)
65{
66 while (*current != '\n') current++;
67 current++;
68 return current;
69}
70
71static inline char *
72_to_begin_of_line(char *current)
73{
74 while (*current != '\n') current--;
75 current++;
76 return current;
77}
78
79static inline char *
80_to_next_number(char *current, int count)
81{
82 int i;
83 for (i = 0; i < count; i++)
84 {
85 while (*current != ' ') current++;
86 current++;
87 }
88 return current;
89}
90
91static inline char *
92_read_data(float *array, int place, int count, char *current, float divider)
93{
94 int i;
95 for (i = 0; i < count; i++)
96 {
97 sscanf (current,"%f", &ARRAY_2D(array, place, i, count));
98 ARRAY_2D(array, place, i, count) /= divider;
99 current = _to_next_number(current, 1);
100 }
101 return current;
102}
103
104static inline PLY_Header
105_read_header(char *start)//Check properties of mesh in .ply file.
106{
107 eina_init();
108
109 Eina_Bool reading_vertices = EINA_TRUE, check_next_char = EINA_FALSE;
110 int vertex_lines, triangles = 0, vertices_in_current_face = 0;
111 char **helping_pointer;
112 char *current;
113 PLY_Header header;
114
115 header = _new_ply_header();
116 helping_pointer = eina_str_split(start, "vertex ", 0);
117
118 if (helping_pointer == NULL)
119 {
120 ERR("File have not kayword vertex. It is necessary.");
121 return header;
122 }
123
124 sscanf(helping_pointer[1], "%d", &header.vertices_count);
125
126 free(helping_pointer);
127 helping_pointer = eina_str_split(start, "end_header\n", 0);
128
129 if (helping_pointer == NULL)
130 {
131 ERR("File have not kayword end_header. It is necessary.");
132 return header;
133 }
134
135 current = helping_pointer[1];
136
137 vertex_lines = header.vertices_count;
138 while (*current != '\0')
139 {
140 if (vertex_lines == 1)
141 reading_vertices = EINA_FALSE;
142 if (*current == '\n')
143 {
144 if (reading_vertices)
145 vertex_lines--;
146 else
147 check_next_char = EINA_TRUE;
148 }
149 if (check_next_char)
150 {
151 if ((*current <= '9') && (*current >= '0'))
152 vertices_in_current_face = (vertices_in_current_face * 10) + (*current - '0');
153 else if (*current >= ' ')
154 {
155 triangles += (vertices_in_current_face - 2);
156 vertices_in_current_face = 0;
157 check_next_char = EINA_FALSE;
158 }
159 }
160 current++;
161 }
162
163 header.triangles_count = triangles;
164 free(helping_pointer);
165
166 /* analyse flags used when file was saved in blender */
167 helping_pointer = eina_str_split(start, "property float ", 0);
168
169 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'x') &&
170 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'y') &&
171 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'z'))
172 header.existence_of_geometries = EINA_TRUE;
173 else return header;
174
175 if ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 'n') &&
176 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 'n') &&
177 (helping_pointer[6] != NULL) && (*helping_pointer[6] == 'n'))
178 header.existence_of_normals = EINA_TRUE;
179
180 if ((header.existence_of_normals &&
181 ((helping_pointer[7] != NULL) && (*helping_pointer[7] == 's') &&
182 (helping_pointer[8] != NULL) && (*helping_pointer[8] == 't'))) ||
183 (!header.existence_of_normals &&
184 ((helping_pointer[4] != NULL) && (*helping_pointer[4] == 's') &&
185 (helping_pointer[5] != NULL) && (*helping_pointer[5] == 't'))))
186 header.existence_of_texcoords = EINA_TRUE;
187
188 helping_pointer = eina_str_split(start, "property uchar ", 0);
189
190 if ((helping_pointer[1] != NULL) && (*helping_pointer[1] == 'r') &&
191 (helping_pointer[2] != NULL) && (*helping_pointer[2] == 'g') &&
192 (helping_pointer[3] != NULL) && (*helping_pointer[3] == 'b'))
193 header.existence_of_colors = EINA_TRUE;
194
195 free(helping_pointer);
196
197 return header;
198}
199
200void
201evas_3d_mesh_file_ply_set(Evas_3D_Mesh *mesh, const char *file)
202{
203 long length;
204 Evas_3D_Mesh_Data *pd;
205 int i = 0, j = 0, k = 0, count_of_triangles_in_line = 0;
206 float *pos, *nor, *tex, *col;
207 int stride_pos, stride_nor, stride_tex, stride_col;
208 char *start, *current;
209 PLY_Header header;
210 float *_vertices_ply = NULL, *_normals_ply = NULL, *_tex_coords_ply = NULL, *_colors_ply = NULL;
211 char **helping_pointer;
212
213 start = _file_to_buf(file, &length);
214
215 if (start == NULL)
216 {
217 ERR("Buffer is empty after preparation file for reading.");
218 return;
219 }
220
221 header = _read_header(start);
222
223 if (!header.existence_of_geometries)
224 {
225 ERR("File have not x, y, or z field as the first 3 float fields. They are necessary.");
226 return;
227 }
228
229 helping_pointer = eina_str_split(start, "end_header\n", 0);
230
231 if (helping_pointer == NULL)
232 {
233 ERR("File have not kayword end_header. It is necessary.");
234 return;
235 }
236
237 current = helping_pointer[1];
238
239 _vertices_ply = malloc(header.vertices_count * 3 * sizeof(float));
240 if (header.existence_of_normals)
241 _normals_ply = malloc(header.vertices_count * 3 * sizeof(float));
242 if (header.existence_of_texcoords)
243 _tex_coords_ply = malloc(header.vertices_count * 2 * sizeof(float));
244 if (header.existence_of_colors)
245 _colors_ply = malloc(header.vertices_count * 3 * sizeof(float));
246 int *_triangles = malloc(header.triangles_count * 3 * sizeof(int));
247
248 if ((header.existence_of_geometries && (_vertices_ply == NULL)) ||
249 (header.existence_of_normals && (_normals_ply == NULL)) ||
250 (header.existence_of_texcoords && (_tex_coords_ply == NULL)) ||
251 (header.existence_of_colors && (_colors_ply == NULL)) ||
252 (_triangles == NULL))
253 {
254 ERR("Allocate memory is failed.");
255 free(start);
256 free(_vertices_ply);
257 free(_normals_ply);
258 free(_tex_coords_ply);
259 free(_colors_ply);
260 free(_triangles);
261 return;
262 }
263
264 for (i = 0; i < header.vertices_count; i++)
265 {
266 current = _read_data(_vertices_ply, i, 3, current, 1.0);
267 if (header.existence_of_normals)
268 current = _read_data(_normals_ply, i, 3, current, 1.0);
269 if (header.existence_of_texcoords)
270 current = _read_data(_tex_coords_ply, i, 2, current, 1.0);
271 if (header.existence_of_colors)
272 current = _read_data(_colors_ply, i, 3, current, 255.0);
273 current = _to_begin_of_line(current);
274 }
275
276 for (i = 0; i < header.triangles_count;)
277 {
278 sscanf (current,"%d", &count_of_triangles_in_line);
279 count_of_triangles_in_line -= 2;
280 current = _to_next_number(current, 1);
281
282 sscanf (current,"%i", &ARRAY_2D(_triangles, i, 0, 3));
283
284 for (j = 0; j < count_of_triangles_in_line; j++)
285 {
286 if (j > 0)
287 ARRAY_2D(_triangles, i, 0, 3) = ARRAY_2D(_triangles, (i - 1), 0, 3);
288 current = _to_next_number(current, 1);
289 sscanf (current,"%i %i",
290 &ARRAY_2D(_triangles, i, 1, 3),
291 &ARRAY_2D(_triangles, i, 2, 3));
292 i++;
293 }
294 current = _to_next_line(current);
295 }
296
297 /* prepare of mesh and take pointers to data which must be read */
298 eo_do(mesh,
299 evas_3d_mesh_vertex_count_set(header.triangles_count * 3),
300 evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
301 evas_3d_mesh_frame_add(0),
302 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_POSITION, 0, NULL),
303 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_NORMAL, 0, NULL),
304 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_TEXCOORD, 0, NULL),
305 evas_3d_mesh_frame_vertex_data_copy_set(0, EVAS_3D_VERTEX_COLOR, 0, NULL),
306
307 pos = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_POSITION),
308 nor = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_NORMAL),
309 tex = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_TEXCOORD),
310 col = (float *)evas_3d_mesh_frame_vertex_data_map(0, EVAS_3D_VERTEX_COLOR),
311
312 stride_pos = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_POSITION),
313 stride_nor = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_NORMAL),
314 stride_tex = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_TEXCOORD),
315 stride_col = evas_3d_mesh_frame_vertex_stride_get(0, EVAS_3D_VERTEX_COLOR));
316
317 if (stride_pos == 0) stride_pos = sizeof(float) * 3;
318 if (stride_nor == 0) stride_nor = sizeof(float) * 3;
319 if (stride_tex == 0) stride_tex = sizeof(float) * 2;
320 if (stride_col == 0) stride_col = sizeof(float) * 4;
321
322 for (j = 0; j < header.triangles_count; j++)
323 {
324 for (k = 0; k < 3; k++)
325 {
326 float *p, *n, *t, *c;
327
328 p = (float *)((char *)pos + stride_pos * (j * 3 + k));
329 n = (float *)((char *)nor + stride_nor * (j * 3 + k));
330 t = (float *)((char *)tex + stride_tex * (j * 3 + k));
331 c = (float *)((char *)col + stride_col * (j * 3 + k));
332
333 p[0] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3);
334 p[1] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3);
335 p[2] = ARRAY_2D(_vertices_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3);
336
337 if (header.existence_of_normals)
338 {
339 n[0] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3);
340 n[1] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3);
341 n[2] = ARRAY_2D(_normals_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3);
342 }
343 else
344 {
345 n[0] = 0.0;
346 n[1] = 0.0;
347 n[2] = 0.0;
348 }
349
350 if (header.existence_of_texcoords)
351 {
352 t[0] = ARRAY_2D(_tex_coords_ply, ARRAY_2D(_triangles, j, k, 3), 0, 2);
353 t[1] = ARRAY_2D(_tex_coords_ply, ARRAY_2D(_triangles, j, k, 3), 1, 2);
354 }
355 else
356 {
357 t[0] = 0.0;
358 t[1] = 0.0;
359 }
360
361 if (header.existence_of_colors)
362 {
363 c[0] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 0, 3);
364 c[1] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 1, 3);
365 c[2] = ARRAY_2D(_colors_ply, ARRAY_2D(_triangles, j, k, 3), 2, 3);
366 c[3] = 1.0;
367 }
368 else
369 {
370 c[0] = 0.0;
371 c[1] = 0.0;
372 c[2] = 0.0;
373 c[3] = 1.0;
374 }
375 }
376 }
377
378 free(helping_pointer);
379 free(_vertices_ply);
380 if (header.existence_of_normals)
381 free(_normals_ply);
382 if (header.existence_of_texcoords)
383 free(_tex_coords_ply);
384 if (header.existence_of_colors)
385 free(_colors_ply);
386 free(_triangles);
387
388 /* Unmap vertex buffer. */
389 eo_do(mesh,
390 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_POSITION),
391 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_NORMAL),
392 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_TEXCOORD),
393 evas_3d_mesh_frame_vertex_data_unmap(0, EVAS_3D_VERTEX_COLOR));
394
395 pd = eo_data_scope_get(mesh, EVAS_3D_MESH_CLASS);
396
397 if (!evas_3d_mesh_aabb_add_to_frame(pd, 0, stride_pos))
398 {
399 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
400 }
401}