Spheres not round in every aspect ratio

Started by
5 comments, last by missionctrl 6 years, 10 months ago

Hi all

I have made a simple ray tracer and all was well until i tried to render a background image for my laptop with a resolution of 1366x768.

When rendering to this aspect ratio of 1.78 my spheres become ovals. In almost every other resolution this doesnt happen. Is there something wrong with my raster space -> camera space code? Or is this some kind of perspective or aspect ratio problem?

Thanks

500x500 - AR = 1

500x500-no_Aspect.png

800x400 - AR = 2

800x400.gif

800x260 - AR = 3

800x260.png'

800x450 - AR = 1.78

800x450.gif

Advertisement

I notice 3 of these have a nice round integer aspect ratio. The only one that doesn't is also the one that breaks? Maybe you are accidentally storing the aspect ratio as an integer (or passing it as an integer into some function somewhere). So, it just clips the number to 1, renders with a 1:1 aspect ratio, and then you are stretching the image to fit into the 1.78 frame. (right now, the 1.78 render looks an awful lot like a stretched version of the 1:1 render)

Try rendering some other non-integer aspects.... like 2.3 or 1.5 or whatever.

Thanks MissionCtrl

I triple checked all my castings etc. I cant see anything wrong.

800x347 - AR = 2.3

800x347.gif

It seems to be just that specific aspect ratio. 16:9

Your 800x347 rendering actually is stretched slightly; you need to look a little carefully, but if you compare the middle (largest) sphere in that with the same sphere in the others you'll see it.

Resizing it to 694x347 brings it back to unstretched, which does indeed seem to confirm that you're getting a truncation-to-integer somewhere.

As a test try a rendering at a skinny-but-tall resolution - something like 480x640; it'll either display nothing, give you a single uniform colour, or crash spectacularly.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Your 800x347 rendering actually is stretched slightly; you need to look a little carefully

Agreed. This looks exactly like the 2:1 ratio from the original post but stretched slightly.

Out of curiosity, what language/framework/library are you using here? Maybe someone knows of some specific oddity in what you're working with.

Thanks guys.

I am using C++ with no libraries.

My camera class:

Camera::Camera(const Vec3 & p, const Vec3 & l, double fov, double aspect)
{
position = p;
lookAt = l;
this->fov = fov;
aspectRatio = aspect;
Vec3 tempUp(0, 1, 0);
forward = (lookAt - position).normalized();
right = cross(forward, tempUp).normalized();
up = cross(right, forward);
HEIGHT = tan(fov);
WIDTH = HEIGHT * aspectRatio;
}
Ray Camera::GetRay(unsigned x, unsigned y, unsigned H, unsigned W, double antiOffsetX, double antiOffsetY)
{
double X, Y;
X = (2.0*(double)x) / (double)W - 1.0 + antiOffsetX;
Y = (-2.0*(double)y) / (double)H + 1.0 - antiOffsetY;
Vec3 direction =
forward + X * WIDTH * right + Y * HEIGHT * up;
return Ray(position, direction.normalized());
}
I was about to post this but checked the code in main where I make the camera:
Camera camera(Vec3(0.0, 1.0, 10.0), Vec3(0.0, 1.0, 0.0), PI / 4.0,WIDTH / HEIGHT);
And sure enough, WIDTH and HEIGHT were unsigned ints lol so aspectRatio was always one.

God damn :P I must have been tired last night lol.
THANKS!
16:9
169.png
Good job rooting out that bug!

This topic is closed to new replies.

Advertisement