Subversion Repositories gelsvn

Rev

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

Rev 107 Rev 125
1
#include <fstream>
1
#include <fstream>
2
#include "Graphics/GLViewController.h"
2
#include "Graphics/GLViewController.h"
-
 
3
 
-
 
4
#if defined(__APPLE__) && defined(__MACH__)
-
 
5
#include <GLUT/glut.h>
-
 
6
#else
3
#include <GL/glut.h>
7
#include <GL/glut.h>
-
 
8
#endif
-
 
9
 
4
#include <glui.h>
10
#include <glui.h>
5
#include <stack>
11
#include <stack>
6
#include <iostream>
12
#include <iostream>
7
#include "Util/Timer.h"
13
#include "Util/Timer.h"
8
#include "Util/ResourceManager.h"
14
#include "Util/ResourceManager.h"
9
 
15
 
10
#include "IMesh/TriMesh.h"
16
#include "IMesh/TriMesh.h"
11
#include "IMesh/TriMeshBuilder.h"
17
#include "IMesh/TriMeshBuilder.h"
12
#include "IMeshUtil/x3d_load.h"
18
#include "IMeshUtil/x3d_load.h"
13
 
19
 
14
using namespace std;
20
using namespace std;
15
using namespace IMesh;
21
using namespace IMesh;
16
using namespace IMeshUtil;
22
using namespace IMeshUtil;
17
using namespace Util;
23
using namespace Util;
18
using namespace CGLA;
24
using namespace CGLA;
19
using namespace GFX;
25
using namespace GFX;
20
 
26
 
21
namespace 
27
namespace 
22
{
28
{
23
	GLViewController* view_ctrl;
29
	GLViewController* view_ctrl;
24
	int WINX=800, WINY=800;
30
	int WINX=800, WINY=800;
25
	int main_window; 
31
	int main_window; 
26
	int light_follows_cam=true;
32
	int light_follows_cam=true;
27
 
33
 
28
	float lights_rot[16]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
34
	float lights_rot[16]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
29
	Vec4f light0_pos(0,0,1,0);
35
	Vec4f light0_pos(0,0,1,0);
30
	int smooth_iter = 8;
36
	int smooth_iter = 8;
31
 
37
 
32
	ResourcePtr<TriMesh> my_mesh;
38
	ResourcePtr<TriMesh> my_mesh;
33
 
39
 
34
	class MyClass
40
	class MyClass
35
	{
41
	{
36
		int x;
42
		int x;
37
	public:
43
	public:
38
		MyClass() {}
44
		MyClass() {}
39
		MyClass(int _x): x(_x) {}
45
		MyClass(int _x): x(_x) {}
40
		void print() const
46
		void print() const
41
		{
47
		{
42
			cout << x << endl;
48
			cout << x << endl;
43
		}
49
		}
44
	};
50
	};
45
}
51
}
46
 
52
 
47
void draw_imesh(const ResourcePtr<TriMesh>& imesh)
53
void draw_imesh(const ResourcePtr<TriMesh>& imesh)
48
{
54
{
49
	glEnable(GL_COLOR_MATERIAL);
55
	glEnable(GL_COLOR_MATERIAL);
50
	glColorMaterial(GL_FRONT, GL_DIFFUSE);
56
	glColorMaterial(GL_FRONT, GL_DIFFUSE);
51
	glBegin(GL_TRIANGLES);
57
	glBegin(GL_TRIANGLES);
52
	glColor3f(0.8,0.8,.2);
58
	glColor3f(0.8,0.8,.2);
53
 	for(int i=0;i<imesh->no_faces();++i)
59
 	for(int i=0;i<imesh->no_faces();++i)
54
		{
60
		{
55
			glNormal3fv(imesh->vnorm(i,0).get());
61
			glNormal3fv(imesh->vnorm(i,0).get());
56
			glVertex3fv(imesh->vpos(i,0).get());
62
			glVertex3fv(imesh->vpos(i,0).get());
57
 
63
 
58
			glNormal3fv(imesh->vnorm(i,1).get());
64
			glNormal3fv(imesh->vnorm(i,1).get());
59
			glVertex3fv(imesh->vpos(i,1).get());
65
			glVertex3fv(imesh->vpos(i,1).get());
60
 
66
 
61
			glNormal3fv(imesh->vnorm(i,2).get());
67
			glNormal3fv(imesh->vnorm(i,2).get());
62
			glVertex3fv(imesh->vpos(i,2).get());
68
			glVertex3fv(imesh->vpos(i,2).get());
63
	}
69
	}
64
	glEnd();
70
	glEnd();
65
}
71
}
66
 
