File: viewpoint.cpp

package info (click to toggle)
clanlib 1.0~svn3827-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 24,600 kB
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (260 lines) | stat: -rw-r--r-- 7,597 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
**  ClanLib SDK
**  Copyright (c) 1997-2005 The ClanLib Team
**
**  This software is provided 'as-is', without any express or implied
**  warranty.  In no event will the authors be held liable for any damages
**  arising from the use of this software.
**
**  Permission is granted to anyone to use this software for any purpose,
**  including commercial applications, and to alter it and redistribute it
**  freely, subject to the following restrictions:
**
**  1. The origin of this software must not be misrepresented; you must not
**     claim that you wrote the original software. If you use this software
**     in a product, an acknowledgment in the product documentation would be
**     appreciated but is not required.
**  2. Altered source versions must be plainly marked as such, and must not be
**     misrepresented as being the original software.
**  3. This notice may not be removed or altered from any source distribution.
**
**  Note: Some of the libraries ClanLib may link to may have additional
**  requirements or restrictions.
**
**  File Author(s):
**
**    Magnus Norddahl
**    (if your name is missing here, please add it)
*/

#include "Display/display_precomp.h"
#include "API/GL/viewpoint.h"
#include "API/GL/opengl_wrap.h"
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif
#include <math.h>

/////////////////////////////////////////////////////////////////////////////
// CL_Viewpoint construction:

CL_Viewpoint::CL_Viewpoint() : pos(0,0,0), dir(0,0,1), up(0,1,0)
{
}

CL_Viewpoint::CL_Viewpoint(
	const CL_Vector &_pos,
	const CL_Vector &_dir,
	const CL_Vector &_up) : pos(_pos), dir(_dir), up(_up)
{
	dir.normalize();
	up.normalize();
}

/////////////////////////////////////////////////////////////////////////////
// CL_Viewpoint attributes:

/////////////////////////////////////////////////////////////////////////////
// CL_Viewpoint operations:

void CL_Viewpoint::translate(float mov_x, float mov_y, float mov_z)
{
	pos.x += mov_x;
	pos.y += mov_y;
	pos.z += mov_z;
}

void CL_Viewpoint::set(float pos_x, float pos_y, float pos_z)
{
	pos.x = pos_x;
	pos.y = pos_y;
	pos.z = pos_z;
}

void CL_Viewpoint::translate_local(float mov_x, float mov_y, float mov_z)
{
	CL_Vector res = dir.cross(up);
	res.normalize();

	float delta_x = res.x*mov_x + up.x*mov_y + dir.x*mov_z;
	float delta_y = res.y*mov_x + up.y*mov_y + dir.y*mov_z;
	float delta_z = res.z*mov_x + up.z*mov_y + dir.z*mov_z;

	pos = pos+CL_Vector(delta_x, delta_y, delta_z, 0.0f);
}

void CL_Viewpoint::rotate_local(double angle, double x, double y, double z)
{
/*
	DESCRIPTION
	glRotate produces a rotation of angle degrees around the
	vector (x,y,z). The current matrix (see glMatrixMode) is
	multiplied by a rotation matrix with the product replacing
	the current matrix, as if glMultMatrix were called with the
	following matrix as its argument:

		( xx(1-c)+c    xy(1-c)-zs    xz(1-c)+ys   0  )
		|                                            |
		| yx(1-c)+zs   yy(1-c)+c     yz(1-c)-xs	  0  |
		| xz(1-c)-ys   yz(1-c)+xs    zz(1-c)+c	  0  |
		|                                            |
		(     0            0             0        1  )

	Where c = cos(angle), s = sine(angle), and ||( x,y,z )|| = 1
	(if not, the GL will normalize this vector).
*/
	CLdouble length = sqrt(x*x+y*y+z*z);
	x /= length;
	y /= length;
	z /= length;

	CLdouble c = cos(angle*3.14/180);
	CLdouble s = sin(angle*3.14/180);

	CLdouble matrix[4*4] =
	{
		x*x*(1-c)+c, x*y*(1-c)-z*s, x*z*(1-c)+y*s, 0.0f,
		y*x*(1-c)+z*s, y*y*(1-c)+c, y*z*(1-c)-x*s, 0.0f,
		x*z*(1-c)-y*s, y*z*(1-c)+x*s, z*z*(1-c)+c, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f
	};

	// Rotate local dir vector:
	CLdouble matrix_dir[4*1] = { 0.0, 0.0, 1.0, 1.0 };
	CLdouble matrix_dir_result[4*1] =
	{
		matrix[0]*matrix_dir[0] + matrix[4]*matrix_dir[1] + matrix[8]*matrix_dir[2] + matrix[12]*matrix_dir[3],
		matrix[1]*matrix_dir[1] + matrix[5]*matrix_dir[1] + matrix[9]*matrix_dir[2] + matrix[13]*matrix_dir[3],
		matrix[2]*matrix_dir[1] + matrix[6]*matrix_dir[1] + matrix[10]*matrix_dir[2] + matrix[14]*matrix_dir[3],
		matrix[3]*matrix_dir[1] + matrix[7]*matrix_dir[1] + matrix[11]*matrix_dir[2] + matrix[15]*matrix_dir[3]
	};

	// Rotate local up vector:
	CLdouble matrix_up[4*1] = { 0.0, 1.0, 0.0, 1.0 };
	CLdouble matrix_up_result[4*1] =
	{
		matrix[0]*matrix_up[0] + matrix[4]*matrix_up[1] + matrix[8]*matrix_up[2] + matrix[12]*matrix_up[3],
		matrix[1]*matrix_up[1] + matrix[5]*matrix_up[1] + matrix[9]*matrix_up[2] + matrix[13]*matrix_up[3],
		matrix[2]*matrix_up[1] + matrix[6]*matrix_up[1] + matrix[10]*matrix_up[2] + matrix[14]*matrix_up[3],
		matrix[3]*matrix_up[1] + matrix[7]*matrix_up[1] + matrix[11]*matrix_up[2] + matrix[15]*matrix_up[3]
	};

	CL_Vector sideways = dir.cross(up);
	sideways.normalize();

	// Convert local vectors to world vectors:
	CL_Vector new_up(
		(float) (matrix_up_result[0]*sideways.x + matrix_up_result[1]*up.x + matrix_up_result[2]*dir.x),
		(float) (matrix_up_result[0]*sideways.y + matrix_up_result[1]*up.y + matrix_up_result[2]*dir.y),
		(float) (matrix_up_result[0]*sideways.z + matrix_up_result[1]*up.z + matrix_up_result[2]*dir.z));

	CL_Vector new_dir(
		(float) (matrix_dir_result[0]*sideways.x + matrix_dir_result[1]*up.x + matrix_dir_result[2]*dir.x),
		(float) (matrix_dir_result[0]*sideways.y + matrix_dir_result[1]*up.y + matrix_dir_result[2]*dir.y),
		(float) (matrix_dir_result[0]*sideways.z + matrix_dir_result[1]*up.z + matrix_dir_result[2]*dir.z));

	new_up.normalize();
	new_dir.normalize();

	up = new_up;
	dir = new_dir;
}

