Subversion Repositories gelsvn

Rev

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

Rev 102 Rev 125
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
// ----------------------------------------
10
// ----------------------------------------
11
 
11
 
12
#if (_MSC_VER >= 1200)
12
#if (_MSC_VER >= 1200)
13
#pragma warning (disable: 4786)
13
#pragma warning (disable: 4786)
14
#endif
14
#endif
15
 
15
 
16
#include <list>
16
#include <list>
17
#include <vector>
17
#include <vector>
18
 
18
 
19
#include <CGLA/Vec3f.h>
19
#include <CGLA/Vec3f.h>
20
#include <CGLA/Mat4x4f.h>
20
#include <CGLA/Mat4x4f.h>
-
 
21
#if defined(__APPLE__) && defined(__MACH__)
-
 
22
#include <GLUT/glut.h>
-
 
23
#else
21
#include <GL/glut.h>
24
#include <GL/glut.h>
-
 
25
#endif
-
 
26
 
22
#include "Geometry/TriMesh.h"
27
#include "Geometry/TriMesh.h"
23
#include "Graphics/SimpleTrackBall.h"
28
#include "Graphics/SimpleTrackBall.h"
24
#include "Geometry/obj_load.h"
29
#include "Geometry/obj_load.h"
25
 
30
 
26
using namespace std;
31
using namespace std;
27
using namespace CGLA;
32
using namespace CGLA;
28
using namespace Geometry;
33
using namespace Geometry;
29
using namespace Graphics;
34
using namespace Graphics;
30
 
35
 
