The key code of coordinates is as follows:
// Convert UV to float u = (x/(float) imagew) * 2.0f-1.0f; float v = (y/(float) imageh) between-1 and-1) * 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 (ey Eray, 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, P OS. z * 0.5f + 0.5f );//?
1. First declare that the world coordinate system is X [-1, 1]; y [-1, 1]; Z [-1, 1]. (The World coordinate system)
2. U and V may change between [-1, 1], because the ranges of X and Y are [0, imagew] and [0, imageh] Change (x/(float) imagew) in [] Change multiplied by 2 in [] change, then minus 1, in [-1, 1] change.
3. The line of sight changes. First, specify the eye position: make_float4 (0.0f, 0.0f, 0.0f, 1.0f); then perform an affine transformation on the eye position: MUL (c_invviewmatrix, make_float4 (0.0f, 0.0f, 0.0f, 1.0f). Here, the transformation matrix is the matrix that transforms the viewpoint, so it is the inverse matrix of the model transformation. Finally, MUL (c_invviewmatrix, eyeray. d); changes the line of sight.
4. The intersection is also in the world coordinate system.
5. Obtain the texture coordinates. Because the Tex. normalized = true; therefore, the coordinates are normalized in the range of [0, 1]. Therefore, the coordinates must be transformed to [0, 1], tex3d (Tex, POS. x * 0.5f + 0.5f, POS. y * 0.5f + 0.5f, POS. z * 0.5f + 0.5f); meaning: [-1, 1] * 0.5 + 0.5 = [0, 1]
Now the coordinate transformation is complete.