summaryrefslogtreecommitdiff
path: root/src/modules/evas/model_savers
diff options
context:
space:
mode:
authorStefan Schmidt <stefan@osg.samsung.com>2015-12-08 22:23:17 +0100
committerStefan Schmidt <stefan@osg.samsung.com>2015-12-08 22:33:05 +0100
commit96a9d75e0c2cd82682387a0812d6f3ea15590345 (patch)
tree53adaf08c7495ff17c069ee58af5ba2151059968 /src/modules/evas/model_savers
parent842a75423fa7b99cf74de1e0823b51f17365b0f1 (diff)
Revert "evas: refactor model's savers and loaders."
This reverts commit 32c33ed64dda542c7cfc952fc656bb711260441b. This refactor broke the evas test cases for the model loaders and savers. I gave it a week to get fixed but a first try did not succeed and its blocks a lot of other automated testing. To be honest, it should have never gone it when it breaks existing test cases. Once fixed this refactor can happily go in. Fixes T2905
Diffstat (limited to 'src/modules/evas/model_savers')
-rw-r--r--src/modules/evas/model_savers/eet/evas_model_save_eet.c1
-rw-r--r--src/modules/evas/model_savers/obj/evas_model_save_obj.c239
-rw-r--r--src/modules/evas/model_savers/ply/evas_model_save_ply.c123
3 files changed, 136 insertions, 227 deletions
diff --git a/src/modules/evas/model_savers/eet/evas_model_save_eet.c b/src/modules/evas/model_savers/eet/evas_model_save_eet.c
index ebe68ec041..ff43a3f851 100644
--- a/src/modules/evas/model_savers/eet/evas_model_save_eet.c
+++ b/src/modules/evas/model_savers/eet/evas_model_save_eet.c
@@ -192,3 +192,4 @@ evas_model_save_file_eet(const Evas_Canvas3D_Mesh *mesh,
192 192
193 _evas_canvas3d_eet_file_free(); 193 _evas_canvas3d_eet_file_free();
194} 194}
195
diff --git a/src/modules/evas/model_savers/obj/evas_model_save_obj.c b/src/modules/evas/model_savers/obj/evas_model_save_obj.c
index 8789ce44eb..d9be3dce44 100644
--- a/src/modules/evas/model_savers/obj/evas_model_save_obj.c
+++ b/src/modules/evas/model_savers/obj/evas_model_save_obj.c
@@ -1,71 +1,86 @@
1#include "evas_model_load_save_common.h" 1#ifdef HAVE_CONFIG_H
2 2# include "config.h"
3static unsigned short* 3#endif
4_init_obj_indices_data(Evas_Model_Load_Save_Header header) 4
5{ 5#include <stdlib.h>
6 unsigned short *i_data; 6#include <time.h>
7 int i = 0; 7#include "stdio.h"
8 8#include "evas_common_private.h"
9 if (header.existence_of_positions) i++; 9#include "evas_private.h"
10 if (header.existence_of_normals) i++; 10
11 if (header.existence_of_tex_coords) i++; 11#define OPEN_FILE(extension)\
12 i_data = calloc(header.vertices_count * i, sizeof(unsigned short)); 12 FILE * _##extension##_file = fopen(_##extension##_file_name, "w+");
13 13
14 return i_data; 14#define SAVE_GEOMETRICS(a, format) \
15} 15 vb = &f->vertices[a]; \
16 16 if (vb->data != NULL) \
17static void 17 { \
18_vertex_data_free_cb(void *data) 18 fprintf(_obj_file, "o %s\n",_obj_file_name); \
19{ 19 src = (float *)vb->data; \
20 eina_stringshare_del(data); 20 for (i = 0; i < pd->vertex_count; i++) \
21} 21 { \
22 fprintf(_obj_file, format, src[0], src[1]); \
23 if (a != EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD) \
24 fprintf(_obj_file, " %.4f", src[2]); \
25 fprintf(_obj_file, "\n"); \
26 src += f->vertices[a].element_count; \
27 } \
28 }
22 29
23static void 30static void
24_write_point(FILE *obj_file, 31_write_point(FILE * obj_file,
25 int num, 32 int num,
26 int num_of_point, 33 int num_of_point,
27 Evas_Model_Load_Save_Header header, 34 Eina_Bool existence_of_normal,
28 unsigned short *i_data) 35 Eina_Bool existence_of_tex_point)
29{ 36{
30 if (num_of_point == 0) 37 if (num_of_point == 1)
31 fprintf(obj_file, "f "); 38 fprintf(obj_file, "f ");
32 39
33 if (header.existence_of_normals) 40 if (existence_of_normal)
34 { 41 {
35 if (header.existence_of_tex_coords) 42 if (existence_of_tex_point)
36 fprintf(obj_file, "%hu/%hu/%hu ", i_data[num], 43 fprintf(obj_file, "%i/%i/%i ", num, num, num);
37 i_data[num + header.vertices_count],
38 i_data[num + 2 * header.vertices_count]);
39 else 44 else
40 fprintf(obj_file, "%hu//%hu ", i_data[num], 45 fprintf(obj_file, "%i//%i ", num, num);
41 i_data[num + header.vertices_count]);
42 } 46 }
43 else 47 else
44 { 48 {
45 if (header.existence_of_tex_coords) 49 if (existence_of_tex_point)
46 fprintf(obj_file, "%hu/%hu ", i_data[num], 50 fprintf(obj_file, "%i/%i ", num, num);
47 i_data[num + header.vertices_count]);
48 else 51 else
49 fprintf(obj_file, "%hu ", i_data[num]); 52 fprintf(obj_file, "%i ", num);
50 } 53 }
51 54
52 if (num_of_point == 2) 55 if (num_of_point == 3)
53 fprintf(obj_file, "\n"); 56 fprintf(obj_file, "\n");
54} 57}
55 58
56static inline Eina_Bool 59static void
57_write_obj_header(FILE *file, 60_save_mesh(Evas_Canvas3D_Mesh_Data *pd, const char *_obj_file_name, Evas_Canvas3D_Mesh_Frame *f)
58 const char *_mtl_file_name)
59{ 61{
62 Evas_Canvas3D_Vertex_Buffer *vb;
60 time_t current_time; 63 time_t current_time;
61 char* c_time_string; 64 char* c_time_string;
65 int i;
66 float *src;
67 Eina_Bool existence_of_normal, existence_of_tex_point;
62 68
69 OPEN_FILE(obj)
70 if (!_obj_file)
71 {
72 ERR("File open '%s' for save failed", _obj_file_name);
73 return;
74 }
75 fprintf(_obj_file, "# Evas_3D saver OBJ v0.03 \n");//_obj_file created in macro
76 /* Adding time comment to .obj file. */
63 current_time = time(NULL); 77 current_time = time(NULL);
64 78
65 if (current_time == ((time_t)-1)) 79 if (current_time == ((time_t)-1))
66 { 80 {
67 ERR("Failure to compute the current time."); 81 ERR("Failure to compute the current time.");
68 return EINA_FALSE; 82 fclose(_obj_file);
83 return;
69 } 84 }
70 85
71 c_time_string = ctime(&current_time); 86 c_time_string = ctime(&current_time);
@@ -73,117 +88,38 @@ _write_obj_header(FILE *file,
73 if (c_time_string == NULL) 88 if (c_time_string == NULL)
74 { 89 {
75 ERR("Failure to convert the current time."); 90 ERR("Failure to convert the current time.");
76 return EINA_FALSE; 91 fclose(_obj_file);
77 } 92 return;
78
79 fprintf(file, "# Evas_Canvas3D saver OBJ v0.03 \n");
80 fprintf(file, "# Current time is %s \n", c_time_string);
81 fprintf(file, "mtllib %s \n\n", _mtl_file_name);
82
83 return EINA_TRUE;
84}
85
86static inline void
87_write_obj_vertex_data(FILE *file,
88 Evas_Model_Load_Save_Header header,
89 Evas_Model_Load_Save_Data data,
90 unsigned short *i_data)
91{
92 int i, j;
93 int v = -1;
94 Eina_Stringshare *str, *cur_str, *cur_index;
95 unsigned short cur_hu;
96
97 eina_init();
98 Eina_Hash *vb;
99#define WRITE_OBJ_VERTEX_DATA(name, num, format) \
100 if (header.existence_of_##name) \
101 { \
102 cur_hu = 0; \
103 v++; \
104 vb = eina_hash_string_superfast_new(_vertex_data_free_cb); \
105 for (i = 0; i < header.vertices_count; i++) \
106 { \
107 str = eina_stringshare_printf(" "); \
108 for (j = 0; j < num; j++) \
109 str = eina_stringshare_printf("%s %f", str, \
110 data.name[i * num + j]); \
111 cur_index = eina_hash_find(vb, str); \
112 if (!cur_index) \
113 { \
114 cur_hu++; \
115 cur_str = eina_stringshare_printf("%hu", cur_hu); \
116 eina_hash_add(vb, str, cur_str); \
117 i_data[v * header.vertices_count + i] = cur_hu; \
118 fprintf(file, "%s%s\n", format, str); \
119 } \
120 else sscanf(cur_index, "%hu", \
121 i_data + v * header.vertices_count + i); \
122 } \
123 eina_hash_free(vb); \
124 } 93 }
125 WRITE_OBJ_VERTEX_DATA(positions, 3, "v")
126 WRITE_OBJ_VERTEX_DATA(tex_coords, 2, "vt")
127 WRITE_OBJ_VERTEX_DATA(normals, 3, "vn")
128#undef WRITE_OBJ_VERTEX_DATA
129 eina_shutdown();
130}
131 94
132static inline void 95 fprintf(_obj_file,"# Current time is %s \n", c_time_string);
133_write_obj_index_data(FILE *file, 96 fprintf(_obj_file,"mtllib %s.mtl \n\n", _obj_file_name);
134 Evas_Model_Load_Save_Header header,
135 Evas_Model_Load_Save_Data data,
136 unsigned short *i_data)
137{
138 int i;
139 int ic;
140 ic = header.indices_count ? header.indices_count : header.vertices_count;
141 for (i = 0; i < ic; i++)
142 _write_point(file, data.indices[i], i % 3, header, i_data);
143}
144
145static void
146_save_mesh(Evas_Canvas3D_Mesh_Data *pd,
147 const char *_obj_file_name,
148 const char *_mtl_file_name,
149 Evas_Canvas3D_Mesh_Frame *f)
150{
151 Evas_Model_Load_Save_Header header;
152 Evas_Model_Load_Save_Data data;
153 unsigned short *index_data;
154 97
155 if (!evas_model_save_header_from_mesh(pd, f, &header)) return; 98 /* Adding geometrics to file. */
156 99 if (f == NULL)
157 index_data = _init_obj_indices_data(header);
158 if (index_data == NULL)
159 { 100 {
160 ERR("Allocation of index data is failed."); 101 ERR("Not existing mesh frame.");
102 fclose(_obj_file);
161 return; 103 return;
162 } 104 }
163 105
164 evas_model_save_data_from_mesh(pd, f, header, &data); 106 SAVE_GEOMETRICS(EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, "v %.4f %.4f")
107 SAVE_GEOMETRICS(EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, "vn %.4f %.4f")
108 SAVE_GEOMETRICS(EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, "vt %.4f %.4f")
165 109
166 FILE * _obj_file = fopen(_obj_file_name, "wb+"); 110 existence_of_normal = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL].data != NULL);
167 if (!_obj_file) 111 existence_of_tex_point = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD].data != NULL);
168 {
169 ERR("File open '%s' for save failed", _obj_file_name);
170 free(index_data);
171 return;
172 }
173 112
174 if (!_write_obj_header(_obj_file, _mtl_file_name)) 113 fprintf(_obj_file,"usemtl Material\n s off\n");
114 for (i = 1; i <= pd->vertex_count; i++)//numeration of faces in .obj started from 1
175 { 115 {
176 fclose(_obj_file); 116 _write_point(_obj_file, i, 1, existence_of_normal, existence_of_tex_point);
177 free(index_data); 117 i++;
178 return; 118 _write_point(_obj_file, i, 2, existence_of_normal, existence_of_tex_point);
119 i++;
120 _write_point(_obj_file, i, 3, existence_of_normal, existence_of_tex_point);
179 } 121 }
180
181 _write_obj_vertex_data(_obj_file, header, data, index_data);
182 fprintf(_obj_file,"usemtl Material\n s off\n");
183 _write_obj_index_data(_obj_file, header, data, index_data);
184 fclose(_obj_file); 122 fclose(_obj_file);
185 free(index_data);
186 free(data.indices);
187} 123}
188 124
189static void 125static void
@@ -191,13 +127,13 @@ _save_material(Evas_Canvas3D_Mesh_Data *pd EINA_UNUSED,
191 const char *_mtl_file_name, 127 const char *_mtl_file_name,
192 Evas_Canvas3D_Material_Data *mat) 128 Evas_Canvas3D_Material_Data *mat)
193{ 129{
194 FILE * _mtl_file = fopen(_mtl_file_name, "wb+"); 130 OPEN_FILE(mtl)
195 if (!_mtl_file) 131 if (!_mtl_file)
196 { 132 {
197 ERR("File open '%s' for save failed", _mtl_file_name); 133 ERR("File open '%s' for save failed", _mtl_file_name);
198 return; 134 return;
199 } 135 }
200 fprintf(_mtl_file, "# Evas_Canvas3D saver OBJ v0.03 \n");//_mtl_file created in macro 136 fprintf(_mtl_file, "# Evas_3D saver OBJ v0.03 \n");//_mtl_file created in macro
201 fprintf(_mtl_file, "# Material Count: 1 \n\n"); 137 fprintf(_mtl_file, "# Material Count: 1 \n\n");
202 fprintf(_mtl_file, "newmtl Material \n"); 138 fprintf(_mtl_file, "newmtl Material \n");
203 fprintf(_mtl_file, "Ns 1.000000 \n");//exp factor for specular highlight 139 fprintf(_mtl_file, "Ns 1.000000 \n");//exp factor for specular highlight
@@ -221,9 +157,7 @@ _save_material(Evas_Canvas3D_Mesh_Data *pd EINA_UNUSED,
221} 157}
222 158
223void 159void
224evas_model_save_file_obj(const Evas_Canvas3D_Mesh *mesh, 160evas_model_save_file_obj(const Evas_Canvas3D_Mesh *mesh, const char *_obj_file_name, Evas_Canvas3D_Mesh_Frame *f)
225 const char *_obj_file_name,
226 Evas_Canvas3D_Mesh_Frame *f)
227{ 161{
228 int len; 162 int len;
229 char *_mtl_file_name, *_without_extention; 163 char *_mtl_file_name, *_without_extention;
@@ -232,15 +166,18 @@ evas_model_save_file_obj(const Evas_Canvas3D_Mesh *mesh,
232 len = strlen(_obj_file_name); 166 len = strlen(_obj_file_name);
233 _without_extention = (char *)malloc(len - 3); 167 _without_extention = (char *)malloc(len - 3);
234 _mtl_file_name = (char *)malloc(len + 1); 168 _mtl_file_name = (char *)malloc(len + 1);
235 eina_strlcpy(_without_extention, _obj_file_name, len - 3);
236 eina_str_join(_mtl_file_name, len + 1, '.', _without_extention, "mtl");
237 free(_without_extention);
238 169
239 Evas_Canvas3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS); 170 Evas_Canvas3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS);
240 mat = eo_data_scope_get(f->material, EVAS_CANVAS3D_MATERIAL_CLASS); 171 _save_mesh(pd, _obj_file_name, f);
241 172
242 if (mat != NULL) _save_material(pd, _mtl_file_name, mat); 173 mat = eo_data_scope_get(f->material, EVAS_CANVAS3D_MATERIAL_CLASS);
243 _save_mesh(pd, _obj_file_name, _mtl_file_name, f); 174 if (mat != NULL)
175 {
176 eina_strlcpy(_without_extention, _obj_file_name, len - 3);
177 eina_str_join(_mtl_file_name, len + 1, '.', _without_extention, "mtl");
178 _save_material(pd, _mtl_file_name, mat);
179 }
244 180
181 free(_without_extention);
245 free(_mtl_file_name); 182 free(_mtl_file_name);
246} 183}
diff --git a/src/modules/evas/model_savers/ply/evas_model_save_ply.c b/src/modules/evas/model_savers/ply/evas_model_save_ply.c
index 805f267bc0..0be42f7f86 100644
--- a/src/modules/evas/model_savers/ply/evas_model_save_ply.c
+++ b/src/modules/evas/model_savers/ply/evas_model_save_ply.c
@@ -1,91 +1,62 @@
1#include "evas_model_load_save_common.h" 1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
2 4
3static inline void 5#include <stdlib.h>
4_write_ply_header(FILE *file, Evas_Model_Load_Save_Header header) 6#include <time.h>
5{ 7#include "stdio.h"
6 const char *pf = "property float", *puc = "property uchar"; 8#include "math.h"
7 fprintf(file, "ply\n" \ 9#include "evas_common_private.h"
8 "format ascii 1.0\n" \ 10#include "evas_private.h"
9 "comment Created by EFL evas_canvas3d_mesh_saver_ply.c version 2 " \
10 "(sub 0) - www.enlightenment.org, source file: ''\n");
11 fprintf(file, "element vertex %d\n", header.vertices_count);
12
13 if (header.existence_of_positions)
14 fprintf(file, "%s x\n%s y\n%s z\n", pf, pf, pf);
15 if (header.existence_of_normals)
16 fprintf(file, "%s nx\n%s ny\n%s nz\n", pf, pf, pf);
17 if (header.existence_of_tex_coords)
18 fprintf(file, "%s s\n%s t\n", pf, pf);
19 if (header.existence_of_colors)
20 fprintf(file, "%s red\n%s green\n%s blue\n", puc, puc, puc);
21
22 fprintf(file, "element face %d\n" \
23 "property list uchar uint vertex_indices\n" \
24 "end_header\n", header.indices_count / 3);
25}
26
27static inline void
28_write_ply_vertex_data(FILE *file,
29 Evas_Model_Load_Save_Header header,
30 Evas_Model_Load_Save_Data data)
31{
32 int i, j;
33 for (i = 0; i < header.vertices_count; i++)
34 {
35#define WRITE_PLY_VERTEX_DATA(name, num)\
36 if (header.existence_of_##name)\
37 for (j = 0; j < num; j++, data.name++)\
38 fprintf(file, "%f ", data.name[0]);
39 WRITE_PLY_VERTEX_DATA(positions, 3)
40 WRITE_PLY_VERTEX_DATA(normals, 3)
41 WRITE_PLY_VERTEX_DATA(tex_coords, 2)
42#undef WRITE_PLY_VERTEX_DATA
43
44 if (header.existence_of_colors)
45 {
46 for (j = 0; j < 3; j++, data.colors++)
47 fprintf(file, "%.0f ", round(data.colors[0] * 255));
48 data.colors++;
49 }
50 fprintf(file, "\n");
51 }
52}
53
54static inline void
55_write_ply_index_data(FILE *file,
56 Evas_Model_Load_Save_Header header,
57 Evas_Model_Load_Save_Data data)
58{
59 int i, triangles_count = header.indices_count / 3;
60 for (i = 0; i < triangles_count; i++, data.indices += 3)
61 fprintf(file, "3 %d %d %d\n", data.indices[0],
62 data.indices[1],
63 data.indices[2]);
64}
65 11
66void 12void
67evas_model_save_file_ply(const Evas_Canvas3D_Mesh *mesh, 13evas_model_save_file_ply(const Evas_Canvas3D_Mesh *mesh, const char *file, Evas_Canvas3D_Mesh_Frame *f)
68 const char *file,
69 Evas_Canvas3D_Mesh_Frame *f)
70{ 14{
71 Evas_Model_Load_Save_Header header; 15 float *src_pos, *src_nor, *src_tex, *src_col;
72 Evas_Model_Load_Save_Data data; 16 int i;
73 17
74 Evas_Canvas3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS); 18 Evas_Canvas3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS);
75 if (!evas_model_save_header_from_mesh(pd, f, &header)) return; 19 FILE *_ply_file = fopen(file, "w+");
76 evas_model_save_data_from_mesh(pd, f, header, &data);
77
78 FILE *_ply_file = fopen(file, "wb+");
79 if (!_ply_file) 20 if (!_ply_file)
80 { 21 {
81 ERR("File open '%s' for save failed", file); 22 ERR("File open '%s' for save failed", file);
82 return; 23 return;
83 } 24 }
84 25
85 _write_ply_header(_ply_file, header); 26 fprintf(_ply_file,"ply\nformat ascii 1.0\ncomment Created by EFL evas_canvas3d_mesh_saver_ply.c" \
86 _write_ply_vertex_data(_ply_file, header, data); 27 "version 1 (sub 0) - www.enlightenment.org, source file: ''\n");
87 _write_ply_index_data(_ply_file, header, data); 28 fprintf(_ply_file,"element vertex %d\n", pd->vertex_count);
29 fprintf(_ply_file,"property float x\nproperty float y\nproperty float z\n" \
30 "property float nx\nproperty float ny\nproperty float nz\n" \
31 "property float s\nproperty float t\n" \
32 "property uchar red\nproperty uchar green\nproperty uchar blue\n");
33 fprintf(_ply_file,"element face %d\nproperty list uchar uint vertex_indices\nend_header\n",
34 pd->index_count / 3);
35
36 src_pos = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION])->data;
37 src_nor = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL])->data;
38 src_tex = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD])->data;
39 src_col = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR])->data;
40
41 for (i = 0; i < pd->vertex_count; i++)
42 {
43 fprintf(_ply_file,"%f %f %f %f %f %f %f %f %.0f %.0f %.0f\n",
44 src_pos[0], src_pos[1], src_pos[2],
45 src_nor[0], src_nor[1], src_nor[2],
46 src_tex[0], src_tex[1],
47 round(src_col[0] * 255), round(src_col[1] * 255), round(src_col[2] * 255));
48 src_pos += 3;
49 src_nor += 3;
50 src_tex += 2;
51 src_col += 4;
52 }
53
54 unsigned short* indices = (unsigned short*) pd->indices;
55
56 for (i = 0; i < pd->index_count / 3; i++)
57 {
58 fprintf(_ply_file,"3 %d %d %d\n", indices[3 * i], indices[3 * i + 1], indices[3 * i + 2]);
59 }
88 60
89 free(data.indices);
90 fclose(_ply_file); 61 fclose(_ply_file);
91} 62}