# Matrix transformations in the VEX function 'fromNDC'

## Recommended Posts

I was trying to use the VEX function 'fromNDC' inside a snippet wrangle in COPS, but it doesn't seem to provide the same results as when used in a point wrangle in SOPS. The documentation for 'fromNDC' warns that NDC space may not be 'well defined' in every context, so I guess I'm up against that.

I would like to perform the calculation manually in that case, but I'm not exactly sure what transformations are involved. I am trying to convert points from NDC space to camera space. I can see that one part of it is to use the camera matrix like this:

```matrix cam_matrix = maketransform(0, 0, cam_position, cam_rotation);
@P *= cam_matrix;```

That brings the points in the correct position/rotation in the scene, but there is something missing - I'm not sure if it is a screenspace or clip space or view space or something else transformation which also needs to occur to get the same result as the 'fromNDC' function.

I have attached a project showing the two methods (1) fromNDC, and (2) the incomplete method doing it by hand.

Can anyone help me?

##### Share on other sites

Posted (edited)

@localstarlight
lene length link to camera focal lenth,
make 4 wrangle to adjust angle of 4 . use lines and make Plane from those 4 lines
Maybe..

```s@cam = chs('cam');
@near = ch(sprintf('%s/%s',s@cam,'near'));
@far =  ch(sprintf('%s/%s',s@cam,'near'));
@focal = ch(sprintf('%s/%s',s@cam,'focal'));
@aperture = chf(sprintf('%s/%s',s@cam,'aperture'));
f@resx = float(chi(sprintf('%s/%s',s@cam,'resx')));
f@resy = float(chi(sprintf('%s/%s',s@cam,'resy')));
f@scale = f@resx/f@resy;
@aperturey = @aperture/f@scale;

//nearplane
float x = @near/(@focal/@aperture);
float y = x/f@scale;
float z = sqrt(pow(x,2)+pow(y,2))/2;
float near = sqrt(pow(z,2)+pow(@near,2));

float u = @far/(@focal/@aperture);
float v = u/f@scale;
float w = sqrt(pow(u,2)+pow(v,2))/2;
float far = sqrt(pow(z,2)+pow(@far,2));

setpointattrib(0,'P',1,set(0,0,-near));
setpointattrib(1,'P',2,set(0,0,-far));```
```@N = set(0,0,1);
v@up = set(0,1,0);
matrix m = maketransform(@N,v@up);
float angle = atan((@aparture/2)/@focal);
float angley= atan((@aparturey/2)/@focal);
vector axis = v@up;
rotate(m,angle,axis);
vector axisy = set(1,0,0);
rotate(m,angle,axisy);
@P *= m;```

Edited by Librarian

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×