Subversion Repositories gelsvn

Rev

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

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