Subversion Repositories gelsvn

Rev

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

Rev 374 Rev 382
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
// - w                              : toggle wireframe on/off
10
// - w                              : toggle wireframe on/off
11
// - t                              : toggle texture on/off
11
// - t                              : toggle texture on/off
12
// - f                              : switch between vertex and face normals
12
// - f                              : switch between vertex and face normals
13
// ----------------------------------------
13
// ----------------------------------------
14
 
14
 
15
#if (_MSC_VER >= 1200)
15
#if (_MSC_VER >= 1200)
16
#pragma warning (disable: 4786)
16
#pragma warning (disable: 4786)
17
#endif
17
#endif
18
 
18
 
19
#include <list>
19
#include <list>
20
#include <vector>
20
#include <vector>
21
 
21
 
22
#include <assert.h>
22
#include <assert.h>
23
#include <stdio.h>
23
#include <stdio.h>
24
#ifdef WIN32
24
#ifdef WIN32
25
#include <windows.h>
25
#include <windows.h>
26
#include <io.h>
26
#include <io.h>
27
#endif
27
#endif
28
#include <string.h>
28
#include <string.h>
29
#include <stdlib.h>
29
#include <stdlib.h>
30
 
30
 
31
#include <iostream>
31
#include <iostream>
32
#include <vector>
32
#include <vector>
33
 
33
 
34
#include <Util/ArgExtracter.h>
34
#include <Util/ArgExtracter.h>
35
#include <CGLA/Vec2i.h>
35
#include <CGLA/Vec2i.h>
36
#include <CGLA/Vec2f.h>
36
#include <CGLA/Vec2f.h>
37
#include <CGLA/Vec3f.h>
37
#include <CGLA/Vec3f.h>
38
#include <CGLA/Mat4x4f.h>
38
#include <CGLA/Mat4x4f.h>
39
#include "GLGraphics/glsl_shader.h"
39
#include "GLGraphics/glsl_shader.h"
40
#include "GLGraphics/gel_glut.h"
40
#include "GLGraphics/gel_glut.h"
41
#include "GLGraphics/QuatTrackBall.h"
41
#include "GLGraphics/QuatTrackBall.h"
42
#include "GLGraphics/draw.h"
42
#include "GLGraphics/draw.h"
43
#include "GLGraphics/SOIL.h"
43
#include "GLGraphics/SOIL.h"
44
#include "Geometry/TriMesh.h"
44
#include "Geometry/TriMesh.h"
45
#include "Geometry/obj_load.h"
45
#include "Geometry/obj_load.h"
46
#include "Geometry/ply_load.h"
46
#include "Geometry/ply_load.h"
47
#include "HMesh/x3d_load.h"
47
#include "HMesh/x3d_load.h"
48
#include "HMesh/FaceCirculator.h"
48
#include "HMesh/FaceCirculator.h"
49
#include "wireframe.h"
49
#include "wireframe.h"
50
 
50
 
51
using namespace std;
51
using namespace std;
52
using namespace CGLA;
52
using namespace CGLA;
53
using namespace Geometry;
53
using namespace Geometry;
54
using namespace HMesh;
54
using namespace HMesh;
55
using namespace GLGraphics;
55
using namespace GLGraphics;
56
 
56
 
57
int win_size_x = 800;
57
int win_size_x = 800;
58
int win_size_y = 800;
58
int win_size_y = 800;
59
bool per_vertex_normals = 1;
59
bool per_vertex_normals = 1;
60
bool redo_display_list = 1;
60
bool redo_display_list = 1;
61
bool do_wireframe = false;
61
bool do_wireframe = false;
62
Vec3f line_col = Vec3f(1,0,0);
62
Vec3f line_col = Vec3f(1,0,0);
63
QuatTrackBall* ball;
63
QuatTrackBall* ball;
64
int spin_timer = 20;
64
int spin_timer = 20;
65
void spin(int x);
65
void spin(int x);
66
int main_window;
66
int main_window;
67
TriMesh mesh;
67
TriMesh mesh;
68
bool do_textures = true;
68
bool do_textures = true;
69
vector<bool> has_texture;
69
vector<bool> has_texture;
70
 
70
 
