Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
107 bj 1
#include <fstream>
157 jab 2
#include "GLGraphics/gel_glut.h"
3
#include "GLGraphics/GLViewController.h"
125 jab 4
 
107 bj 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 Util;
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
	glutPostRedisplay();
188
}
189
 
190
void mouse(int button, int state, int x, int y) 
191
{
192
	Vec2i pos(x,y);
193
	if (state==GLUT_DOWN) 
194
		{
195
			if (button==GLUT_LEFT_BUTTON) 
196
				view_ctrl->grab_ball(ROTATE_ACTION,pos);
197
			else if (button==GLUT_MIDDLE_BUTTON) 
198
				view_ctrl->grab_ball(ZOOM_ACTION,pos);
199
			else if (button==GLUT_RIGHT_BUTTON) 
200
				view_ctrl->grab_ball(PAN_ACTION,pos);
201
		}
202
	else if (state==GLUT_UP)
203
		view_ctrl->release_ball();
204
}
205
 
206
void motion(int x, int y) {
207
	Vec2i pos(x,y);
208
	view_ctrl->roll_ball(Vec2i(x,y));
209
}
210
 
211
void keyboard(unsigned char key, int x, int y) {	
212
	switch(key) {
213
	case '\033': 
214
		my_mesh.relinquish_resource();
215
		ofstream ofs(".meshviewer.trackball",ofstream::binary);
216
		view_ctrl->save(ofs);
217
		ofs.close();
218
		exit(0); break;
219
	}
220
}
221
 
222
int main(int argc, char** argv)
223
{ 
224
	if(argv[1])
225
		my_mesh = load_mesh(argv[1]);
226
	else
227
		{
228
			my_mesh = create_box();
229
			cout << "Should print 4 below" << endl;
230
			AttrHandle<MyClass> h;
231
			my_mesh->get_vattr_handle("MyClass", h);
232
			my_mesh->vattr(h,4).print();
233
		}
234
 
235
 
236
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
237
	glutInitWindowSize(WINX, WINY);
238
	glutInit(&argc, argv);
239
	main_window = glutCreateWindow("X3D Viewer");
240
	glutDisplayFunc(display);
241
	glutKeyboardFunc(keyboard);
242
	glutReshapeFunc(reshape);
243
	glutMouseFunc(mouse);
244
	glutMotionFunc(motion);
157 jab 245
	glutIdleFunc(animate);
107 bj 246
 
247
	Vec3f c(0.152875, -4.92971, 0.04389);
248
	float r = 2.14036;
249
 
250
	my_mesh->get_bsphere(c,r);
251
	view_ctrl = new GLViewController(WINX,WINY, c,r);
252
 
253
 
254
	// --------------------------------------------------
255
	// GL Init
256
	glMatrixMode(GL_PROJECTION);
257
	glLoadIdentity();
258
	glMatrixMode(GL_MODELVIEW);
259
	glLoadIdentity();
260
	glClearColor(1.0f, 1.0f, 1.0f, 0.f);
261
	glEnable(GL_DEPTH_TEST);
262
	glEnable(GL_NORMALIZE);
263
	glEnable(GL_LIGHTING);
264
	glEnable(GL_LIGHT0);
265
 
266
	glutMainLoop();
267
	return 0;
268
}
269