Subversion Repositories gelsvn

Rev

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

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