71
void enable_textures(TriMesh& tm)
71
void enable_textures(TriMesh& tm)
72
{
72
{
73
	for(unsigned int i=0;i<has_texture.size(); ++i)
73
	for(unsigned int i=0;i<has_texture.size(); ++i)
74
		tm.materials[i].has_texture = has_texture[i];
74
		tm.materials[i].has_texture = has_texture[i];
75
}
75
}
76
 
76
 
77
void disable_textures(TriMesh& tm)
77
void disable_textures(TriMesh& tm)
78
{
78
{
79
	for(unsigned int i=0;i<has_texture.size(); ++i)
79
	for(unsigned int i=0;i<has_texture.size(); ++i)
80
		tm.materials[i].has_texture = false;
80
		tm.materials[i].has_texture = false;
81
}
81
}
82
 
82
 
83
void load_textures(TriMesh& tm)
83
void load_textures(TriMesh& tm)
84
{
84
{
85
  has_texture.resize(tm.materials.size());
85
  has_texture.resize(tm.materials.size());
86
	for(unsigned int i=0;i<tm.materials.size(); ++i)
86
	for(unsigned int i=0;i<tm.materials.size(); ++i)
87
	{
87
	{
88
		Material& mat = tm.materials[i];
88
		Material& mat = tm.materials[i];
89
		if(mat.tex_name != "")
89
		if(mat.tex_name != "")
90
		{
90
		{
91
			string name = mat.tex_path + mat.tex_name;
91
			string name = mat.tex_path + mat.tex_name;
92
			mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
92
			mat.tex_id = SOIL_load_OGL_texture(name.data(), 0, 0, SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_POWER_OF_TWO);
93
      if(mat.tex_id)
93
      if(mat.tex_id)
94
        has_texture[i] = true;
94
        has_texture[i] = true;
95
		}
95
		}
96
	}
96
	}
97
}
97
}
98
 
98
 
99
void load_mesh(const string& fn)
99
void load_mesh(const string& fn)
100
{
100
{
101
	if(fn.substr(fn.length()-4,fn.length())==".obj")
101
	if(fn.substr(fn.length()-4,fn.length())==".obj")
102
	{
102
	{
103
		obj_load(fn, mesh);
103
		obj_load(fn, mesh);
104
	}
104
	}
105
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
105
	else if(fn.substr(fn.length()-4,fn.length())==".ply")
106
	{
106
	{
107
		ply_load(fn, mesh);
107
		ply_load(fn, mesh);
108
	}	
108
	}	
109
	else if(fn.substr(fn.length()-4,fn.length())==".x3d")
109
	else if(fn.substr(fn.length()-4,fn.length())==".x3d")
110
	{
110
	{
111
		Manifold m;
111
		Manifold m;
112
		x3d_load(fn, m);
112
		x3d_load(fn, m);
113
		
113
		
114
		for(VertexIter v=m.vertices_begin(); v != m.vertices_end();++v)
114
		for(VertexIter v=m.vertices_begin(); v != m.vertices_end();++v)
115
			v->touched = mesh.geometry.add_vertex(v->pos);
115
			v->touched = mesh.geometry.add_vertex(v->pos);
116
		
116
		
117
		for(FaceIter f = m.faces_begin(); f!= m.faces_end(); ++f)
117
		for(FaceIter f = m.faces_begin(); f!= m.faces_end(); ++f)
118
		{
118
		{
119
			Vec3i face;
119
			Vec3i face;
120
			int i=0;
120
			int i=0;
121
			for(FaceCirculator fc(f); !fc.end(); ++fc,++i)
121
			for(FaceCirculator fc(f); !fc.end(); ++fc,++i)
122
			{
122
			{
123
				if(i<2)
123
				if(i<2)
124
					face[i] = fc.get_vertex()->touched;
124
					face[i] = fc.get_vertex()->touched;
125
				else
125
				else
126
				{
126
				{
127
					face[2] = fc.get_vertex()->touched;
127
					face[2] = fc.get_vertex()->touched;
128
					mesh.geometry.add_face(face);
128
					mesh.geometry.add_face(face);
129
					face[1] = face[2];
129
					face[1] = face[2];
130
				}
130
				}
131
			}	
131
			}	
132
		}
132
		}
133
	}
133
	}
134
	else
134
	else
135
	{
135
	{
136
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
136
		cout << "Either the format was unrecognized or the file did not have the appropriate extension" << endl;
137
		exit(0);
137
		exit(0);
138
	}
138
	}
139
  load_textures(mesh);	
139
  load_textures(mesh);	
140
  if(!do_textures)
140
  if(!do_textures)
141
    disable_textures(mesh);		
141
    disable_textures(mesh);		
142
}
142
}
143
 
