Bitmap Perspective transformation

Source: Internet
Author: User
Tags cos sin

Import flash.geom.*;
var photo:sprite=new Sprite ();
AddChild (photo);
IMG is a picture class that inherits from BitmapData
var bitmapsource = new Img (400,300);
var showline = true;
var inbitmapwidth = 1/bitmapsource.width;
var inbitmapheight = 1/bitmapsource.height;
var bitmapwidth = bitmapsource.width;
var bitmapheight = bitmapsource.height;
Bitmap transformation distance of mouse
var Bitmapmtrx = new Matrix ();
var Subbitmapmtix = new Matrix ();
Stage width Variable declaration
var stwidth = 550;
var stheight = 400;
Varōx = STWIDTH/2;
Varōy = STHEIGHT/2;
Focal length
var focus = 80;
Camera angle Variable
var Angu = 0;
var ANGV = 0;
Number of fine segments
var subdiv = 5;
var subdivpic = subdiv* (Subdiv-1);
var subdivvy = new Array ();
var subdivvx = new Array ();
var subdivvz = new Array ();
var subdivx = new Array ();
var subdivy = new Array ();
var subdivenable = new Array ();
var Mtrx = new Array ();
var mtrx2 = new Array ();
Subdivide bitmap x and y within the map
var subdivmapx = new Array ();
var subdivmapy = new Array ();
var subdivwidth = bitmapsource.width/(Subdiv-1);
var subdivheight = bitmapsource.height/(Subdiv-1);
for (var i = 0; i<subdiv; i++) {
for (var j = 0; j<subdiv; j + +) {
Subdivmapx.push (J*subdivwidth);
Subdivmapy.push (I*subdivheight);
}
}
//
for (var h = 0; h<subdivpic; h++) {
Mtrx.push (New Flash.geom.Matrix ());
Mtrx2.push (New Flash.geom.Matrix ());
Subdivvy.push (New Array (subdivpic));
Subdivvx.push (New Array (subdivpic));
Subdivvz.push (New Array (subdivpic));
Subdivx.push (New Array (subdivpic));
Subdivy.push (New Array (subdivpic));
Subdivenable.push (New Array (subdivpic));
}
var mtrxsx = subdivwidth/bitmapwidth;
var mtrxsy = subdivheight/bitmapheight;
var mtrxsxy = subdivwidth/bitmapheight;
var mtrxsyx = subdivheight/bitmapwidth;
for (var m = 0; m<subdiv-1; ++m) {
for (var n = 0; n<subdiv-1; ++n) {
var SubID = N+m*subdiv;
var mtrxtx = subdivmapx[subdiv*m+n];
var mtrxty = subdivmapy[subdiv*m+n];
Mtrx[subid].tx = MTRXTX;
Mtrx[subid].ty = mtrxty;
MTRX[SUBID].A = MTRXSX;
mtrx[subid].b = 0;
MTRX[SUBID].C = Mtrxsxy;
MTRX[SUBID].D = Mtrxsy;
Mtrx[subid].invert ();
Mtrx2[subid].tx = MTRXTX;
Mtrx2[subid].ty = mtrxty;
MTRX2[SUBID].A = MTRXSX;
mtrx2[subid].b = Mtrxsyx;
mtrx2[subid].c = 0;
MTRX2[SUBID].D = Mtrxsy;
Mtrx2[subid].invert ();
}
}
var mtrxa = mtrx[0].a;
var mtrxb = mtrx[0].b;
var mtrxc = mtrx[0].c;
var mtrxd = mtrx[0].d;
var mtrx2a = mtrx2[0].a;
var mtrx2b = mtrx2[0].b;
var mtrx2c = mtrx2[0].c;
var mtrx2d = mtrx2[0].d;
Initial spatial position of each bitmap subdivision node in the regular 3D viewport space
function Fun6 (Bitmapno, New19, NEW20, New21, New22, new25, New23, new26, New24, new27) {
var new15 = Subdiv-1 >> 1;
for (var i = 0; i<subdiv; i++) {
for (var j = 0; j<subdiv; j + +) {
var SubID = J+i*subdiv;
Subdivvy[bitmapno][subid] = new19*new15+new22* (J-NEW15) +new25* (I-NEW15);
Subdivvx[bitmapno][subid] = new20*new15+new23* (J-NEW15) +new26* (I-NEW15);
Subdivvz[bitmapno][subid] = new21*new15+new24* (J-NEW15) +new27* (I-NEW15);
Trace (subdivvy[bitmapno][subid]+ "/" +subdivvx[bitmapno][subid]+ "/" +subdivvz[bitmapno][subid]);
}
}
}
Fun6 (0,1,0,0,0,0,-1,0,0,-1);
function Fun7 (bitmapno) {
for (var i = 0; i<subdivpic; i++) {
This is the core part: The coordinate transformation operation of subdivision map vertex in camera coordinate space _loc5 is z value, and the two equations of _LOC5 and _loc6 are the simplification of space vector distance matrix calculation.
var _loc6 = cos_angu*subdivvy[bitmapno][i]+sin_angu*subdivvx[bitmapno][i];
var _loc5 = cos_angv*_loc6+sin_angv*subdivvz[bitmapno][i];
If the vertex is in front of the view, the projection is calculated
if (_loc5>=0.1) {
var _loc7 = focus/_loc5;
Calculate the x,y of a projection
Subdivx[bitmapno][i] = (sin_angu*subdivvy[bitmapno][i]-cos_angu*subdivvx[bitmapno][i]) *_loc7+ox;
Subdivy[bitmapno][i] = (sin_angv*_loc6-cos_angv*subdivvz[bitmapno][i]) *_loc7+oy;
if (subdivx[bitmapno][i]>0 && subdivx[bitmapno][i]<stwidth && subdivy[bitmapno][i]>0 & & Subdivy[bitmapno][i]<stheight) {
Subdivenable[bitmapno][i] = 1;
} else {
Subdivenable[bitmapno][i] = 0;
}
}
}
}
var subdivv = Subdiv-1;
function render () {
for (var i = 0; i<6; i++) {
Fun7 (i);
for (var j = 0; j<subdivv; ++j) {
for (var k = 0; k<subdivv; k++) {
var pointa = K+j*subdiv;
var pointc = k + (j+1) *subdiv;
var pointb = pointa+1;
var pointd = pointc+1;
var _loc2 = Subdivenable[i][pointa]+subdivenable[i][pointd]+subdivenable[i][pointb];
var _loc3 = Subdivenable[i][pointa]+subdivenable[i][pointd]+subdivenable[i][pointc];
If the subdivision surface has 1 vertices on the screen, the surface is rendered
if (_loc2>0) {
Calculation of stretching distance matrix of subdivision surface
SUBBITMAPMTIX.A = Mtrxa;
subbitmapmtix.b = MTRXB;
SUBBITMAPMTIX.C = MTRXC;
SUBBITMAPMTIX.D = Mtrxd;
Subbitmapmtix.tx = MTRX[POINTA].TX;
Subbitmapmtix.ty = Mtrx[pointa].ty;
Renderbitmap (I,subdivx[i][pointa],subdivy[i][pointa],subdivx[i][pointb],subdivy[i][pointb],subdivx[i][pointd], Subdivy[i][pointd],subbitmapmtix);
}
If the subdivision surface has 1 vertices on the screen, the surface is rendered
if (_loc3>0) {
Calculation of stretching distance matrix of subdivision surface
SUBBITMAPMTIX.A = mtrx2a;
subbitmapmtix.b = mtrx2b;
SUBBITMAPMTIX.C = mtrx2c;
SUBBITMAPMTIX.D = mtrx2d;
Subbitmapmtix.tx = MTRX2[POINTA].TX;
Subbitmapmtix.ty = Mtrx2[pointa].ty;
Renderbitmap (I,SUBDIVX[I][POINTA],SUBDIVY[I][POINTA],SUBDIVX[I][POINTD],SUBDIVY[I][POINTD],SUBDIVX[I][POINTC], Subdivy[i][pointc],subbitmapmtix);
}
}
}
}
}
function Renderbitmap (Bitmapno, point1x, point1y, point2x, point2y, point3x, point3y, Subbitmapmtix) {
Subdivision surface mapping Distance matrix calculation
THIS.BITMAPMTRX.A = (point2x-point1x) *inbitmapwidth;
this.bitmapmtrx.b = (point2y-point1y) *inbitmapwidth;
THIS.BITMAPMTRX.C = (point3x-point1x) *inbitmapheight;
THIS.BITMAPMTRX.D = (point3y-point1y) *inbitmapheight;
This.bitmapMtrx.tx = point1x;
This.bitmapMtrx.ty = point1y;
Subbitmapmtix.concat (THIS.BITMAPMTRX);
Texture rendering
This.photo.graphics.beginBitmapFill (Bitmapsource,subbitmapmtix,false,false);
if (ShowLine = = True) {
This.photo.graphics.lineStyle (1,0x000000,100);
}
This.photo.graphics.moveTo (POINT1X,POINT1Y);
This.photo.graphics.lineTo (POINT2X,POINT2Y);
This.photo.graphics.lineTo (POINT3X,POINT3Y);
This.photo.graphics.endFill ();
}//Initial rendering
Photo.graphics.clear ();
var Cos_angu = Math.Cos (Angu);
var Sin_angu = Math.sin (Angu);
var COS_ANGV = Math.Cos (ANGV);
var SIN_ANGV = Math.sin (ANGV);
var dx;
var dy;
Stage.addeventlistener (Mouseevent.mouse_down,onmousedownhandler);
Stage.addeventlistener (Mouseevent.mouse_up,onmouseuphandler);
function Onmousedownhandler (evt:mouseevent): void {
Stage.addeventlistener (Event.enter_frame,onenterframehandler);
DX = MouseX;
dy = Mousey;
}
function Onenterframehandler (evt:event): void {
var xwidth = MOUSEX-DX;
var ywidth = Mousey-dy;
if (xwidth!= 0 | | | ywidth!= 0) {
Photo.graphics.clear ();
Angu = angu-xwidth*0.001;
ANGV = angv-ywidth*0.001;
Cos_angu = Math.Cos (Angu);
Sin_angu = Math.sin (Angu);
COS_ANGV = Math.Cos (ANGV);
SIN_ANGV = Math.sin (ANGV);
Render ();
}
}
function Onmouseuphandler (evt:mouseevent): void {
Stage.removeeventlistener (Event.enter_frame,onenterframehandler);
Photo.graphics.clear ();
Render ();
}
Render ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.