Jump to content
  • Advertisement

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.

Thanks in advance.


Share this post

Link to post
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!