143
 
144
bool depth_pick(int x, int y,Vec3f& wp)
144
bool depth_pick(int x, int y,Vec3f& wp)
145
{
145
{
146
	// Enquire about the viewport dimensions
146
	// Enquire about the viewport dimensions
147
	GLint viewport[4];
147
	GLint viewport[4];
148
	glGetIntegerv(GL_VIEWPORT, viewport);
148
	glGetIntegerv(GL_VIEWPORT, viewport);
149
	
149
	
150
	// Get the minimum and maximum depth values.
150
	// Get the minimum and maximum depth values.
151
	float minmax_depth[2];
151
	float minmax_depth[2];
152
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
152
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
153
	
153
	
154
	// Read a single pixel at the position of the mouse cursor.
154
	// Read a single pixel at the position of the mouse cursor.
155
	float depth;
155
	float depth;
156
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
156
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
157
				 GL_FLOAT, (void*) &depth);
157
				 GL_FLOAT, (void*) &depth);
158
	
158
	
159
	// If the depth corresponds to the far plane, we clicked on the
159
	// If the depth corresponds to the far plane, we clicked on the
160
	// background.
160
	// background.
161
	if(depth == minmax_depth[1])
161
	if(depth == minmax_depth[1])
162
		return false;
162
		return false;
163
	
163
	
164
	// The lines below copy the viewing transformation from OpenGL
164
	// The lines below copy the viewing transformation from OpenGL
165
	// to local variables. The call to gluLookAt must have exactly
165
	// to local variables. The call to gluLookAt must have exactly
166
	// the same parameters as when the scene is drawn.
166
	// the same parameters as when the scene is drawn.
167
	glLoadIdentity();
167
	glLoadIdentity();
168
	ball->set_gl_modelview();
168
	ball->set_gl_modelview();
169
	double mvmat[16];
169
	double mvmat[16];
170
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
170
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
171
	
171
	
172
	// Copy the projection matrix. We assume it is unchanged.
172
	// Copy the projection matrix. We assume it is unchanged.
173
	double prjmat[16];
173
	double prjmat[16];
174
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
174
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
175
	
175
	
176
	// Now unproject the point from screen to world coordinates.
176
	// Now unproject the point from screen to world coordinates.
177
	double ox, oy, oz;
177
	double ox, oy, oz;
178
	gluUnProject(x,viewport[3]-y,depth,
178
	gluUnProject(x,viewport[3]-y,depth,
179
				 mvmat,prjmat,viewport,
179
				 mvmat,prjmat,viewport,
180
				 &ox, &oy, &oz);
180
				 &ox, &oy, &oz);
181
	
181
	
182
	wp = Vec3f(ox,oy,oz);
182
	wp = Vec3f(ox,oy,oz);
183
	
183
	
184
	return true;
184
	return true;
185
}
185
}
186
 
186
 
187
 
187
 
188
 
188
 
189
void mouse_motion(int x, int y)
189
void mouse_motion(int x, int y)
190
{
190
{
191
    ball->roll_ball(Vec2i(x,y));
191
    ball->roll_ball(Vec2i(x,y));
192
}
192
}
193
 
193
 
194
void mouse(int btn, int state, int x, int y)
194
void mouse(int btn, int state, int x, int y)
195
{
195
{
196
	if(state == GLUT_DOWN) 
196
	if(state == GLUT_DOWN) 
197
	{
197
	{
198
		if(btn == GLUT_LEFT_BUTTON) 
198
		if(btn == GLUT_LEFT_BUTTON) 
199
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
199
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
200
		else if(btn == GLUT_MIDDLE_BUTTON) 
200
		else if(btn == GLUT_MIDDLE_BUTTON) 
201
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
201
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
202
		else if(btn == GLUT_RIGHT_BUTTON) 
202
		else if(btn == GLUT_RIGHT_BUTTON) 
203
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
203
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
204
	}
204
	}
205
	else if(state == GLUT_UP)
205
	else if(state == GLUT_UP)
206
		ball->release_ball();	
206
		ball->release_ball();	
207
}
207
}
208
 
