Subversion Repositories gelsvn

Rev

Rev 368 | Rev 374 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 368 Rev 370
Line 25... Line 25...
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>
-
 
31
#include <IL/ilu.h>
30
#include <Util/ArgExtracter.h>
32
 
-
 
33
#include <CGLA/Vec2i.h>
31
#include <CGLA/Vec2i.h>
34
#include <CGLA/Vec2f.h>
32
#include <CGLA/Vec2f.h>
35
#include <CGLA/Vec3f.h>
33
#include <CGLA/Vec3f.h>
36
#include <CGLA/Mat4x4f.h>
34
#include <CGLA/Mat4x4f.h>
37
#include "glsl_shader.h"
35
#include "GLGraphics/glsl_shader.h"
38
#include "GLGraphics/gel_glut.h"
36
#include "GLGraphics/gel_glut.h"
39
#include "GLGraphics/QuatTrackBall.h"
37
#include "GLGraphics/QuatTrackBall.h"
40
#include "GLGraphics/draw.h"
38
#include "GLGraphics/draw.h"
-
 
39
#include "GLGraphics/SOIL.h"
41
#include "Geometry/TriMesh.h"
40
#include "Geometry/TriMesh.h"
42
#include "Geometry/obj_load.h"
41
#include "Geometry/obj_load.h"
-
 
42
#include "Geometry/ply_load.h"
43
#include "HMesh/x3d_load.h"
43
#include "HMesh/x3d_load.h"
-
 
44
#include "HMesh/FaceCirculator.h"
44
#include "ply_load.h"
45
#include "wireframe.h"
45
 
46
 
46
using namespace std;
47
using namespace std;
47
using namespace CGLA;
48
using namespace CGLA;
48
using namespace Geometry;
49
using namespace Geometry;
49
using namespace HMesh;
50
using namespace HMesh;
50
using namespace GLGraphics;
51
using namespace GLGraphics;
51
 
52
 
52
namespace
-
 
