Subversion Repositories gelsvn

Rev

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

Rev 630 Rev 653
1
// ----------------------------------------
1
// ----------------------------------------
2
// A simple OBJ viewer.
2
// A simple OBJ viewer.
3
//
3
//
4
// Controls:
4
// Controls:
5
// - left mouse down + mouse motion : rotate
5
// - left mouse down + mouse motion : rotate
6
// - Scroll button and +- buttons   : zoom
6
// - Scroll button and +- buttons   : zoom
7
// - right mouse click              : centre trackball
7
// - right mouse click              : centre trackball
8
// - esc                            : exits
8
// - esc                            : exits
9
// - x,y,z buttons                  : switch trackball up axis
9
// - x,y,z buttons                  : switch trackball up axis
10
// - w                              : toggle wireframe on/off
10
// - w                              : toggle wireframe on/off
11
// - t                              : toggle texture on/off
11
// - t                              : toggle texture on/off
12
// - f                              : switch between vertex and face normals
12
// - f                              : switch between vertex and face normals
13
// ----------------------------------------
13
// ----------------------------------------
14
 
14
 
15
#if (_MSC_VER >= 1200)
15
#if (_MSC_VER >= 1200)
16
#pragma warning (disable: 4786)
16
#pragma warning (disable: 4786)
17
#endif
17
#endif
18
 
18
 
19
#include <list>
19
#include <list>
20
#include <vector>
20
#include <vector>
21
 
21
 
22
#include <assert.h>
22
#include <assert.h>
23
#include <stdio.h>
23
#include <stdio.h>
24
#ifdef WIN32
24
#ifdef WIN32
25
#include <windows.h>
25
#include <windows.h>
26
#include <io.h>
26
#include <io.h>
27
#endif
27
#endif
28
#include <string.h>
28
#include <string.h>
29
#include <stdlib.h>
29
#include <stdlib.h>
30
 
30
 
31
#include <fstream>
31
#include <fstream>
32
#include <iostream>
32
#include <iostream>
33
#include <vector>
33
#include <vector>
34
 
34
 
35
#include <Util/ArgExtracter.h>
35
#include <Util/ArgExtracter.h>
36
#include <CGLA/Vec2i.h>
36
#include <CGLA/Vec2i.h>
37
#include <CGLA/Vec2f.h>
37
#include <CGLA/Vec2f.h>
38
#include <CGLA/Vec3f.h>
38
#include <CGLA/Vec3f.h>
39
#include <CGLA/Mat4x4f.h>
39
#include <CGLA/Mat4x4f.h>
40
#include "GLGraphics/glsl_shader.h"
40
#include "GLGraphics/glsl_shader.h"
41
#include "GLGraphics/gel_glut.h"
-
 
42
#include "GLGraphics/QuatTrackBall.h"
41
#include "GLGraphics/QuatTrackBall.h"
43
#include "GLGraphics/draw.h"
42
#include "GLGraphics/draw.h"
44
#include "GLGraphics/SOIL.h"
43
#include "GLGraphics/SOIL.h"
45
#include "Geometry/TriMesh.h"
44
#include "Geometry/TriMesh.h"
46
#include "Geometry/load.h"
45
#include "Geometry/load.h"
47
#include "Geometry/GridAlgorithm.h"
46
#include "Geometry/GridAlgorithm.h"
48
#include "Geometry/HGrid.h"
47
#include "Geometry/HGrid.h"
-
 
48
 
-
 
49
#ifdef __APPLE__
49
//#include "HMesh/x3d_load.h"
50
#include <GLUT/GLUT.h>
-
 
51
#else
50
//#include "HMesh/FaceCirculator.h"
52
#include <GL/glut.h>
-
 
53
#endif
51
 
54
 
52
using namespace std;
55
using namespace std;
53
using namespace CGLA;
56
using namespace CGLA;
54
using namespace Geometry;
57
using namespace Geometry;
55
using namespace HMesh;
58
using namespace HMesh;
56
using namespace GLGraphics;
59
using namespace GLGraphics;
57
 
60
 