208
 
209
void spin(int x)
209
void spin(int x)
210
{
210
{
211
	ball->do_spin();
211
	ball->do_spin();
212
	glutTimerFunc(spin_timer, spin, 0);  
212
	glutTimerFunc(spin_timer, spin, 0);  
213
	glutPostRedisplay();
213
	glutPostRedisplay();
214
}
214
}
215
 
215
 
216
void display()
216
void display()
217
{
217
{
218
	static unsigned int l;
218
	static unsigned int l;
219
    if(redo_display_list)
219
    if(redo_display_list)
220
    {
220
    {
221
        cout << "Creating display list" << endl;
221
        cout << "Creating display list" << endl;
222
        l = glGenLists(1);
222
        l = glGenLists(1);
223
        glNewList(l, GL_COMPILE);
223
        glNewList(l, GL_COMPILE);
224
        draw(mesh, per_vertex_normals);
224
        draw(mesh, per_vertex_normals);
225
        glEndList();
225
        glEndList();
226
        redo_display_list = false;
226
        redo_display_list = false;
227
		glutTimerFunc(spin_timer, spin, 0);	
227
		glutTimerFunc(spin_timer, spin, 0);	
228
	}
228
	}
229
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
229
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
230
    glLoadIdentity();
230
    glLoadIdentity();
231
    ball->set_gl_modelview();
231
    ball->set_gl_modelview();
232
	
232
	
233
	if(do_wireframe)
233
	if(do_wireframe)
234
	{
234
	{
235
		if(GLEW_EXT_geometry_shader4)
235
		if(GLEW_EXT_geometry_shader4)
236
		{
236
		{
237
			enable_wireframe();
237
			enable_wireframe();
238
			glCallList(l);
238
			glCallList(l);
239
			glUseProgram(0);
239
			glUseProgram(0);
240
		}
240
		}
241
		else
241
		else
242
		{
242
		{
243
			glDisable(GL_LIGHTING);
243
			glDisable(GL_LIGHTING);
244
			glColor3f(1,1,1);
244
			glColor3f(1,1,1);
245
			glCallList(l);
245
			glCallList(l);
246
			glEnable(GL_POLYGON_OFFSET_LINE);
246
			glEnable(GL_POLYGON_OFFSET_LINE);
247
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
247
			glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
248
			glColor3fv(line_col.get());
248
			glColor3fv(line_col.get());
249
			glPolygonOffset(0,-5);
249
			glPolygonOffset(0,-5);
250
			glCallList(l);
250
			glCallList(l);
251
			glEnable(GL_LIGHTING);
251
			glEnable(GL_LIGHTING);
252
			glDisable(GL_POLYGON_OFFSET_LINE);
252
			glDisable(GL_POLYGON_OFFSET_LINE);
253
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
253
			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
254
		}
254
		}
255
	}
255
	}
256
	else
256
	else
257
		glCallList(l);
257
		glCallList(l);
258
		
258
		
259
    glutSwapBuffers();
259
    glutSwapBuffers();
260
}
260
}
261
 
261
 
262
void keyboard(unsigned char key, int x, int y)
262
void keyboard(unsigned char key, int x, int y)
263
{
263
{
264
    switch(key)
264
    switch(key)
265
    {
265
    {
266
		case '\033': exit(0); break;
266
		case '\033': exit(0); break;
267
		case 'w': do_wireframe = !do_wireframe; break;
267
		case 'w': do_wireframe = !do_wireframe; break;
268
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
268
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
269
    case 't': 
269
    case 't': 
270
        do_textures = !do_textures; 
270
        do_textures = !do_textures; 
271
        if(do_textures) 
271
        if(do_textures) 
272
          enable_textures(mesh); 
272
          enable_textures(mesh); 
273
        else 
273
        else 
274
          disable_textures(mesh); 
274
          disable_textures(mesh); 
275
        redo_display_list = true; 
275
        redo_display_list = true; 
276
        break;
276
        break;
277
    }
277
    }
278
}
278
}
279
 
279
 
