Subversion Repositories gelsvn

Rev

Rev 107 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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