58
int win_size_x = 800;
61
int win_size_x = 800;
59
int win_size_y = 800;
62
int win_size_y = 800;
60
bool per_vertex_normals = 1;
63
bool per_vertex_normals = 1;
61
bool redo_display_list = 1;
64
bool redo_display_list = 1;
62
bool do_wireframe = false;
65
bool do_wireframe = false;
63
Vec3f line_col = Vec3f(1,0,0);
66
Vec3f line_col = Vec3f(1,0,0);
64
QuatTrackBall* ball;
67
QuatTrackBall* ball;
65
int spin_timer = 20;
68
int spin_timer = 20;
66
void spin(int x);
69
void spin(int x);
67
int main_window;
70
int main_window;
68
TriMesh mesh;
71
TriMesh mesh;
69
bool do_textures = true;
72
bool do_textures = true;
70
 
73
 
71
 
74
 
72
bool depth_pick(int x, int y,Vec3f& wp)
75
bool depth_pick(int x, int y,Vec3f& wp)
73
{
76
{
74
	// Enquire about the viewport dimensions
77
	// Enquire about the viewport dimensions
75
	GLint viewport[4];
78
	GLint viewport[4];
76
	glGetIntegerv(GL_VIEWPORT, viewport);
79
	glGetIntegerv(GL_VIEWPORT, viewport);
77
	
80
	
78
	// Get the minimum and maximum depth values.
81
	// Get the minimum and maximum depth values.
79
	float minmax_depth[2];
82
	float minmax_depth[2];
80
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
83
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
81
	
84
	
82
	// Read a single pixel at the position of the mouse cursor.
85
	// Read a single pixel at the position of the mouse cursor.
83
	float depth;
86
	float depth;
84
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
87
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
85
				 GL_FLOAT, (void*) &depth);
88
				 GL_FLOAT, (void*) &depth);
86
	
89
	
87
	// If the depth corresponds to the far plane, we clicked on the
90
	// If the depth corresponds to the far plane, we clicked on the
88
	// background.
91
	// background.
89
	if(depth == minmax_depth[1])
92
	if(depth == minmax_depth[1])
90
		return false;
93
		return false;
91
	
94
	
92
	// The lines below copy the viewing transformation from OpenGL
95
	// The lines below copy the viewing transformation from OpenGL
93
	// to local variables. The call to gluLookAt must have exactly
96
	// to local variables. The call to gluLookAt must have exactly
94
	// the same parameters as when the scene is drawn.
97
	// the same parameters as when the scene is drawn.
95
	glLoadIdentity();
98
	glLoadIdentity();
96
	ball->set_gl_modelview();
99
	ball->set_gl_modelview();
97
	double mvmat[16];
100
	double mvmat[16];
98
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
101
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
99
	
102
	
100
	// Copy the projection matrix. We assume it is unchanged.
103
	// Copy the projection matrix. We assume it is unchanged.
101
	double prjmat[16];
104
	double prjmat[16];
102
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
105
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
103
	
106
	
104
	// Now unproject the point from screen to world coordinates.
107
	// Now unproject the point from screen to world coordinates.
105
	double ox, oy, oz;
108
	double ox, oy, oz;
106
	gluUnProject(x,viewport[3]-y,depth,
109
	gluUnProject(x,viewport[3]-y,depth,
107
				 mvmat,prjmat,viewport,
110
				 mvmat,prjmat,viewport,
108
				 &ox, &oy, &oz);
111
				 &ox, &oy, &oz);
109
	
112
	
110
	wp = Vec3f(ox,oy,oz);
113
	wp = Vec3f(ox,oy,oz);
111
	
114
	
112
	return true;
115
	return true;
113
}
116
}
114
 
117
 
115
 
118
 
116
void mouse_motion(int x, int y)
119
void mouse_motion(int x, int y)
117
{
120
{
118
    ball->roll_ball(Vec2i(x,y));
121
    ball->roll_ball(Vec2i(x,y));
119
}
122
}
120
 
123
 
121
void mouse(int btn, int state, int x, int y)
124
void mouse(int btn, int state, int x, int y)
122
{
125
{
123
	if(state == GLUT_DOWN) 
126
	if(state == GLUT_DOWN) 
124
	{
127
	{
125
		if(btn == GLUT_LEFT_BUTTON) 
128
		if(btn == GLUT_LEFT_BUTTON) 
126
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
129
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
127
		else if(btn == GLUT_MIDDLE_BUTTON) 
130
		else if(btn == GLUT_MIDDLE_BUTTON) 
128
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
131
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
129
		else if(btn == GLUT_RIGHT_BUTTON) 
132
		else if(btn == GLUT_RIGHT_BUTTON) 
130
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
133
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
131
	}
134
	}
132
	else if(state == GLUT_UP)
135
	else if(state == GLUT_UP)
133
		ball->release_ball();	
136
		ball->release_ball();	
134
}
137
}
135
 
