Subversion Repositories gelsvn

Rev

Rev 382 | 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
 
102 bj 15
#if (_MSC_VER >= 1200)
82 jab 16
#pragma warning (disable: 4786)
102 bj 17
#endif
82 jab 18
 
19
#include <list>
20
#include <vector>
21
 
195 jrf 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 <iostream>
374 jrf 32
#include <vector>
337 jab 33
 
370 jab 34
#include <Util/ArgExtracter.h>
337 jab 35
#include <CGLA/Vec2i.h>
36
#include <CGLA/Vec2f.h>
82 jab 37
#include <CGLA/Vec3f.h>
38
#include <CGLA/Mat4x4f.h>
370 jab 39
#include "GLGraphics/glsl_shader.h"
178 bj 40
#include "GLGraphics/gel_glut.h"
299 jrf 41
#include "GLGraphics/QuatTrackBall.h"
178 bj 42
#include "GLGraphics/draw.h"
370 jab 43
#include "GLGraphics/SOIL.h"
82 jab 44
#include "Geometry/TriMesh.h"
45
#include "Geometry/obj_load.h"
370 jab 46
#include "Geometry/ply_load.h"
365 jab 47
#include "HMesh/x3d_load.h"
370 jab 48
#include "HMesh/FaceCirculator.h"
49
#include "wireframe.h"
337 jab 50
 
82 jab 51
using namespace std;
52
using namespace CGLA;
53
using namespace Geometry;
365 jab 54
using namespace HMesh;
178 bj 55
using namespace GLGraphics;
82 jab 56
 
370 jab 57
int win_size_x = 800;
58
int win_size_y = 800;
59
bool per_vertex_normals = 1;
60
bool redo_display_list = 1;
61
bool do_wireframe = false;
62
Vec3f line_col = Vec3f(1,0,0);
63
QuatTrackBall* ball;
64
int spin_timer = 20;
65
void spin(int x);
66
int main_window;
67
TriMesh mesh;
374 jrf 68
bool do_textures = true;
69
vector<bool> has_texture;
370 jab 70
 
71
void enable_textures(TriMesh& tm)
82 jab 72
{
374 jrf 73
	for(unsigned int i=0;i<has_texture.size(); ++i)
74
		tm.materials[i].has_texture = has_texture[i];
75
}
76
 
77
void disable_textures(TriMesh& tm)
78
{
79
	for(unsigned int i=0;i<has_texture.size(); ++i)
80
		tm.materials[i].has_texture = false;
81
}
82
 
83
void load_textures(TriMesh& tm)
84
{
85
  has_texture.resize(tm.materials.size());
370 jab 86
	for(unsigned int i=0;i<tm.materials.size(); ++i)
337 jab 87
	{
370 jab 88
		Material& mat = tm.materials[i];
89
		if(mat.tex_name != "")
337 jab 90
		{
370 jab 91
			string name = mat.tex_path + mat.tex_name;
92
			mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
374 jrf 93
      if(mat.tex_id)
94
        has_texture[i] = true;
337 jab 95
		}
370 jab 96
	}
97
}
98
 
99
void load_mesh(const string& fn)
100
{
101
	if(fn.substr(fn.length()-4,fn.length())==".obj")
102
	{
103
		obj_load(fn, mesh);
104
	}
105
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
106
	{
107
		ply_load(fn, mesh);
108
	}	
109
	else if(fn.substr(fn.length()-4,fn.length())==".x3d")
110
	{
111
		Manifold m;
112
		x3d_load(fn, m);
337 jab 113
 
370 jab 114
		for(VertexIter v=m.vertices_begin(); v != m.vertices_end();++v)
115
			v->touched = mesh.geometry.add_vertex(v->pos);
337 jab 116
 
370 jab 117
		for(FaceIter f = m.faces_begin(); f!= m.faces_end(); ++f)
337 jab 118
		{
370 jab 119
			Vec3i face;
120
			int i=0;
121
			for(FaceCirculator fc(f); !fc.end(); ++fc,++i)
122
			{
123
				if(i<2)
124
					face[i] = fc.get_vertex()->touched;
125
				else
126
				{
127
					face[2] = fc.get_vertex()->touched;
128
					mesh.geometry.add_face(face);
129
					face[1] = face[2];
130
				}
131
			}	
337 jab 132
		}
133
	}
370 jab 134
	else
337 jab 135
	{
370 jab 136
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
137
		exit(0);
337 jab 138
	}
374 jrf 139
  load_textures(mesh);	
140
  if(!do_textures)
141
    disable_textures(mesh);		
370 jab 142
}
368 jrf 143
 
370 jab 144
bool depth_pick(int x, int y,Vec3f& wp)
145
{
146
	// Enquire about the viewport dimensions
147
	GLint viewport[4];
148
	glGetIntegerv(GL_VIEWPORT, viewport);
149
 
150
	// Get the minimum and maximum depth values.
151
	float minmax_depth[2];
152
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
153
 
154
	// Read a single pixel at the position of the mouse cursor.
155
	float depth;
156
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
157
				 GL_FLOAT, (void*) &depth);
158
 
159
	// If the depth corresponds to the far plane, we clicked on the
160
	// background.
161
	if(depth == minmax_depth[1])
162
		return false;
163
 
164
	// The lines below copy the viewing transformation from OpenGL
165
	// to local variables. The call to gluLookAt must have exactly
166
	// the same parameters as when the scene is drawn.
167
	glLoadIdentity();
168
	ball->set_gl_modelview();
169
	double mvmat[16];
170
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
171
 
172
	// Copy the projection matrix. We assume it is unchanged.
173
	double prjmat[16];
174
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
175
 
176
	// Now unproject the point from screen to world coordinates.
177
	double ox, oy, oz;
