座標關鍵代碼如下:
//將uv轉換為-1到1之間的浮點數 float u = (x / (float) imageW)*2.0f-1.0f; float v = (y / (float) imageH)*2.0f-1.0f; // calculate eye ray in world space Ray eyeRay; eyeRay.o = make_float3(mul(c_invViewMatrix, make_float4(0.0f, 0.0f, 0.0f, 1.0f))); eyeRay.d = normalize(make_float3(u, v, -2.0f));// eyeRay.d = mul(c_invViewMatrix, eyeRay.d);// // find intersection with boxfloat tnear, tfar;int hit = intersectBox(eyeRay, boxMin, boxMax, &tnear, &tfar); if (!hit) return;if (tnear < 0.0f) tnear = 0.0f; // clamp to near plane // march along ray from front to back, accumulating color float4 sum = make_float4(0.0f); float t = tnear; float3 pos = eyeRay.o + eyeRay.d*tnear; float3 step = eyeRay.d*tstep; for(int i=0; i<maxSteps; i++) { // read from 3D texture // remap position to [0, 1] coordinates float sample = tex3D(tex, pos.x*0.5f+0.5f, pos.y*0.5f+0.5f, pos.z*0.5f+0.5f);//?
1、先聲明,全局座標係為x [-1, 1]; y [-1, 1]; z[-1, 1]。(自己隨便說是全局座標系)
2、u,v可以肯定是在[-1, 1]之間變化的,因為x和y 的變化範圍分別在[0,imageW]和[0,imageH]變化(x / (float) imageW)在[0,1]變化乘以2在[0,2]變化,再減1,在[-1, 1]變化。
3、說明視線的變化。首先規定眼睛的位置:make_float4(0.0f, 0.0f, 0.0f, 1.0f);然後對眼睛的位置進行仿射變換:mul(c_invViewMatrix, make_float4(0.0f, 0.0f, 0.0f, 1.0f)),這裡變換矩陣是變換視點的矩陣,所以是模型變換的逆矩陣。最後,mul(c_invViewMatrix, eyeRay.d); 對視線進行變換。
4、求交,求交也是在全局座標系下。
5、求紋理座標。由於紋理產生時,使用了:tex.normalized = true; 所以會對座標進行標準化範圍[0, 1],所以又需要將座標變換到[0, 1]之間,tex3D(tex, pos.x*0.5f+0.5f, pos.y*0.5f+0.5f, pos.z*0.5f+0.5f); 意思是:[-1, 1]*0.5 + 0.5 = [0, 1]
至此座標變換完成。