Subversion Repositories gelsvn

Rev

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

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