Subversion Repositories gelsvn

Rev

Rev 382 | Go to most recent revision | Details | Compare with Previous | 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
		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
		}
382 jab 59
		return 1;
371 jab 60
	}
61
}
62
 
63
namespace Geometry
64
{
65
	void ply_load(const std::string& fn, Geometry::TriMesh& _mesh)
66
	{
67
		mesh = &_mesh;
68
 
69
		_mesh.materials.resize(1);
373 jrf 70
		_mesh.materials[0].diffuse[0] = 172.0f/256.0f; 
71
		_mesh.materials[0].diffuse[1] = 48.0f/256.0f;
72
		_mesh.materials[0].diffuse[2] = 72.0f/256.0f;
73
		_mesh.materials[0].specular[0] = 0.6f; 
74
		_mesh.materials[0].specular[1] = 0.6f;
75
		_mesh.materials[0].specular[2] = 0.6f;
76
		_mesh.materials[0].shininess = 128.0f;
371 jab 77
 
78
		long nvertices, ntriangles;
79
		p_ply ply = ply_open(fn.c_str(), NULL);
80
		if (!ply) return;
81
		if (!ply_read_header(ply)) return;
82
		nvertices = ply_set_read_cb(ply, "vertex", "x", vertex_cb, NULL, 0);
83
		ply_set_read_cb(ply, "vertex", "y", vertex_cb, NULL, 0);
84
		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);
86
		ply_read(ply);
87
		ply_close(ply);
88
	}
89
}