Subversion Repositories gelsvn

Rev

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