Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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