<p>Recently I've became an intern in <code>company-name</code> and my internship task is(for now) create rasterization using rays.</p>
<p>I'm mostly interested in pure math, so basic ideas on how to do it I've caught up really fast: I need to find an intersection of a ray that starts in center of a pixel with a plane of a triangle, compute barycentric coordinates in order to understand whether the intersection point is in the triangle or not.</p>
<p>But when it comes to code realization, I'm getting stuck.</p>
<p>First of all, and probably my main &quot;problem&quot; is that I can't really understand what &quot;camera&quot; is really about. I know it might have a lot of different properties(so called &quot;intrinsic&quot; and &quot;extrinsic&quot;), but I just need a basic one. For now I understand this as an &quot;eye&quot; located somewhere in the space.</p>
<p>And here is the second &quot;problem&quot;: I said, the camera is located somewhere in the space, but <em>where is it really located</em>? I really can't get how to put in in the space.  Also, I need to put the &quot;screen&quot; somewhere in the space as well, preferably between the &quot;eye&quot; and object/scene(I'm considering pinhole camera as a primer model).</p>
<p>For now, I do the following: I identify &quot;eye&quot; and &quot;screen&quot; as one object(going down to biology, &quot;screen&quot; is something like the cornea of a human eye). I scale my object/scene in <span class="math-container">$[-1,1]^3$</span>, and put my &quot;eye&quot;/&quot;screen&quot; on the 2-sphere of radius <span class="math-container">$2\sqrt3$</span>, because <span class="math-container">$[-1,1]^3$</span> is inside of this sphere. Therefore I can move my &quot;camera&quot; on the sphere, getting different camera positions. But I'm not quite sure that this is how it should be done.</p>
<p>So this are my problems, basically about the essence of camera(and so of screen) and its positions in the space. I hope someone will help me to tackle my confusion. Thanks.</p>

