summaryrefslogtreecommitdiff
path: root/src/lib/evas/common3d
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2015-11-09 15:01:54 -0800
committerCedric BAIL <cedric@osg.samsung.com>2015-11-09 15:27:26 -0800
commit8ab190daec637892465f2c5ea08298b3fafc4e98 (patch)
treeada85acad5e04360454547458ebfd54aadbe786f /src/lib/evas/common3d
parentf416f5adde537ec55f513230ef4907d0cd67f0cd (diff)
evas: add common part of savers and loaders.
Summary: Common part will make savers and loaders shorter and easier for understanding and refactoring. https://phab.enlightenment.org/T2713 - due to this task. Should be merged after https://phab.enlightenment.org/D3030 Should be merged to start adding refactored savers and loaders. Reviewers: Hermet, raster, Oleksander, cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D3038 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/lib/evas/common3d')
-rw-r--r--src/lib/evas/common3d/save_load/evas_model_common.c236
-rw-r--r--src/lib/evas/common3d/save_load/evas_model_common.h62
2 files changed, 298 insertions, 0 deletions
diff --git a/src/lib/evas/common3d/save_load/evas_model_common.c b/src/lib/evas/common3d/save_load/evas_model_common.c
new file mode 100644
index 0000000000..612bccd5c8
--- /dev/null
+++ b/src/lib/evas/common3d/save_load/evas_model_common.c
@@ -0,0 +1,236 @@
1#include "evas_model_common.h"
2
3# define SAVE_MESH_INDICES_COPY \
4 if (header.indices_count) \
5 { \
6 data->indices = malloc(header.indices_count \
7 * sizeof(unsigned short)); \
8 if (pd->index_format == EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_BYTE) \
9 { \
10 for (i = 0; i < header.indices_count; i++) \
11 data->indices[i] = ((unsigned char*)(pd->indices))[i]; \
12 } \
13 else \
14 { \
15 for (i = 0; i < header.indices_count; i++) \
16 data->indices[i] = ((unsigned short*)(pd->indices))[i]; \
17 } \
18 } \
19 else \
20 { \
21 data->indices = malloc(header.vertices_count * 3 \
22 * sizeof(unsigned short)); \
23 for (i = 0; i < header.vertices_count * 3; i++) \
24 data->indices[i] = (unsigned short)i; \
25 }
26
27/* create new header */
28Evas_Model_Load_Save_Header
29evas_model_load_save_header_new()
30{
31 Evas_Model_Load_Save_Header header;
32
33 header.vertices_count = 0;
34 header.indices_count = 0;
35 header.existence_of_positions = EINA_FALSE;
36 header.existence_of_normals = EINA_FALSE;
37 header.existence_of_tex_coords = EINA_FALSE;
38 header.existence_of_colors = EINA_FALSE;
39
40 return header;
41}
42
43void
44evas_model_load_vertex_data_unmap(Evas_Canvas3D_Mesh *mesh,
45 int frame,
46 Evas_Model_Load_Save_Header header)
47{
48#define UNMAP_IF_EXIST(existence, vertex_data_type) \
49 if (existence) \
50 { \
51 eo_do(mesh, \
52 evas_canvas3d_mesh_frame_vertex_data_unmap(frame, \
53 vertex_data_type)); \
54 }
55 UNMAP_IF_EXIST(header.existence_of_positions, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION)
56 UNMAP_IF_EXIST(header.existence_of_normals, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL)
57 UNMAP_IF_EXIST(header.existence_of_tex_coords, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD)
58 UNMAP_IF_EXIST(header.existence_of_colors, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR)
59#undef UNMAP_IF_EXIST
60}
61
62void
63evas_model_load_save_data_free(Evas_Model_Load_Save_Header header,
64 Evas_Model_Load_Save_Data *data)
65{
66 if (header.existence_of_positions) free(data->positions);
67 if (header.existence_of_normals) free(data->normals);
68 if (header.existence_of_tex_coords) free(data->tex_coords);
69 if (header.existence_of_colors) free(data->colors);
70 free(data->indices);
71}
72
73void
74evas_model_load_vertex_data_to_mesh(Evas_Canvas3D_Mesh *mesh,
75 Evas_Model_Load_Save_Header header,
76 Evas_Model_Load_Save_Data data,
77 Evas_Model_Load_Save_Stride *stride)
78{
79 Evas_Model_Load_Save_Data map;
80 int i, j;
81
82 eo_do(mesh,
83 evas_canvas3d_mesh_vertex_count_set(header.vertices_count),
84 evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES),
85 evas_canvas3d_mesh_frame_add(0));
86
87#define VERTEX_DATA_MAP(name, vertex_data_type, default_size) \
88 if (header.existence_of_##name) \
89 { \
90 eo_do(mesh, \
91 evas_canvas3d_mesh_frame_vertex_data_copy_set(0, vertex_data_type, 0, NULL), \
92 map.name = (float *)evas_canvas3d_mesh_frame_vertex_data_map(0, vertex_data_type), \
93 stride->name = evas_canvas3d_mesh_frame_vertex_stride_get(0, vertex_data_type)); \
94 if (stride->name == 0) stride->name = sizeof(float) * default_size; \
95 }
96 VERTEX_DATA_MAP(positions, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 3)
97 VERTEX_DATA_MAP(normals, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, 3)
98 VERTEX_DATA_MAP(tex_coords, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, 2)
99 VERTEX_DATA_MAP(colors, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, 4)
100#undef VERTEX_DATA_MAP
101
102 for (i = 0; i < header.vertices_count; i++)
103 {
104 Evas_Model_Load_Save_Data current_pointer;
105
106#define FILL_VERTEX_DATA(name, size) \
107 if (header.existence_of_##name) \
108 { \
109 current_pointer.name = (float *)((char *)map.name + stride->name * i); \
110 for (j = 0; j < size; j++) \
111 current_pointer.name[j] = ARRAY_2D(data.name, i, j, size); \
112 }
113 FILL_VERTEX_DATA(positions, 3)
114 FILL_VERTEX_DATA(normals, 3)
115 FILL_VERTEX_DATA(tex_coords, 2)
116 FILL_VERTEX_DATA(colors, 3)
117#undef FILL_VERTEX_DATA
118
119 if (header.existence_of_colors) current_pointer.colors[3] = 1.0;
120 }
121}
122
123void
124evas_model_load_indices_data_to_mesh(Evas_Canvas3D_Mesh *mesh,
125 Evas_Model_Load_Save_Header header,
126 Evas_Model_Load_Save_Data data)
127{
128 eo_do(mesh,
129 evas_canvas3d_mesh_index_data_copy_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT,
130 header.indices_count,
131 data.indices));
132}
133
134Eina_Bool
135evas_model_load_allocate_data_due_to_header(Evas_Model_Load_Save_Header header,
136 Evas_Model_Load_Save_Data *data)
137{
138 if (header.existence_of_positions)
139 data->positions = malloc(header.vertices_count * 3 * sizeof(float));
140 if (header.existence_of_normals)
141 data->normals = malloc(header.vertices_count * 3 * sizeof(float));
142 if (header.existence_of_tex_coords)
143 data->tex_coords = malloc(header.vertices_count * 2 * sizeof(float));
144 if (header.existence_of_colors)
145 data->colors = malloc(header.vertices_count * 3 * sizeof(float));
146 data->indices = malloc(header.indices_count * sizeof(unsigned short));
147
148 if ((header.existence_of_positions && (data->positions == NULL)) ||
149 (header.existence_of_normals && (data->normals == NULL)) ||
150 (header.existence_of_tex_coords && (data->tex_coords == NULL)) ||
151 (header.existence_of_colors && (data->colors == NULL)) ||
152 (data->indices == NULL))
153 {
154 free(data->positions);
155 free(data->normals);
156 free(data->tex_coords);
157 free(data->colors);
158 free(data->indices);
159 return EINA_FALSE;
160 }
161 return EINA_TRUE;
162}
163
164void
165evas_model_load_aabb_add_to_frame(Evas_Canvas3D_Mesh *mesh,
166 int frame,
167 Evas_Model_Load_Save_Stride stride)
168{
169 Evas_Canvas3D_Mesh_Data *pd;
170 pd = eo_data_scope_get(mesh, EVAS_CANVAS3D_MESH_CLASS);
171
172 if (!evas_canvas3d_mesh_aabb_add_to_frame(pd, frame, stride.positions))
173 {
174 ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
175 }
176}
177
178Eina_Bool
179evas_model_save_header_from_mesh(Evas_Canvas3D_Mesh_Data *pd,
180 Evas_Canvas3D_Mesh_Frame *f,
181 Evas_Model_Load_Save_Header *header)
182{
183 header->indices_count = pd->index_count;
184 if (header->indices_count % 3 != 0)
185 {
186 printf("Index count is %d. It cannot be divided to triangles correctly.\n",
187 header->indices_count);
188 return EINA_FALSE;
189 }
190
191 header->existence_of_positions = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION].data != NULL);
192 header->existence_of_normals = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL].data != NULL);
193 header->existence_of_tex_coords = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD].data != NULL);
194 header->existence_of_colors = (f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR].data != NULL);
195 header->vertices_count = pd->vertex_count;
196
197 return EINA_TRUE;
198}
199
200void
201evas_model_save_data_from_mesh(Evas_Canvas3D_Mesh_Data *pd,
202 Evas_Canvas3D_Mesh_Frame *f,
203 Evas_Model_Load_Save_Header header,
204 Evas_Model_Load_Save_Data *data)
205{
206 int i;
207 if (header.existence_of_positions)
208 data->positions = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION])->data;
209 if (header.existence_of_normals)
210 data->normals = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL])->data;
211 if (header.existence_of_tex_coords)
212 data->tex_coords = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD])->data;
213 if (header.existence_of_colors)
214 data->colors = (float*)(&f->vertices[EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR])->data;
215 SAVE_MESH_INDICES_COPY
216}
217
218void
219evas_model_save_data_copy_from_mesh(Evas_Canvas3D_Mesh_Data *pd,
220 Evas_Canvas3D_Mesh_Frame *f,
221 Evas_Model_Load_Save_Header header,
222 Evas_Model_Load_Save_Data *data)
223{
224 int i;
225#define SAVE_MESH_VERTICES_COPY(name, vertex_data_type) \
226 if (header.existence_of_##name) \
227 memcpy(data->name, \
228 (&f->vertices[vertex_data_type])->data, \
229 header.vertices_count);
230 SAVE_MESH_VERTICES_COPY(positions, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION)
231 SAVE_MESH_VERTICES_COPY(normals, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL)
232 SAVE_MESH_VERTICES_COPY(tex_coords, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD)
233 SAVE_MESH_VERTICES_COPY(colors, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR)
234#undef SAVE_MESH_VERTICES_COPY
235 SAVE_MESH_INDICES_COPY
236}
diff --git a/src/lib/evas/common3d/save_load/evas_model_common.h b/src/lib/evas/common3d/save_load/evas_model_common.h
new file mode 100644
index 0000000000..bda841920b
--- /dev/null
+++ b/src/lib/evas/common3d/save_load/evas_model_common.h
@@ -0,0 +1,62 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#ifndef MODEL_LOAD_COMMON
6#define MODEL_LOAD_COMMON
7
8#include <stdlib.h>
9#include <time.h>
10#include "stdio.h"
11#include "evas_common_private.h"
12#include "evas_private.h"
13#include <Eina.h>
14
15/* set value to position [x][y] to array name which have. */
16#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
17
18/* Structures for reading data from file. */
19typedef struct _Evas_Model_Load_Save_Header Evas_Model_Load_Save_Header;
20typedef struct _Evas_Model_Load_Save_Data Evas_Model_Load_Save_Data;
21typedef struct _Evas_Model_Load_Save_Stride Evas_Model_Load_Save_Stride;
22
23struct _Evas_Model_Load_Save_Header
24{
25 int vertices_count;
26 int indices_count;
27 Eina_Bool existence_of_positions;
28 Eina_Bool existence_of_normals;
29 Eina_Bool existence_of_tex_coords;
30 Eina_Bool existence_of_colors;
31};
32
33struct _Evas_Model_Load_Save_Data
34{
35 float *positions;
36 float *normals;
37 float *tex_coords;
38 float *colors;
39 unsigned short *indices;
40};
41
42struct _Evas_Model_Load_Save_Stride
43{
44 int positions;
45 int normals;
46 int tex_coords;
47 int colors;
48};
49
50/* create new header */
51Evas_Model_Load_Save_Header evas_model_load_save_header_new();
52void evas_model_load_vertex_data_unmap(Evas_Canvas3D_Mesh *mesh, int frame, Evas_Model_Load_Save_Header header);
53void evas_model_load_save_data_free(Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data *data);
54void evas_model_load_vertex_data_to_mesh(Evas_Canvas3D_Mesh *mesh, Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data data, Evas_Model_Load_Save_Stride *stride);
55void evas_model_load_indices_data_to_mesh(Evas_Canvas3D_Mesh *mesh, Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data data);
56Eina_Bool evas_model_load_allocate_data_due_to_header(Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data *data);
57void evas_model_load_aabb_add_to_frame(Evas_Canvas3D_Mesh *mesh, int frame, Evas_Model_Load_Save_Stride stride);
58Eina_Bool evas_model_save_header_from_mesh(Evas_Canvas3D_Mesh_Data *pd, Evas_Canvas3D_Mesh_Frame *f, Evas_Model_Load_Save_Header *header);
59void evas_model_save_data_from_mesh(Evas_Canvas3D_Mesh_Data *pd, Evas_Canvas3D_Mesh_Frame *f, Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data *data);
60void evas_model_save_data_copy_from_mesh(Evas_Canvas3D_Mesh_Data *pd, Evas_Canvas3D_Mesh_Frame *f, Evas_Model_Load_Save_Header header, Evas_Model_Load_Save_Data *data);
61
62#endif //MODEL_LOAD_COMMON