Subversion Repositories gelsvn

Rev

Rev 341 | Rev 363 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 341 Rev 357
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
// ----------------------------------------
10
// ----------------------------------------
11
 
11
 
12
#if (_MSC_VER >= 1200)
12
#if (_MSC_VER >= 1200)
13
#pragma warning (disable: 4786)
13
#pragma warning (disable: 4786)
14
#endif
14
#endif
15
 
15
 
16
#include <list>
16
#include <list>
17
#include <vector>
17
#include <vector>
18
 
18
 
19
#include <assert.h>
19
#include <assert.h>
20
#include <stdio.h>
20
#include <stdio.h>
21
#ifdef WIN32
21
#ifdef WIN32
22
#include <windows.h>
22
#include <windows.h>
23
#include <io.h>
23
#include <io.h>
24
#endif
24
#endif
25
#include <string.h>
25
#include <string.h>
26
#include <stdlib.h>
26
#include <stdlib.h>
27
 
27
 
28
#include <iostream>
28
#include <iostream>
29
 
29
 
30
#include <IL/il.h>
30
#include <IL/il.h>
31
#include <IL/ilu.h>
31
#include <IL/ilu.h>
32
 
32
 
33
#include <CGLA/Vec2i.h>
33
#include <CGLA/Vec2i.h>
34
#include <CGLA/Vec2f.h>
34
#include <CGLA/Vec2f.h>
35
#include <CGLA/Vec3f.h>
35
#include <CGLA/Vec3f.h>
36
#include <CGLA/Mat4x4f.h>
36
#include <CGLA/Mat4x4f.h>
-
 
37
#include "glsl_shader.h"
37
#include "GLGraphics/gel_glut.h"
38
#include "GLGraphics/gel_glut.h"
38
#include "GLGraphics/QuatTrackBall.h"
39
#include "GLGraphics/QuatTrackBall.h"
39
#include "GLGraphics/draw.h"
40
#include "GLGraphics/draw.h"
40
#include "Geometry/TriMesh.h"
41
#include "Geometry/TriMesh.h"
41
#include "Geometry/obj_load.h"
42
#include "Geometry/obj_load.h"
42
 
43
 
43
#include "ply_load.h"
44
#include "ply_load.h"
44
 
45
 
45
using namespace std;
46
using namespace std;
46
using namespace CGLA;
47
using namespace CGLA;
47
using namespace Geometry;
48
using namespace Geometry;
48
using namespace GLGraphics;
49
using namespace GLGraphics;
49
 
50
 
