• Advertisement
Sign in to follow this  

Question about generating rays from camera.

This topic is 1972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey. I just finished doing my ray marcher with distance fields. It supports ambient occlusion, shadows, diffuse lighting. It's pretty basic. Here's render from it: (1024 x 1024)
[spoiler]
hWqos.png
[/spoiler]

But ok, let's get to the question. How do you generate eye rays? What's the easiest way to do this?
I'm using this method: http://www.unknownroad.com/rtfm/graphics/rt_eyerays.html
But it distorts my image even at field of view of 45... The fov on render above me is 22.5.
What's the proper method to generate eye rays?

Share this post


Link to post
Share on other sites
Advertisement
There will always be some form of distortion with a perspective plane projection, no matter what field of view you use. Higher fields of view will lead to extreme distortion at edges, and smaller fields of view will lead to loss of depth. Are you sure you are using the [eqn]\tan(\mathrm{fov} \times 0.5)[/eqn] factor correctly? In my raytracer, at a field of view of 45 degrees the distortion is not very noticeable except if I had tiny spheres at the edges, and quite acceptable, of course it depends on your standards.

Other solutions involve projecting the scene on a section of a sphere, which eliminate distortion but introduce other artifacts such as straight lines being projected curved and aren't as easy to compute... there is no perfect projection as you necessarily throw away information from a three-dimensional world to a two-dimensional plane.

Share this post


Link to post
Share on other sites
@Bacterius:
Here's my camera code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;

namespace SphereTracing
{
public class Camera
{
public Scene BaseScene { get; set; }
public Vector3 Position { get; set; }
private float FovX;
private float FovY;
private float TanFovX;
private float TanFovY;

public Camera(Scene sc)
{
BaseScene = sc;
SetFov(MathHelper.PiOver4);
}

public void SetFov(float fov)
{
float width, height;
width = (float)BaseScene.Renderer.Width;
height = (float)BaseScene.Renderer.Height;
FovX = fov;
FovY = height / width * FovX;
TanFovX = (float)Math.Tan(FovX);
TanFovY = (float)Math.Tan(FovY);
}

public float GetFov()
{
return FovX;
}

public Ray GetRay(int x, int y)
{
Ray r = new Ray();
float width, height;
width = BaseScene.Renderer.Width;
height = BaseScene.Renderer.Height;

float dx = (2 * x - width) / width * TanFovX;
float dy = (2 * y - height) / height * TanFovY;

r.Position = this.Position;
r.Direction = new Vector3(dx, dy, -1.0f);
r.Direction = Vector3.Normalize(r.Direction);

return r;
}
}
}

Here's simple plane rendered at fov 45:
LLU57.png
It doesn't look right for me.

Share this post


Link to post
Share on other sites
You are ray-marching, not ray-tracing, so your rays die after a certain distance travelled (as far as I can guess - you haven't mentioned what you do with your rays) so that would explain the arcing horizon. A perspective projection cannot by definition cause straight lines to appear curved, so the curved horizon doesn't come from the camera - as far as I can tell anyway, the camera code looks fine, although the camera position and direction are hardcoded.

Share this post


Link to post
Share on other sites

You are ray-marching, not ray-tracing, so your rays die after a certain distance travelled (as far as I can guess - you haven't mentioned what you do with your rays) so that would explain the arcing horizon. A perspective projection cannot by definition cause straight lines to appear curved, so the curved horizon doesn't come from the camera - as far as I can tell anyway, the camera code looks fine, although the camera position and direction are hardcoded.


The position isn't hardcoded, there is no rotation. To rotate a camera I have to multiply the ray direction vector by rotation matrix, right?

@edit And yes, I increased the max distance from 100 to 1000, and the plane is now flat. Thanks for the help. Edited by MrOMGWTF

Share this post


Link to post
Share on other sites

The position isn't hardcoded, there is no rotation. To rotate a camera I have to multiply the ray direction vector by rotation matrix, right?

Ah, my bad, correct about position. For rotation, yes that will work just fine.

@edit And yes, I increased the max distance from 100 to 1000, and the plane is now flat. Thanks for the help.[/quote]
You are welcome. Was your original question about field of view solved too, though?

Share this post


Link to post
Share on other sites
You are welcome. Was your original question about field of view solved too, though?

Yes, I'll use my current method. It's fine. Edited by MrOMGWTF

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement