コード例 #1
0
void
evas_model_load_file_obj(Evas_Canvas3D_Mesh *mesh, Eina_File *file)
{
   long i;
   OBJ_Counts counts;//count elements of mesh in .obj
   Eina_Bool will_check_next_char = EINA_FALSE;
   Eina_Bool first_char_is_v = EINA_FALSE;
   Eina_Bool first_char_is_f = EINA_FALSE;
   float *pos = NULL, *nor = NULL, *tex = NULL;
   int stride_pos = 0, stride_nor = 0, stride_tex = 0;
   int j, k, data_for_one_point;
   char *current, *map;
   float *_vertices_obj = NULL, *_normales_obj = NULL, *_tex_coords_obj = NULL;
   int *_triangles;

   map = eina_file_map_all(file, EINA_FILE_SEQUENTIAL);

   if (map == NULL)
     {
        ERR("Failed to create map from file %s\n", eina_file_filename_get(file));
        return;
     }

   counts = _count_elements(map);
   _vertices_obj = malloc(counts._vertex_counter * 3 * sizeof(float));
   data_for_one_point = 1;
   if (counts.existence_of_normal)
     {
        data_for_one_point++;
        _normales_obj = malloc(counts._normal_counter * 3 * sizeof(float));
     }
   if (counts.existence_of_tex_point)
     {
        data_for_one_point++;
        _tex_coords_obj = malloc(counts._texture_point_counter * 3 * sizeof(float));
     }
   _triangles = malloc(counts._triangles_counter * 9 * sizeof(int));

   if ((map == NULL) || (_vertices_obj == NULL) || (_triangles == NULL) ||
       ((counts.existence_of_normal) && (_normales_obj == NULL)) ||
       ((counts.existence_of_tex_point) && (_tex_coords_obj == NULL)))
     {
        ERR("Allocate memory is failed.");
        free(_vertices_obj);
        free(_triangles);
        if (counts.existence_of_normal)
          free(_normales_obj);
        if (counts.existence_of_tex_point)
          free(_tex_coords_obj);
        return;
     }

   current = map;
   i = 0;
   /* put data to arrays */
   for (; *current != '\00'; i++)
     {
        if (will_check_next_char)
          {
             if (first_char_is_v)
               {
                  switch (*current)
                    {
                     case ' ':
                       PUT_DATA_TO_ARRAY(vertices, vertex)
                       i--;
                       break;
                     case 't':
                       current++;
                       if (counts.existence_of_tex_point)
                         {
                            PUT_DATA_TO_ARRAY(tex_coords, texture_point)
                         }
                       break;
                     case 'n':
                       current++;
                       if (counts.existence_of_normal)
                         {
                            PUT_DATA_TO_ARRAY(normales, normal)
                         }
                       break;
                     default:
                       break;
                    }
                  first_char_is_v = EINA_FALSE;
                  will_check_next_char = EINA_FALSE;
               }
             else if (first_char_is_f)
               {
                  char *auxiliary_pointer = current;
                  int count_of_triangles_in_line;
                  int the_first_point = counts.current_triangles_counter;

                  _analyze_face_line(auxiliary_pointer,
                                   &count_of_triangles_in_line);
                  current++;
                  i++;
                  _read_point(_triangles, 1, counts,
                              the_first_point,
                              current);

                  AFTER_NEXT_SPACE(current)

                  for (j = 0; j < count_of_triangles_in_line; j++)
                    {
                       auxiliary_pointer = current;
                       if (counts.current_triangles_counter != the_first_point)
                         {
                            ARRAY_2D(_triangles, counts.current_triangles_counter, 0, 9) = \
                            ARRAY_2D(_triangles, the_first_point, 0, 9);
                            ARRAY_2D(_triangles, counts.current_triangles_counter, 1, 9) = \
                            ARRAY_2D(_triangles, the_first_point, 1, 9);
                            ARRAY_2D(_triangles, counts.current_triangles_counter, 2, 9) = \
                            ARRAY_2D(_triangles, the_first_point, 2, 9);
                         }

                       _read_point(_triangles, 2, counts,
                                   counts.current_triangles_counter,
                                   auxiliary_pointer);
                       AFTER_NEXT_SPACE(auxiliary_pointer);
                       _read_point(_triangles, 3, counts,
                                   counts.current_triangles_counter,
                                   auxiliary_pointer);
                       AFTER_NEXT_SPACE(current);

                       counts.current_triangles_counter++;
                    }
                  first_char_is_f = EINA_FALSE;
               }
             else
               {
                  switch (*current)
                    {
                     case 'v':
                       first_char_is_v = EINA_TRUE;
                       break;
                     case 'f':
                       first_char_is_f = EINA_TRUE;
                       break;
                     case 'm':
                       will_check_next_char = EINA_FALSE;
                       break;
                     default:
                       will_check_next_char = EINA_FALSE;
                       break;
                    }
               }
          }
コード例 #2
0
ファイル: evas_3d_mesh_loader_obj.c プロジェクト: jgfenix/efl
void
evas_3d_mesh_file_obj_set(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
   Eina_Bool will_check_next_char = EINA_FALSE;
   Eina_Bool first_char_is_v = EINA_FALSE;
   Eina_Bool first_char_is_f = EINA_FALSE;
   float *pos, *nor, *tex;
   int stride_pos, stride_nor, stride_tex;
   int j, k;
   char * current;

   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) ||
        (_normales_obj == NULL) || (_tex_coords_obj == NULL) || (_triangles == NULL))
     {
        ERR("Allocate memory is failed.");
        free(start);
        free(_vertices_obj);
        free(_normales_obj);
        free(_tex_coords_obj);
        free(_triangles);
        return;
     }

   current = start;
   i = 0;

   /* put data to arrays */
   for (; length > i; i++)
     {
        if (will_check_next_char)
          {
             if (first_char_is_v)
               {
                  switch (*current)
                    {
                     case ' ':
                       PUT_DATA_TO_ARRAY(vertices, vertex)
                       i--;
                       break;
                     case 't':
                       current++;
                       PUT_DATA_TO_ARRAY(tex_coords, texture_point)
                       break;
                     case 'n':
                       current++;
                       PUT_DATA_TO_ARRAY(normales, normal)
                       break;
                     default:
                       break;
                    }
                  first_char_is_v = EINA_FALSE;
                  will_check_next_char = EINA_FALSE;
               }
             else if (first_char_is_f)
               {
                  char * auxiliary_pointer = current;
                  int count_of_triangles_in_line;

                  _analyze_face_line(auxiliary_pointer,
                                   &count_of_triangles_in_line);
                  current++;
                  i++;
                  int first_pos, first_tex, first_norm;
                  sscanf (current,"%i/%i/%i",
                            &first_pos,
                            &first_tex,
                            &first_norm);

                  do
                    {
                       current++;
                       i++;
                    }
                  while (*current != ' ');

                  current++;
                  i++;

                  for (j = 0; j < count_of_triangles_in_line; j++)
                    {
                       auxiliary_pointer = current;
                       ARRAY_2D(_triangles, counts.current_triangles_counter, 0, 9) = first_pos;
                       ARRAY_2D(_triangles, counts.current_triangles_counter, 1, 9) = first_tex;
                       ARRAY_2D(_triangles, counts.current_triangles_counter, 2, 9) = first_norm;
                       sscanf (auxiliary_pointer,"%i/%i/%i %i/%i/%i",
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 3, 9),
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 4, 9),
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 5, 9),
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 6, 9),
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 7, 9),
                         &ARRAY_2D(_triangles, counts.current_triangles_counter, 8, 9));

                       while (*current != ' ')
                         {
                            current++;
                            i++;
                         }

                       counts.current_triangles_counter++;
                    }
                  first_char_is_f = EINA_FALSE;
               }
             else
               {
                  switch (*current)
                    {
                     case 'v':
                       first_char_is_v = EINA_TRUE;
                       break;
                     case 'f':
                       first_char_is_f = EINA_TRUE;
                       break;
                     case 'm':
                       will_check_next_char = EINA_FALSE;
                       break;
                     default:
                       will_check_next_char = EINA_FALSE;
                       break;
                    }
               }
          }
        else if (*current == '\n')