Subversion Repositories gelsvn

Rev

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