summaryrefslogtreecommitdiff
path: root/src/lib/evas/canvas/evas_3d_mesh_saver_ply.c
blob: f2376a33bb95a111a551b36af148e1ccad311f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <time.h>
#include "stdio.h"
#include "math.h"
#include "evas_common_private.h"
#include "evas_private.h"

void
evas_3d_mesh_save_ply(Evas_3D_Mesh *mesh, const char *file, Evas_3D_Mesh_Frame *f)
{
   float *src_pos, *src_nor, *src_tex, *src_col;
   int    i;

   Evas_3D_Mesh_Data *pd = eo_data_scope_get(mesh, EVAS_3D_MESH_CLASS);
   FILE *_ply_file = fopen(file, "w+");

   fprintf(_ply_file,"ply\nformat ascii 1.0\ncomment Created by EFL evas_3d_mesh_saver_ply.c" \
                      "version 1 (sub 0) - www.enlightenment.org, source file: ''\n");
   fprintf(_ply_file,"element vertex %d\n", pd->vertex_count);
   fprintf(_ply_file,"property float x\nproperty float y\nproperty float z\n" \
                     "property float nx\nproperty float ny\nproperty float nz\n" \
                     "property float s\nproperty float t\n" \
                     "property uchar red\nproperty uchar green\nproperty uchar blue\n");
   fprintf(_ply_file,"element face %d\nproperty list uchar uint vertex_indices\nend_header\n",
           pd->vertex_count / 3);

   src_pos = (float*)(&f->vertices[EVAS_3D_VERTEX_POSITION])->data;
   src_nor = (float*)(&f->vertices[EVAS_3D_VERTEX_NORMAL])->data;
   src_tex = (float*)(&f->vertices[EVAS_3D_VERTEX_TEXCOORD])->data;
   src_col = (float*)(&f->vertices[EVAS_3D_VERTEX_COLOR])->data;

   for (i = 0; i < pd->vertex_count; i++)
     {
        fprintf(_ply_file,"%f %f %f %f %f %f %f %f %.0f %.0f %.0f\n",
                src_pos[0], src_pos[1], src_pos[2],
                src_nor[0], src_nor[1], src_nor[2],
                src_tex[0], src_tex[1],
                round(src_col[0] * 255), round(src_col[1] * 255), round(src_col[2] * 255));
        src_pos += 3;
        src_nor += 3;
        src_tex += 2;
        src_col += 4;
     }

   for (i = 0; i < pd->vertex_count;)
     {
        fprintf(_ply_file,"3 %d %d %d\n", i, i + 1, i + 2);
        i += 3;
     }

   fclose(_ply_file);
}