aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/evas/model_loaders/obj
diff options
context:
space:
mode:
authorBogdan Devichev <b.devichev@samsung.com>2014-12-03 12:24:03 +0200
committerCedric BAIL <cedric@osg.samsung.com>2014-12-23 21:19:20 +0100
commite024f861e1aa3ffd74ea6a79ce480242930471dd (patch)
tree81234f5585ce2956e540e932f9507afbaadd46bc /src/modules/evas/model_loaders/obj
parentevas: test cases are changed according to changes of API in step 4. Specific ... (diff)
downloadefl-e024f861e1aa3ffd74ea6a79ce480242930471dd.tar.gz
evas: ply_loader and obj_loader work with Eina_File.
Diffstat (limited to 'src/modules/evas/model_loaders/obj')
-rw-r--r--src/modules/evas/model_loaders/obj/evas_model_load_obj.c99
1 files changed, 68 insertions, 31 deletions
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 36977464ba..359d29904e 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
@@ -7,6 +7,7 @@
#include "evas_common_private.h"
#include "evas_private.h"
+//TODO Increase of stability (reading .obj file saved with any flags).
/* set value to position [x][y] to array name which have. */
#define ARRAY_2D(name, x, y, count_y) (*(name + x * count_y + y))
@@ -37,29 +38,56 @@ struct _OBJ_Counts
Eina_Bool existence_of_tex_point;
};
-static inline char*
-_file_to_buf(const char *file, long *length)//prepare text file for reading
+typedef struct _OBJ_Loader
{
- FILE *file_for_print;
- char *buf;
- int unused __attribute__((unused));//this variable fixes warning "ignoring return value of fread"
-
- *length = 0;
- file_for_print = fopen(file, "rb");
- if (!file_for_print) return NULL;
- fseek(file_for_print, 0, SEEK_END);//set file_for_print to the end of file
- *length = ftell(file_for_print);//set current position of file_for_print
- if (*length < 0)
+ Eina_File *file;
+ char *map;
+} OBJ_Loader;
+
+static inline void
+_obj_loader_fini(OBJ_Loader *loader)
+{
+ if (loader->map)
+ {
+ eina_file_map_free(loader->file, loader->map);
+ loader->map = NULL;
+ }
+
+ if (loader->file)
{
- fclose(file_for_print);
- return NULL;
+ eina_file_close(loader->file);
+ loader->file = NULL;
}
- buf = malloc(*length + 1);
- fseek(file_for_print, 0, SEEK_SET);//set file_for_print to the begining of file
- unused = fread(buf, *length, 1, file_for_print);
- fclose(file_for_print);
- buf[*length] = '\0';
- return buf;
+}
+
+static inline Eina_Bool
+_obj_loader_init(OBJ_Loader *loader, const char *file)
+{
+ memset(loader, 0x00, sizeof(OBJ_Loader));
+
+ /* Open given file. */
+ loader->file = eina_file_open(file, 0);
+
+ if (loader->file == NULL)
+ {
+ ERR("Failed to open file %s\n", file);
+ goto error;
+ }
+
+ /* Map the file. */
+ loader->map = eina_file_map_all(loader->file, EINA_FILE_SEQUENTIAL);
+
+ if (loader->map == NULL)
+ {
+ ERR("Failed to create map from file %s\n", file);
+ goto error;
+ }
+
+ return EINA_TRUE;
+
+error:
+ _obj_loader_fini(loader);
+ return EINA_FALSE;
}
/* create new counter */
@@ -107,11 +135,11 @@ _analyze_face_line(char * face_analyzer,
}
static inline OBJ_Counts
-_count_elements(char *start, long length)//count elements of mesh in .obj
+_count_elements(OBJ_Loader loader)//count elements of mesh in .obj
{
OBJ_Counts counts = _new_count_elements();
- char * current = start;
+ char * current = loader.map;
int polygon_checker = -2;//polygons with n vertices can be represented as n-2 triangles
Eina_Bool will_check_next_char = EINA_FALSE;
Eina_Bool first_char_is_v = EINA_FALSE;
@@ -120,7 +148,7 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
long i = 0;
/* count elements of mesh in .obj */
- for (; length > i; i++)
+ for (; *current != '\00'; i++)
{
if (will_check_next_char)
{
@@ -204,9 +232,9 @@ _count_elements(char *start, long length)//count elements of mesh in .obj
void
evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
{
- long length, i;
- char * start = _file_to_buf(file, &length);
- OBJ_Counts counts = _count_elements(start, length);//count elements of mesh in .obj
+ long i;
+ OBJ_Counts counts;//count elements of mesh in .obj
+ OBJ_Loader loader;
Eina_Bool will_check_next_char = EINA_FALSE;
Eina_Bool first_char_is_v = EINA_FALSE;
Eina_Bool first_char_is_f = EINA_FALSE;
@@ -215,17 +243,26 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
int j, k;
char * current;
+ /* Initialize PLY loader */
+ if (!_obj_loader_init(&loader, file))
+ {
+ ERR("Failed to initialize PLY loader.");
+ return;
+ }
+
+ counts = _count_elements(loader);
+
float *_vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float));
float *_normales_obj = malloc(counts._normal_counter * 3 * sizeof(float));
float *_tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float));
/* triangle has 3 points, every point has 3(vertix, texture and normal) coord */
int *_triangles = malloc(counts._triangles_counter * 9 * sizeof(int));
- if ((start == NULL) || (_vertices_obj == NULL) ||
+ if ((loader.map == NULL) || (_vertices_obj == NULL) ||
(_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL))
{
ERR("Allocate memory is failed.");
- free(start);
+ _obj_loader_fini(&loader);
free(_vertices_obj);
free(_normales_obj);
free(_tex_coords_obj);
@@ -233,11 +270,11 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
return;
}
- current = start;
+ current = loader.map;
i = 0;
/* put data to arrays */
- for (; length > i; i++)
+ for (; *current != '\00'; i++)
{
if (will_check_next_char)
{
@@ -337,7 +374,6 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
}
current++;
}
- free(start);
/* prepare of mesh and take pointers to data which must be read */
eo_do(mesh,
@@ -398,4 +434,5 @@ evas_model_load_file_obj(Evas_3D_Mesh *mesh, const char *file)
{
ERR("Axis-Aligned Bounding Box wan't added in frame %d ", 0);
}
+ _obj_loader_fini(&loader);
}