Subversion Repositories gelsvn

Rev

Rev 125 | Rev 195 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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