50
namespace
51
namespace
51
{
52
{
52
	int win_size_x = 800;
53
	int win_size_x = 800;
53
	int win_size_y = 800;
54
	int win_size_y = 800;
54
	QuatTrackBall* ball;
55
	QuatTrackBall* ball;
55
	int spin_timer = 20;
56
	int spin_timer = 20;
56
	void spin(int x);
57
	void spin(int x);
57
	int main_window;
58
	int main_window;
58
	TriMesh mesh;
59
	TriMesh mesh;
59
	
60
	
60
	
61
	
61
	bool load_image_into_texture(const std::string& name, GLuint& id)
62
	bool load_image_into_texture(const std::string& name, GLuint& id)
62
	{
63
	{
63
		unsigned char* image = 0;
64
		unsigned char* image = 0;
64
		unsigned int bpp = 0;
65
		unsigned int bpp = 0;
65
		unsigned int size_x = 0;
66
		unsigned int size_x = 0;
66
		unsigned int size_y = 0;
67
		unsigned int size_y = 0;
67
		unsigned int load_size_x = 0;
68
		unsigned int load_size_x = 0;
68
		unsigned int load_size_y = 0;
69
		unsigned int load_size_y = 0;
69
		
70
		
70
		
71
		
71
		ILenum Error;
72
		ILenum Error;
72
		ILuint  ImgId;
73
		ILuint  ImgId;
73
		
74
		
74
		static bool washere = false;
75
		static bool washere = false;
75
		if(!washere)
76
		if(!washere)
76
		{
77
		{
77
			ilInit();
78
			ilInit();
78
			iluInit();
79
			iluInit();
79
			washere=true;
80
			washere=true;
80
		}
81
		}
81
		ilEnable(IL_CONV_PAL);
82
		ilEnable(IL_CONV_PAL);
82
		ilEnable(IL_ORIGIN_SET);
83
		ilEnable(IL_ORIGIN_SET);
83
		ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
84
		ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
84
		ilGenImages(1, &ImgId);
85
		ilGenImages(1, &ImgId);
85
		ilBindImage(ImgId);
86
		ilBindImage(ImgId);
86
		char* name_cstr = const_cast<char*>(name.c_str());
87
		char* name_cstr = const_cast<char*>(name.c_str());
87
		if(!ilLoadImage(name_cstr))
88
		if(!ilLoadImage(name_cstr))
88
		{
89
		{
89
			cout << "could not load <" << name_cstr  << ">" << endl;
90
			cout << "could not load <" << name_cstr  << ">" << endl;
90
			return false;
91
			return false;
91
		}
92
		}
92
		
93
		
93
		load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
94
		load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
94
		load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
95
		load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
95
		bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
96
		bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
96
		
97
		
97
		if (bpp==24)
98
		if (bpp==24)
98
			ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
99
			ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
99
		else if (bpp==32)
100
		else if (bpp==32)
100
			ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
101
			ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
101
		else if (bpp==8)
102
		else if (bpp==8)
102
			ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
103
			ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
103
		else
104
		else
104
			assert(0);
105
			assert(0);
105
		
106
		
106
		unsigned int i;
107
		unsigned int i;
107
		for (i=2;i<=4096 ;i*=2)
108
		for (i=2;i<=4096 ;i*=2)
108
			if (i>=load_size_x)
109
			if (i>=load_size_x)
109
				break;
110
				break;
110
		size_x = i;
111
		size_x = i;
111
		for (i=2;i<=4096 ;i*=2)
112
		for (i=2;i<=4096 ;i*=2)
112
			if (i>=load_size_y)
113
			if (i>=load_size_y)
113
				break;
114
				break;
114
		size_y = i;
115
		size_y = i;
115
		
116
		
116
		if(size_x != load_size_x || size_y != load_size_y)
117
		if(size_x != load_size_x || size_y != load_size_y)
117
		{
118
		{
118
			iluImageParameter(ILU_FILTER, ILU_BILINEAR);
119
			iluImageParameter(ILU_FILTER, ILU_BILINEAR);
119
			iluScale(size_x, size_y, 1);
120
			iluScale(size_x, size_y, 1);
120
		}
121
		}
121
		
122
		
122
		const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
123
		const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
123
		image = new unsigned char[image_size];
124
		image = new unsigned char[image_size];
124
		memcpy(image, ilGetData(), image_size);
125
		memcpy(image, ilGetData(), image_size);
125
		ilDeleteImages(1, &ImgId);
126
		ilDeleteImages(1, &ImgId);
126
		
127
		
127
		bool any_errors=false;
128
		bool any_errors=false;
128
		while ((Error = ilGetError())) {
129
		while ((Error = ilGetError())) {
129
			cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
130
			cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
130
			any_errors = true;
131
			any_errors = true;
131
		}
132
		}
132
		if(any_errors) return false;
133
		if(any_errors) return false;
133
		
134
		
134
		GLint internalFormat=0;
135
		GLint internalFormat=0;
135
		GLenum format=0;
136
		GLenum format=0;
136
		
137
		
137
		glGenTextures(1, &id);
138
		glGenTextures(1, &id);
138
		switch (bpp)
139
		switch (bpp)
139
		{
140
		{
140
			case 8:
141
			case 8:
141
				internalFormat =  GL_LUMINANCE;
142
				internalFormat =  GL_LUMINANCE;
142
				format = GL_LUMINANCE;
143
				format = GL_LUMINANCE;
143
				break;
144
				break;
144
			case 24:
145
			case 24:
145
				internalFormat =  GL_RGB;
146
				internalFormat =  GL_RGB;
146
				format = GL_RGB;
147
				format = GL_RGB;
147
				break;
148
				break;
148
			case 32:
149
			case 32:
149
				internalFormat =  GL_RGBA;
150
				internalFormat =  GL_RGBA;
150
				format = GL_RGBA;
151
				format = GL_RGBA;
151
				break;
152
				break;
152
			default:
153
			default:
153
				return false;
154
				return false;
154
		}
155
		}
155
		
156
		
156
		glBindTexture(GL_TEXTURE_2D, id);
157
		glBindTexture(GL_TEXTURE_2D, id);
157
		glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0,
158
		glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0,
158
					 format, GL_UNSIGNED_BYTE, image);
159
					 format, GL_UNSIGNED_BYTE, image);
159
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
160
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
160
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
161
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
161
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
162
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
162
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163
		
164
		
164
		return true;
165
		return true;
165
	}
166
	}
