Changeset 29553

Show
Ignore:
Timestamp:
02/05/09 02:23:15 (4 years ago)
Author:
gyuque
Message:

added new sample

Location:
lang/javascript/jscloth/trunk
Files:
6 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/jscloth/trunk/parpevision.js

    r29477 r29553  
    110110} 
    111111 
     112P3D.drawByIndexBuffer = function(pos_buf, ix_buf, tx_buf, culling) { 
     113        var w = this.texture.width; 
     114        var h = this.texture.height; 
     115        var g = this.g; 
     116        var m = new M22(); 
     117 
     118        if (!culling) culling = 0; 
     119 
     120        if ((ix_buf.length%3) != 0) 
     121                throw "invalid index buffer length!"; 
     122 
     123        var i, ibase, vbase, tbase, poss = [{},{},{}], uvs = [{},{},{}]; 
     124        var len = ix_buf.length/3; 
     125 
     126        g.strokeStyle = "#050"; 
     127        g.fillStyle = "#fff"; 
     128        for (i = 0, ibase = 0;i < len;++i) 
     129        { 
     130                tbase = ix_buf[ibase++] << 1 
     131                vbase = tbase << 1; 
     132                poss[0].x = pos_buf[vbase++]; uvs[0].u = tx_buf[tbase++]; 
     133                poss[0].y = pos_buf[vbase  ]; uvs[0].v = tx_buf[tbase]; 
     134 
     135                tbase = ix_buf[ibase++] << 1 
     136                vbase = tbase << 1; 
     137                poss[1].x = pos_buf[vbase++]; uvs[1].u = tx_buf[tbase++]; 
     138                poss[1].y = pos_buf[vbase  ]; uvs[1].v = tx_buf[tbase]; 
     139 
     140                tbase = ix_buf[ibase++] << 1 
     141                vbase = tbase << 1; 
     142                poss[2].x = pos_buf[vbase++]; uvs[2].u = tx_buf[tbase++]; 
     143                poss[2].y = pos_buf[vbase  ]; uvs[2].v = tx_buf[tbase]; 
     144/* 
     145g.beginPath(); 
     146g.moveTo(poss[0].x, poss[0].y); 
     147g.lineTo(poss[1].x, poss[1].y); 
     148g.lineTo(poss[2].x, poss[2].y); 
     149g.stroke(); 
     150g.fill(); 
     151continue; 
     152*/ 
     153                var vAd = [ poss[1].x - poss[0].x , poss[1].y - poss[0].y ]; 
     154                var vBd = [ poss[2].x - poss[0].x , poss[2].y - poss[0].y ]; 
     155 
     156                var vCd = [ poss[2].x - poss[1].x , poss[2].y - poss[1].y ]; 
     157                 
     158                // z component of cross product < 0 ? 
     159                if( (((vAd[0] * vCd[1]) - (vAd[1] * vCd[0]))*culling) < 0) 
     160                        continue; 
     161 
     162                var vA = [ uvs[1].u - uvs[0].u , uvs[1].v - uvs[0].v ]; 
     163                var vB = [ uvs[2].u - uvs[0].u , uvs[2].v - uvs[0].v ]; 
     164 
     165                vA[0] *= w; 
     166                vA[1] *= h; 
     167 
     168                vB[0] *= w; 
     169                vB[1] *= h; 
     170 
     171                m._11 = vA[0]; 
     172                m._12 = vA[1]; 
     173                m._21 = vB[0]; 
     174                m._22 = vB[1]; 
     175 
     176                var im = m.getInvert(); 
     177                if (!im) { continue;} 
     178 
     179                var a = im._11 * vAd[0] + im._12 * vBd[0]; 
     180                var b = im._21 * vAd[0] + im._22 * vBd[0]; 
     181 
     182                var c = im._11 * vAd[1] + im._12 * vBd[1]; 
     183                var d = im._21 * vAd[1] + im._22 * vBd[1]; 
     184 
     185                var wu = uvs[0].u * w; 
     186                var hv = uvs[0].v * h; 
     187                var du = wu * a + hv * b; 
     188                var dv = wu * c + hv * d; 
     189 
     190                g.save(); 
     191 
     192                g.beginPath(); 
     193                g.moveTo(poss[0].x, poss[0].y); 
     194                g.lineTo(poss[1].x, poss[1].y); 
     195                g.lineTo(poss[2].x, poss[2].y); 
     196                g.clip(); 
     197 
     198                g.transform(a, c, b, d, poss[0].x - du, poss[0].y - dv); 
     199 
     200                // bounds 
     201                var bx = [wu, wu+vA[0], wu+vB[0]]; 
     202                var by = [hv, hv+vA[1], hv+vB[1]]; 
     203 
     204                bx.sort(P3D.num_cmp); 
     205                by.sort(P3D.num_cmp); 
     206 
     207                var bw = bx[2] - bx[0]; 
     208                var bh = by[2] - by[0]; 
     209 
     210                if ((bx[0]+bw) <= (w-1)) bw++; 
     211                if ((by[0]+bh) <= (h-1)) bh++; 
     212                if (bx[0] >= 1) {bx[0]--; bw++;} 
     213                if (by[0] >= 1) {by[0]--; bh++;} 
     214 
     215                g.drawImage(this.texture, bx[0], by[0], bw, bh, bx[0], by[0], bw, bh); 
     216/* 
     217                if (shade_clr) { 
     218                        g.fillStyle = shade_clr; 
     219                        g.fillRect(bx[0], by[0], bw, bh); 
     220                } 
     221*/ 
     222                g.restore(); 
     223 
     224        } 
     225 
     226} 
    112227 
    113228function Vec3(_x, _y, _z) 
     
    188303                this.copyFrom(cpy); 
    189304        else { 
    190                 this._11 = 1, this._12 = 0, this._13 = 0, this._14 = 0; 
    191                 this._21 = 0, this._22 = 1, this._23 = 0, this._24 = 0; 
    192                 this._31 = 0, this._32 = 0, this._33 = 1, this._34 = 0; 
    193                 this._41 = 0, this._42 = 0, this._43 = 0, this._44 = 1; 
     305                this.ident(); 
    194306        } 
    195307} 
    196308 
    197309M44.prototype = { 
     310        ident: function() { 
     311                          this._12 = this._13 = this._14 = 0; 
     312                this._21 =       this._23 = this._24 = 0; 
     313                this._31 = this._32 =       this._34 = 0; 
     314                this._41 = this._42 = this._43 =       0; 
     315 
     316                this._11 = this._22 = this._33 = this._44 = 1; 
     317 
     318                return this; 
     319        }, 
     320 
    198321        copyFrom: function(m) { 
    199322                this._11 = m._11; 
     
    301424 
    302425                return this; 
     426        }, 
     427 
     428        translate: function(x, y, z) { 
     429                this._11 = 1;  this._12 = 0;  this._13 = 0;  this._14 = 0; 
     430                this._21 = 0;  this._22 = 1;  this._23 = 0;  this._24 = 0; 
     431                this._31 = 0;  this._32 = 0;  this._33 = 1;  this._34 = 0; 
     432 
     433                this._41 = x;  this._42 = y;  this._43 = z;  this._44 = 1; 
     434                return this; 
     435        }, 
     436 
     437        transpose33: function() { 
     438                var t; 
     439 
     440                t = this._12; 
     441                this._12 = this._21; 
     442                this._21 = t; 
     443 
     444                t = this._13; 
     445                this._13 = this._31; 
     446                this._31 = t; 
     447 
     448                t = this._23; 
     449                this._23 = this._32; 
     450                this._32 = t; 
     451 
     452                return this; 
     453        }, 
     454 
     455        // OpenGL style rotation 
     456        glRotate: function(angle, x, y, z) { 
     457                var s = Math.sin( angle ); 
     458                var c = Math.cos( angle ); 
     459 
     460                var xx = x * x; 
     461                var yy = y * y; 
     462                var zz = z * z; 
     463                var xy = x * y; 
     464                var yz = y * z; 
     465                var zx = z * x; 
     466                var xs = x * s; 
     467                var ys = y * s; 
     468                var zs = z * s; 
     469                var one_c = 1.0 - c; 
     470/* 
     471                this._11 = (one_c * xx) + c; 
     472                this._21 = (one_c * xy) - zs; 
     473                this._31 = (one_c * zx) + ys; 
     474                this._41 = 0; 
     475 
     476                this._12 = (one_c * xy) + zs; 
     477                this._22 = (one_c * yy) + c; 
     478                this._32 = (one_c * yz) - xs; 
     479                this._42 = 0; 
     480 
     481                this._13 = (one_c * zx) - ys; 
     482                this._23 = (one_c * yz) + xs; 
     483                this._33 = (one_c * zz) + c; 
     484                this._43 = 0; 
     485 
     486                this._14 = 0; 
     487                this._24 = 0; 
     488                this._34 = 0; 
     489                this._44 = 1; 
     490*/ 
     491 
     492                this._11 = (one_c * xx) + c; 
     493                this._12 = (one_c * xy) - zs; 
     494                this._13 = (one_c * zx) + ys; 
     495                this._14 = 0; 
     496 
     497                this._21 = (one_c * xy) + zs; 
     498                this._22 = (one_c * yy) + c; 
     499                this._23 = (one_c * yz) - xs; 
     500                this._24 = 0; 
     501 
     502                this._31 = (one_c * zx) - ys; 
     503                this._32 = (one_c * yz) + xs; 
     504                this._33 = (one_c * zz) + c; 
     505                this._34 = 0; 
     506 
     507                this._41 = 0; 
     508                this._42 = 0; 
     509                this._43 = 0; 
     510                this._44 = 1; 
     511 
     512                return this; 
    303513        } 
     514 
    304515} 
    305516