Subversion Repositories gelsvn

Rev

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