Subversion Repositories gelsvn

Rev

Rev 382 | Rev 595 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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

Generated by GNU Enscript 1.6.6.
91

Generated by GNU Enscript 1.6.6.
93
 
92
 
94
 
93
 
95
 
94