Subversion Repositories gelsvn

Rev

Rev 335 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 335 Rev 596
1
// ----------------------------------------
1
// ----------------------------------------
2
// A simple Range Scan Viewer
2
// A simple Range Scan Viewer
3
//
3
//
4
// Controls:
4
// Controls:
5
// - left mouse down + mouse motion : rotate
5
// - left mouse down + mouse motion : rotate
6
// - Scroll button and +- buttons   : zoom
6
// - Scroll button and +- buttons   : zoom
7
// - right mouse click              : centre trackball
7
// - right mouse click              : centre trackball
8
// - esc                            : exits
8
// - esc                            : exits
9
// - x,y,z buttons                  : switch trackball up axis
9
// - x,y,z buttons                  : switch trackball up axis
10
// ----------------------------------------
10
// ----------------------------------------
11
 
11
 
12
#if (_MSC_VER >= 1200)
12
#if (_MSC_VER >= 1200)
13
#pragma warning (disable: 4786)
13
#pragma warning (disable: 4786)
14
#endif
14
#endif
15
 
15
 
16
#include <list>
16
#include <list>
17
#include <vector>
17
#include <vector>
18
 
18
 
19
#include <assert.h>
19
#include <assert.h>
20
#include <stdio.h>
20
#include <stdio.h>
21
#ifdef WIN32
21
#ifdef WIN32
22
#include <windows.h>
22
#include <windows.h>
23
#include <io.h>
23
#include <io.h>
24
#endif
24
#endif
25
#include <string.h>
25
#include <string.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
 
27
 
28
#include <iostream>
28
#include <iostream>
29
#include <CGLA/Vec2i.h>
29
#include <CGLA/Vec2i.h>
30
#include <CGLA/Vec2f.h>
30
#include <CGLA/Vec2f.h>
31
 
31
 
32
#include <CGLA/Vec3f.h>
32
#include <CGLA/Vec3f.h>
33
#include <CGLA/Mat4x4f.h>
33
#include <CGLA/Mat4x4f.h>
34
#include "GLGraphics/gel_glut.h"
34
#include "GLGraphics/gel_glut.h"
35
#include "GLGraphics/QuatTrackBall.h"
35
#include "GLGraphics/QuatTrackBall.h"
36
#include "Geometry/KDTree.h"
36
#include "Geometry/KDTree.h"
37
 
37
 
38
 
38
 
39
#include "ply.h"
39
#include "ply.h"
40
 
40
 
41
#include <CGLA/statistics.h>
41
#include <CGLA/statistics.h>
42
#include <CGLA/eigensolution.h>
42
#include <CGLA/eigensolution.h>
43
 
43
 
44
using namespace std;
44
using namespace std;
45
using namespace CGLA;
45
using namespace CGLA;
46
using namespace Geometry;
46
using namespace Geometry;
47
using namespace GLGraphics;
47
using namespace GLGraphics;
48
 
48
 