31
namespace
36
namespace
32
{
37
{
33
	int win_size_x = 800;
38
	int win_size_x = 800;
34
	int win_size_y = 800;
39
	int win_size_y = 800;
35
 
40
 
36
	SimpleTrackBall* ball;
41
	SimpleTrackBall* ball;
37
 
42
 
38
	int main_window; 
43
	int main_window; 
39
 
44
 
40
 
45
 
41
	bool depth_pick(int x, int y,Vec3f& wp)
46
	bool depth_pick(int x, int y,Vec3f& wp)
42
	{
47
	{
43
		// Enquire about the viewport dimensions
48
		// Enquire about the viewport dimensions
44
		GLint viewport[4];
49
		GLint viewport[4];
45
		glGetIntegerv(GL_VIEWPORT, viewport);
50
		glGetIntegerv(GL_VIEWPORT, viewport);
46
		
51
		
47
		// Get the minimum and maximum depth values.
52
		// Get the minimum and maximum depth values.
48
		float minmax_depth[2];
53
		float minmax_depth[2];
49
		glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
54
		glGetFloatv(GL_DEPTH_RANGE, minmax_depth);
50
		
55
		
51
		// Read a single pixel at the position of the mouse cursor.
56
		// Read a single pixel at the position of the mouse cursor.
52
		float depth;
57
		float depth;
53
		glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
58
		glReadPixels(x, viewport[3]-y, 1,1, GL_DEPTH_COMPONENT,
54
								 GL_FLOAT, (void*) &depth);
59
								 GL_FLOAT, (void*) &depth);
55
		
60
		
56
		// 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
57
		// background.
62
		// background.
58
		if(depth == minmax_depth[1])
63
		if(depth == minmax_depth[1])
59
			return false;
64
			return false;
60
		
65
		
61
		// The lines below copy the viewing transformation from OpenGL
66
		// The lines below copy the viewing transformation from OpenGL
62
		// to local variables. The call to gluLookAt must have exactly 
67
		// to local variables. The call to gluLookAt must have exactly 
63
		// the same parameters as when the scene is drawn.
68
		// the same parameters as when the scene is drawn.
64
		glLoadIdentity();
69
		glLoadIdentity();
65
		ball->gl_view();
70
		ball->gl_view();
66
		double mvmat[16];
71
		double mvmat[16];
67
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
72
		glGetDoublev(GL_MODELVIEW_MATRIX, mvmat);
68
		
73
		
69
		// Copy the projection matrix. We assume it is unchanged.
74
		// Copy the projection matrix. We assume it is unchanged.
70
		double prjmat[16];
75
		double prjmat[16];
71
		glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
76
		glGetDoublev(GL_PROJECTION_MATRIX, prjmat);
72
		
77
		
73
		// Now unproject the point from screen to world coordinates.
78
		// Now unproject the point from screen to world coordinates.
74
		double ox, oy, oz;
79
		double ox, oy, oz;
75
		gluUnProject(x,viewport[3]-y,depth,
80
		gluUnProject(x,viewport[3]-y,depth,
76
								 mvmat,prjmat,viewport,
81
								 mvmat,prjmat,viewport,
77
								 &ox, &oy, &oz);
82
								 &ox, &oy, &oz);
78
		
83
		
79
		wp = Vec3f(ox,oy,oz);
84
		wp = Vec3f(ox,oy,oz);
80
		
85
		
81
		return true;
86
		return true;
82
	}
87
	}
83
 
88
 
84
	
89
	
85
	TriMesh mesh;
90
	TriMesh mesh;
86
 
91
 
87
	void mouse_motion(int x, int y)
92
	void mouse_motion(int x, int y)
88
	{
93
	{
89
		ball->roll(x,y);
94
		ball->roll(x,y);
90
	}
95
	}
91
 
96
 
92
	void mouse(int button, int state, int x, int y)
97
	void mouse(int button, int state, int x, int y)
93
	{
98
	{
94
		if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
99
		if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
95
			{
100
			{
96
				Vec3f p;
101
				Vec3f p;
97
				if(depth_pick(x, y, p))
102
				if(depth_pick(x, y, p))
98
					ball->set_center(p);
103
					ball->set_center(p);
99
			}
104
			}
100
		else if(button==3) 
105
		else if(button==3) 
101
			ball->farther(); 
106
			ball->farther(); 
102
		else if(button==4)
107
		else if(button==4)
103
			ball->closer();
108
			ball->closer();
104
	}
109
	}
105
	
110
	
106
	void idle()
111
	void idle()
107
	{
112
	{
108
		if ( glutGetWindow() != main_window ) 
113
		if ( glutGetWindow() != main_window ) 
109
			glutSetWindow(main_window);  
114
			glutSetWindow(main_window);  
110
		glutPostRedisplay();
115
		glutPostRedisplay();
111
	}
116
	}
112
	
117
	
113
	void display() 
118
	void display() 
114
	{
119
	{
115
			static bool washere = false;
120
			static bool washere = false;
116
			static unsigned int l;
121
			static unsigned int l;
117
			if(!washere)
122
			if(!washere)
118
			{
123
			{
119
					cout << "Creating display list" << endl;
124
					cout << "Creating display list" << endl;
120
					l = glGenLists(1);
125
					l = glGenLists(1);
121
					glNewList(l, GL_COMPILE);
126
					glNewList(l, GL_COMPILE);
122
					mesh.gl_draw();
127
					mesh.gl_draw();
123
					glEndList();
128
					glEndList();
124
					washere = true;
129
					washere = true;
125
			}
130
			}
126
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
131
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127
			glLoadIdentity();
132
			glLoadIdentity();
128
			ball->gl_view();
133
			ball->gl_view();
129
			glCallList(l);
134
			glCallList(l);
130
			glutSwapBuffers();
135
			glutSwapBuffers();
131
	}
136
	}
132
 
137
 
133
	void keyboard(unsigned char key, int x, int y) 
138
	void keyboard(unsigned char key, int x, int y) 
134
	{	
139
	{	
135
		switch(key) 
140
		switch(key) 
136
			{
141
			{
137
			case '\033': exit(0); break;
142
			case '\033': exit(0); break;
138
			case '+': ball->closer(); break;
143
			case '+': ball->closer(); break;
139
			case '-': ball->farther(); break;
144
			case '-': ball->farther(); break;
140
			default:
145
			default:
141
				ball->up_axis(key);
146
				ball->up_axis(key);
142
			}
147
			}
143
	}
148
	}
144
}
149
}
145
 
