Subversion Repositories gelsvn

Rev

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

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