53
{
-
 
54
	int win_size_x = 800;
53
int win_size_x = 800;
55
	int win_size_y = 800;
54
int win_size_y = 800;
56
	bool per_vertex_normals = 1;
55
bool per_vertex_normals = 1;
57
	bool redo_display_list = 1;
56
bool redo_display_list = 1;
-
 
57
bool do_wireframe = false;
-
 
58
Vec3f line_col = Vec3f(1,0,0);
58
	QuatTrackBall* ball;
59
QuatTrackBall* ball;
59
	int spin_timer = 20;
60
int spin_timer = 20;
60
	void spin(int x);
61
void spin(int x);
61
	int main_window;
62
int main_window;
62
	TriMesh mesh;
63
TriMesh mesh;
63
  bool brute_wire = false;
-
 
64
  bool do_textures = true;
-
 
65
  vector<bool> has_texture;
-
 
66
	
-
 
67
	bool load_image_into_texture(const std::string& name, GLuint& id)
-
 
68
	{
-
 
69
		unsigned char* image = 0;
-
 
70
		unsigned int bpp = 0;
-
 
71
		unsigned int size_x = 0;
-
 
72
		unsigned int size_y = 0;
-
 
73
		unsigned int load_size_x = 0;
-
 
74
		unsigned int load_size_y = 0;
-
 
75
		
-
 
76
		
-
 
77
		ILenum Error;
-
 
78
		ILuint  ImgId;
-
 
79
		
-
 
80
		static bool washere = false;
-
 
81
		if(!washere)
-
 
82
		{
-
 
83
			ilInit();
-
 
84
			iluInit();
-
 
85
			washere=true;
-
 
86
		}
-
 
87
		ilEnable(IL_CONV_PAL);
-
 
88
		ilEnable(IL_ORIGIN_SET);
-
 
89
		ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
-
 
90
		ilGenImages(1, &ImgId);
-
 
91
		ilBindImage(ImgId);
-
 
92
		char* name_cstr = const_cast<char*>(name.c_str());
-
 
93
		if(!ilLoadImage(name_cstr))
-
 
94
		{
-
 
95
			cout << "could not load <" << name_cstr  << ">" << endl;
-
 
96
			return false;
-
 
97
		}
-
 
98
		
-
 
99
		load_size_x = ilGetInteger(IL_IMAGE_WIDTH);
-
 
100
		load_size_y = ilGetInteger(IL_IMAGE_HEIGHT);
-
 
101
		bpp = ilGetInteger(IL_IMAGE_BITS_PER_PIXEL);
-
 
102
		
-
 
103
		if (bpp==24)
-
 
104
			ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
-
 
105
		else if (bpp==32)
-
 
106
			ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
-
 
107
		else if (bpp==8)
-
 
108
			ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
-
 
109
		else
-
 
110
			assert(0);
-
 
111
		
-
 
112
		unsigned int i;
-
 
113
		for (i=2;i<=4096 ;i*=2)
-
 
114
			if (i>=load_size_x)
-
 
115
				break;
-
 
116
		size_x = i;
-
 
117
		for (i=2;i<=4096 ;i*=2)
-
 
118
			if (i>=load_size_y)
-
 
119
				break;
-
 
120
		size_y = i;
-
 
121
		
-
 
122
		if(size_x != load_size_x || size_y != load_size_y)
-
 
123
		{
-
 
124
			iluImageParameter(ILU_FILTER, ILU_BILINEAR);
-
 
125
			iluScale(size_x, size_y, 1);
-
 
126
		}
-
 
127
		
-
 
128
		const int image_size =size_x*size_y*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
-
 
129
		image = new unsigned char[image_size];
-
 
130
		memcpy(image, ilGetData(), image_size);
-
 
131
		ilDeleteImages(1, &ImgId);
-
 
132
		
-
 
133
		bool any_errors=false;
-
 
134
		while ((Error = ilGetError())) {
-
 
135
			cout << __LINE__ << "Error: " << iluErrorString(Error) << endl;
-
 
136
			any_errors = true;
-
 
137
		}
-
 
138
		if(any_errors) return false;
-
 
139
		
-
 
140
		GLint internalFormat=0;
-
 
141
		GLenum format=0;
-
 
142
		
-
 
143
		glGenTextures(1, &id);
-
 
144
		switch (bpp)
-
 
145
		{
-
 
146
			case 8:
-
 
147
				internalFormat =  GL_LUMINANCE;
-
 
148
				format = GL_LUMINANCE;
-
 
149
				break;
-
 
150
			case 24:
-
 
151
				internalFormat =  GL_RGB;
-
 
152
				format = GL_RGB;
-
 
153
				break;
-
 
154
			case 32:
-
 
155
				internalFormat =  GL_RGBA;
-
 
156
				format = GL_RGBA;
-
 
157
				break;
-
 
158
			default:
-
 
159
				return false;
-
 
160
		}
-
 
161
		
-
 
162
		glBindTexture(GL_TEXTURE_2D, id);
-
 
163
		glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size_x, size_y, 0,
-
 
164
					 format, GL_UNSIGNED_BYTE, image);
-
 
165
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-
 
166
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-
 
167
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
 
168
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-
 
169
		
-
 
170
		return true;
-
 
171
	}
-
 
172
	
64
 
173
	void load_textures(TriMesh& tm)
65
void enable_textures(TriMesh& tm)
174
	{
66
{
175
    has_texture.resize(tm.materials.size());
-
 
176
		for(unsigned int i=0;i<tm.materials.size(); ++i)
67
	for(unsigned int i=0;i<tm.materials.size(); ++i)
177
		{
68
	{
178
			Material& mat = tm.materials[i];
69
		Material& mat = tm.materials[i];
179
			if(mat.tex_name != "")
70
		if(mat.tex_name != "")
180
			{
71
		{
181
				string name = mat.tex_path + mat.tex_name;
72
			string name = mat.tex_path + mat.tex_name;
182
				
-
 
183
				GLuint tex_id;
-
 
184
				if(load_image_into_texture(name, tex_id))
73
			mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
185
        {
-
 
186
					mat.tex_id = tex_id;
-
 
187
          has_texture[i] = true;
-
 
188
        }
-
 
189
			}
74
		}
190
		}
75
	}
191
	}
76
}
192
 
