~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to cobweb.js/cobweb/Browser/Geometry3D/QuadSphereOptions.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
 
 *******************************************************************************
3
 
 *
4
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 
 *
6
 
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
7
 
 *
8
 
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
9
 
 *
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
13
 
 * create3000.
14
 
 *
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.
18
 
 *
19
 
 * NON-MILITARY USE ONLY
20
 
 *
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.
25
 
 *
26
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27
 
 *
28
 
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
29
 
 *
30
 
 * This file is part of the Cobweb Project.
31
 
 *
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.
35
 
 *
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).
40
 
 *
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.
44
 
 *
45
 
 * For Silvio, Joy and Adi.
46
 
 *
47
 
 ******************************************************************************/
48
 
 
49
 
 
50
 
define ([
51
 
        "jquery",
52
 
        "cobweb/Fields",
53
 
        "cobweb/Basic/X3DBaseNode",
54
 
        "cobweb/Components/Geometry3D/IndexedFaceSet",
55
 
        "cobweb/Components/Rendering/Coordinate",
56
 
        "cobweb/Components/Texturing/TextureCoordinate",
57
 
        "standard/Math/Numbers/Complex",
58
 
        "standard/Math/Numbers/Vector2",
59
 
        "standard/Math/Numbers/Vector3",
60
 
],
61
 
function ($,
62
 
          Fields,
63
 
          X3DBaseNode,
64
 
          IndexedFaceSet,
65
 
          Coordinate,
66
 
          TextureCoordinate,
67
 
          Complex,
68
 
          Vector2,
69
 
          Vector3)
70
 
{
71
 
"use strict";
72
 
        
73
 
        function QuadSphereOptions (executionContext)
74
 
        {
75
 
                X3DBaseNode .call (this, executionContext);
76
 
 
77
 
                this .addChildObjects ("xDimension", new Fields .SFInt32 (32),
78
 
                                       "yDimension", new Fields .SFInt32 (15))
79
 
        }
80
 
 
81
 
        QuadSphereOptions .prototype = $.extend (Object .create (X3DBaseNode .prototype),
82
 
        {
83
 
                constructor: QuadSphereOptions,
84
 
                getTypeName: function ()
85
 
                {
86
 
                        return "QuadSphereOptions";
87
 
                },
88
 
                getComponentName: function ()
89
 
                {
90
 
                        return "Cobweb";
91
 
                },
92
 
                getContainerField: function ()
93
 
                {
94
 
                        return "quadSphereOptions";
95
 
                },
96
 
                initialize: function ()
97
 
                {
98
 
                        X3DBaseNode .prototype .initialize .call (this);
99
 
 
100
 
                        this .addInterest ("eventsProcessed", this);
101
 
                },
102
 
                getGeometry: function ()
103
 
                {
104
 
                        if (! this .geometry)
105
 
                                this .eventsProcessed ();
106
 
                        
107
 
                        return this .geometry;
108
 
                },
109
 
                createTexCoordIndex: function ()
110
 
                {
111
 
                        var
112
 
                                xDimension    = this .xDimension_ .getValue () + 1,
113
 
                                yDimension    = this .yDimension_ .getValue (),
114
 
                                texCoordIndex = this .geometry .texCoordIndex_;
115
 
 
116
 
                        // North pole
117
 
                        
118
 
                        for (var u = 0, uLength = xDimension - 1; u < uLength; ++ u)
119
 
                        {
120
 
                                texCoordIndex .push (u);
121
 
                                texCoordIndex .push (u + xDimension - 1);
122
 
                                texCoordIndex .push (u + xDimension);
123
 
                                texCoordIndex .push (-1);
124
 
                        }
125
 
 
126
 
                        // Sphere segments
127
 
                        
128
 
                        for (var p = xDimension - 1, v = 0, vLength = yDimension - 3; v < vLength; ++ v, ++ p)
129
 
                        {
130
 
                                for (var u = 0, uLength = xDimension - 1; u < uLength; ++ u, ++ p)
131
 
                                {
132
 
                                        texCoordIndex .push (p);
133
 
                                        texCoordIndex .push (p + xDimension);
134
 
                                        texCoordIndex .push (p + xDimension + 1);
135
 
                                        texCoordIndex .push (p + 1);
136
 
                                        texCoordIndex .push (-1);
137
 
                                }
138
 
                        }
139
 
                        
140
 
                        // South pole
141
 
 
142
 
                        var p = (yDimension - 2) * xDimension - 1;
143
 
 
144
 
                        for (var u = 0, uLength = xDimension - 1; u < uLength; ++ u, ++ p)
145
 
                        {
146
 
                                texCoordIndex .push (p + xDimension);
147
 
                                texCoordIndex .push (p + 1);
148
 
                                texCoordIndex .push (p);
149
 
                                texCoordIndex .push (-1)
150
 
                        }
151
 
                },
152
 
                createTexCoord: function ()
153
 
                {
154
 
                        var
155
 
                                xDimension = this .xDimension_ .getValue () + 1,
156
 
                                yDimension = this .yDimension_ .getValue (),
157
 
                                point      = this .geometry .texCoord_ .getValue () .point_;
158
 
 
159
 
                                var poleOffset = -0.5 / (xDimension - 1);
160
 
 
161
 
                                // North pole
162
 
                                
163
 
                                for (var u = 1; u < xDimension; ++ u)
164
 
                                {
165
 
                                        var x = u / (xDimension - 1) + poleOffset;
166
 
                                        
167
 
                                        point .push (new Vector2 (x, 1));
168
 
                                }
169
 
 
170
 
                                // Sphere segments
171
 
                                
172
 
                                for (var v = 1, vLength = yDimension - 1; v < vLength; ++ v)
173
 
                                {
174
 
                                        var y = 1 - v / (yDimension - 1);
175
 
                                        
176
 
                                        for (var u = 0; u < xDimension; ++ u)
177
 
                                        {
178
 
                                                var x = u / (xDimension - 1);
179
 
                                                
180
 
                                                point .push (new Vector2 (x, y));
181
 
                                        }
182
 
                                }
183
 
 
184
 
                                // South pole
185
 
                                
186
 
                                for (var u = 1; u < xDimension; ++ u)
187
 
                                {
188
 
                                        var x = u / (xDimension - 1) + poleOffset;
189
 
                                        
190
 
                                        point .push (new Vector2 (x, 0));
191
 
                                }
192
 
                },
193
 
                createCoordIndex: function ()
194
 
                {
195
 
                        var
196
 
                                xDimension = this .xDimension_ .getValue () + 1,
197
 
                                yDimension = this .yDimension_ .getValue (),
198
 
                                coordIndex = this .geometry .coordIndex_;
199
 
 
200
 
                        // North pole
201
 
                        
202
 
                        for (var u = 1, uLength = xDimension - 1; u < uLength; ++ u)
203
 
                        {
204
 
                                coordIndex .push (0);
205
 
                                coordIndex .push (u);
206
 
                                coordIndex .push (u + 1);
207
 
                                coordIndex .push (-1);
208
 
                        }
209
 
 
210
 
                        coordIndex .push (0);
211
 
                        coordIndex .push (u);
212
 
                        coordIndex .push (1);
213
 
                        coordIndex .push (-1);
214
 
 
215
 
                        // Sphere segments
216
 
                        
217
 
                        var p = 1;
218
 
 
219
 
                        for (var v = 0, vLength = yDimension - 3; v < vLength; ++ v, ++ p)
220
 
                        {
221
 
                                for (var u = 0, uLength = xDimension - 2; u < uLength; ++ u, ++ p)
222
 
                                {
223
 
                                        coordIndex .push (p);
224
 
                                        coordIndex .push (p + xDimension - 1);
225
 
                                        coordIndex .push (p + xDimension);
226
 
                                        coordIndex .push (p + 1);
227
 
                                        coordIndex .push (-1);
228
 
                                }
229
 
 
230
 
                                coordIndex .push (p);
231
 
                                coordIndex .push (p + xDimension - 1);
232
 
                                coordIndex .push (p + 1);
233
 
                                coordIndex .push (p - xDimension + 2);
234
 
                                coordIndex .push (-1);
235
 
                        }
236
 
 
237
 
                        // South pole
238
 
                        
239
 
                        var last = p + xDimension - 1;
240
 
 
241
 
                        for (var u = 0, uLength = xDimension - 2; u < uLength; ++ u, ++ p)
242
 
                        {
243
 
                                coordIndex .push (last);
244
 
                                coordIndex .push (p + 1);
245
 
                                coordIndex .push (p);
246
 
                                coordIndex .push (-1);
247
 
                        }
248
 
 
249
 
                        coordIndex .push (last);
250
 
                        coordIndex .push (last - xDimension + 1);
251
 
                        coordIndex .push (p);
252
 
                        coordIndex .push (-1);
253
 
                },
254
 
                createPoints: function ()
255
 
                {
256
 
                        var
257
 
                                xDimension = this .xDimension_ .getValue () + 1,
258
 
                                yDimension = this .yDimension_ .getValue (),
259
 
                                point      = this .geometry .coord_ .getValue () .point_;
260
 
 
261
 
                        // North pole
262
 
                        point .push (new Vector3 (0, 1, 0));
263
 
 
264
 
                        // Sphere segments
265
 
                        for (var v = 1, vLength = yDimension - 1; v < vLength; ++ v)
266
 
                        {
267
 
                                var zPlane = Complex .Polar (1, -Math .PI * v / vLength);
268
 
 
269
 
                                for (var u = 0, uLength = xDimension - 1; u < uLength; ++ u)
270
 
                                {
271
 
                                        var yPlane = Complex .Polar (zPlane .imag, 2 * Math .PI * u / uLength);
272
 
 
273
 
                                        point .push (new Vector3 (yPlane .imag, zPlane .real, yPlane .real));
274
 
                                }
275
 
                        }
276
 
 
277
 
                        // South pole
278
 
                        point .push (new Vector3 (0, -1, 0));
279
 
                },
280
 
                eventsProcessed: function ()
281
 
                {
282
 
                        this .geometry            = new IndexedFaceSet (this .getExecutionContext ());
283
 
                        this .geometry .texCoord_ = new TextureCoordinate (this .getExecutionContext ());
284
 
                        this .geometry .coord_    = new Coordinate (this .getExecutionContext ());
285
 
 
286
 
                        this .createTexCoordIndex ();
287
 
                        this .createTexCoord ();
288
 
                        this .createCoordIndex ();
289
 
                        this .createPoints ();
290
 
 
291
 
                        var
292
 
                                geometry = this .geometry,
293
 
                                texCoord = this .geometry .texCoord_ .getValue (),
294
 
                                coord    = this .geometry .coord_ .getValue ();
295
 
 
296
 
                        geometry .creaseAngle_ = Math .PI;
297
 
 
298
 
                        texCoord .setup ();
299
 
                        coord    .setup ();
300
 
                        geometry .setup ();
301
 
                },
302
 
        });
303
 
 
304
 
        return QuadSphereOptions;
305
 
});