280
int main(int argc, char** argv)
280
int main(int argc, char** argv)
281
{
281
{
282
	Util::ArgExtracter ae(argc, argv);
282
	Util::ArgExtracter ae(argc, argv);
283
	
283
	
284
	bool redo_normals = ae.extract("-n");
284
	bool redo_normals = ae.extract("-n");
285
 
285
 
286
    // GLUT INIT
286
    // GLUT INIT
287
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
287
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
288
    glutInitWindowSize(win_size_x, win_size_y);
288
    glutInitWindowSize(win_size_x, win_size_y);
289
    glutInit(&argc, argv);
289
    glutInit(&argc, argv);
290
    main_window = glutCreateWindow("OBJ Viewer");
290
    main_window = glutCreateWindow("OBJ Viewer");
291
    glutDisplayFunc(display);
291
    glutDisplayFunc(display);
292
    glutKeyboardFunc(keyboard);
292
    glutKeyboardFunc(keyboard);
293
    glutMotionFunc(mouse_motion);
293
    glutMotionFunc(mouse_motion);
294
    glutMouseFunc(mouse);
294
    glutMouseFunc(mouse);
295
    //glutIdleFunc(idle);
295
    //glutIdleFunc(idle);
296
	
296
	
297
	glewInit();
297
	glewInit();
298
	
298
	
299
    // GL INIT
299
    // GL INIT
300
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
300
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
301
    glEnable(GL_DEPTH_TEST);
301
    glEnable(GL_DEPTH_TEST);
302
    glEnable(GL_LIGHTING);
302
    glEnable(GL_LIGHTING);
303
    glEnable(GL_LIGHT0);
303
    glEnable(GL_LIGHT0);
304
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
304
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
305
    glShadeModel(GL_SMOOTH);
305
    glShadeModel(GL_SMOOTH);
306
	
306
	
307
    // LOAD OBJ
307
    // LOAD OBJ
308
    string fn;
308
    string fn;
309
    if(ae.no_remaining_args())
309
    if(ae.no_remaining_args())
310
        fn = ae.get_last_arg();
310
        fn = ae.get_last_arg();
311
    else
311
    else
312
        fn = "../../data/head.obj";
312
        fn = "../../data/head.obj";
313
	
313
	
314
	load_mesh(fn);	
314
	load_mesh(fn);	
315
	
315
	
316
	if(!mesh.has_normals() || redo_normals)
316
	if(!mesh.has_normals() || redo_normals)
317
	{
317
	{
318
		cout << "Computing normals" << endl;
318
		cout << "Computing normals" << endl;
319
		mesh.compute_normals();
319
		mesh.compute_normals();
320
	}
320
	}
-
 
321
	cout<< __FILE__ << __LINE__ << " "  << mesh.geometry.no_faces() << " " << mesh.geometry.no_vertices() << endl;
321
	
322
	
322
	// Initialize Trackball
323
	// Initialize Trackball
323
	Vec3f c;
324
	Vec3f c;
324
	float r;
325
	float r;
325
	mesh.get_bsphere(c,r);
326
	mesh.get_bsphere(c,r);
326
	r *= 1.5;
327
	r *= 1.5;
327
	ball = new QuatTrackBall(c,r,800,800);
328
	ball = new QuatTrackBall(c,r,800,800);
328
	
329
	
329
	// Setup projection
330
	// Setup projection
330
	glMatrixMode(GL_PROJECTION);
331
	glMatrixMode(GL_PROJECTION);
331
	glLoadIdentity();
332
	glLoadIdentity();
332
	gluPerspective(53,1.0f,r/100.0,r*3.0);
333
	gluPerspective(53,1.0f,r/100.0,r*3.0);
333
	glMatrixMode(GL_MODELVIEW);
334
	glMatrixMode(GL_MODELVIEW);
334
	
335
	
335
	if(GLEW_EXT_geometry_shader4)
336
	if(GLEW_EXT_geometry_shader4)
336
		initialize_wireframe_shaders();
337
		initialize_wireframe_shaders();
337
			
338
			
338
	
339
	
339
	// Pass control to GLUT
340
	// Pass control to GLUT
340
	glutMainLoop();
341
	glutMainLoop();
341
	
342
	
342
	return 0;
343
	return 0;
343
}
344
}
344
 
345