CC BY-SA 4.0
2021-05-06T21:41:34.430Z
Welcome to CG. I think it would be beneficial if you summarize what you are asking in the title or at the end of your question. Maybe it is just me being tired, but I had to re-read your question several times to get a clue what you are asking.
wychmaster
CC BY-SA 4.0
2021-05-06T21:45:13.250Z
You can represent your camera as you like. The simplest one is a pinhole camera. You have an origin $\vec{o}$ and a film $\vec{S}(u,v) = \vec{o} + z\vec{f} + u \vec{e}_1 + v\vec{e}_2$, where $\vec{f}$ is the forward vector of your camera, $z$ is the zoom, and $\vec{e}_1, \vec{e}_2$ are the edge vectors of the film. I typically produce a mapping from $[0,W] \times [0,H]$ to $[-W/H, W/H] \times [-1,1]$ which gives me $(u,v)$ for every pixel. Having this you construct the ray $(\vec{o}, \vec{S}(u,v) - \vec{o})$. You may also generate multiple samples within a pixel, but let's ignore that for now.
lightxbulb
CC BY-SA 4.0
2021-05-06T21:45:31.830Z
@wychmaster, thank you! I edited the title. Well, probably the question is really messy, sorry for this, I'll try to fix it with the fresh mind asap
Pavel Snopov
CC BY-SA 4.0
2021-05-06T21:49:02.060Z
@lightxbulb, can you elaborate a bit: considering the pinhole camera, I basically have an origin and a film. This construction is determined by an origin vector(basically a point in affine space), direction vector and basis vectors of a film. But where is this construction located in $\mathbb{R}^3$? Could it possibly be "inside" an object?
Pavel Snopov
CC BY-SA 4.0
2021-05-06T21:50:55.027Z
For a start you can pick: $\vec{o} = \vec{0}$, $\vec{f} = (0,0,1)$, $\vec{e}_1 = (1,0,0)$, $\vec{e}_2 = (0,1,0)$, $z= W/(H \tan(hfov/2))$ (where hfov is the horizontal field of view angle, e.g. you can pick $\pi/2$). Then you can situate your objects in front of it. You can move the camera by translating $\vec{o}$, and you can rotate it by applying a rotation matrix to the $\vec{f}, \vec{e}_1, \vec{e}_2$. Here's an example in code (I hope you know glsl): https://www.shadertoy.com/view/wlGcD1
lightxbulb
CC BY-SA 4.0
2021-05-06T21:56:58.200Z
@lightxbulb, thank you! Gonna try asap, but perhaps I'll run into some other problems. I hope it is okay for me to keep commenting here along the way(thus notifying you)
Pavel Snopov
CC BY-SA 4.0
2021-05-06T22:01:27.330Z
Typically it's not ok to message back an forth in the messages, that's what the chat is for. I can suggest joining a certain graphics programming discord (just google it), and then asking there since your goal doesn't really seem to fit stack's format.
lightxbulb
CC BY-SA 4.0
2021-05-07T18:36:31.060Z
@lightxbulb, Also, I see in your first comment you create a ray with direction $S(u,v) - o$. But can I simplify this up to using $f$ vector as the direction of a ray, that starts in $S(u,v)$ (possibly + $(0.5,0.5)$)? I thought that if I need to rasterize a pixel, I just can use this, and thought, that your formula is used mostly for shades or smth like this.
Also, can you say if I understand the picture correct: the "camera" and the "film" can be considered as an "united" object and basically the tuple $(o,f,e_1,e_2)$?
Pavel Snopov
CC BY-SA 4.0
2021-05-07T19:49:04.727Z
Using $f$ as direction for every pixel would produce a thing known as an orthographic camera. What I suggested is a pinhole or perspective camera, and is the "standard" one. I am not sure how it is relevant whether you construct a tuple or not.
lightxbulb
CC BY-SA 4.0
2021-05-07T21:42:57.997Z
As already mentioned by @lightxbulb , elongated comment discussions are usually discouraged and should be moved to the chat. However, since we already have quite a lot of comments, just go on and try to solve the problem. Once you got there, consider writing an answer containing all the information you gathered in the comments. I will do the cleanup afterwards.
wychmaster
CC BY-SA 4.0
2021-05-12T08:24:04.177Z
Okay, thank you all, I've made rasterization using rays but via orthographic camera, because before I was using orthographic projection. And now I consider camera as a tuple (origin, direction, basis vectors) of information, which is probably not natural way of thinking of camera(I mean, the most natural way of thinking a camera is just consider this as sort of a model of a real camera), but I found this helpful for me.
Pavel Snopov
<raytracing><physically-based><camera>
Ray casting and rasterization 101
Pavel Snopov
# Ray casting and rasterization 101

(Asked by: username_0 on 2021-05-06)

Recently I've became an intern in `company-name` and my internship task is(for now) create rasterization using rays.

I'm mostly interested in pure math, so basic ideas on how to do it I've caught up really fast: I need to find an intersection of a ray that starts in center of a pixel with a plane of a triangle, compute barycentric coordinates in order to understand whether the intersection point is in the triangle or not.

But when it comes to code realization, I'm getting stuck.

First of all, and probably my main "problem" is that I can't really understand what "camera" is really about. I know it might have a lot of different properties(so called "intrinsic" and "extrinsic"), but I just need a basic one. For now I understand this as an "eye" located somewhere in the space.

And here is the second "problem": I said, the camera is located somewhere in the space, but *where is it really located*? I really can't get how to put in in the space. Also, I need to put the "screen" somewhere in the space as well, preferably between the "eye" and object/scene(I'm considering pinhole camera as a primer model).

For now, I do the following: I identify "eye" and "screen" as one object(going down to biology, "screen" is something like the cornea of a human eye). I scale my object/scene in $[-1,1]^3$, and put my "eye"/"screen" on the 2-sphere of radius $2\sqrt3$, because $[-1,1]^3$ is inside of this sphere. Therefore I can move my "camera" on the sphere, getting different camera positions. But I'm not quite sure that this is how it should be done.