49
namespace
49
namespace
50
{
50
{
51
	int win_size_x = 800;
51
	int win_size_x = 800;
52
	int win_size_y = 800;
52
	int win_size_y = 800;
53
 
53
 
54
	QuatTrackBall* ball;
54
	QuatTrackBall* ball;
55
	int spin_timer = 20;
55
	int spin_timer = 20;
56
	void spin(int x);
56
	void spin(int x);
57
 
57
 
58
	int main_window;
58
	int main_window;
59
 
59
 
60
	Vec3f cen;
60
	Vec3f cen;
61
	Vec3f pmin;
61
	Vec3f pmin;
62
	Vec3f bbsize;
62
	Vec3f bbsize;
63
	std::vector<CGLA::Vec3f> verts;
63
	std::vector<CGLA::Vec3f> verts;
64
	std::vector<CGLA::Vec3f> norms;
64
	std::vector<CGLA::Vec3f> norms;
65
	float rad;
65
	float rad;
66
 
66
 
67
	void verts_from_conf(std::string filename, float dist=0.002)
67
	void verts_from_conf(std::string filename, float dist=0.002)
68
	{
68
	{
69
		verts.clear();
69
		verts.clear();
70
		norms.clear();
70
		norms.clear();
71
 
71
 
72
		CGLA::Quatf qrot;
72
		CGLA::Quatf qrot;
73
		CGLA::Vec3f tr;
73
		CGLA::Vec3f tr;
74
		CGLA::Mat4x4f transform;
74
		CGLA::Mat4x4f transform;
75
		CGLA::Mat4x4f Ntransform;
75
		CGLA::Mat4x4f Ntransform;
76
 
76
 
77
		char plyfile[50];
77
		char plyfile[50];
78
		char elemtag[50];
78
		char elemtag[50];
79
		FILE * pFile;
79
		FILE * pFile;
80
		//filename = "data/" + filename;
80
		//filename = "data/" + filename;
81
		pFile = fopen (filename.c_str(),"r");
81
		pFile = fopen (filename.c_str(),"r");
82
		if (pFile==NULL)
82
		if (pFile==NULL)
83
		{
83
		{
84
			cout << "file error " << filename << endl;
84
			cout << "file error " << filename << endl;
85
			return;
85
			return;
86
		}
86
		}
87
 
87
 
88
		bool end=false;
88
		bool end=false;
89
 
89
 
90
		cen=Vec3f(0,0,0);
90
		cen=Vec3f(0,0,0);
91
		pmin=Vec3f(1000,10000,1000);
91
		pmin=Vec3f(1000,10000,1000);
92
		bbsize=Vec3f(-100,-100,-100);
92
		bbsize=Vec3f(-100,-100,-100);
93
 
93
 
94
		cout << "using file " << filename << endl;
94
		cout << "using file " << filename << endl;
95
 
95
 
96
		while(!end)
96
		while(!end)
97
		{
97
		{
98
			//bmesh bun045.ply -0.0520211 -0.000383981 -0.0109223 0.00548449 -0.294635 -0.0038555 0.955586
98
			//bmesh bun045.ply -0.0520211 -0.000383981 -0.0109223 0.00548449 -0.294635 -0.0038555 0.955586
99
 
99
 
100
			//int params = fscanf (pFile, "%s %s %f %f %f %f %f %f %f", &elemtag, &plyfile, &tr[0], &tr[1], &tr[2], &qrot.qw ,&qrot.qv[0], &qrot.qv[1], &qrot.qv[2]);
100
			//int params = fscanf (pFile, "%s %s %f %f %f %f %f %f %f", &elemtag, &plyfile, &tr[0], &tr[1], &tr[2], &qrot.qw ,&qrot.qv[0], &qrot.qv[1], &qrot.qv[2]);
101
			int params = fscanf (pFile, "%s %s %f %f %f %f %f %f %f", &elemtag, &plyfile, &tr[0], &tr[1], &tr[2],&qrot.qv[0], &qrot.qv[1], &qrot.qv[2], &qrot.qw );
101
			int params = fscanf (pFile, "%s %s %f %f %f %f %f %f %f", &elemtag, &plyfile, &tr[0], &tr[1], &tr[2],&qrot.qv[0], &qrot.qv[1], &qrot.qv[2], &qrot.qw );
102
			//int params = fscanf (pFile, "%s ", &elemtag);
102
			//int params = fscanf (pFile, "%s ", &elemtag);
103
			if (params>0)
103
			if (params>0)
104
			{
104
			{
105
				if ( strcmp(elemtag, "bmesh")==0 )
105
				if ( strcmp(elemtag, "bmesh")==0 )
106
				{
106
				{
107
					//  fscanf (pFile, "%s %f %f %f %f %f %f %f\n", &plyfile, &tr[0], &tr[1], &tr[2],&qrot.qv[0], &qrot.qv[1], &qrot.qv[2], &qrot.qw );
107
					//  fscanf (pFile, "%s %f %f %f %f %f %f %f\n", &plyfile, &tr[0], &tr[1], &tr[2],&qrot.qv[0], &qrot.qv[1], &qrot.qv[2], &qrot.qw );
108
					string splyfile = string(plyfile);
108
					string splyfile = string(plyfile);
109
					cout << "found plyfile " << splyfile << endl;
109
					cout << "found plyfile " << splyfile << endl;
110
 
110
 
111
					qrot = qrot.inverse();
111
					qrot = qrot.inverse();
112
					transform = CGLA::translation_Mat4x4f(tr) * qrot.get_Mat4x4f();
112
					transform = CGLA::translation_Mat4x4f(tr) * qrot.get_Mat4x4f();
113
					Ntransform = transform;
113
					Ntransform = transform;
114
					Ntransform = invert(Ntransform);
114
					Ntransform = invert(Ntransform);
115
					Ntransform = transpose(Ntransform);
115
					Ntransform = transpose(Ntransform);
116
 
116
 
117
					PlyFile *ply;
117
					PlyFile *ply;
118
					int nelems;
118
					int nelems;
119
					int nprops;
119
					int nprops;
120
					char **elist;
120
					char **elist;
121
					int file_type;
121
					int file_type;
122
					float version;
122
					float version;
123
					int num_elems;
123
					int num_elems;
124
					//PlyProperty **plist;
124
					//PlyProperty **plist;
125
					char *elem_name;
125
					char *elem_name;
126
 
126
 
127
 
127
 
128
					ply = ply_open_for_reading(splyfile.c_str(), &nelems, &elist, &file_type, &version);
128
					ply = ply_open_for_reading(splyfile.c_str(), &nelems, &elist, &file_type, &version);
129
 
129
 
130
					/* print what we found out about the file */
130
					/* print what we found out about the file */
131
					printf ("version %f\n", version);
131
					printf ("version %f\n", version);
132
					printf ("type %d\n", file_type);
132
					printf ("type %d\n", file_type);
133
 
133
 
134
					/* go through each kind of element that we learned is in the file */
134
					/* go through each kind of element that we learned is in the file */
135
					/* and read them */
135
					/* and read them */
136
 
136
 
-
 
137
                    cout << "nelems: " << nelems << endl;
137
					for (int i = 0; i < nelems; i++) 
138
					for (int i = 0; i < nelems; i++) 
138
					{
139
					{
139
 
140
 
-
 
141
                        cout << "." << flush;
140
						/* get the description of the first element */
142
						/* get the description of the first element */
141
						elem_name = elist[i];
143
						elem_name = elist[i];
142
						//plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
144
						//plist = ply_get_element_description (ply, elem_name, &num_elems, &nprops);
143
						ply_get_element_description (ply, elem_name, &num_elems, &nprops);
145
						ply_get_element_description (ply, elem_name, &num_elems, &nprops);
144
 
146
 
145
						/* print the name of the element, for debugging */
147
						/* print the name of the element, for debugging */
146
						printf ("element %s %d\n", elem_name, num_elems);
148
						printf ("element %s %d\n", elem_name, num_elems);
147
 
149
 
148
						/* if we're on vertex elements, read them in */
150
						/* if we're on vertex elements, read them in */
149
						if (equal_strings ("vertex", elem_name)) 
151
						if (equal_strings ("vertex", elem_name)) 
150
						{
152
						{
-
 
153
                            cout << "v" << flush;
151
							typedef struct PlyVertex {
154
							typedef struct PlyVertex {
152
								float x,y,z;             /* the usual 3-space position of a vertex */
155
								float x,y,z;             /* the usual 3-space position of a vertex */
153
							} PlyVertex;
156
							} PlyVertex;
154
 
157
 
155
							PlyProperty vert_props[] = { /* list of property information for a vertex */
158
							PlyProperty vert_props[] = { /* list of property information for a vertex */
156
								{"x", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,x), 0, 0, 0, 0},
159
								{"x", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,x), 0, 0, 0, 0},
157
								{"y", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,y), 0, 0, 0, 0},
160
								{"y", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,y), 0, 0, 0, 0},
158
								{"z", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,z), 0, 0, 0, 0},
161
								{"z", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,z), 0, 0, 0, 0},
159
							};
162
							};
160
 
163
 
161
							/* set up for getting vertex elements */
164
							/* set up for getting vertex elements */
162
							ply_get_property (ply, elem_name, &vert_props[0]);
165
							ply_get_property (ply, elem_name, &vert_props[0]);
163
							ply_get_property (ply, elem_name, &vert_props[1]);
166
							ply_get_property (ply, elem_name, &vert_props[1]);
164
							ply_get_property (ply, elem_name, &vert_props[2]);
167
							ply_get_property (ply, elem_name, &vert_props[2]);
165
 
168
 
166
 
169
 
167
							/* grab all the vertex elements */
170
							/* grab all the vertex elements */
168
							for (int j = 0; j < num_elems; j++) 
171
							for (int j = 0; j < num_elems; j++) 
169
							{
172
							{
170
								PlyVertex * pv = (PlyVertex *) malloc (sizeof (PlyVertex));
173
								PlyVertex * pv = (PlyVertex *) malloc (sizeof (PlyVertex));
171
 
174
 
172
								/* grab and element from the file */
175
								/* grab and element from the file */
173
								ply_get_element (ply, (void*)pv);
176
								ply_get_element (ply, (void*)pv);
174
 
177
 
175
								Vec3f v = transform.mul_3D_point(Vec3f(pv->x, pv->y, pv->z));
178
								Vec3f v = 	Ntransform.mul_3D_point(Vec3f(pv->x, pv->y, pv->z));
176
								verts.push_back(v);
179
								verts.push_back(v);
177
 
180
 
178
								Vec3f n = Ntransform.mul_3D_vector(Vec3f(0,0,1));
181
								Vec3f n = Ntransform.mul_3D_vector(Vec3f(0,0,1));
179
								norms.push_back(n);
182
								norms.push_back(n);
180
 
183
 
181
								cen += v;
184
								cen += v;
182
								pmin = v_min(pmin,v);
185
								pmin = v_min(pmin,v);
183
								bbsize = v_max(bbsize,v);
186
								bbsize = v_max(bbsize,v);
184
							}
187
							}
185
						}
188
						}
186
					}
189
					}
187
				}
190
				}
188
			}
191
			}
