Subversion Repositories gelsvn

Rev

Rev 601 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
371 jab 6
 
7
#include <stdio.h>
8
#include <math.h>
9
#include <string.h>
10
#include "rply.h"
11
#include "ply_load.h"
601 jab 12
#include "../CGLA/Vec4f.h"
371 jab 13
 
595 jab 14
namespace Geometry
371 jab 15
{
595 jab 16
    using namespace std;
17
    using namespace CGLA;
371 jab 18
 
595 jab 19
    namespace 
20
    {
21
        TriMesh *mesh;
22
 
23
        int vertex_cb(p_ply_argument argument) {
24
            static int idx=0;
25
            static Vec3f p;
26
            int eol;
27
            ply_get_argument_user_data(argument, NULL, &eol);
28
            if(idx<3)
29
                p[idx] = ply_get_argument_value(argument);
30
            ++idx;
31
            if (eol) 
32
            {
33
                mesh->geometry.add_vertex(p);
34
                idx=0;
35
            }
36
            return 1;
37
        }
38
 
39
        int face_cb(p_ply_argument argument) {
40
            static Vec3i f;
41
            int length, value_index;
42
            ply_get_argument_property(argument, NULL, &length, &value_index);
43
            if(value_index >= 0)
44
            {
45
                if(value_index < 2)
46
                    f[value_index] = ply_get_argument_value(argument);	
47
                else
48
                {
49
                    f[2] = ply_get_argument_value(argument);
50
                    mesh->mat_idx.push_back(0);
51
                    mesh->geometry.add_face(f);
52
                    f[1] = f[2];
53
                }
54
            }
55
            return 1;
56
        }
57
    }
58
 
59
    bool ply_load(const std::string& fn, Geometry::TriMesh& _mesh)
60
    {
61
        mesh = &_mesh;
62
 
63
        _mesh.materials.resize(1);
64
        _mesh.materials[0].diffuse[0] = 172.0f/256.0f; 
65
        _mesh.materials[0].diffuse[1] = 48.0f/256.0f;
66
        _mesh.materials[0].diffuse[2] = 72.0f/256.0f;
67
        _mesh.materials[0].specular[0] = 0.6f; 
68
        _mesh.materials[0].specular[1] = 0.6f;
69
        _mesh.materials[0].specular[2] = 0.6f;
70
        _mesh.materials[0].shininess = 128.0f;
71
 
72
        p_ply ply = ply_open(fn.c_str(), NULL);
73
        if (!ply) return false;
74
        if (!ply_read_header(ply)) return false;
75
        ply_set_read_cb(ply, "vertex", "x", vertex_cb, NULL, 0);
76
        ply_set_read_cb(ply, "vertex", "y", vertex_cb, NULL, 0);
77
        ply_set_read_cb(ply, "vertex", "z", vertex_cb, NULL, 1);
78
        ply_set_read_cb(ply, "face", "vertex_indices", face_cb, NULL, 0);
79
        ply_read(ply);
80
        ply_close(ply);
81
        return true;
82
    }
371 jab 83
}