77
 
193
  void enable_textures(TriMesh& tm)
78
void load_mesh(const string& fn)
-
 
79
{
-
 
80
	if(fn.substr(fn.length()-4,fn.length())==".obj")
194
	{
81
	{
195
		for(unsigned int i=0;i<has_texture.size(); ++i)
82
		obj_load(fn, mesh);
196
			tm.materials[i].has_texture = has_texture[i];
-
 
197
	}
83
	}
198
 
-
 
199
  void disable_textures(TriMesh& tm)
84
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
200
	{
85
	{
201
		for(unsigned int i=0;i<has_texture.size(); ++i)
-
 
202
			tm.materials[i].has_texture = false;
86
		ply_load(fn, mesh);
203
	}
87
	}	
204
	
-
 
205
	void toggle_wire()
88
	else if(fn.substr(fn.length()-4,fn.length())==".x3d")
206
	{
89
	{
207
		static bool wire= false;
90
		Manifold m;
208
		static string shader_path = "/Users/jab/SrcTree/Appsrc/wireframepaper/";
-
 
209
		static GLhandleARB prog_P0;
91
		x3d_load(fn, m);
210
		
92
		
-
 
93
		for(VertexIter v=m.vertices_begin(); v != m.vertices_end();++v)
211
    wire = !wire;
94
			v->touched = mesh.geometry.add_vertex(v->pos);
212
 
95
		
213
		static bool washere = false;
96
		for(FaceIter f = m.faces_begin(); f!= m.faces_end(); ++f)
-
 
97
		{
214
		if(!washere)
98
			Vec3i face;
-
 
99
			int i=0;
-
 
100
			for(FaceCirculator fc(f); !fc.end(); ++fc,++i)
215
		{
101
			{
-
 
102
				if(i<2)
216
      if(!GLEW_EXT_geometry_shader4)
103
					face[i] = fc.get_vertex()->touched;
-
 
104
				else
217
      {
105
				{
218
        brute_wire = !brute_wire;
106
					face[2] = fc.get_vertex()->touched;
219
        redo_display_list = true;
107
					mesh.geometry.add_face(face);
220
        return;
108
					face[1] = face[2];
-
 
109
				}
221
      }
110
			}	
222
      
-
 
223
      washere = true;
-
 
224
			
-
 
225
			// Create s	haders directly from file
-
 
226
			GLuint vs = create_glsl_shader(GL_VERTEX_SHADER, shader_path, "tri.vert");
-
 
227
			GLuint gs = create_glsl_shader(GL_GEOMETRY_SHADER_EXT, shader_path, "tri.geom");
-
 
228
			GLuint fs = create_glsl_shader(GL_FRAGMENT_SHADER, shader_path, "tri.frag");
-
 
229
			
-
 
230
			// Create the program
-
 
231
			prog_P0 = glCreateProgram();
-
 
232
			
-
 
233
			// Attach all shaders
-
 
234
			if(vs) glAttachShader(prog_P0, vs);
-
 
235
			if(gs) glAttachShader(prog_P0, gs);
-
 
236
			if(fs) glAttachShader(prog_P0, fs);
-
 
237
			
-
 
238
			// Specify input and output for the geometry shader. Note that this must be
-
 
239
			// done before linking the program.
-
 
240
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_INPUT_TYPE_EXT,GL_TRIANGLES);
-
 
241
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_VERTICES_OUT_EXT,3);
-
 
242
			glProgramParameteriEXT(prog_P0,GL_GEOMETRY_OUTPUT_TYPE_EXT,GL_TRIANGLE_STRIP);
-
 
243
			
-
 
244
			// Link the program object and print out the info log
-
 
245
			glLinkProgram(prog_P0);
-
 
246
		}
111
		}
247
		if(wire)
-
 
248
		{
-
 
249
			glUseProgram(prog_P0);
-
 
250
			// Set the value of a uniform
-
 
251
			glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
-
 
252
		}
112
	}