189
			else
192
			else
190
			{
193
			{
191
				end=true;
194
				end=true;
192
				fclose(pFile);
195
				fclose(pFile);
193
			}
196
			}
194
		}
197
		}
195
 
198
 
196
		if (dist>0)
199
		if (dist>0)
197
		{
200
		{
198
			Geometry::KDTree<CGLA::Vec3f,int>* tree = new Geometry::KDTree<CGLA::Vec3f,int>();
201
			Geometry::KDTree<CGLA::Vec3f,int>* tree = new Geometry::KDTree<CGLA::Vec3f,int>();
199
			for (unsigned int i=0; i<verts.size(); ++i)
202
			for (unsigned int i=0; i<verts.size(); ++i)
200
				tree->insert(verts[i], i);
203
				tree->insert(verts[i], i);
201
			tree->build();
204
			tree->build();
202
 
205
 
203
			int skip=0;
206
			int skip=0;
204
			for (unsigned int i=0; i<verts.size(); ++i)
207
			for (unsigned int i=0; i<verts.size(); ++i)
205
			{
208
			{
206
				if (i % 100000 == 0)
209
				if (i % 100000 == 0)
207
					cerr << ".";
210
					cerr << ".";
208
 
211
 
209
				vector<CGLA::Vec3f> neigbours;
212
				vector<CGLA::Vec3f> neigbours;
210
				vector<int> neigbour_ids;
213
				vector<int> neigbour_ids;
211
				int num = tree->in_sphere(verts[i], dist, neigbours, neigbour_ids);
214
				int num = tree->in_sphere(verts[i], dist, neigbours, neigbour_ids);
212
 
215
 
213
				if (num > 5)
216
				if (num > 5)
214
				{
217
				{
215
					Mat3x3f A;
218
					Mat3x3f A;
216
					Vec3f m = covariance(neigbours, A);
219
					Vec3f m = covariance(neigbours, A);
217
					Mat3x3f Q,L;
220
					Mat3x3f Q,L;
218
					int n = power_eigensolution(A, Q, L);
221
					int n = power_eigensolution(A, Q, L);
219
 
222
 
220
					int min_id;
223
					int min_id;
221
					if ( L[0][0] < L[1][1])
224
					if ( L[0][0] < L[1][1])
222
					{
225
					{
223
						if ( L[0][0] < L[2][2])
226
						if ( L[0][0] < L[2][2])
224
							min_id = 0;
227
							min_id = 0;
225
						else
228
						else
226
							min_id = 2;
229
							min_id = 2;
227
					}
230
					}
228
					else
231
					else
229
					{
232
					{
230
						if ( L[1][1] < L[2][2])
233
						if ( L[1][1] < L[2][2])
231
							min_id = 1;
234
							min_id = 1;
232
						else
235
						else
233
							min_id = 2;
236
							min_id = 2;
234
					}
237
					}
235
 
238
 
236
					Vec3f temp_n(Q[min_id][0], Q[min_id][1], Q[min_id][2]);
239
					Vec3f temp_n(Q[min_id][0], Q[min_id][1], Q[min_id][2]);
237
					temp_n.normalize();
240
					temp_n.normalize();
238
					float sgn = sign(dot(temp_n, norms[i]));
241
					float sgn = sign(dot(temp_n, norms[i]));
239
					norms[i] = sgn*temp_n;
242
					norms[i] = sgn*temp_n;
240
				}
243
				}
241
				else
244
				else
242
				{
245
				{
243
					norms[i] = Vec3f(0,0,0);
246
					norms[i] = Vec3f(0,0,0);
244
					skip++;
247
					skip++;
245
				}
248
				}
246
			}
249
			}
247
 
250
 
248
			cout << skip << " of " << verts.size() << " with zero normal" << endl;;
251
			cout << skip << " of " << verts.size() << " with zero normal" << endl;;
249
		}
252
		}
