-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsun.js
More file actions
267 lines (199 loc) · 9.08 KB
/
Copy pathsun.js
File metadata and controls
267 lines (199 loc) · 9.08 KB
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
261
262
263
264
265
266
267
"use strict"
/* Copyright (c) 2014, Robert Buchholz <rbuch703@gmail.com>
The contents of this file are licensed under the GNU General Public License version 3
(see the LICENSE file in the project root for details)
*/
/**
* @constructor
*/
function Sun(position) {
this.lat = position.lat;
this.lng = position.lng;
this.dayOfYear = 229;
this.time = 12; //noon;
this.buildGlGeometry();
}
Sun.prototype.setMomentInTime = function(day, time)
{
if (day)
this.dayOfYear = day;
if (time)
this.time = time;
if (day || time)
this.buildGlGeometry();
}
// source of computation: http://www.pveducation.org/pvcdrom/properties-of-sunlight/suns-position
Sun.getAngles = function(lat, lng, dayOfYear, timeOfDay)
{
var dtGmt = 1; //usually one hour time difference to GMT
// day-of-the-year at which summer time begins. Technically, European summer time starts on the last Sunday in March. But since we do not want the user to have to enter a year, we'll just use the end of March as an approximation
var summerTimeBegins = 31+28+31;
// day-of-the-yeat at which summer time ends. We use the last of October as an approximation
var summerTimeEnds = 365 - 31 - 30 - 31;
if (dayOfYear > summerTimeBegins && dayOfYear < summerTimeEnds)
dtGmt = 2;
var LSTM = 15 * dtGmt; //Local Standard Time Meridian
var B = (dayOfYear - 81) / 365 * 2 * Math.PI;
var EoT = 9.87 * Math.sin(2*B) - 7.53*Math.cos(B) - 1.5*Math.sin(B); //Equation of Time;
//console.log("EoT: %s", EoT);
var TC = 4 * (lng - LSTM) + EoT; // Time Correction Factor
var LST = timeOfDay + TC/60; //Local Solar Time
var HRA = (15 * (LST - 12)) / 180 * Math.PI; // Hour Angle in radiants
var delta = (23.45 * Math.sin(B)) / 180 * Math.PI; // declination in radiants
//console.log("Declination: %s", delta/Math.PI*180);
var phi = lat / 180 * Math.PI; // latitude in radiants
var elevation = Math.asin( Math.sin(delta) * Math.sin(phi) +
Math.cos(delta) * Math.cos(phi) * Math.cos(HRA));
var azimuth = Math.acos( (Math.sin(delta) * Math.cos(phi) -
Math.cos(delta) * Math.sin(phi) * Math.cos(HRA)) /
Math.cos(elevation));
if (HRA > 0) azimuth = 2 * Math.PI - azimuth;
return {"elevation": elevation, "azimuth": azimuth};
}
Sun.prototype.getAngles = function() {
return Sun.getAngles( this.lat, this.lng, this.dayOfYear, this.time );
}
Sun.getPosition = function(azimuth, elevation, radius) {
if (radius === undefined)
radius = SkyDome.RADIUS;
//var RADIUS = 00; //Skybox radius (on which the sun is pinned)
return [ radius * Math.sin(azimuth) * Math.cos(elevation),
-radius * Math.cos(azimuth) * Math.cos(elevation),
radius * Math.sin(elevation)];
}
Sun.getSunriseTime = function(lat, lng, dayOfYear)
{
var hi = 12.0; //noon;
var lo = 0.0; //midnight;
// check for polar day/night (sun is always above/below the horizon) where sunrise/sunset have no meaning.
// Note that the computation here is not mathematically correct (noon and midnight need not be the points of highest/lowest
// elevation, and thus sun may rise/set even though it has not risen till noon/set till midnight),
// but it works for the latitude range we are interested in.
if (Sun.getAngles(lat, lng, dayOfYear, hi).elevation < 0 || Sun.getAngles(lat, lng, dayOfYear, lo).elevation > 0)
return null;
for (var i = 0; i < 10; i++)
{
var mid = (hi +lo) / 2.0;
if ( Sun.getAngles(lat, lng, dayOfYear, mid).elevation < 0)
lo = mid;
else
hi = mid;
}
return (hi + lo) / 2.0;
}
Sun.prototype.getSunriseTime = function()
{
return Sun.getSunriseTime(this.lat, this.lng, this.dayOfYear);
}
Sun.getSunsetTime = function(lat, lng, dayOfYear)
{
var lo = 12.0; //noon;
var hi = 24.0; //midnight;
// check for polar day/night (sun is always above/below the horizon) where sunrise/sunset have no meaning.
// Note that the computation here is not mathematically correct (noon and midnight need not be the points of highest/lowest
// elevation, and thus sun may rise/set even though it has not risen till noon/set till midnight),
// but it works for the latitude range we are interested in.
if (Sun.getAngles(lat, lng, dayOfYear, hi).elevation > 0 || Sun.getAngles(lat, lng, dayOfYear, lo).elevation < 0)
return null;
for (var i = 0; i < 10; i++)
{
var mid = (hi +lo) / 2.0;
if ( Sun.getAngles(lat, lng, dayOfYear, mid).elevation < 0)
hi = mid;
else
lo = mid;
}
return (hi + lo) / 2.0;
}
Sun.prototype.getSunsetTime = function()
{
return Sun.getSunsetTime(this.lat, this.lng, this.dayOfYear);
}
Sun.prototype.getPosition = function() {
var angles = this.getAngles();
return Sun.getPosition(angles.azimuth, angles.elevation, SkyDome.RADIUS - 100);
}
Sun.prototype.buildOrbitGlGeometry = function() {
if (this.orbitVertices)
gl.deleteBuffer(this.orbitVertices);
var vertices = [];
for (var t = 0; t <= 24; t+=0.1)
{
var angles = Sun.getAngles(this.lat, this.lng, this.dayOfYear, t);
var pos = Sun.getPosition( angles.azimuth, angles.elevation, SkyDome.RADIUS - 100);
[].push.apply(vertices, pos);
}
this.numOrbitVertices = vertices.length / 3 | 0;
//console.log(vertices);
this.orbitVertices = glu.createArrayBuffer(vertices);
}
Sun.prototype.buildGlGeometry = function() {
if (this.vertices)
gl.deleteBuffer(this.vertices);
var shift = this.getPosition();
//console.log(shift);
var vertices= [];
var base = [];
var top = [];
var NUM_H_SLICES = 10;
var NUM_V_SLICES = 10;
for (var i = 0; i < NUM_H_SLICES; i++)
{
var azimuth1 = i / NUM_H_SLICES * 2 * Math.PI; //convert to radiants in [0...2*PI]
var x1 = Math.cos(azimuth1) * Sun.RADIUS;
var y1 = Math.sin(azimuth1) * Sun.RADIUS;
var azimuth2 = (i+1) / NUM_H_SLICES * 2 * Math.PI;
var x2 = Math.cos(azimuth2) * Sun.RADIUS;
var y2 = Math.sin(azimuth2) * Sun.RADIUS;
for (var j = 0; j+1 <= NUM_V_SLICES; j++)
{
var polar1 = j * Math.PI / (2.0 * NUM_V_SLICES); //convert to radiants in [0..1/2*PI]
var polar2 = (j+1) * Math.PI / (2.0 * NUM_V_SLICES);
var A = [x1 * Math.cos(polar1), y1 * Math.cos(polar1), Sun.RADIUS * Math.sin(polar1)];
var B = [x2 * Math.cos(polar1), y2 * Math.cos(polar1), Sun.RADIUS * Math.sin(polar1)];
var C = [x2 * Math.cos(polar2), y2 * Math.cos(polar2), Sun.RADIUS * Math.sin(polar2)];
var D = [x1 * Math.cos(polar2), y1 * Math.cos(polar2), Sun.RADIUS * Math.sin(polar2)];
var verts = [].concat(A, C, B, A, D, C);
vertices.push.apply( vertices, verts);
A[2] = -A[2];
B[2] = -B[2];
C[2] = -C[2];
D[2] = -D[2];
verts = [].concat(A, B, C, A, C, D);
//var verts = [].concat(A, C, B, A, D, C);
vertices.push.apply( vertices, verts);
}
}
for (var i = 0; i < vertices.length; i+=3)
{
vertices[i ] += shift[0];
vertices[i+1] += shift[1];
vertices[i+2] += shift[2];
}
this.numVertices = vertices.length / 3;
this.vertices = glu.createArrayBuffer(vertices);
//this.texCoords= glu.createArrayBuffer(this.texCoords);
this.buildOrbitGlGeometry();
}
Sun.prototype.render = function(modelViewMatrix, projectionMatrix) {
if (! Shaders.ready)
return;
gl.useProgram( Shaders.flat ); // Install the program as part of the current rendering state
glu.enableVertexAttribArrays(Shaders.flat);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertices); //select the vertex buffer as the currrently active ARRAY_BUFFER (for subsequent calls)
gl.vertexAttribPointer(Shaders.flat.locations["vertexPosition"], 3, gl.FLOAT, false, 0, 0); //assigns array "vertices" bound above as the vertex attribute "vertexPosition"
var mvpMatrix = mat4.create();
mat4.mul(mvpMatrix, projectionMatrix, modelViewMatrix);
gl.uniformMatrix4fv(Shaders.flat.locations["modelViewProjectionMatrix"], false, mvpMatrix);
gl.uniform4fv( Shaders.flat.locations["color"], [1.0, 1.0, 0.90, 1.0]);
gl.drawArrays(gl.TRIANGLES, 0, this.numVertices);
//render orbit
gl.useProgram(Shaders.flat); // Install the program as part of the current rendering state
gl.bindBuffer(gl.ARRAY_BUFFER, this.orbitVertices); //select the vertex buffer as the currrently active ARRAY_BUFFER (for subsequent calls)
gl.vertexAttribPointer(Shaders.flat.locations["vertexPosition"], 3, gl.FLOAT, false, 0, 0); //assigns array "vertices" bound above as the vertex attribute "vertexPosition"
gl.uniformMatrix4fv(Shaders.flat.locations["modelViewProjectionMatrix"], false, mvpMatrix);
gl.uniform4fv( Shaders.flat.locations["color"], [0.6, 0.2, 0.2, 1.0]);
gl.drawArrays(gl.LINES, 0, this.numOrbitVertices);
glu.disableVertexAttribArrays(Shaders.flat);
}
Sun.RADIUS = 100;