253
		else
113
	else
-
 
114
	{
-
 
115
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
-
 
116
		exit(0);
-
 
117
	}
254
			glUseProgram(0);		
118
	enable_textures(mesh);	
-
 
119
		
255
	}
120
}
256
	
121
 
257
	bool depth_pick(int x, int y,Vec3f& wp)
122
bool depth_pick(int x, int y,Vec3f& wp)
258
	{
123
{
259
		// Enquire about the viewport dimensions
124
	// Enquire about the viewport dimensions
Line 295... Line 160...
295
		wp = Vec3f(ox,oy,oz);
160
	wp = Vec3f(ox,oy,oz);
296
		
161
	
297
		return true;
162
	return true;
298
	}
163
}
299
	
164
 
-
 
165
 
-
 
166
 
300
  void mouse_motion(int x, int y)
167
void mouse_motion(int x, int y)
301
  {
168
{
302
      ball->roll_ball(Vec2i(x,y));
169
    ball->roll_ball(Vec2i(x,y));
303
  }
170
}
304
 
171
 
Line 330... Line 197...
330
      if(redo_display_list)
197
    if(redo_display_list)
331
      {
198
    {
332
          cout << "Creating display list" << endl;
199
        cout << "Creating display list" << endl;
333
          l = glGenLists(1);
200
        l = glGenLists(1);
334
          glNewList(l, GL_COMPILE);
201
        glNewList(l, GL_COMPILE);
335
          draw(mesh, per_vertex_normals, brute_wire);
202
        draw(mesh, per_vertex_normals);
336
          glEndList();
203
        glEndList();
337
          redo_display_list = false;
204
        redo_display_list = false;
338
		  glutTimerFunc(spin_timer, spin, 0);	
205
		glutTimerFunc(spin_timer, spin, 0);	
339
	  }
206
	}
340
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
207
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
341
      glLoadIdentity();
208
    glLoadIdentity();
342
      ball->set_gl_modelview();
209
    ball->set_gl_modelview();
-
 
210
	
-
 
211
	if(do_wireframe)
-
 
212
	{
-
 
213
		if(GLEW_EXT_geometry_shader4)
-
 
214
		{
-
 
215
			enable_wireframe();
-
 
216
			glCallList(l);
-
 
217
			glUseProgram(0);
-
 
218
		}
-
 
219
		else
-
 
220
		{
-
 
221
			glDisable(GL_LIGHTING);
-
 
222
			glColor3f(1,1,1);
343
      glCallList(l);
223
			glCallList(l);
-
 
224
			glEnable(GL_POLYGON_OFFSET_LINE);
-
 
225
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-
 
226
			glColor3fv(line_col.get());
-
 
227
			glPolygonOffset(0,-5);
-
 
228
			glCallList(l);
-
 
229
			glEnable(GL_LIGHTING);
-
 
230
			glDisable(GL_POLYGON_OFFSET_LINE);
-
 
231
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-
 
232
		}
-
 
233
	}
-
 
234
	else
-
 
235
		glCallList(l);
-
 
236
		
344
      glutSwapBuffers();
237
    glutSwapBuffers();
345
  }
238
}
346
 
239
 
347
  void keyboard(unsigned char key, int x, int y)
