Subversion Repositories gelsvn

Rev

Rev 373 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
371 jab 1
/*
2
 *  ply_load.cpp
3
 *  GEL
4
 *
5
 *  Created by J. Andreas Bærentzen on 08/08/07.
6
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
7
 *
8
 */
9
 
10
 
11
#include <stdio.h>
12
#include <math.h>
13
#include <string.h>
14
#include "rply.h"
15
#include "ply_load.h"
16
#include <CGLA/Vec4f.h>
17
 
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
		static int idx = 0;
46
		long length, value_index;
47
		ply_get_argument_property(argument, NULL, &length, &value_index);
48
		if(value_index >= 0)
49
		{
50
			if(value_index < 2)
51
				f[value_index] = ply_get_argument_value(argument);	
52
			else
53
			{
54
				f[2] = ply_get_argument_value(argument);
55
				mesh->mat_idx.push_back(0);
56
				mesh->geometry.add_face(f);
57
				f[1] = f[2];
58
			}
59
			return 1;
60
		}
61
	}
62
}
63
 
64
namespace Geometry
65
{
66
	void ply_load(const std::string& fn, Geometry::TriMesh& _mesh)
67
	{
68
		mesh = &_mesh;
69
 
70
		_mesh.materials.resize(1);
71
		_mesh.materials[0].diffuse[0] = 172.0/256; 
72
		_mesh.materials[0].diffuse[1] = 48.0/256;
73
		_mesh.materials[0].diffuse[2] = 72.0/256;
74
		_mesh.materials[0].specular[0] = 0.6; 
75
		_mesh.materials[0].specular[1] = 0.6;
76
		_mesh.materials[0].specular[2] = 0.6;
77
		_mesh.materials[0].shininess =128;
78
 
79
		long nvertices, ntriangles;
80
		p_ply ply = ply_open(fn.c_str(), NULL);
81
		if (!ply) return;
82
		if (!ply_read_header(ply)) return;
83
		nvertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb, NULL, 0);
84
		ply_set_read_cb(ply, "vertex", "y", vertex_cb, NULL, 0);
85
		ply_set_read_cb(ply, "vertex", "z", vertex_cb, NULL, 1);
86
		ntriangles = ply_set_read_cb(ply, "face", "vertex_indices", face_cb, NULL, 0);
87
		ply_read(ply);
88
		ply_close(ply);
89
	}
90
}