我目前正在學習3D計算機圖形學,并將并行投影規(guī)范化為canocial view volume(LookAt Matrix作為熟悉的名字)。我嘗試使用純javascript作為下面的參數(shù)將其實現(xiàn)到代碼中。var VRP = new Vertex(0,0,0);var VPN = new Vertex(0,0,1);var VUP = new Vertex(0,1,0);var PRP = new Vertex(8,8,100);var Window = [-1,17,-1,17];var F = 1, B = -1;現(xiàn)在,這是我的嘗試。我首先將其轉(zhuǎn)換為正交視圖體積。注意:您可以將這些步驟直接跳到此處的代碼中,并幫助我修復代碼以將立方體向前移動到相機(屏幕)而不是移開1. 將 VRP 轉(zhuǎn)換為源var TVRP = [];TVRP[0] = [1, 0, 0, -VRP.x];TVRP[1] = [0, 1, 0, -VRP.y];TVRP[2] = [0, 0, 1, -VRP.z];TVRP[3] = [0, 0, 0, 1];2. 旋轉(zhuǎn) VRC,使 n 軸、u 軸和 v 軸按順序與 z 軸、x 軸和 y 軸對齊function normalizeViewPlane(VPN) { var unitVector = calculateUnitVector(VPN); //VPN/|VPN| return normalizeVector(VPN,unitVector);}function normalizeViewUp(VUP, n) { var dtProd = dotProduct(n,VUP); var nVUP = new Vertex(n.x*dtProd, n.y*dtProd, n.z*dtProd); VUP = new Vertex(VUP.x-nVUP.x, VUP.y-nVUP.y, VUP.z-nVUP.z); var unitVector = calculateUnitVector(VUP); //VUP/|VUP| return normalizeVector(VUP,unitVector);}function normalizeUVN(n,u) { var V = crossProduct(n,u); var unitVector = calculateUnitVector(V); //V/|V| return normalizeVector(V,unitVector);}var n = normalizeViewPlane(VPN);var v = normalizeViewUp(VUP, n);var u = normalizeUVN(v, n);var RVRC = [];RVRC[0] = [u.x, u.y, u.z, 0];RVRC[1] = [v.x, v.y, v.z, 0];RVRC[2] = [n.x, n.y, n.z, 0];RVRC[3] = [0, 0, 0, 1];//Perform matrix multiplication 4x4 R.T(-VRP)var res = multiplyMatrix4x4(RVRC, TVRP);3. 剪切 DOP 變得平行于 z 軸function shearDOP(PRP, uMaxMin, vMaxMin) { var CW = new Vertex(uMaxMin,vMaxMin,0); var mPRP = new Vertex(PRP.x,PRP.y,PRP.z); return new Vertex(CW.x - mPRP.x, CW.y - mPRP.y, CW.z - mPRP.z);}var uMaxMin = (Window[1]+Window[0])/2;var vMaxMin = (Window[3]+Window[2])/2;var DOP = shearDOP(PRP,uMaxMin,vMaxMin); var HX = (DOP.x/DOP.z)*-1;var HY = (DOP.y/DOP.z)*-1; var Hpar = []; Hpar[0] = [1,0,HX,0]; Hpar[1] = [0,1,HY,0]; Hpar[2] = [0,0,1,0]; Hpar[3] = [0,0,0,1]; //res = R.T(-VRP) res = multiplyMatrix4x4(Hpar,res);將其轉(zhuǎn)換為類視圖體積后,我決定將立方體頂點乘以此最終結(jié)果轉(zhuǎn)換矩陣。
LookAt 矩陣(歸一化平行投影)
守著星空守著你
2022-08-18 10:07:30