72
 
67
 
73
 
68
ResourcePtr<TriMesh> create_box() 
74
ResourcePtr<TriMesh> create_box() 
69
{
75
{
70
	IMesh::TriMeshBuilder bldr;
76
	IMesh::TriMeshBuilder bldr;
71
 
77
 
72
	// Test that clearing is ok
78
	// Test that clearing is ok
73
	bldr.clear();
79
	bldr.clear();
74
	
80
	
75
	// Add all vertices
81
	// Add all vertices
76
	bldr.add_vpos(Vec3f(0, 0, 0));
82
	bldr.add_vpos(Vec3f(0, 0, 0));
77
	bldr.add_vpos(Vec3f(1, 0, 0));
83
	bldr.add_vpos(Vec3f(1, 0, 0));
78
	bldr.add_vpos(Vec3f(0, 1, 0));
84
	bldr.add_vpos(Vec3f(0, 1, 0));
79
	bldr.add_vpos(Vec3f(1, 1, 0));
85
	bldr.add_vpos(Vec3f(1, 1, 0));
80
	bldr.add_vpos(Vec3f(0, 0, 1));
86
	bldr.add_vpos(Vec3f(0, 0, 1));
81
	bldr.add_vpos(Vec3f(1, 0, 1));
87
	bldr.add_vpos(Vec3f(1, 0, 1));
82
	bldr.add_vpos(Vec3f(0, 1, 1));
88
	bldr.add_vpos(Vec3f(0, 1, 1));
83
	bldr.add_vpos(Vec3f(1, 1, 1));
89
	bldr.add_vpos(Vec3f(1, 1, 1));
84
 
90
 
85
	// Add all faces.
91
	// Add all faces.
86
	bldr.add_face(Vec3i(0, 2, 3)); 
92
	bldr.add_face(Vec3i(0, 2, 3)); 
87
	bldr.add_face(Vec3i(0, 3, 1));
93
	bldr.add_face(Vec3i(0, 3, 1));
88
	bldr.add_face(Vec3i(5, 7, 6)); 
94
	bldr.add_face(Vec3i(5, 7, 6)); 
89
	bldr.add_face(Vec3i(5, 6, 4));
95
	bldr.add_face(Vec3i(5, 6, 4));
90
	bldr.add_face(Vec3i(0, 1, 5)); 
96
	bldr.add_face(Vec3i(0, 1, 5)); 
91
	bldr.add_face(Vec3i(0, 5, 4));
97
	bldr.add_face(Vec3i(0, 5, 4));
92
	bldr.add_face(Vec3i(2, 6, 7)); 
98
	bldr.add_face(Vec3i(2, 6, 7)); 
93
	bldr.add_face(Vec3i(2, 7, 3));
99
	bldr.add_face(Vec3i(2, 7, 3));
94
	bldr.add_face(Vec3i(0, 4, 6)); 
100
	bldr.add_face(Vec3i(0, 4, 6)); 
95
	bldr.add_face(Vec3i(0, 6, 2));
101
	bldr.add_face(Vec3i(0, 6, 2));
96
	bldr.add_face(Vec3i(1, 3, 7)); 
102
	bldr.add_face(Vec3i(1, 3, 7)); 
97
	bldr.add_face(Vec3i(1, 7, 5));
103
	bldr.add_face(Vec3i(1, 7, 5));
98
 
104
 
99
	// Compute face normals
105
	// Compute face normals
100
	compute_face_normals(bldr);
106
	compute_face_normals(bldr);
101
 
107
 
102
	// Add smoothing group attributes to all faces.
108
	// Add smoothing group attributes to all faces.
103
	AttrHandle<int> attrib;
109
	AttrHandle<int> attrib;
104
	bldr.register_face_attribute("smoothing_group", attrib);
110
	bldr.register_face_attribute("smoothing_group", attrib);
105
	bldr.add_fattr(attrib, 1);
111
	bldr.add_fattr(attrib, 1);
106
	bldr.add_fattr(attrib, 1);
112
	bldr.add_fattr(attrib, 1);
107
	bldr.add_fattr(attrib, 2);
113
	bldr.add_fattr(attrib, 2);
108
	bldr.add_fattr(attrib, 2);
114
	bldr.add_fattr(attrib, 2);
109
	bldr.add_fattr(attrib, 1);
115
	bldr.add_fattr(attrib, 1);
110
	bldr.add_fattr(attrib, 1);
116
	bldr.add_fattr(attrib, 1);
111
	bldr.add_fattr(attrib, 8);
117
	bldr.add_fattr(attrib, 8);
112
	bldr.add_fattr(attrib, 8);
118
	bldr.add_fattr(attrib, 8);
113
	bldr.add_fattr(attrib, 1);
119
	bldr.add_fattr(attrib, 1);
114
	bldr.add_fattr(attrib, 1);
120
	bldr.add_fattr(attrib, 1);
115
	bldr.add_fattr(attrib, 32);
121
	bldr.add_fattr(attrib, 32);
116
	bldr.add_fattr(attrib, 32);
122
	bldr.add_fattr(attrib, 32);
117
	
123
	
118
	// Compute vertex normals using the smoothing groups
124
	// Compute vertex normals using the smoothing groups
119
	compute_vertex_normals(bldr, attrib);
125
	compute_vertex_normals(bldr, attrib);
120
 
126
 
121
	// Create a new vertex attribute registered to a new face set.
127
	// Create a new vertex attribute registered to a new face set.
122
	AttrHandle<MyClass> myhandle;
128
	AttrHandle<MyClass> myhandle;
123
	int mfs = bldr.register_face_set();
129
	int mfs = bldr.register_face_set();
124
	bldr.register_vertex_attribute("MyClass",myhandle, mfs);
130
	bldr.register_vertex_attribute("MyClass",myhandle, mfs);
125
	for(int i=0;i<8;++i)
131
	for(int i=0;i<8;++i)
126
		bldr.add_vattr(myhandle, MyClass(i));
132
		bldr.add_vattr(myhandle, MyClass(i));
127
 
133
 
128
 
134
 
129
	// Return a resource pointer to the new trimesh.
135
	// Return a resource pointer to the new trimesh.
130
	ResourcePtr<TriMesh> res_ptr = 
136
	ResourcePtr<TriMesh> res_ptr = 
131
		register_dynamic_resource("cube", bldr.get_trimesh());
137
		register_dynamic_resource("cube", bldr.get_trimesh());
132
	return res_ptr;
138
	return res_ptr;
133
}
139
}
134
 