166
	
167
	
167
	void enable_textures(TriMesh& tm)
168
	void enable_textures(TriMesh& tm)
168
	{
169
	{
169
		for(unsigned int i=0;i<tm.materials.size(); ++i)
170
		for(unsigned int i=0;i<tm.materials.size(); ++i)
170
		{
171
		{
171
			Material& mat = tm.materials[i];
172
			Material& mat = tm.materials[i];
172
			if(mat.tex_name != "")
173
			if(mat.tex_name != "")
173
			{
174
			{
174
				string name = mat.tex_path + mat.tex_name;
175
				string name = mat.tex_path + mat.tex_name;
175
				
176
				
176
				GLuint tex_id;
177
				GLuint tex_id;
177
				if(load_image_into_texture(name, tex_id))
178
				if(load_image_into_texture(name, tex_id))
178
					mat.tex_id = tex_id;
179
					mat.tex_id = tex_id;
179
			}
180
			}
180
		}
181
		}
181
	}
182
	}
182
	
183
	
-
 
184
	void toggle_wire()
-
 
185
	{
-
 
186
		static bool wire= false;
-
 
187
		static string shader_path = "/Users/jab/SrcTree/Appsrc/wireframepaper/";
-
 
188
		static GLhandleARB prog_P0;
-
 
189
		
-
 
190
		wire = !wire;
-
 
191
		static bool washere = false;
-
 
192
		if(!washere)
-
 
193
		{
-
 
194
			washere = true;
-
 
195
			
-
 
196
			// Create s	haders directly from file
-
 
197
			GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
-
 
198
			GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, shader_path, "tri.geom");
-
 
199
			GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
-
 
200
			
-
 
201
			// Create the program
-
 
202
			prog_P0 = glCreateProgram();
-
 
203
			
-
 
204
			// Attach all shaders
-
 
205
			if(vs) glAttachShader(prog_P0, vs);
-
 
206
			if(gs) glAttachShader(prog_P0, gs);
-
 
207
			if(fs) glAttachShader(prog_P0, fs);
-
 
208
			
-
 
209
			// Specify input and output for the geometry shader. Note that this must be
-
 
210
			// done before linking the program.
-
 
211
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
-
 
212
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
-
 
213
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
-
 
214
			
-
 
215
			// Link the program object and print out the info log
-
 
216
			glLinkProgram(prog_P0);
-
 
217
		}
-
 
218
		if(wire)
-
 
219
		{
-
 
220
			glUseProgram(prog_P0);
-
 
221
			// Set the value of a uniform
-
 
222
			glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
-
 
223
		}
-
 
224
		else
-
 
225
			glUseProgram(0);		
-
 
226
	}
-
 
227
	
183
	bool depth_pick(int x, int y,Vec3f& wp)
228
	bool depth_pick(int x, int y,Vec3f& wp)
184
	{
229
	{
185
		// Enquire about the viewport dimensions
230
		// Enquire about the viewport dimensions
186
		GLint viewport[4];
231
		GLint viewport[4];
187
		glGetIntegerv(GL_VIEWPORT, viewport);
232
		glGetIntegerv(GL_VIEWPORT, viewport);
188
		
233
		
189
		// Get the minimum and maximum depth values.
234
		// Get the minimum and maximum depth values.
190
		float minmax_depth[2];
235
		float minmax_depth[2];
191
		glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
236
		glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
192
		
237
		
193
		// Read a single pixel at the position of the mouse cursor.
238
		// Read a single pixel at the position of the mouse cursor.
194
		float depth;
239
		float depth;
195
		glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
240
		glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
196
					 GL_FLOAT, (void*) &depth);
241
					 GL_FLOAT, (void*) &depth);
197
		
242
		
198
		// If the depth corresponds to the far plane, we clicked on the
243
		// If the depth corresponds to the far plane, we clicked on the
199
		// background.
244
		// background.
200
		if(depth == minmax_depth[1])
245
		if(depth == minmax_depth[1])
201
			return false;
246
			return false;
202
		
247
		
203
		// The lines below copy the viewing transformation from OpenGL
248
		// The lines below copy the viewing transformation from OpenGL
204
		// to local variables. The call to gluLookAt must have exactly
