Subversion Repositories gelsvn

Rev

Rev 404 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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