So this are my problems, basically about the essence of camera(and so of screen) and its positions in the space. I hope someone will help me to tackle my confusion. Thanks.

**Tags:** raytracing, physically-based, camera

> **Comments:**
> **username_1**: Welcome to CG. I think it would be beneficial if you summarize what you are asking in the title or at the end of your question. Maybe it is just me being tired, but I had to re-read your question several times to get a clue what you are asking.  
> **username_2**: You can represent your camera as you like. The simplest one is a pinhole camera. You have an origin $\vec{o}$ and a film $\vec{S}(u,v) = \vec{o} + z\vec{f} + u \vec{e}_1 + v\vec{e}_2$, where $\vec{f}$ is the forward vector of your camera, $z$ is the zoom, and $\vec{e}_1, \vec{e}_2$ are the edge vectors of the film. I typically produce a mapping from $[0,W] \times [0,H]$ to $[-W/H, W/H] \times [-1,1]$ which gives me $(u,v)$ for every pixel. Having this you construct the ray $(\vec{o}, \vec{S}(u,v) - \vec{o})$. You may also generate multiple samples within a pixel, but let's ignore that for now.  
> **username_0**: @username_1, thank you! I edited the title. Well, probably the question is really messy, sorry for this, I'll try to fix it with the fresh mind asap  
> **username_0**: @username_2, can you elaborate a bit: considering the pinhole camera, I basically have an origin and a film. This construction is determined by an origin vector(basically a point in affine space), direction vector and basis vectors of a film. But where is this construction located in $\mathbb{R}^3$? Could it possibly be "inside" an object?  
> **username_2**: For a start you can pick: $\vec{o} = \vec{0}$, $\vec{f} = (0,0,1)$, $\vec{e}_1 = (1,0,0)$, $\vec{e}_2 = (0,1,0)$, $z= W/(H \tan(hfov/2))$ (where hfov is the horizontal field of view angle, e.g. you can pick $\pi/2$). Then you can situate your objects in front of it. You can move the camera by translating $\vec{o}$, and you can rotate it by applying a rotation matrix to the $\vec{f}, \vec{e}_1, \vec{e}_2$. Here's an example in code (I hope you know glsl): https://www.shadertoy.com/view/wlGcD1  
> **username_0**: @username_2, thank you! Gonna try asap, but perhaps I'll run into some other problems. I hope it is okay for me to keep commenting here along the way(thus notifying you)  
> **username_2**: Typically it's not ok to message back an forth in the messages, that's what the chat is for. I can suggest joining a certain graphics programming discord (just google it), and then asking there since your goal doesn't really seem to fit stack's format.  
> **username_0**: @username_2, Also, I see in your first comment you create a ray with direction $S(u,v) - o$. But can I simplify this up to using $f$ vector as the direction of a ray, that starts in $S(u,v)$ (possibly + $(0.5,0.5)$)? I thought that if I need to rasterize a pixel, I just can use this, and thought, that your formula is used mostly for shades or smth like this. Also, can you say if I understand the picture correct: the "camera" and the "film" can be considered as an "united" object and basically the tuple $(o,f,e_1,e_2)$?  
> **username_2**: Using $f$ as direction for every pixel would produce a thing known as an orthographic camera. What I suggested is a pinhole or perspective camera, and is the "standard" one. I am not sure how it is relevant whether you construct a tuple or not.  
> **username_1**: As already mentioned by @username_2 , elongated comment discussions are usually discouraged and should be moved to the chat. However, since we already have quite a lot of comments, just go on and try to solve the problem. Once you got there, consider writing an answer containing all the information you gathered in the comments. I will do the cleanup afterwards.  
> **username_0**: Okay, thank you all, I've made rasterization using rays but via orthographic camera, because before I was using orthographic projection. And now I consider camera as a tuple (origin, direction, basis vectors) of information, which is probably not natural way of thinking of camera(I mean, the most natural way of thinking a camera is just consider this as sort of a model of a real camera), but I found this helpful for me.  

---

## 0 answers