140
 
135
ResourcePtr<TriMesh> load_mesh(const string& fname)
141
ResourcePtr<TriMesh> load_mesh(const string& fname)
136
{
142
{
137
	ResourcePtr<TriMesh> res_ptr;
143
	ResourcePtr<TriMesh> res_ptr;
138
	
144
	
139
	TriMeshBuilder bldr;
145
	TriMeshBuilder bldr;
140
	if(x3d_load(fname, bldr))
146
	if(x3d_load(fname, bldr))
141
		{
147
		{
142
			compute_face_normals(bldr);
148
			compute_face_normals(bldr);
143
			compute_vertex_normals(bldr, 0.95);
149
			compute_vertex_normals(bldr, 0.95);
144
			res_ptr = register_dynamic_resource(fname, bldr.get_trimesh());
150
			res_ptr = register_dynamic_resource(fname, bldr.get_trimesh());
145
		}
151
		}
146
	return res_ptr;
152
	return res_ptr;
147
} 
153
} 
148
 
154
 
149
 
155
 
150
 
156
 
151
void reshape(int W, int H)
157
void reshape(int W, int H)
152
{
158
{
153
	 view_ctrl->reshape(W,H);
159
	 view_ctrl->reshape(W,H);
154
}
160
}
155
 
161
 
156
 
162
 