void CL_Viewpoint::enter_local() const
{
	// Move local model coordinates into place in the world:
	clMatrixMode(CL_MODELVIEW);
	clPushMatrix();

	CL_Vector f = -dir;
	CL_Vector up2 = -up;
	f.normalize();
	up2.normalize();
	CL_Vector s = f.cross(up2);
	CL_Vector u = s.cross(f);

	// Inverted gluLookAt coordinate switch matrix:
	CLfloat matrix[4*4] =
	{
		 s.x,  s.y,  s.z,  0.0f,
		 u.x,  u.y,  u.z,  0.0f,
		-f.x, -f.y, -f.z,  0.0f,
		 0.0f, 0.0f, 0.0f, 1.0f
	};

	clTranslatef(pos.x, pos.y, pos.z);
	clMultMatrixf(matrix);
}

void CL_Viewpoint::leave_local() const
{
	// Restore model view matrix:
	clMatrixMode(CL_MODELVIEW);
	clPopMatrix();
/*
	// Render direction arrow for debugging:
	clDisable(CL_DEPTH_TEST);
	clDisable(CL_TEXTURE_2D);
	clBegin(CL_LINE_STRIP);
	clColor3f(1.0f,1.0f,1.0f);
	clVertex3f(pos.x+up.x, pos.y+up.y, pos.z+up.z);
	clVertex3f(pos.x+dir.x, pos.y+dir.y, pos.z+dir.z);
	clVertex3f(pos.x-up.x, pos.y-up.y, pos.z-up.z);
	clEnd();
	clEnable(CL_TEXTURE_2D);
	clEnable(CL_DEPTH_TEST);
*/
}

CL_Vector CL_Viewpoint::make_local_direction(const CL_Vector &v) const
{
	CLfloat matrix_v[4] = { v.x, v.y, v.z, v.w };

	CL_Vector f = -dir;
	CL_Vector up2 = -up;
	f.normalize();
	up2.normalize();
	CL_Vector s = f.cross(up2);
	CL_Vector u = s.cross(f);

	// gluLookAt coordinate switch matrix:
	CLfloat matrix[4*4] =
	{
		s.x, u.x, -f.x, 0.0f,
		s.y, u.y, -f.y, 0.0f,
		s.z, u.z, -f.z, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f
	};

	CLfloat matrix_lv[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
	for (int r=0; r<4; r++)
	{
		for (int c=0; c<4; c++)
		{
			matrix_lv[r] += matrix[c*4+r]*matrix_v[c];
		}
	}

	return CL_Vector(matrix_lv[0], matrix_lv[1], matrix_lv[2], matrix_lv[3]);
}

void CL_Viewpoint::setup_modelview() const
{
	clMatrixMode(CL_MODELVIEW);
	clLoadIdentity();			
	gluLookAt(
		pos.x,          // position
		pos.y,
		pos.z,
		pos.x+dir.x,    // center
		pos.y+dir.y,
		pos.z+dir.z,
		up.x,           // up
		up.y,
		up.z);
}

/////////////////////////////////////////////////////////////////////////////
// CL_Viewpoint implementation