250
		cen /= (float)verts.size();
253
		cen /= (float)verts.size();
251
		bbsize =  bbsize-pmin;
254
		bbsize =  bbsize-pmin;
252
		rad = 0.7f*bbsize.length();
255
		rad = 0.7f*bbsize.length();
253
 
256
 
254
		//for(unsigned int i=0; i<verts.size(); ++i )
257
		//for(unsigned int i=0; i<verts.size(); ++i )
255
		//  scale( verts[i], rad);
258
		//  scale( verts[i], rad);
256
		//
259
		//
257
		//pmin= Vec3f(0,0,0);
260
		//pmin= Vec3f(0,0,0);
258
		//  //bbsize = Vec3f(cur_dim);
261
		//  //bbsize = Vec3f(cur_dim);
259
		//  bbsize = Vec3f(1,1,1);
262
		//  bbsize = Vec3f(1,1,1);
260
		//  cen = 0.5f*bbsize;
263
		//  cen = 0.5f*bbsize;
261
	}
264
	}
262
 
265
 
263
	void write_obj(string fn)
266
	void write_obj(string fn)
264
	{
267
	{
265
		FILE* pFile = fopen (fn.c_str(),"w+");
268
		FILE* pFile = fopen (fn.c_str(),"w+");
266
		if (pFile==NULL)
269
		if (pFile==NULL)
267
		{
270
		{
268
			cout << "file error " << fn << endl;
271
			cout << "file error " << fn << endl;
269
			return;
272
			return;
270
		}
273
		}
271
 
274
 
272
		fprintf( pFile, "# %d vertices with normal\n", verts.size());
275
		fprintf( pFile, "# %lu vertices with normal\n", verts.size());
273
		//fprintf( pFile, "# center : %d vertices with normal", verts.size());
276
		//fprintf( pFile, "# center : %d vertices with normal", verts.size());
274
		
277
		
275
		for(unsigned int i=0; i<verts.size(); ++i )
278
		for(unsigned int i=0; i<verts.size(); ++i )
276
		  fprintf( pFile, "v %f %f %f\n", verts[i][0], verts[i][1], verts[i][2]);
279
		  fprintf( pFile, "v %f %f %f\n", verts[i][0], verts[i][1], verts[i][2]);
277
		for(unsigned int i=0; i<norms.size(); ++i )
280
		for(unsigned int i=0; i<norms.size(); ++i )
278
		  fprintf( pFile, "vn %f %f %f\n", norms[i][0], norms[i][1], norms[i][2]);
281
		  fprintf( pFile, "vn %f %f %f\n", norms[i][0], norms[i][1], norms[i][2]);
279
 
282
 
280
		fclose(pFile);
283
		fclose(pFile);
281
 
284
 
282
	}
285
	}