157
void display() 
163
void display() 
158
{
164
{
159
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
165
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
160
 
166
 
161
	glMatrixMode(GL_PROJECTION);
167
	glMatrixMode(GL_PROJECTION);
162
	glLoadIdentity();
168
	glLoadIdentity();
163
	view_ctrl->reset_projection();
169
	view_ctrl->reset_projection();
164
	glMatrixMode(GL_MODELVIEW);
170
	glMatrixMode(GL_MODELVIEW);
165
	glLoadIdentity();
171
	glLoadIdentity();
166
 
172
 
167
	if(!light_follows_cam)
173
	if(!light_follows_cam)
168
		view_ctrl->set_gl_modelview();
174
		view_ctrl->set_gl_modelview();
169
	
175
	
170
	glMatrixMode( GL_MODELVIEW );
176
	glMatrixMode( GL_MODELVIEW );
171
	glPushMatrix();
177
	glPushMatrix();
172
	glMultMatrixf( lights_rot);
178
	glMultMatrixf( lights_rot);
173
	glLightfv(GL_LIGHT0, GL_POSITION, light0_pos.get());
179
	glLightfv(GL_LIGHT0, GL_POSITION, light0_pos.get());
174
	glPopMatrix();
180
	glPopMatrix();
175
 
181
 
176
	if(light_follows_cam)	
182
	if(light_follows_cam)	
177
		view_ctrl->set_gl_modelview();
183
		view_ctrl->set_gl_modelview();
178
	glEnable(GL_LIGHTING);
184
	glEnable(GL_LIGHTING);
179
	glEnable(GL_LIGHT0);
185
	glEnable(GL_LIGHT0);
180
	draw_imesh(my_mesh);
186
	draw_imesh(my_mesh);
181
 
187
 
182
	glutSwapBuffers();
188
	glutSwapBuffers();
183
}
189
}
184
 
190
 
185
void animate() 
191
void animate() 
186
{
192
{
187
	if ( glutGetWindow() != main_window ) 
193
	if ( glutGetWindow() != main_window ) 
188
		glutSetWindow(main_window);  
194
		glutSetWindow(main_window);  
189
}
195
}
190
 
196
 
191
void timer(int) 
197
void timer(int) 
192
{
198
{
193
	if ( glutGetWindow() != main_window ) 
199
	if ( glutGetWindow() != main_window ) 
194
		glutSetWindow(main_window);  
200
		glutSetWindow(main_window);  
195
	view_ctrl->try_spin();
201
	view_ctrl->try_spin();
196
	glutPostRedisplay();
202
	glutPostRedisplay();
197
	glutTimerFunc(20, timer, 0); 
203
	glutTimerFunc(20, timer, 0); 
198
}
204
}
199
 
205
 
200
 
206
 
201
void mouse(int button, int state, int x, int y) 
207
void mouse(int button, int state, int x, int y) 
202
{
208
{
203
	Vec2i pos(x,y);
209
	Vec2i pos(x,y);
204
	if (state==GLUT_DOWN) 
210
	if (state==GLUT_DOWN) 
205
		{
211
		{
206
			if (button==GLUT_LEFT_BUTTON) 
212
			if (button==GLUT_LEFT_BUTTON) 
207
				view_ctrl->grab_ball(ROTATE_ACTION,pos);
213
				view_ctrl->grab_ball(ROTATE_ACTION,pos);
208
			else if (button==GLUT_MIDDLE_BUTTON) 
214
			else if (button==GLUT_MIDDLE_BUTTON) 
209
				view_ctrl->grab_ball(ZOOM_ACTION,pos);
215
				view_ctrl->grab_ball(ZOOM_ACTION,pos);
210
			else if (button==GLUT_RIGHT_BUTTON) 
216
			else if (button==GLUT_RIGHT_BUTTON) 
211
				view_ctrl->grab_ball(PAN_ACTION,pos);
217
				view_ctrl->grab_ball(PAN_ACTION,pos);
212
		}
218
		}
213
	else if (state==GLUT_UP)
219
	else if (state==GLUT_UP)
214
		view_ctrl->release_ball();
220
		view_ctrl->release_ball();
215
}
221
}
216
 
222
 
217
void motion(int x, int y) {
223
void motion(int x, int y) {
218
	Vec2i pos(x,y);
224
	Vec2i pos(x,y);
219
	view_ctrl->roll_ball(Vec2i(x,y));
225
	view_ctrl->roll_ball(Vec2i(x,y));
220
}
226
}
221
 
227
 
222
void keyboard(unsigned char key, int x, int y) {	
228
void keyboard(unsigned char key, int x, int y) {	
223
	switch(key) {
229
	switch(key) {
224
	case '\033': 
230
	case '\033': 
225
		my_mesh.relinquish_resource();
231
		my_mesh.relinquish_resource();
226
		ofstream ofs(".meshviewer.trackball",ofstream::binary);
232
		ofstream ofs(".meshviewer.trackball",ofstream::binary);
227
		view_ctrl->save(ofs);
233
		view_ctrl->save(ofs);
228
		ofs.close();
234
		ofs.close();
229
		exit(0); break;
235
		exit(0); break;
230
	}
236
	}
231
}
237
}
232
 
