aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/evas/model_savers/obj/evas_model_save_obj.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/evas/model_savers/obj/evas_model_save_obj.c')
-rw-r--r--src/modules/evas/model_savers/obj/evas_model_save_obj.c107
1 files changed, 72 insertions, 35 deletions
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 1cc7e8a744..00f8a76cc4 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
@@ -9,25 +9,54 @@
#include "evas_private.h"
#define OPEN_FILE(extension)\
- FILE * _##extension##_file = fopen(_##extension##_file_name, "w+");\
-
-#define SAVE_GEOMETRICS(a, format)\
- vb = &f->vertices[a];\
- fprintf(_obj_file, "o %s\n",_obj_file_name);\
- if (vb->data == NULL)\
- {\
- ERR("Reading of geometrics is failed.");\
- fclose(_obj_file);\
- return;\
- }\
- src = (float *)vb->data;\
- for (i = 0; i < pd->vertex_count; i++)\
- {\
- fprintf(_obj_file, format, src[0], src[1], src[2]);\
- src += f->vertices[a].element_count;\
+ FILE * _##extension##_file = fopen(_##extension##_file_name, "w+");
+
+#define SAVE_GEOMETRICS(a, format) \
+ vb = &f->vertices[a]; \
+ if (vb->data != NULL) \
+ { \
+ fprintf(_obj_file, "o %s\n",_obj_file_name); \
+ src = (float *)vb->data; \
+ for (i = 0; i < pd->vertex_count; i++) \
+ { \
+ fprintf(_obj_file, format, src[0], src[1]); \
+ if (a != EVAS_3D_VERTEX_TEXCOORD) \
+ fprintf(_obj_file, " %.4f", src[2]); \
+ fprintf(_obj_file, "\n"); \
+ src += f->vertices[a].element_count; \
+ } \
}
static void
+_write_point(FILE * obj_file,
+ int num,
+ int num_of_point,
+ Eina_Bool existence_of_normal,
+ Eina_Bool existence_of_tex_point)
+{
+ if (num_of_point == 1)
+ fprintf(obj_file, "f ");
+
+ if (existence_of_normal)
+ {
+ if (existence_of_tex_point)
+ fprintf(obj_file, "%i/%i/%i ", num, num, num);
+ else
+ fprintf(obj_file, "%i//%i ", num, num);
+ }
+ else
+ {
+ if (existence_of_tex_point)
+ fprintf(obj_file, "%i/%i ", num, num);
+ else
+ fprintf(obj_file, "%i ", num);
+ }
+
+ if (num_of_point == 3)
+ fprintf(obj_file, "\n");
+}
+
+static void
_save_mesh(Evas_3D_Mesh_Data *pd, const char *_obj_file_name, Evas_3D_Mesh_Frame *f)
{
Evas_3D_Vertex_Buffer *vb;
@@ -35,6 +64,7 @@ _save_mesh(Evas_3D_Mesh_Data *pd, const char *_obj_file_name, Evas_3D_Mesh_Frame
char* c_time_string;
int i;
float *src;
+ Eina_Bool existence_of_normal, existence_of_tex_point;
OPEN_FILE(obj)
if (!_obj_file)
@@ -73,26 +103,30 @@ _save_mesh(Evas_3D_Mesh_Data *pd, const char *_obj_file_name, Evas_3D_Mesh_Frame
return;
}
- SAVE_GEOMETRICS(EVAS_3D_VERTEX_POSITION, "v %.4f %.4f %.4f \n")
- SAVE_GEOMETRICS(EVAS_3D_VERTEX_NORMAL, "vn %.4f %.4f %.4f \n")
- SAVE_GEOMETRICS(EVAS_3D_VERTEX_TEXCOORD, "vt %.4f %.4f %.4f \n")
+ SAVE_GEOMETRICS(EVAS_3D_VERTEX_POSITION, "v %.4f %.4f")
+ SAVE_GEOMETRICS(EVAS_3D_VERTEX_NORMAL, "vn %.4f %.4f")
+ SAVE_GEOMETRICS(EVAS_3D_VERTEX_TEXCOORD, "vt %.4f %.4f")
+
+ existence_of_normal = (f->vertices[EVAS_3D_VERTEX_NORMAL].data != NULL);
+ existence_of_tex_point = (f->vertices[EVAS_3D_VERTEX_TEXCOORD].data != NULL);
+
fprintf(_obj_file,"usemtl Material\n s off\n");
for (i = 1; i <= pd->vertex_count; i++)//numeration of faces in .obj started from 1
{
- fprintf(_obj_file,"f %i/%i/%i ", i, i, i);
+ _write_point(_obj_file, i, 1, existence_of_normal, existence_of_tex_point);
i++;
- fprintf(_obj_file,"%i/%i/%i ", i, i, i);
+ _write_point(_obj_file, i, 2, existence_of_normal, existence_of_tex_point);
i++;
- fprintf(_obj_file,"%i/%i/%i \n", i, i, i);
+ _write_point(_obj_file, i, 3, existence_of_normal, existence_of_tex_point);
}
fclose(_obj_file);
}
static void
-_save_material(Evas_3D_Mesh_Data *pd EINA_UNUSED, const char *_mtl_file_name, Evas_3D_Mesh_Frame *f)
+_save_material(Evas_3D_Mesh_Data *pd EINA_UNUSED,
+ const char *_mtl_file_name,
+ Evas_3D_Material_Data *mat)
{
- Evas_3D_Material_Data *mat = eo_data_scope_get(f->material, EVAS_3D_MATERIAL_CLASS);
-
OPEN_FILE(mtl)
if (!_mtl_file)
{
@@ -126,21 +160,24 @@ void
evas_model_save_file_obj(Evas_3D_Mesh *mesh, const char *_obj_file_name, Evas_3D_Mesh_Frame *f)
{
int len;
- char *without_extention, *_mtl_extension, *_mtl_file_name;
+ char *_mtl_file_name, *_without_extention;
+ Evas_3D_Material_Data *mat;
len = strlen(_obj_file_name);
- without_extention = (char*)malloc((len - 4) * sizeof(char));
- _mtl_extension = ".mtl";
- _mtl_file_name = (char *)malloc(len * sizeof(char));
-
- memcpy(without_extention, _obj_file_name, len-4);
- strcpy(_mtl_file_name, without_extention);
- strcpy(_mtl_file_name + len - 4, _mtl_extension);
+ _without_extention = (char *)malloc(len - 3);
+ _mtl_file_name = (char *)malloc(len + 1);
Evas_3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_3D_MESH_CLASS);
_save_mesh(pd, _obj_file_name, f);
- _save_material(pd, _mtl_file_name, f);
- free(without_extention);
+ mat = eo_data_scope_get(f->material, EVAS_3D_MATERIAL_CLASS);
+ if (mat != NULL)
+ {
+ eina_strlcpy(_without_extention, _obj_file_name, len - 3);
+ eina_str_join(_mtl_file_name, len + 1, '.', _without_extention, "mtl");
+ _save_material(pd, _mtl_file_name, mat);
+ }
+
+ free(_without_extention);
free(_mtl_file_name);
}