283
 
286
 
284
 
287
 
285
	void mouse_motion(int x, int y)
288
	void mouse_motion(int x, int y)
286
	{
289
	{
287
		ball->roll_ball(Vec2i(x,y));
290
		ball->roll_ball(Vec2i(x,y));
288
	}
291
	}
289
 
292
 
290
	void mouse(int btn, int state, int x, int y)
293
	void mouse(int btn, int state, int x, int y)
291
	{
294
	{
292
		if(state == GLUT_DOWN) 
295
		if(state == GLUT_DOWN) 
293
		{
296
		{
294
			if(btn == GLUT_LEFT_BUTTON) 
297
			if(btn == GLUT_LEFT_BUTTON) 
295
				ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
298
				ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
296
			else if(btn == GLUT_MIDDLE_BUTTON) 
299
			else if(btn == GLUT_MIDDLE_BUTTON) 
297
				ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
300
				ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
298
			else if(btn == GLUT_RIGHT_BUTTON) 
301
			else if(btn == GLUT_RIGHT_BUTTON) 
299
				ball->grab_ball(PAN_ACTION, Vec2i(x, y));
302
				ball->grab_ball(PAN_ACTION, Vec2i(x, y));
300
		}
303
		}
301
		else if(state == GLUT_UP)
304
		else if(state == GLUT_UP)
302
			ball->release_ball();	
305
			ball->release_ball();	
303
	}
306
	}
