Subversion Repositories gelsvn

Rev

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

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