Subversion Repositories gelsvn

Rev

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