# 3D Manually calculating z co-ordinates

## Recommended Posts

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.

##### Share on other sites

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.

Good chat.  :P

##### Share on other sites

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

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 14
• 41
• 63