Subversion Repositories gelsvn

Rev

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

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