-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere_textures_class.js
221 lines (175 loc) · 8.08 KB
/
sphere_textures_class.js
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
const SPHERE_LATS=20;
const SPHERE_LONS=30;
class Sphere
{
constructor(gl, nlat, nlon)
{
this.gl = gl
this.sphere_points = [];
this.sphere_normals = [];
this.sphere_faces = [];
this.sphere_edges = [];
this.sphere_texCoords = [];
this.sphere_points_buffer = undefined;
this.sphere_normals_buffer = undefined;
this.sphere_faces_buffer = undefined;
this.sphere_edges_buffer = undefined;
this.sphere_texCoords_buffer = undefined;
nlat = nlat | SPHERE_LATS;
nlon = nlon | SPHERE_LONS;
this.sphereBuild(nlat, nlon);
this.sphereUploadData();
}
// Generate points using polar coordinates
sphereBuild(nlat, nlon)
{
// phi will be latitude
// theta will be longitude
var d_phi = Math.PI / (nlat+1);
var d_theta = 2*Math.PI / nlon;
var r = 0.5;
// Generate north polar cap
var north = vec3(0,r,0);
this.sphere_points.push(north);
this.sphere_normals.push(vec3(0,1,0));
this.sphere_texCoords.push(vec2(1.0, 0.5));
// Generate middle
for(var i=0, phi=Math.PI/2-d_phi; i<=nlat; i++, phi-=d_phi) {
for(var j=0, theta=0; j<=nlon; j++, theta+=d_theta) {
var pt = vec3(r*Math.cos(phi)*Math.cos(theta),r*Math.sin(phi),r*Math.cos(phi)*Math.sin(theta));
this.sphere_points.push(pt);
var n = vec3(pt);
this.sphere_normals.push(normalize(n));
this.sphere_texCoords.push(vec2(1-j/nlon, i/nlat));
}
}
// Generate norh south cap
var south = vec3(0,-r,0);
this.sphere_points.push(south);
this.sphere_normals.push(vec3(0,-1,0));
this.sphere_texCoords.push(vec2(0.5, 0.0));
// Generate the faces
// north pole faces
for(var i=0; i<nlon-1; i++) {
this.sphere_faces.push(0);
this.sphere_faces.push(i+2);
this.sphere_faces.push(i+1);
}
this.sphere_faces.push(0);
this.sphere_faces.push(1);
this.sphere_faces.push(nlon);
// general middle faces
var offset=1;
for(var i=0; i<nlat-1; i++) {
for(var j=0; j<nlon-1; j++) {
var p = offset+i*nlon+j;
this.sphere_faces.push(p);
this.sphere_faces.push(p+nlon+1);
this.sphere_faces.push(p+nlon);
this.sphere_faces.push(p);
this.sphere_faces.push(p+1);
this.sphere_faces.push(p+nlon+1);
}
var p = offset+i*nlon+nlon-1;
this.sphere_faces.push(p);
this.sphere_faces.push(p+1);
this.sphere_faces.push(p+nlon);
this.sphere_faces.push(p);
this.sphere_faces.push(p-nlon+1);
this.sphere_faces.push(p+1);
}
// south pole faces
var offset = 1 + (nlat-1) * nlon;
for(var j=0; j<nlon-1; j++) {
this.sphere_faces.push(offset+nlon);
this.sphere_faces.push(offset+j);
this.sphere_faces.push(offset+j+1);
}
this.sphere_faces.push(offset+nlon);
this.sphere_faces.push(offset+nlon-1);
this.sphere_faces.push(offset);
// Build the edges
for(var i=0; i<nlon; i++) {
this.sphere_edges.push(0); // North pole
this.sphere_edges.push(i+1);
}
for(var i=0; i<nlat; i++, p++) {
for(var j=0; j<nlon;j++, p++) {
var p = 1 + i*nlon + j;
this.sphere_edges.push(p); // horizontal line (same latitude)
if(j!=nlon-1)
this.sphere_edges.push(p+1);
else this.sphere_edges.push(p+1-nlon);
if(i!=nlat-1) {
this.sphere_edges.push(p); // vertical line (same longitude)
this.sphere_edges.push(p+nlon);
}
else {
this.sphere_edges.push(p);
this.sphere_edges.push(this.sphere_points.length-1);
}
}
}
}
sphereUploadData()
{
this.sphere_points_buffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_points_buffer);
this.gl.bufferData(this.gl.ARRAY_BUFFER, flatten(this.sphere_points), this.gl.STATIC_DRAW);
this.sphere_normals_buffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_normals_buffer);
this.gl.bufferData(this.gl.ARRAY_BUFFER, flatten(this.sphere_normals), this.gl.STATIC_DRAW);
this.sphere_faces_buffer = gl.createBuffer();
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.sphere_faces_buffer);
this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.sphere_faces), this.gl.STATIC_DRAW);
this.sphere_edges_buffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.sphere_edges_buffer);
this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.sphere_edges), this.gl.STATIC_DRAW);
this.sphere_texCoords_buffer = this.gl.createBuffer();
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_texCoords_buffer);
this.gl.bufferData(this.gl.ARRAY_BUFFER, flatten(this.sphere_texCoords), this.gl.STATIC_DRAW);
}
sphereDrawWireFrame(program)
{
this.gl.useProgram(program);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_points_buffer);
var vPosition = this.gl.getAttribLocation(program, "vPosition");
this.gl.vertexAttribPointer(vPosition, 3, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vPosition);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_normals_buffer);
var vNormal = this.gl.getAttribLocation(program, "vNormal");
this.gl.vertexAttribPointer(vNormal, 3, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vNormal);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.sphere_edges_buffer);
this.gl.drawElements(this.gl.LINES, this.sphere_edges.length, this.gl.UNSIGNED_SHORT, 0);
}
sphereDrawFilled(program, texture)
{
this.gl.useProgram(program);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_points_buffer);
var vPosition = this.gl.getAttribLocation(program, "vPosition");
this.gl.vertexAttribPointer(vPosition, 3, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vPosition);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_normals_buffer);
var vNormal = this.gl.getAttribLocation(program, "vNormal");
this.gl.vertexAttribPointer(vNormal, 3, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vNormal);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_texCoords_buffer);
var vTexCoords = this.gl.getAttribLocation(program, "vTexCoord");
this.gl.vertexAttribPointer(vTexCoords, 2, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vTexCoords);
this.gl.activeTexture(this.gl.TEXTURE0);
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
this.gl.uniform1i(this.gl.getUniformLocation(program, "texture"), 0);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.sphere_texCoords_buffer);
var vTexCoords = this.gl.getAttribLocation(program, "vTexCoord");
this.gl.vertexAttribPointer(vTexCoords, 2, this.gl.FLOAT, false, 0, 0);
this.gl.enableVertexAttribArray(vTexCoords);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.sphere_faces_buffer);
this.gl.drawElements(this.gl.TRIANGLES, this.sphere_faces.length, this.gl.UNSIGNED_SHORT, 0);
}
sphereDraw(program, filled=false) {
if(filled) sphereDrawFilled(this.gl, program);
else sphereDrawWireFrame(this.gl, program);
}
}