Subversion Repositories gelsvn

Rev

Rev 365 | Rev 370 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 365 Rev 368
Line 58... Line 58...
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;
Line 166... Line 168...
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");
Line 269... Line 295...
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)
-
 
277
{
-
 
278
    ball->roll_ball(Vec2i(x,y));
-
 
279
}
-
 
280
 
-
 
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)
320
  void spin(int x)
297
{
321
  {
298
	ball->do_spin();
322
	  ball->do_spin();
299
	glutTimerFunc(spin_timer, spin, 0);  
323
	  glutTimerFunc(spin_timer, spin, 0);  
300
	glutPostRedisplay();
324
	  glutPostRedisplay();
301
}
325
  }
302
 
326
 
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
Line 342... Line 374...
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);
Line 383... Line 415...
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