Subversion Repositories gelsvn

Rev

Rev 398 | Rev 492 | 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"
442 jab 45
#include "Geometry/load.h"
365 jab 46
#include "HMesh/x3d_load.h"
370 jab 47
#include "HMesh/FaceCirculator.h"
337 jab 48
 
82 jab 49
using namespace std;
50
using namespace CGLA;
51
using namespace Geometry;
365 jab 52
using namespace HMesh;
178 bj 53
using namespace GLGraphics;
82 jab 54
 
370 jab 55
int win_size_x = 800;
56
int win_size_y = 800;
57
bool per_vertex_normals = 1;
58
bool redo_display_list = 1;
59
bool do_wireframe = false;
60
Vec3f line_col = Vec3f(1,0,0);
61
QuatTrackBall* ball;
62
int spin_timer = 20;
63
void spin(int x);
64
int main_window;
65
TriMesh mesh;
374 jrf 66
bool do_textures = true;
370 jab 67
 
374 jrf 68
 
370 jab 69
bool depth_pick(int x, int y,Vec3f& wp)
70
{
71
	// Enquire about the viewport dimensions
72
	GLint viewport[4];
73
	glGetIntegerv(GL_VIEWPORT, viewport);
74
 
75
	// Get the minimum and maximum depth values.
76
	float minmax_depth[2];
77
	glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
78
 
79
	// Read a single pixel at the position of the mouse cursor.
80
	float depth;
81
	glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
82
				 GL_FLOAT, (void*) &depth);
83
 
84
	// If the depth corresponds to the far plane, we clicked on the
85
	// background.
86
	if(depth == minmax_depth[1])
87
		return false;
88
 
89
	// The lines below copy the viewing transformation from OpenGL
90
	// to local variables. The call to gluLookAt must have exactly
91
	// the same parameters as when the scene is drawn.
92
	glLoadIdentity();
93
	ball->set_gl_modelview();
94
	double mvmat[16];
95
	glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
96
 
97
	// Copy the projection matrix. We assume it is unchanged.
98
	double prjmat[16];
99
	glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
100
 
101
	// Now unproject the point from screen to world coordinates.
102
	double ox, oy, oz;
103
	gluUnProject(x,viewport[3]-y,depth,
104
				 mvmat,prjmat,viewport,
105
				 &ox, &oy, &oz);
106
 
107
	wp = Vec3f(ox,oy,oz);
108
 
109
	return true;
110
}
111
 
112
 
113
 
114
void mouse_motion(int x, int y)
115
{
116
    ball->roll_ball(Vec2i(x,y));
117
}
118
 
119
void mouse(int btn, int state, int x, int y)
120
{
121
	if(state == GLUT_DOWN) 
368 jrf 122
	{
370 jab 123
		if(btn == GLUT_LEFT_BUTTON) 
124
			ball->grab_ball(ROTATE_ACTION, Vec2i(x,y));
125
		else if(btn == GLUT_MIDDLE_BUTTON) 
126
			ball->grab_ball(ZOOM_ACTION, Vec2i(x, y));
127
		else if(btn == GLUT_RIGHT_BUTTON) 
128
			ball->grab_ball(PAN_ACTION, Vec2i(x, y));
368 jrf 129
	}
370 jab 130
	else if(state == GLUT_UP)
131
		ball->release_ball();	
132
}
368 jrf 133
 
370 jab 134
void spin(int x)
135
{
136
	ball->do_spin();
137
	glutTimerFunc(spin_timer, spin, 0);  
138
	glutPostRedisplay();
139
}
140
 
141
void display()
142
{
143
	static unsigned int l;
144
    if(redo_display_list)
145
    {
146
        cout << "Creating display list" << endl;
147
        l = glGenLists(1);
148
        glNewList(l, GL_COMPILE);
149
        draw(mesh, per_vertex_normals);
150
        glEndList();
151
        redo_display_list = false;
152
		glutTimerFunc(spin_timer, spin, 0);	
368 jrf 153
	}
370 jab 154
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155
    glLoadIdentity();
156
    ball->set_gl_modelview();
337 jab 157
 
370 jab 158
	if(do_wireframe)
357 jab 159
	{
370 jab 160
		if(GLEW_EXT_geometry_shader4)
398 jab 161
			draw_triangles_in_wireframe(mesh, per_vertex_normals, Vec3f(1,0,0));
370 jab 162
		else
398 jab 163
			draw_wireframe_oldfashioned(mesh, per_vertex_normals, Vec3f(1,0,0));
357 jab 164
	}
370 jab 165
	else
166
		glCallList(l);
337 jab 167
 
370 jab 168
    glutSwapBuffers();
169
}
82 jab 170
 
370 jab 171
void keyboard(unsigned char key, int x, int y)
172
{
173
    switch(key)
174
    {
175
		case '\033': exit(0); break;
176
		case 'w': do_wireframe = !do_wireframe; break;
177
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
374 jrf 178
        break;
370 jab 179
    }
442 jab 180
	redo_display_list=true;
370 jab 181
}
82 jab 182
 
183
int main(int argc, char** argv)
178 bj 184
{
370 jab 185
	Util::ArgExtracter ae(argc, argv);
186
 
187
	bool redo_normals = ae.extract("-n");
188
 
178 bj 189
    // GLUT INIT
190
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
191
    glutInitWindowSize(win_size_x, win_size_y);
192
    glutInit(&argc, argv);
193
    main_window = glutCreateWindow("OBJ Viewer");
194
    glutDisplayFunc(display);
195
    glutKeyboardFunc(keyboard);
196
    glutMotionFunc(mouse_motion);
197
    glutMouseFunc(mouse);
299 jrf 198
    //glutIdleFunc(idle);
337 jab 199
 
370 jab 200
	glewInit();
357 jab 201
 
178 bj 202
    // GL INIT
203
    glClearColor(.8f, 0.9f, 1.0f, 0.f);
204
    glEnable(GL_DEPTH_TEST);
205
    glEnable(GL_LIGHTING);
206
    glEnable(GL_LIGHT0);
207
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
208
    glShadeModel(GL_SMOOTH);
337 jab 209
 
178 bj 210
    // LOAD OBJ
211
    string fn;
370 jab 212
    if(ae.no_remaining_args())
213
        fn = ae.get_last_arg();
178 bj 214
    else
215
        fn = "../../data/head.obj";
370 jab 216
 
442 jab 217
	load(fn, mesh);
218
	load_textures(mesh);
370 jab 219
 
220
	if(!mesh.has_normals() || redo_normals)
337 jab 221
	{
370 jab 222
		cout << "Computing normals" << endl;
223
		mesh.compute_normals();
337 jab 224
	}
225
 
370 jab 226
	// Initialize Trackball
227
	Vec3f c;
228
	float r;
229
	mesh.get_bsphere(c,r);
230
	r *= 1.5;
231
	ball = new QuatTrackBall(c,r,800,800);
337 jab 232
 
370 jab 233
	// Setup projection
234
	glMatrixMode(GL_PROJECTION);
235
	glLoadIdentity();
236
	gluPerspective(53,1.0f,r/100.0,r*3.0);
237
	glMatrixMode(GL_MODELVIEW);
398 jab 238
 
370 jab 239
	// Pass control to GLUT
240
	glutMainLoop();
241
 
242
	return 0;
82 jab 243
}