138
 
136
void spin(int x)
139
void spin(int x)
137
{
140
{
138
	ball->do_spin();
141
	ball->do_spin();
139
	glutTimerFunc(spin_timer, spin, 0);  
142
	glutTimerFunc(spin_timer, spin, 0);  
140
	glutPostRedisplay();
143
	glutPostRedisplay();
141
}
144
}
142
 
145
 
143
void setupshader()
146
void setupshader()
144
{
147
{
145
	static GLuint vs,fs,prog;
148
	static GLuint vs,fs,prog;
146
	static bool was_here = false;
149
	static bool was_here = false;
147
	if(!was_here)
150
	if(!was_here)
148
	{
151
	{
149
		was_here = true;
152
		was_here = true;
150
		const string vss = 
153
		const string vss = 
151
		"varying vec3 n;\n"
154
		"varying vec3 n;\n"
152
		"varying vec3 v;\n"
155
		"varying vec3 v;\n"
153
		"varying vec3 v_obj;\n"
156
		"varying vec3 v_obj;\n"
154
		"\n"
157
		"\n"
155
		"void main(void)\n"
158
		"void main(void)\n"
156
		"{\n"
159
		"{\n"
157
		"	gl_Position = ftransform();\n"
160
		"	gl_Position = ftransform();\n"
158
		"   v_obj = gl_Vertex.xyz;\n"
161
		"   v_obj = gl_Vertex.xyz;\n"
159
		"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
162
		"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
160
		"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
163
		"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
161
		"}\n"
164
		"}\n"
162
		"\n";
165
		"\n";
163
		
166
		
164
		const string fss =
167
		const string fss =
165
		"varying vec3 n;\n"
168
		"varying vec3 n;\n"
166
		"varying vec3 v;\n"
169
		"varying vec3 v;\n"
167
		"varying vec3 v_obj;\n"
170
		"varying vec3 v_obj;\n"
168
		"\n"
171
		"\n"
169
		"vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
172
		"vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
170
		"{\n"
173
		"{\n"
171
		"	vec3 e = normalize(-v);\n"
174
		"	vec3 e = normalize(-v);\n"
172
		"	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
175
		"	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
173
		"	float d = max(0.05,dot(light_dir, n));\n"
176
		"	float d = max(0.05,dot(light_dir, n));\n"
174
		"	vec4 diff = mat_col * light_col *d; 	\n"
177
		"	vec4 diff = mat_col * light_col *d; 	\n"
175
		"	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
178
		"	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
176
		"	return 0.1*refl + 2.25*diff;\n"
179
		"	return 0.1*refl + 2.25*diff;\n"
177
		"}\n"
180
		"}\n"
178
		"\n"
181
		"\n"
179
		"void main(void)\n"
182
		"void main(void)\n"
180
		"{\n"
183
		"{\n"
181
		"	vec4 mat_col = vec4(0.7,0.6,1.0,1.0);\n"
184
		"	vec4 mat_col = vec4(0.7,0.6,1.0,1.0);\n"
182
		"	\n"
185
		"	\n"
183
		"	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
186
		"	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
184
		"	vec4 light0_col = vec4(0.9,0.95,0.95,1.0);\n"
187
		"	vec4 light0_col = vec4(0.9,0.95,0.95,1.0);\n"
185
		"	\n"
188
		"	\n"
186
		"	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
189
		"	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
187
		"	vec4 light1_col = vec4(.8,.8,.6,1.0);\n"
190
		"	vec4 light1_col = vec4(.8,.8,.6,1.0);\n"
188
		"	\n"
191
		"	\n"
189
		"	gl_FragColor = \n"
192
		"	gl_FragColor = \n"
190
		"	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
193
		"	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
191
		"	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
194
		"	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
192
		"	\n"
195
		"	\n"
193
		"	gl_FragColor.a = 1.0;\n"
196
		"	gl_FragColor.a = 1.0;\n"
194
		"}\n";
197
		"}\n";
195
		
198
		
196
		vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
199
		vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
197
		print_glsl_program_log(vs);
200
		print_glsl_program_log(vs);
198
		
201
		
199
		fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
202
		fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
200
		print_glsl_program_log(fs);
203
		print_glsl_program_log(fs);
201
		
204
		
202
		prog = glCreateProgram();
205
		prog = glCreateProgram();
203
		
206
		
204
		if(vs) glAttachShader(prog, vs);
207
		if(vs) glAttachShader(prog, vs);
205
		if(fs) glAttachShader(prog, fs);
208
		if(fs) glAttachShader(prog, fs);
206
		
209
		
207
		glLinkProgram(prog);
210
		glLinkProgram(prog);
208
		print_glsl_program_log(prog);
211
		print_glsl_program_log(prog);
209
	}
212
	}
210
	glUseProgram(prog);
213
	glUseProgram(prog);
211
	
214
	
212
}
215
}
213
 