304
 
307
 
305
	void spin(int x)
308
	void spin(int x)
306
	{
309
	{
307
		ball->do_spin();
310
		ball->do_spin();
308
		glutTimerFunc(spin_timer, spin, 0);  
311
		glutTimerFunc(spin_timer, spin, 0);  
309
		glutPostRedisplay();
312
		glutPostRedisplay();
310
	}
313
	}
311
 
314
 
312
 
315
 
313
	void draw()
316
	void draw()
314
	{
317
	{
315
		glBegin(GL_POINTS);
318
		glBegin(GL_POINTS);
316
		for(unsigned int i=0; i<verts.size(); ++i )
319
		for(unsigned int i=0; i<verts.size(); ++i )
317
		{
320
		{
318
			glVertex3fv( verts[i].get());
321
			glVertex3fv( verts[i].get());
319
		}
322
		}
320
		glEnd();
323
		glEnd();
321
	}
324
	}
322
 
325
 
323
	void draw_normals(float l=0.001)
326
	void draw_normals(float l=0.001)
324
	{
327
	{
325
		glBegin(GL_LINES);
328
		glBegin(GL_LINES);
326
		for(unsigned int i=0; i<verts.size(); ++i )
329
		for(unsigned int i=0; i<verts.size(); ++i )
327
		{
330
		{
328
			Vec3f p1 = verts[i] + l*norms[i];
331
			Vec3f p1 = verts[i] + l*norms[i];
329
			glVertex3fv( verts[i].get());
332
			glVertex3fv( verts[i].get());
330
			glVertex3fv( p1.get());
333
			glVertex3fv( p1.get());
331
		}
334
		}
332
		glEnd();
335
		glEnd();
333
	}
336
	}
334
 
337
 
335
	void display()
338
	void display()
336
	{
339
	{
337
		static bool washere = false;
340
		static bool washere = false;
338
		static unsigned int lp, ln;
341
		static unsigned int lp, ln;
339
		if(!washere)
342
		if(!washere)
340
		{
343
		{
341
			cout << "Creating display list" << endl;
344
			cout << "Creating display list" << endl;
342
			lp = glGenLists(1);
345
			lp = glGenLists(1);
343
			glNewList(lp, GL_COMPILE);
346
			glNewList(lp, GL_COMPILE);
344
			draw();
347
			draw();
345
			glEndList();
348
			glEndList();
346
			cout << "Creating display list" << endl;
349
			cout << "Creating display list" << endl;
347
			ln = glGenLists(1);
350
			ln = glGenLists(1);
348
			glNewList(ln, GL_COMPILE);
351
			glNewList(ln, GL_COMPILE);
349
			draw_normals();
352
			draw_normals();
350
			glEndList();
353
			glEndList();
351
			
354
			
352
			washere = true;
355
			washere = true;
353
			glutTimerFunc(spin_timer, spin, 0);	
356
			glutTimerFunc(spin_timer, spin, 0);	
354
		}
357
		}
355
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
358
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
356
		glLoadIdentity();
359
		glLoadIdentity();
357
		ball->set_gl_modelview();
360
		ball->set_gl_modelview();
358
		glColor3f(0.9,0.1,0);
361
		glColor3f(0.9,0.1,0);
359
		glCallList(lp);
362
		glCallList(lp);
360
		glColor3f(0.0,0.9,0.5);
363
		glColor3f(0.0,0.9,0.5);
361
		glCallList(ln);
364
		glCallList(ln);
362
		
365
		
363
		glPushMatrix();
366
		glPushMatrix();
364
		glColor3f(0.7,0.9,0.3);
367
		glColor3f(0.7,0.9,0.3);
365
 
368
 
366
		glTranslatef(cen[0], cen[1], cen[2]);
369
		glTranslatef(cen[0], cen[1], cen[2]);
367
		glScalef(bbsize[0], bbsize[1], bbsize[2]);
370
		glScalef(bbsize[0], bbsize[1], bbsize[2]);
368
		glutWireCube(1.0);
371
		glutWireCube(1.0);
369
		glPopMatrix();
372
		glPopMatrix();
370
		glutSwapBuffers();
373
		glutSwapBuffers();
371
	}
374
	}
