Advertisement

Manually calculating z co-ordinates

Started by September 09, 2019 05:21 AM
3 comments, last by Jozin 5 years, 4 months ago

Hi Guys,

I am playing around with making a basic software driven 3D framework for learning purposes only. I know it will not be very performant.

Hard one to explain, so hit me up with any questions if it isn't clear.

I am manually calculating each point of a triangle, which I am drawing to the screen with lines.

In the code snippet below, zero_x and zero_y are the co-ordinates of the centre of the window. So the positions are between -1 and 1 like DirectX etc...

The triangle points have already been transformed in to 3D space against a World, View, and Projection matrix. np0x & y are the points in screen co-ordinates.


np0x = (wvp[12] * zero_x) + zero_x; 
np0y = (wvp[13] * zero_y) + zero_y;

Looping through each of the points to render a triangle with lines is working great (aspect ratio, position, etc.), however depth isn't working. If I set the camera z distance to any number, the triangle always renders the same size on the screen.

I suspect that my calculations require wvp[14] (the z transform coordinate) to be multiplied in there as well to scale the x and y up and down.

I have tried all sorts of combinations with wvp[14] value but am unable to get the triangle to render correctly when I do so.

Any advice on this would be awesome.

Thanks in advance.

 

I think I am closer now, with doing the following


np0x = ((wvp[12] / wvp[15]) * zero_x) + zero_x; 
np0y = ((wvp[13] / wvp[15]) * zero_y) + zero_y;

It's close, but still looks a bit off.

Advertisement

Good chat.  :P

Play with something like this np0x = np0x * 0.5 + 0.5 its likely your coordinates transformed to [-1, 1] range

Jeronimo!

This topic is closed to new replies.

Advertisement