249
		// to local variables. The call to gluLookAt must have exactly
205
		// the same parameters as when the scene is drawn.
250
		// the same parameters as when the scene is drawn.
206
		glLoadIdentity();
251
		glLoadIdentity();
207
		ball->set_gl_modelview();
252
		ball->set_gl_modelview();
208
		double mvmat[16];
253
		double mvmat[16];
209
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
254
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
210
		
255
		
211
		// Copy the projection matrix. We assume it is unchanged.
256
		// Copy the projection matrix. We assume it is unchanged.
212
		double prjmat[16];
257
		double prjmat[16];
213
		glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
258
		glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
214
		
259
		
215
		// Now unproject the point from screen to world coordinates.
260
		// Now unproject the point from screen to world coordinates.
216
		double ox, oy, oz;
261
		double ox, oy, oz;
217
		gluUnProject(x,viewport[3]-y,depth,
262
		gluUnProject(x,viewport[3]-y,depth,
218
					 mvmat,prjmat,viewport,
263
					 mvmat,prjmat,viewport,
219
					 &ox, &oy, &oz);
264
					 &ox, &oy, &oz);
220
		
265
		
221
		wp = Vec3f(ox,oy,oz);
266
		wp = Vec3f(ox,oy,oz);
222
		
267
		
223
		return true;
268
		return true;
224
	}
269
	}
225
	
270
	
226
 
271
 
227
 
272
 
228
void mouse_motion(int x, int y)
273
void mouse_motion(int x, int y)
229
{
274
{
230
    ball->roll_ball(Vec2i(x,y));
275
    ball->roll_ball(Vec2i(x,y));
231
}
276
}
232
 
277
 
233
void mouse(int btn, int state, int x, int y)
278
void mouse(int btn, int state, int x, int y)
234
{
279
{
235
	if(state == GLUT_DOWN) 
280
	if(state == GLUT_DOWN) 
236
	{
281
	{
237
		if(btn == GLUT_LEFT_BUTTON) 
282
		if(btn == GLUT_LEFT_BUTTON) 
238
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
283
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
239
		else if(btn == GLUT_MIDDLE_BUTTON) 
284
		else if(btn == GLUT_MIDDLE_BUTTON) 
240
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
285
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
241
		else if(btn == GLUT_RIGHT_BUTTON) 
286
		else if(btn == GLUT_RIGHT_BUTTON) 
242
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
287
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
243
	}
288
	}
244
	else if(state == GLUT_UP)
289
	else if(state == GLUT_UP)
245
		ball->release_ball();	
290
		ball->release_ball();	
246
}
291
}
247
 
292
 
248
void spin(int x)
293
void spin(int x)
249
{
294
{
250
	ball->do_spin();
295
	ball->do_spin();
251
	glutTimerFunc(spin_timer, spin, 0);  
296
	glutTimerFunc(spin_timer, spin, 0);  
252
	glutPostRedisplay();
297
	glutPostRedisplay();
253
}
298
}
254
 
299
 
255
void display()
300
void display()
256
{
301
{
257
    static bool washere = false;
302
    static bool washere = false;
258
    static unsigned int l;
303
    static unsigned int l;
259
    if(!washere)
304
    if(!washere)
260
    {
305
    {
261
        cout << "Creating display list" << endl;
306
        cout << "Creating display list" << endl;
262
        l = glGenLists(1);
307
        l = glGenLists(1);
263
        glNewList(l, GL_COMPILE);
308
        glNewList(l, GL_COMPILE);
264
        draw(mesh);
309
        draw(mesh);
265
        glEndList();
310
        glEndList();
266
        washere = true;
311
        washere = true;
267
		glutTimerFunc(spin_timer, spin, 0);	
312
		glutTimerFunc(spin_timer, spin, 0);	
268
	}
313
	}
269
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
314
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
270
    glLoadIdentity();
315
    glLoadIdentity();
271
    ball->set_gl_modelview();
316
    ball->set_gl_modelview();
272
    glCallList(l);
317
    glCallList(l);
273
    glutSwapBuffers();
318
    glutSwapBuffers();
274
}
319
}
275
 
320
 
