• Create Account

## Calculating points behind camera

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1Vrokulaf  Members

106
Like
0Likes
Like

Posted 17 February 2013 - 10:52 AM

Hello guys.

I've been getting into crafting my own 3D-engine in Java (mostly for kicks, but also I like to have full control over everything I use).

It's been going great for the most part (haven't gotten far), and I can now draw wireframes almost perfectly.

The formula I use for calculating where to draw a point on the screen is:

zDifference = abs(z - camera.z);

screenX = camera.lens * (x - camera.x) / zDifference);
screenY = camera.lens * (y - camera.y) / zDifference);


This works just fine, except for one thing.

When the point goes back behind the camera, it completely stops working.

Ok, so obviously I shouldn't draw a point behind the camera, right? Problem solved? Well, almost...

Image a line, drawn between two points. Imagine walking past the first one, but the second one still being visible. When this happens, the line goes haywire, since the calculations for the first point stop working. So I can't just "not draw the point", since I need it to draw the complete line. I still need to calculate where on the screen (it will be off-screen, but still) this point should be drawn, so I can after that draw a line between the two.

You can check out and see the issue in-action here.

So, how should I go about fixing this? Is the formula completely stupid, or should I find a way to work around this issue?

Any response is greatly appreciated

Have a good one,

~Vrokulaf

Edited by Vrokulaf, 17 February 2013 - 10:53 AM.

Freelancing musical composer!

### #2Brother Bob  Moderators

10103
Like
0Likes
Like

Posted 17 February 2013 - 11:07 AM

You can see how other APIs, such as OpenGL and Direct3D, handles this. What they do is basically inserting a clipping stage in the middle of transforming the object from model space to screen space to clip the object so that nothing extends outside the view volume (basically represented by the borders of your window and the near and far clip planes of your camera).

Instead of transforming your coordinates all the way to screen coordinates in one go, transform the coordinates to an intermediate clip space coordinate. This coordinate system is typically bounded by a unit cube or something similar, and you can then clip your line or polygon against this unit cube to ensure that only the visible parts remain. Then, take the final step to transform the clipped objects into screen space and rasterize it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.