Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calculating points behind camera

Started by Vrokulaf Feb 17, 2013 at 4:52 PM 0 replies 1.2k views
Original Post
Vrokulaf
Vrokulaf

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 smile.png

Have a good one,

~Vrokulaf

Freelancing musical composer!
For more info check out my website!
Brother Bob
Brother Bob

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.