240
void keyboard(unsigned char key, int x, int y)
348
  {
241
{
349
      switch(key)
242
    switch(key)
350
      {
243
    {
351
		  case '\033': exit(0);
244
		case '\033': exit(0); break;
352
		  case 'w': toggle_wire(); break;
245
		case 'w': do_wireframe = !do_wireframe; break;
353
		  case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
246
		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;
-
 
362
      }
-
 
363
  }
247
    }
364
}
248
}
365
 
249
 
-
 
250
 
366
int main(int argc, char** argv)
251
int main(int argc, char** argv)
367
{
252
{
-
 
253
	Util::ArgExtracter ae(argc, argv);
-
 
254
	
-
 
255
	bool redo_normals = ae.extract("-n");
-
 
256
 
368
    // GLUT INIT
257
    // GLUT INIT
369
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
258
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
370
    glutInitWindowSize(win_size_x, win_size_y);
259
    glutInitWindowSize(win_size_x, win_size_y);
371
    glutInit(&argc, argv);
260
    glutInit(&argc, argv);
372
    main_window = glutCreateWindow("OBJ Viewer");
261
    main_window = glutCreateWindow("OBJ Viewer");
Line 386... Line 275...
386
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
275
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
387
    glShadeModel(GL_SMOOTH);
276
    glShadeModel(GL_SMOOTH);
388
	
277
	
389
    // LOAD OBJ
278
    // LOAD OBJ
390
    string fn;
279
    string fn;
391
    if(argc>1)
280
    if(ae.no_remaining_args())
392
        fn = argv[1];
281
        fn = ae.get_last_arg();
393
    else
282
    else
394
        fn = "../../data/head.obj";
283
        fn = "../../data/head.obj";
395
		
284
	
396
	if(fn.substr(fn.length()-4,fn.length())==".obj")
-
 
397
	{
-
 
398
		obj_load(fn, mesh);
-
 
399
	}
-
 
400
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
-
 
401
	{
-
 
402
		ply_load(fn, mesh);
-
 
403
	}	
-
 
404
/*	else if(fn.substr(fn.length()-4,fn.length())==".x3d")
-
 
405
	{
-
 
406
		TriMesh mesh;
-
 
407
		Manifold m;
-
 
408
		x3d_load(fn, m);
285
	load_mesh(fn);	
409
		for(FaceIter f = m.faces_begin(); f!= m.faces_end(); ++f)
-
 
410
			{
-
 
411
				mesh.
-
 
412
			}
-
 
413
	}	
-
 
414
	*/
-
 
415
	else
-
 
416
	{
286
	
417
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
-
 
418
		exit(0);
-
 
419
	}		
-
 
420
    load_textures(mesh);	
-
 
421
    if(!do_textures)
-
 
422
      disable_textures(mesh);
-
 
423
    if(!mesh.has_normals() || (argc > 2 && !strcmp(argv[2], "-n")))
287
	if(!mesh.has_normals() || redo_normals)
424
    {
288
	{
425
        cout << "Computing normals" << endl;
289
		cout << "Computing normals" << endl;
426
        mesh.compute_normals();
290
		mesh.compute_normals();
427
    }
291
	}
428
 
292
	
Line 437... Line 301...
437
    glMatrixMode(GL_PROJECTION);
301
	glMatrixMode(GL_PROJECTION);
438
    glLoadIdentity();
302
	glLoadIdentity();
439
    gluPerspective(53,1.0f,r/100.0,r*3.0);
303
	gluPerspective(53,1.0f,r/100.0,r*3.0);
440
    glMatrixMode(GL_MODELVIEW);
304
	glMatrixMode(GL_MODELVIEW);
441
	
305
	
-
 
306
	if(GLEW_EXT_geometry_shader4)
-
 
307
		initialize_wireframe_shaders();
-
 
308
			
-
 
309
	
442
    // Pass control to GLUT
310
	// Pass control to GLUT
443
    glutMainLoop();
311
	glutMainLoop();
444
	
312
	
445
    return 0;
313
	return 0;
446
}
314
}