Subversion Repositories gelsvn

Rev

Rev 596 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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