1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
10
* The copyright notice above does not evidence any actual of intended
11
* publication of such source code, and is an unpublished work by create3000.
12
* This material contains CONFIDENTIAL INFORMATION that is the property of
15
* No permission is granted to copy, distribute, or create derivative works from
16
* the contents of this software, in whole or in part, without the prior written
17
* permission of create3000.
19
* NON-MILITARY USE ONLY
21
* All create3000 software are effectively free software with a non-military use
22
* restriction. It is free. Well commented source is provided. You may reuse the
23
* source in any way you please with the exception anything that uses it must be
24
* marked to indicate is contains 'non-military use only' components.
26
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
28
* Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
30
* This file is part of the Cobweb Project.
32
* Cobweb is free software: you can redistribute it and/or modify it under the
33
* terms of the GNU General Public License version 3 only, as published by the
34
* Free Software Foundation.
36
* Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
37
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
38
* A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
39
* details (a copy is included in the LICENSE file that accompanied this code).
41
* You should have received a copy of the GNU General Public License version 3
42
* along with Cobweb. If not, see <http://www.gnu.org/licenses/gpl.html> for a
43
* copy of the GPLv3 License.
45
* For Silvio, Joy and Adi.
47
******************************************************************************/
52
"standard/Math/Geometry/Line3",
53
"standard/Math/Geometry/Plane3",
54
"standard/Math/Geometry/Triangle3",
55
"standard/Math/Numbers/Vector3",
56
"standard/Math/Numbers/Vector4",
57
"standard/Math/Numbers/Matrix4",
59
function ($, Line3, Plane3, Triangle3, Vector3, Vector4, Matrix4)
64
p1 = new Vector3 (0, 0, 0),
65
p2 = new Vector3 (0, 0, 0),
66
p3 = new Vector3 (0, 0, 0),
67
p4 = new Vector3 (0, 0, 0),
68
p5 = new Vector3 (0, 0, 0),
69
p6 = new Vector3 (0, 0, 0),
70
near = new Vector3 (0, 0, 0),
71
far = new Vector3 (0, 0, 0),
72
matrix = new Matrix4 (),
73
normal = new Vector3 (0, 0, 0),
74
vin = new Vector4 (0, 0, 0, 0);
76
function ViewVolume ()
78
this .viewport = new Vector4 (0, 0, 0, 0);
79
this .scissor = new Vector4 (0, 0, 0, 0);
82
new Plane3 (Vector3 .Zero, Vector3 .Zero), // front
83
new Plane3 (Vector3 .Zero, Vector3 .Zero), // left
84
new Plane3 (Vector3 .Zero, Vector3 .Zero), // right
85
new Plane3 (Vector3 .Zero, Vector3 .Zero), // top
86
new Plane3 (Vector3 .Zero, Vector3 .Zero), // bottom
87
new Plane3 (Vector3 .Zero, Vector3 .Zero), // back
91
ViewVolume .prototype =
93
constructor: ViewVolume,
94
set: function (projectionMatrix, viewport, scissor)
98
this .viewport .assign (viewport);
99
this .scissor .assign (scissor);
103
x2 = scissor [0] + scissor [2],
105
y2 = scissor [1] + scissor [3];
107
matrix .assign (projectionMatrix) .inverse ();
109
ViewVolume .unProjectPointMatrix (x1, y2, 1, matrix, viewport, p1),
110
ViewVolume .unProjectPointMatrix (x1, y1, 1, matrix, viewport, p2),
111
ViewVolume .unProjectPointMatrix (x1, y1, 0, matrix, viewport, p3),
112
ViewVolume .unProjectPointMatrix (x2, y1, 0, matrix, viewport, p4),
113
ViewVolume .unProjectPointMatrix (x2, y2, 0, matrix, viewport, p5),
114
ViewVolume .unProjectPointMatrix (x2, y2, 1, matrix, viewport, p6);
116
this .planes [0] .set (p4, Triangle3 .normal (p3, p4, p5, normal)); // front
117
this .planes [1] .set (p2, Triangle3 .normal (p1, p2, p3, normal)); // left
118
this .planes [2] .set (p5, Triangle3 .normal (p6, p5, p4, normal)); // right
119
this .planes [3] .set (p6, Triangle3 .normal (p5, p6, p1, normal)); // top
120
this .planes [4] .set (p3, Triangle3 .normal (p4, p3, p2, normal)); // bottom
121
this .planes [5] .set (p1, Triangle3 .normal (p2, p1, p6, normal)); // back
128
console .log (error);
133
getViewport: function ()
135
return this .viewport;
137
getScissor: function ()
139
return this .scissor;
141
intersectsSphere: function (radius, center)
143
var planes = this .planes;
145
if (planes [0] .getDistanceToPoint (center) > radius)
148
if (planes [1] .getDistanceToPoint (center) > radius)
151
if (planes [2] .getDistanceToPoint (center) > radius)
154
if (planes [3] .getDistanceToPoint (center) > radius)
157
if (planes [4] .getDistanceToPoint (center) > radius)
160
if (planes [5] .getDistanceToPoint (center) > radius)
167
$.extend (ViewVolume,
169
unProjectPoint: function (winx, winy, winz, modelViewMatrix, projectionMatrix, viewport, point)
171
matrix .assign (modelViewMatrix) .multRight (projectionMatrix) .inverse ();
173
return this .unProjectPointMatrix (winx, winy, winz, matrix, viewport, point);
175
unProjectPointMatrix: function (winx, winy, winz, invModelViewProjection, viewport, point)
177
// Transformation of normalized coordinates between -1 and 1
178
vin .set ((winx - viewport [0]) / viewport [2] * 2 - 1,
179
(winy - viewport [1]) / viewport [3] * 2 - 1,
183
//Objects coordinates
184
invModelViewProjection .multVecMatrix (vin);
187
throw new Error ("Couldn't unproject point: divisor is 0.");
191
return point .set (vin .x * d, vin .y * d, vin .z * d);
193
unProjectRay: function (winx, winy, modelViewMatrix, projectionMatrix, viewport, result)
195
matrix .assign (modelViewMatrix) .multRight (projectionMatrix) .inverse ();
197
ViewVolume .unProjectPointMatrix (winx, winy, 0.0, matrix, viewport, near);
198
ViewVolume .unProjectPointMatrix (winx, winy, 0.9, matrix, viewport, far);
200
return result .setPoints (near, far);
202
projectPoint: function (point, modelViewMatrix, projectionMatrix, viewport, vout)
204
vin .set (point .x, point .y, point .z, 1);
206
projectionMatrix .multVecMatrix (modelViewMatrix .multVecMatrix (vin));
209
throw new Error ("Couldn't project point: divisor is 0.");
211
var d = 1 / (2 * vin .w);
213
return vout .set ((vin .x * d + 0.5) * viewport [2] + viewport [0],
214
(vin .y * d + 0.5) * viewport [3] + viewport [1],
217
projectLine: function (line, modelViewMatrix, projectionMatrix, viewport, result)
219
ViewVolume .projectPoint (line .point, modelViewMatrix, projectionMatrix, viewport, near);
220
ViewVolume .projectPoint (Vector3 .multiply (line .direction, 1e9) .add (line .point), modelViewMatrix, projectionMatrix, viewport, far);
225
return result .setPoints (near, far);