我目前正在学习3D计算机图形学,并将并行投影规范化为canocial view volume(LookAt Matrix作为熟悉的名字)。我尝试使用纯javascript作为下面的参数将其实现到代码中。
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;
现在,这是我的尝试。我首先将其转换为正交视图体积。
注意:您可以将这些步骤直接跳到此处的代码中,并帮助我修复代码以将立方体向前移动到相机(屏幕)而不是移开
1. 将 VRP 转换为源
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. 旋转 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);
将其转换为类视图体积后,我决定将立方体顶点乘以此最终结果转换矩阵。
郎朗坤
相关分类