276
void keyboard(unsigned char key, int x, int y)
321
void keyboard(unsigned char key, int x, int y)
277
{
322
{
278
    switch(key)
323
    switch(key)
279
    {
324
    {
280
		case '\033': exit(0); break;
325
		case '\033': exit(0); break;
281
			//case '+': ball->closer(); break;
-
 
282
			//case '-': ball->farther(); break;
326
		case 'w': toggle_wire(); break;
283
			//default:
-
 
284
			//    ball->up_axis(key);
-
 
285
    }
327
    }
286
}
328
}
287
}
329
}
288
 
330
 
289
int main(int argc, char** argv)
331
int main(int argc, char** argv)
290
{
332
{
291
    // GLUT INIT
333
    // GLUT INIT
292
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
334
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
293
    glutInitWindowSize(win_size_x, win_size_y);
335
    glutInitWindowSize(win_size_x, win_size_y);
294
    glutInit(&argc, argv);
336
    glutInit(&argc, argv);
295
    main_window = glutCreateWindow("OBJ Viewer");
337
    main_window = glutCreateWindow("OBJ Viewer");
296
    glutDisplayFunc(display);
338
    glutDisplayFunc(display);
297
    glutKeyboardFunc(keyboard);
339
    glutKeyboardFunc(keyboard);
298
    glutMotionFunc(mouse_motion);
340
    glutMotionFunc(mouse_motion);
299
    glutMouseFunc(mouse);
341
    glutMouseFunc(mouse);
300
    //glutIdleFunc(idle);
342
    //glutIdleFunc(idle);
301
	
343
	
-
 
344
	glewInit();
-
 
345
	
302
    // GL INIT
346
    // GL INIT
303
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
347
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
304
    glEnable(GL_DEPTH_TEST);
348
    glEnable(GL_DEPTH_TEST);
305
    glEnable(GL_LIGHTING);
349
    glEnable(GL_LIGHTING);
306
    glEnable(GL_LIGHT0);
350
    glEnable(GL_LIGHT0);
307
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
351
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
308
    glShadeModel(GL_SMOOTH);
352
    glShadeModel(GL_SMOOTH);
309
	
353
	
310
    // LOAD OBJ
354
    // LOAD OBJ
311
    string fn;
355
    string fn;
312
    if(argc>1)
356
    if(argc>1)
313
        fn = argv[1];
357
        fn = argv[1];
314
    else
358
    else
315
        fn = "../../data/head.obj";
359
        fn = "../../data/head.obj";
316
		
360
		
317
	if(fn.substr(fn.length()-4,fn.length())==".obj")
361
	if(fn.substr(fn.length()-4,fn.length())==".obj")
318
	{
362
	{
319
		obj_load(fn, mesh);
363
		obj_load(fn, mesh);
320
	}
364
	}
321
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
365
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
322
	{
366
	{
323
		ply_load(fn, mesh);
367
		ply_load(fn, mesh);
324
	}	
368
	}	
325
	else
369
	else
326
	{
370
	{
327
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
371
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
328
		exit(0);
372
		exit(0);
329
	}		
373
	}		
330
    enable_textures(mesh);	
374
    enable_textures(mesh);	
331
    if(!mesh.has_normals())
375
    if(!mesh.has_normals())
332
    {
376
    {
333
        cout << "Computing normals" << endl;
377
        cout << "Computing normals" << endl;
334
        mesh.compute_normals();
378
        mesh.compute_normals();
335
    }
379
    }
336
 
380
 
337
    // Initialize Trackball
381
    // Initialize Trackball
338
    Vec3f c;
382
    Vec3f c;
339
    float r;
383
    float r;
340
    mesh.get_bsphere(c,r);
384
    mesh.get_bsphere(c,r);
341
    r *= 1.5;
385
    r *= 1.5;
342
    ball = new QuatTrackBall(c,r,800,800);
386
    ball = new QuatTrackBall(c,r,800,800);
343
	
387
	
344
    // Setup projection
388
    // Setup projection
345
    glMatrixMode(GL_PROJECTION);
389
    glMatrixMode(GL_PROJECTION);
346
    glLoadIdentity();
390
    glLoadIdentity();
347
    gluPerspective(53,1.0f,r/100.0,r*3.0);
391
    gluPerspective(53,1.0f,r/100.0,r*3.0);
348
    glMatrixMode(GL_MODELVIEW);
392
    glMatrixMode(GL_MODELVIEW);
349
	
393
	
350
    // Pass control to GLUT
394
    // Pass control to GLUT
351
    glutMainLoop();
395
    glutMainLoop();
352
	
396
	
353
    return 0;
397
    return 0;
354
}
398
}
355
 
399