Subversion Repositories gelsvn

Rev

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

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