238
 
233
int main(int argc, char** argv)
239
int main(int argc, char** argv)
234
{ 
240
{ 
235
	if(argv[1])
241
	if(argv[1])
236
		my_mesh = load_mesh(argv[1]);
242
		my_mesh = load_mesh(argv[1]);
237
	else
243
	else
238
		{
244
		{
239
			my_mesh = create_box();
245
			my_mesh = create_box();
240
			cout << "Should print 4 below" << endl;
246
			cout << "Should print 4 below" << endl;
241
			AttrHandle<MyClass> h;
247
			AttrHandle<MyClass> h;
242
			my_mesh->get_vattr_handle("MyClass", h);
248
			my_mesh->get_vattr_handle("MyClass", h);
243
			my_mesh->vattr(h,4).print();
249
			my_mesh->vattr(h,4).print();
244
		}
250
		}
245
	
251
	
246
 
252
 
247
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
253
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
248
	glutInitWindowSize(WINX, WINY);
254
	glutInitWindowSize(WINX, WINY);
249
	glutInit(&argc, argv);
255
	glutInit(&argc, argv);
250
	main_window = glutCreateWindow("X3D Viewer");
256
	main_window = glutCreateWindow("X3D Viewer");
251
	glutDisplayFunc(display);
257
	glutDisplayFunc(display);
252
	glutKeyboardFunc(keyboard);
258
	glutKeyboardFunc(keyboard);
253
	glutReshapeFunc(reshape);
259
	glutReshapeFunc(reshape);
254
	glutMouseFunc(mouse);
260
	glutMouseFunc(mouse);
255
	glutMotionFunc(motion);
261
	glutMotionFunc(motion);
256
 
262
 
257
	Vec3f c(0.152875, -4.92971, 0.04389);
263
	Vec3f c(0.152875, -4.92971, 0.04389);
258
	float r = 2.14036;
264
	float r = 2.14036;
259
	
265
	
260
	my_mesh->get_bsphere(c,r);
266
	my_mesh->get_bsphere(c,r);
261
	view_ctrl = new GLViewController(WINX,WINY, c,r);
267
	view_ctrl = new GLViewController(WINX,WINY, c,r);
262
 
268
 
263
 
269
 
264
	// --------------------------------------------------
270
	// --------------------------------------------------
265
	// GL Init
271
	// GL Init
266
	glMatrixMode(GL_PROJECTION);
272
	glMatrixMode(GL_PROJECTION);
267
	glLoadIdentity();
273
	glLoadIdentity();
268
	glMatrixMode(GL_MODELVIEW);
274
	glMatrixMode(GL_MODELVIEW);
269
	glLoadIdentity();
275
	glLoadIdentity();
270
	glClearColor(1.0f, 1.0f, 1.0f, 0.f);
276
	glClearColor(1.0f, 1.0f, 1.0f, 0.f);
271
	glEnable(GL_DEPTH_TEST);
277
	glEnable(GL_DEPTH_TEST);
272
	glEnable(GL_NORMALIZE);
278
	glEnable(GL_NORMALIZE);
273
	glEnable(GL_LIGHTING);
279
	glEnable(GL_LIGHTING);
274
	glEnable(GL_LIGHT0);
280
	glEnable(GL_LIGHT0);
275
 
281
 
276
	// --------------------------------------------------
282
	// --------------------------------------------------
277
	// creat glui interface.
283
	// creat glui interface.
278
 
284
 
279
	GLUI *glui = GLUI_Master.create_glui( "GLUI" );
285
	GLUI *glui = GLUI_Master.create_glui( "GLUI" );
280
	glui->add_checkbox( "Light follows cam ", &light_follows_cam );
286
	glui->add_checkbox( "Light follows cam ", &light_follows_cam );
281
  glui->add_rotation( "Light direction", lights_rot );
287
  glui->add_rotation( "Light direction", lights_rot );
282
	glui->set_main_gfx_window( main_window );
288
	glui->set_main_gfx_window( main_window );
283
	GLUI_Master.set_glutIdleFunc( animate ); 
289
	GLUI_Master.set_glutIdleFunc( animate ); 
284
	glutTimerFunc(20, timer, 0); 
290
	glutTimerFunc(20, timer, 0); 
285
 
291
 
286
 
292
 
287
	glutMainLoop();
293
	glutMainLoop();
288
	return 0;
294
	return 0;
289
}
295
}
290
 
296
 
291
 
297