Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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