150
 
146
int main(int argc, char** argv)
151
int main(int argc, char** argv)
147
{ 
152
{ 
148
	// GLUT INIT
153
	// GLUT INIT
149
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
154
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
150
	glutInitWindowSize(win_size_x, win_size_y);
155
	glutInitWindowSize(win_size_x, win_size_y);
151
	glutInit(&argc, argv);
156
	glutInit(&argc, argv);
152
	main_window = glutCreateWindow("OBJ Viewer");
157
	main_window = glutCreateWindow("OBJ Viewer");
153
	glutDisplayFunc(display);
158
	glutDisplayFunc(display);
154
	glutKeyboardFunc(keyboard);
159
	glutKeyboardFunc(keyboard);
155
	glutMotionFunc(mouse_motion);
160
	glutMotionFunc(mouse_motion);
156
	glutMouseFunc(mouse);
161
	glutMouseFunc(mouse);
157
	glutIdleFunc(idle);
162
	glutIdleFunc(idle);
158
 
163
 
159
	// GL INIT
164
	// GL INIT
160
	glClearColor(.8f, 0.9f, 1.0f, 0.f);
165
	glClearColor(.8f, 0.9f, 1.0f, 0.f);
161
	glEnable(GL_DEPTH_TEST);
166
	glEnable(GL_DEPTH_TEST);
162
	glEnable(GL_LIGHTING);
167
	glEnable(GL_LIGHTING);
163
	glEnable(GL_LIGHT0);
168
	glEnable(GL_LIGHT0);
164
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
169
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
165
	glShadeModel(GL_SMOOTH);
170
	glShadeModel(GL_SMOOTH);
166
 
171
 
167
	// LOAD OBJ
172
	// LOAD OBJ
168
	string fn;
173
	string fn;
169
	if(argc>1)
174
	if(argc>1)
170
			fn = argv[1];
175
			fn = argv[1];
171
		else
176
		else
172
			fn = "../../data/head.obj";
177
			fn = "../../data/head.obj";
173
 
178
 
174
	cout << "Loading " << fn << endl;
179
	cout << "Loading " << fn << endl;
175
	if(fn == "") exit(0);
180
	if(fn == "") exit(0);
176
	obj_load(fn, mesh);
181
	obj_load(fn, mesh);
177
	if(!mesh.has_normals())
182
	if(!mesh.has_normals())
178
		{
183
		{
179
			cout << "Computing normals" << endl;
184
			cout << "Computing normals" << endl;
180
			mesh.compute_normals();
185
			mesh.compute_normals();
181
		}
186
		}
182
	// Initialize textures
187
	// Initialize textures
183
	mesh.gl_init_textures();
188
	mesh.gl_init_textures();
184
	
189
	
185
	// Initialize Trackball
190
	// Initialize Trackball
186
	Vec3f c;
191
	Vec3f c;
187
	float r;
192
	float r;
188
	mesh.get_bsphere(c,r);
193
	mesh.get_bsphere(c,r);
189
	r *= 1.5;
194
	r *= 1.5;
190
	ball = new SimpleTrackBall(c,r);
195
	ball = new SimpleTrackBall(c,r);
191
 
196
 
192
	// Setup projection
197
	// Setup projection
193
	glMatrixMode(GL_PROJECTION);
198
	glMatrixMode(GL_PROJECTION);
194
	glLoadIdentity();
199
	glLoadIdentity();
195
	gluPerspective(53,1.0f,r/100,r*3);
200
	gluPerspective(53,1.0f,r/100,r*3);
196
	glMatrixMode(GL_MODELVIEW);
201
	glMatrixMode(GL_MODELVIEW);
197
 
202
 
198
	// Pass control to GLUT
203
	// Pass control to GLUT
199
	glutMainLoop();
204
	glutMainLoop();
200
 
205
 
201
	return 0;
206
	return 0;
202
}
207
}
203
 
208