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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
//Copyright Chris Xiong 2018
//License: Expat (MIT)
var cvs,WGL,vpw,vph,hdiv;
var fsh=`
varying lowp vec4 vC;
void main(void)
{
gl_FragColor=vC;
}`;
var vsh=`
attribute vec3 vp;
attribute vec4 vc;
uniform mat4 mmodelview;
uniform mat4 mprojection;
varying lowp vec4 vC;
void main(void)
{
gl_Position=mprojection*mmodelview*vec4(vp,1.);
vC=vc;
}`;
var shp;
var vbufsize=4096;
var vbo,ibo;
var vb=new Float32Array(vbufsize*7);
var ib=new Uint16Array(vbufsize*6/4);
var Mprojection,Mmodelview;
var primc;
var kst=[];
var dbc,mbtn,mup,mdn,mx,my,mox,moy;
var shapes=[];
var curshape=null;
var dragging=false,drg;
var Shape=function(t)
{
this.type=t;
this.vert=[];
}
function copyshape(s)
{
var r=new Shape(s.type);
for(var i=0;i<s.vert.length;++i)
r.vert.push(new v3d(s.vert[i].x,s.vert[i].y,s.vert[i].z));
return r;
}
Shape.prototype.draw=function(a,r,g,b)
{
var drawseg=function(a,b,c,d,aa,rr,gg,bb)
{
var pa=new v3d(a,b,0);
var pb=new v3d(c,d,0);
var d=pb.subtract(pa);
var pd1=new v3d(-d.y,d.x,0),pd2=new v3d(d.y,-d.x,0);
pd1=pd1.n();pd2=pd2.n();
var p1=pa.add(pd1),p2=pa.add(pd2);
var p3=pb.add(pd2),p4=pb.add(pd1);
var q=[];
q.push(p1.x);q.push(vph-p1.y);q.push(p1.z);
q.push(rr);q.push(gg);q.push(bb);q.push(aa);
q.push(p2.x);q.push(vph-p2.y);q.push(p2.z);
q.push(rr);q.push(gg);q.push(bb);q.push(aa);
q.push(p3.x);q.push(vph-p3.y);q.push(p3.z);
q.push(rr);q.push(gg);q.push(bb);q.push(aa);
q.push(p4.x);q.push(vph-p4.y);q.push(p4.z);
q.push(rr);q.push(gg);q.push(bb);q.push(aa);
pushQuad(q);
}
if(this.type=='segment')
drawseg(this.vert[0].x,this.vert[0].y,this.vert[1].x,this.vert[1].y,
a,r,g,b);
if(this.type=='segstrip'||this.type=='segloop')
{
for(var i=0;i<this.vert.length-1;++i)
drawseg(this.vert[i].x,this.vert[i].y,this.vert[i+1].x,this.vert[i+1].y,
a,r,g,b);
if(this.type=='segloop')
{
var i=this.vert.length-1;
drawseg(this.vert[i].x,this.vert[i].y,this.vert[0].x,this.vert[0].y,
a,r,g,b);
}
}
}
Shape.prototype.coltest=function(x,y)
{
var dp2s=function(x,y,_a,_b,_c,_d)
{
var a=new v3d(_a,_b,0);
var b=new v3d(_c,_d,0);
var p=new v3d(x,y,0);
var ab=b.subtract(a);
var ap=p.subtract(a);
var pl=ab.dot(ap)/ab.l();
var pp=a.add(ab.n().ms(pl));
if(pl<0||pl>ab.l())pp=new v3d(1e9,1e9,1e9);
return Math.min(p.subtract(pp).l(),p.subtract(a).l(),p.subtract(b).l());
}
if(this.type=='segment')
{
var d=dp2s(x,y,this.vert[0].x,this.vert[0].y,
this.vert[1].x,this.vert[1].y);
if(d<5)return true;
}
if(this.type=='segstrip'||this.type=="segloop")
{
for(var i=0;i<this.vert.length-1;++i)
{
var d=dp2s(x,y,this.vert[i].x,this.vert[i].y,
this.vert[i+1].x,this.vert[i+1].y);
if(d<5)return true;
}
if(this.type=="segloop")
{
var i=this.vert.length-1;
var d=dp2s(x,y,this.vert[i].x,this.vert[i].y,
this.vert[0].x,this.vert[0].y);
if(d<5)return true;
}
}
return false;
}
function createShader(sh,t)
{
var shader=WGL.createShader(t);
WGL.shaderSource(shader,sh);
WGL.compileShader(shader);
if(!WGL.getShaderParameter(shader,WGL.COMPILE_STATUS))
{
alert("shader error..."+WGL.getShaderInfoLog(shader));
return null;
}
return shader;
}
function pushQuad(a)
{
if(primc>=vbufsize/4)batchGL();
for(var i=0;i<4;++i)
for(var j=0;j<7;++j)vb[primc*7*4+i*7+j]=a[i*7+j];
++primc;
}
function init()
{
cvs=document.getElementById("glCvs");
if(!window.devicePixelRatio)window.devicePixelRatio=1;
cvs.width=window.innerWidth;
cvs.height=window.innerHeight;
cvs.style.width=cvs.width+"px";
cvs.style.height=cvs.height+"px";
cvs.width=cvs.width*window.devicePixelRatio;
cvs.height=cvs.height*window.devicePixelRatio;
vpw=cvs.width;vph=cvs.height;
try
{WGL=cvs.getContext("webgl")||cvs.getContext("experimental-webgl");}
catch(e){WGL=null;}
if(!WGL){alert("WebGL is not supported by your browser...");return;}
WGL.clearColor(0.5,0.5,0.5,1.0);
WGL.clearDepth(1);
WGL.enable(WGL.DEPTH_TEST);
WGL.depthFunc(WGL.LESS);
WGL.enable(WGL.BLEND);
WGL.blendFunc(WGL.SRC_ALPHA,WGL.ONE_MINUS_SRC_ALPHA);
WGL.clear(WGL.COLOR_BUFFER_BIT|WGL.DEPTH_BUFFER_BIT);
WGL.viewport(0,0,cvs.width,cvs.height);
shp=WGL.createProgram();
var fshs=createShader(fsh,WGL.FRAGMENT_SHADER);
var vshs=createShader(vsh,WGL.VERTEX_SHADER);
WGL.attachShader(shp,vshs);WGL.attachShader(shp,fshs);
WGL.linkProgram(shp);
if(!WGL.getProgramParameter(shp,WGL.LINK_STATUS))
{
alert("shader link error...");
}
WGL.useProgram(shp);
vbo=WGL.createBuffer();
WGL.bindBuffer(WGL.ARRAY_BUFFER,vbo);
WGL.bufferData(WGL.ARRAY_BUFFER,vb,WGL.STATIC_DRAW);
var vpp=WGL.getAttribLocation(shp,"vp");
var vcp=WGL.getAttribLocation(shp,"vc");
WGL.enableVertexAttribArray(vpp);
WGL.enableVertexAttribArray(vcp);
WGL.bindBuffer(WGL.ARRAY_BUFFER,vbo);
WGL.vertexAttribPointer(vpp,3,WGL.FLOAT,false,7*4,0);
WGL.vertexAttribPointer(vcp,4,WGL.FLOAT,false,7*4,3*4);
for(var i=0,n=0,p=0;i<vbufsize/4;++i)
{
ib[p++]=n;ib[p++]=n+1;
ib[p++]=n+2;ib[p++]=n+2;
ib[p++]=n+3;ib[p++]=n;
n+=4;
}
ibo=WGL.createBuffer();
WGL.bindBuffer(WGL.ELEMENT_ARRAY_BUFFER,ibo);
WGL.bufferData(WGL.ELEMENT_ARRAY_BUFFER,ib,WGL.STATIC_DRAW);
document.addEventListener('keydown',function(e){kst[e.code]=1;});
document.addEventListener('keyup',function(e){kst[e.code]=0;});
document.addEventListener('mousedown',function(e){mdn=true;mbtn=e.button;});
document.addEventListener('contextmenu',function(e){e.preventDefault();});
document.addEventListener('mouseup',function(e){mup=true;});
document.addEventListener('dblclick',function(e){dbc=true;});
document.addEventListener('mousemove',function(e)
{mx=e.clientX/window.innerWidth*vpw;my=e.clientY/window.innerHeight*vph;});
requestAnimationFrame(mainloop);
}
function getpick()
{
for(var i=0;i<shapes.length;++i)
if(shapes[i].coltest(mx,my))return i;
return -1;
}
function mainloop()
{
WGL.clearColor(0.5,0.5,0.5,1.0);
WGL.clearDepth(1);
WGL.clear(WGL.COLOR_BUFFER_BIT|WGL.DEPTH_BUFFER_BIT);
primc=0;
Mmodelview=buildIdentityMatrix();
var picked=getpick();
//interaction shit
if(dragging)
{
if(mup)
{dragging=false;curshape=null;mdn=mup=false;}
if(!mdn&&mbtn==0&&dragging)
for(var i=0;i<shapes[drg].vert.length;++i)
shapes[drg].vert[i]=curshape.vert[i].add(new v3d(mx,my,0).subtract(new v3d(mox,moy,0)));
if(!mdn&&mbtn==2&&dragging)
{
var newscale=new v3d(mox,moy,0).subtract(new v3d(mx,my,0)).l()/20;
if(mx<mox||my<moy)newscale=-newscale;
if(newscale<-95)newscale=-95;
var sc=(100+newscale)/100.;
var mmx=curshape.vert[0].x,mmy=curshape.vert[0].y;
for(var i=1;i<curshape.vert.length;++i)
mmx=Math.min(mmx,curshape.vert[i].x),
mmy=Math.min(mmy,curshape.vert[i].y);
var c=new v3d(mmx,mmy,0);
for(var i=0;i<shapes[drg].vert.length;++i)
shapes[drg].vert[i]=curshape.vert[i].subtract(c).n().ms
(curshape.vert[i].subtract(c).l()*sc).add(c);
}
}
if(dbc)
{
if(dragging)
{dragging=false;curshape=null;}
else
if(curshape)
{
if(kst['ControlLeft']==1&&curshape.type=='segstrip')
curshape.type='segloop';
shapes.push(curshape);
curshape=null;
}
}
else
{
if(mup&&kst['ControlLeft']==1&&!curshape)
{
if(picked!=-1)
{
dragging=true;
mox=mx,moy=my;drg=picked;
curshape=copyshape(shapes[picked]);
}
}
if(mup&&!dragging&&!kst['ControlLeft'])
{
if(!curshape)
{
curshape=new Shape('segment');
curshape.vert.push(new v3d(mx,my,0));
curshape.vert.push(new v3d(mx,my,0));
}
else
{
if(curshape.type=='segment')curshape.type='segstrip';
curshape.vert.push(new v3d(mx,my,0));
}
}
}
if(curshape&&!dragging)
curshape.vert[curshape.vert.length-1]=new v3d(mx,my,0);
for(var i=0;i<shapes.length;++i)
if(!curshape&&!dragging&&i==picked)shapes[i].draw(1,0.8,0,0);
else shapes[i].draw(1,0.8,0.8,0.8);
if(curshape&&!dragging)curshape.draw(1,0.8,0.8,0);
batchGL();
mup=mdn=dbc=false;
requestAnimationFrame(mainloop);
}
function batchGL()
{
Mprojection=buildProjectionMatrix2D(vpw,vph);
var mpp=WGL.getUniformLocation(shp,"mprojection");
WGL.uniformMatrix4fv(mpp,false,Mprojection);
var mvpp=WGL.getUniformLocation(shp,"mmodelview");
WGL.uniformMatrix4fv(mvpp,false,Mmodelview);
WGL.bindBuffer(WGL.ARRAY_BUFFER,vbo);
WGL.bufferData(WGL.ARRAY_BUFFER,vb,WGL.STATIC_DRAW);
WGL.drawElements(WGL.TRIANGLES,6*primc,WGL.UNSIGNED_SHORT,0);
primc=0;
}
|