372
 
375
 
373
	void keyboard(unsigned char key, int x, int y)
376
	void keyboard(unsigned char key, int x, int y)
374
	{
377
	{
375
		switch(key)
378
		switch(key)
376
		{
379
		{
377
		case '\033': exit(0); break;
380
		case '\033': exit(0); break;
378
			//case '+': ball->closer(); break;
381
			//case '+': ball->closer(); break;
379
			//case '-': ball->farther(); break;
382
			//case '-': ball->farther(); break;
380
			//default:
383
			//default:
381
			//    ball->up_axis(key);
384
			//    ball->up_axis(key);
382
		}
385
		}
383
	}
386
	}
384
}
387
}
385
 
388
 
386
int main(int argc, char** argv)
389
int main(int argc, char** argv)
387
{
390
{
388
	// GLUT INIT
391
	// GLUT INIT
389
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
392
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
390
	glutInitWindowSize(win_size_x, win_size_y);
393
	glutInitWindowSize(win_size_x, win_size_y);
391
	glutInit(&argc, argv);
394
	glutInit(&argc, argv);
392
	main_window = glutCreateWindow("OBJ Viewer");
395
	main_window = glutCreateWindow("OBJ Viewer");
393
	glutDisplayFunc(display);
396
	glutDisplayFunc(display);
394
	glutKeyboardFunc(keyboard);
397
	glutKeyboardFunc(keyboard);
395
	glutMotionFunc(mouse_motion);
398
	glutMotionFunc(mouse_motion);
396
	glutMouseFunc(mouse);
399
	glutMouseFunc(mouse);
397
	//glutIdleFunc(idle);
400
	//glutIdleFunc(idle);
398
 
401
 
399
	// GL INIT
402
	// GL INIT
400
	glClearColor(.8f, 0.9f, 1.0f, 0.f);
403
	glClearColor(.8f, 0.9f, 1.0f, 0.f);
401
	glEnable(GL_DEPTH_TEST);
404
	glEnable(GL_DEPTH_TEST);
402
 
405
 
403
	// LOAD OBJ
406
	// LOAD OBJ
404
	string fn;
407
	string fn;
405
	if(argc>1)
408
	if(argc>1)
406
		fn = argv[1];
409
		fn = argv[1];
407
	else
410
	else
408
		fn = "bun.conf";
411
		fn = "/Users/jab/Studio/3DModels/OriginalBunnyScans/bunny/data/bun.conf";
409
 
412
 
410
	verts_from_conf(fn, 0.002);
413
	verts_from_conf(fn, 0.002);
411
 
414
 
412
	cout << "Loading " << fn << endl;
415
	cout << "Loading " << fn << endl;
413
	if(fn == "") exit(0);
416
	if(fn == "") exit(0);
414
 
417
 
415
	write_obj("bunny.obj");
418
	write_obj("/Users/jab/Studio/3DModels/OriginalBunnyScans/bunny/data/bunny.obj");
416
	//return 0;
419
	//return 0;
417
 
420
 
418
	// Initialize Trackball
421
	// Initialize Trackball
419
 
422
 
420
	ball = new QuatTrackBall(cen,2*rad,800,800);
423
	ball = new QuatTrackBall(cen,2*rad,800,800);
421
 
424
 
422
	// Setup projection
425
	// Setup projection
423
	glMatrixMode(GL_PROJECTION);
426
	glMatrixMode(GL_PROJECTION);
424
	glLoadIdentity();
427
	glLoadIdentity();
425
	gluPerspective(53,1.0f,rad/100.0,rad*3.0);
428
	gluPerspective(53,1.0f,rad/100.0,rad*3.0);
426
	glMatrixMode(GL_MODELVIEW);
429
	glMatrixMode(GL_MODELVIEW);
427
 
430
 
428
	// Pass control to GLUT
431
	// Pass control to GLUT
429
	glutMainLoop();
432
	glutMainLoop();
430
 
433
 
431
	return 0;
434
	return 0;
432
}
435
}
433
 
436