178
	gluUnProject(x,viewport[3]-y,depth,
179
				 mvmat,prjmat,viewport,
180
				 &ox, &oy, &oz);
181
 
182
	wp = Vec3f(ox,oy,oz);
183
 
184
	return true;
185
}
186
 
187
 
188
 
189
void mouse_motion(int x, int y)
190
{
191
    ball->roll_ball(Vec2i(x,y));
192
}
193
 
194
void mouse(int btn, int state, int x, int y)
195
{
196
	if(state == GLUT_DOWN) 
368 jrf 197
	{
370 jab 198
		if(btn == GLUT_LEFT_BUTTON) 
199
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
200
		else if(btn == GLUT_MIDDLE_BUTTON) 
201
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
202
		else if(btn == GLUT_RIGHT_BUTTON) 
203
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
368 jrf 204
	}
370 jab 205
	else if(state == GLUT_UP)
206
		ball->release_ball();	
207
}
368 jrf 208
 
370 jab 209
void spin(int x)
210
{
211
	ball->do_spin();
212
	glutTimerFunc(spin_timer, spin, 0);  
213
	glutPostRedisplay();
214
}
215
 
216
void display()
217
{
218
	static unsigned int l;
219
    if(redo_display_list)
220
    {
221
        cout << "Creating display list" << endl;
222
        l = glGenLists(1);
223
        glNewList(l, GL_COMPILE);
224
        draw(mesh, per_vertex_normals);
225
        glEndList();
226
        redo_display_list = false;
227
		glutTimerFunc(spin_timer, spin, 0);	
368 jrf 228
	}
370 jab 229
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
230
    glLoadIdentity();
231
    ball->set_gl_modelview();
337 jab 232
 
370 jab 233
	if(do_wireframe)
357 jab 234
	{
370 jab 235
		if(GLEW_EXT_geometry_shader4)
357 jab 236
		{
370 jab 237
			enable_wireframe();
238
			glCallList(l);
239
			glUseProgram(0);
357 jab 240
		}
370 jab 241
		else
357 jab 242
		{
370 jab 243
			glDisable(GL_LIGHTING);
244
			glColor3f(1,1,1);
245
			glCallList(l);
246
			glEnable(GL_POLYGON_OFFSET_LINE);
247
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
248
			glColor3fv(line_col.get());
249
			glPolygonOffset(0,-5);
250
			glCallList(l);
251
			glEnable(GL_LIGHTING);
252
			glDisable(GL_POLYGON_OFFSET_LINE);
253
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
357 jab 254
		}
255
	}
370 jab 256
	else
257
		glCallList(l);
337 jab 258
 
370 jab 259
    glutSwapBuffers();
260
}
82 jab 261
 
370 jab 262
void keyboard(unsigned char key, int x, int y)
263
{
264
    switch(key)
265
    {
266
		case '\033': exit(0); break;
267
		case 'w': do_wireframe = !do_wireframe; break;
268
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
374 jrf 269
    case 't': 
270
        do_textures = !do_textures; 
271
        if(do_textures) 
272
          enable_textures(mesh); 
273
        else 
274
          disable_textures(mesh); 
275
        redo_display_list = true; 
276
        break;
370 jab 277
    }
278
}
82 jab 279
 
280
int main(int argc, char** argv)
178 bj 281
{
370 jab 282
	Util::ArgExtracter ae(argc, argv);
283
 
284
	bool redo_normals = ae.extract("-n");
285
 
178 bj 286
    // GLUT INIT
287
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
288
    glutInitWindowSize(win_size_x, win_size_y);
289
    glutInit(&argc, argv);
290
    main_window = glutCreateWindow("OBJ Viewer");
291
    glutDisplayFunc(display);
292
    glutKeyboardFunc(keyboard);
293
    glutMotionFunc(mouse_motion);
294
    glutMouseFunc(mouse);
299 jrf 295
    //glutIdleFunc(idle);
337 jab 296
 
370 jab 297
	glewInit();
357 jab 298
 
178 bj 299
    // GL INIT
300
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
301
    glEnable(GL_DEPTH_TEST);
302
    glEnable(GL_LIGHTING);
303
    glEnable(GL_LIGHT0);
304
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
305
    glShadeModel(GL_SMOOTH);
337 jab 306
 
178 bj 307
    // LOAD OBJ
308
    string fn;
370 jab 309
    if(ae.no_remaining_args())
310
        fn = ae.get_last_arg();
178 bj 311
    else
312
        fn = "../../data/head.obj";
370 jab 313
 
314
	load_mesh(fn);	
315
 
316
	if(!mesh.has_normals() || redo_normals)
337 jab 317
	{
370 jab 318
		cout << "Computing normals" << endl;
319
		mesh.compute_normals();
337 jab 320
	}
382 jab 321
	cout<< __FILE__ << __LINE__ << " "  << mesh.geometry.no_faces() << " " << mesh.geometry.no_vertices() << endl;
337 jab 322
 
370 jab 323
	// Initialize Trackball
324
	Vec3f c;
325
	float r;
326
	mesh.get_bsphere(c,r);
327
	r *= 1.5;
328
	ball = new QuatTrackBall(c,r,800,800);
337 jab 329
 
370 jab 330
	// Setup projection
331
	glMatrixMode(GL_PROJECTION);
332
	glLoadIdentity();
333
	gluPerspective(53,1.0f,r/100.0,r*3.0);
334
	glMatrixMode(GL_MODELVIEW);
337 jab 335
 
370 jab 336
	if(GLEW_EXT_geometry_shader4)
337
		initialize_wireframe_shaders();
338
 
339
 
340
	// Pass control to GLUT
341
	glutMainLoop();
342
 
343
	return 0;
82 jab 344
}