Subversion Repositories gelsvn

Rev

Rev 382 | Rev 442 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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