216
 
214
void display()
217
void display()
215
{
218
{
216
	static unsigned int l;
219
	static unsigned int l;
217
    if(redo_display_list)
220
    if(redo_display_list)
218
    {
221
    {
219
        cout << "Creating display list" << endl;
222
        cout << "Creating display list" << endl;
220
        l = glGenLists(1);
223
        l = glGenLists(1);
221
        glNewList(l, GL_COMPILE);
224
        glNewList(l, GL_COMPILE);
222
        draw(mesh, per_vertex_normals);
225
        draw(mesh, per_vertex_normals);
223
        glEndList();
226
        glEndList();
224
        redo_display_list = false;
227
        redo_display_list = false;
225
		glutTimerFunc(spin_timer, spin, 0);	
228
		glutTimerFunc(spin_timer, spin, 0);	
226
	}
229
	}
227
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
230
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228
    glLoadIdentity();
231
    glLoadIdentity();
229
    ball->set_gl_modelview();
232
    ball->set_gl_modelview();
230
	if(do_wireframe)
233
	if(do_wireframe)
231
	{
234
	{
232
		if(GLEW_EXT_geometry_shader4)
235
		if(GLEW_EXT_geometry_shader4)
233
			draw_triangles_in_wireframe(mesh, per_vertex_normals, Vec3f(1,0,0));
236
			draw_triangles_in_wireframe(mesh, per_vertex_normals, Vec3f(1,0,0));
234
		else
237
		else
235
			draw_wireframe_oldfashioned(mesh, per_vertex_normals, Vec3f(1,0,0));
238
			draw_wireframe_oldfashioned(mesh, per_vertex_normals, Vec3f(1,0,0));
236
	}
239
	}
237
	else if(!do_textures)
240
	else if(!do_textures)
238
	{
241
	{
239
		setupshader();	
242
		setupshader();	
240
		glCallList(l);
243
		glCallList(l);
241
	}
244
	}
242
	else
245
	else
243
		glCallList(l);
246
		glCallList(l);
244
	
247
	
245
    glutSwapBuffers();
248
    glutSwapBuffers();
246
}
249
}
247
 
250
 
248
void keyboard(unsigned char key, int x, int y)
251
void keyboard(unsigned char key, int x, int y)
249
{
252
{
250
    switch(key)
253
    switch(key)
251
    {
254
    {
252
		case '\033': exit(0); break;
255
		case '\033': exit(0); break;
253
		case 'w': do_wireframe = !do_wireframe; break;
256
		case 'w': do_wireframe = !do_wireframe; break;
254
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
257
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
255
		case 's': 
258
		case 's': 
256
		{
259
		{
257
			ofstream f("ball.out", ios::binary);
260
			ofstream f("ball.out", ios::binary);
258
			if(f) f.write(reinterpret_cast<const char*>(ball),sizeof(QuatTrackBall));
261
			if(f) f.write(reinterpret_cast<const char*>(ball),sizeof(QuatTrackBall));
259
		}
262
		}
260
		break;
263
		break;
261
		case 'l':
264
		case 'l':
262
		{
265
		{
263
			ifstream f("ball.out", ios::binary);
266
			ifstream f("ball.out", ios::binary);
264
			if(f) f.read(reinterpret_cast<char*>(ball),sizeof(QuatTrackBall));
267
			if(f) f.read(reinterpret_cast<char*>(ball),sizeof(QuatTrackBall));
265
		}
268
		}
266
			break;
269
			break;
267
		case 't': do_textures = !do_textures;
270
		case 't': do_textures = !do_textures;
268
			break;
271
			break;
269
    }
272
    }
270
	redo_display_list=true;
273
	redo_display_list=true;
271
}
274
}
272
 
