From e1af5e214c389aea2b55daf82bdec92472db3f19 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 9 Feb 2019 00:00:53 +0800 Subject: Moving shitty stuff sitting around in the www folder here. --- xp/cgxp/main1.js | 320 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 xp/cgxp/main1.js (limited to 'xp/cgxp/main1.js') diff --git a/xp/cgxp/main1.js b/xp/cgxp/main1.js new file mode 100644 index 0000000..bd52e13 --- /dev/null +++ b/xp/cgxp/main1.js @@ -0,0 +1,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;iab.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=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