275
 
273
int main(int argc, char** argv)
276
int main(int argc, char** argv)
274
{
277
{
275
	Util::ArgExtracter ae(argc, argv);
278
	Util::ArgExtracter ae(argc, argv);
276
	
279
	
277
	bool redo_normals = ae.extract("-n");
280
	bool redo_normals = ae.extract("-n");
278
	
281
	
279
    // GLUT INIT
282
    // GLUT INIT
280
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
283
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
281
    glutInitWindowSize(win_size_x, win_size_y);
284
    glutInitWindowSize(win_size_x, win_size_y);
282
    glutInit(&argc, argv);
285
    glutInit(&argc, argv);
283
    main_window = glutCreateWindow("OBJ Viewer");
286
    main_window = glutCreateWindow("OBJ Viewer");
284
    glutDisplayFunc(display);
287
    glutDisplayFunc(display);
285
    glutKeyboardFunc(keyboard);
288
    glutKeyboardFunc(keyboard);
286
    glutMotionFunc(mouse_motion);
289
    glutMotionFunc(mouse_motion);
287
    glutMouseFunc(mouse);
290
    glutMouseFunc(mouse);
288
    //glutIdleFunc(idle);
291
    //glutIdleFunc(idle);
289
	
292
	
290
	glewInit();
293
	glewInit();
291
	
294
	
292
    // GL INIT
295
    // GL INIT
293
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
296
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
294
    glEnable(GL_DEPTH_TEST);
297
    glEnable(GL_DEPTH_TEST);
295
    glEnable(GL_LIGHTING);
298
    glEnable(GL_LIGHTING);
296
    glEnable(GL_LIGHT0);
299
    glEnable(GL_LIGHT0);
297
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
300
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
298
    glShadeModel(GL_SMOOTH);
301
    glShadeModel(GL_SMOOTH);
299
	
302
	
300
    // LOAD OBJ
303
    // LOAD OBJ
301
    string fn;
304
    string fn;
302
    int arg_no = ae.no_remaining_args();
305
    int arg_no = ae.no_remaining_args();
303
    if(arg_no>1)
306
    if(arg_no>1)
304
        fn = ae.get_last_arg();
307
        fn = ae.get_last_arg();
305
    else
308
    else
306
        fn = "../../data/head.obj";
309
        fn = "../../data/head.obj";
307
	
310
	
308
	load(fn, mesh);
311
	load(fn, mesh);
309
	load_textures(mesh);
312
	load_textures(mesh);
310
	
313
	
311
	if(!mesh.has_normals() || redo_normals)
314
	if(!mesh.has_normals() || redo_normals)
312
	{
315
	{
313
		cout << "Computing normals" << endl;
316
		cout << "Computing normals" << endl;
314
		mesh.compute_normals();
317
		mesh.compute_normals();
315
	}
318
	}
316
	
319
	
317
	// Initialize Trackball
320
	// Initialize Trackball
318
	Vec3f c;
321
	Vec3f c;
319
	float r;
322
	float r;
320
	mesh.get_bsphere(c,r);
323
	mesh.get_bsphere(c,r);
321
	r *= 1.5;
324
	r *= 1.5;
322
	ball = new QuatTrackBall(c,r,800,800);
325
	ball = new QuatTrackBall(c,r,800,800);
323
	
326
	
324
	// Setup projection
327
	// Setup projection
325
	glMatrixMode(GL_PROJECTION);
328
	glMatrixMode(GL_PROJECTION);
326
	glLoadIdentity();
329
	glLoadIdentity();
327
	gluPerspective(53,1.0f,r/100.0,r*3.0);
330
	gluPerspective(53,1.0f,r/100.0,r*3.0);
328
	glMatrixMode(GL_MODELVIEW);
331
	glMatrixMode(GL_MODELVIEW);
329
	
332
	
330
	// Pass control to GLUT
333
	// Pass control to GLUT
331
	glutMainLoop();
334
	glutMainLoop();
332
	
335
	
333
	return 0;
336
	return 0;
334
}
337
}
335
 
338