How to calculate ray












0












$begingroup$


In ray-tracing technique critical point is to calculate rays which came out from eye $E$ to target $T$ through pixel $P_{ij}$ on viewport. The "viewport" is represented as rectangle divided to square pixels - this rectangle is perpendicular to line which go through points $E$, $C$ (viewport center) and $T$. The ray (red line on image) is represented by point $E$ and unit vector $r_{ij}$ (not shown in picture but it lay on red line) - below is picture which show "geometry" - but what are the formulas to calculate $r_{ij}$?



The given input values are:




  • eye position $E$,

  • target position $T$,

  • field of view $theta$ (angle, for human eye $approx 90^circ$),

  • number of square pixels $k$ (horizontal direction) and $m$ (vertical direction).

  • we also know vertical $w$ vector usually equal to $w=[wx,wy,wz]=[0,1,0]$ (not shown on picture) which indicate where is up and where is down


The orthogonal vectors $v$ and $b$ (and $t$) on picture are determined by $w$ and $t=T-E$ and maybe will useful in $r_{ij}$ calculations. The $d$ and pixel size is arbitrary and don't change the result because of fixed $theta$.



Question: How to calculate unit vector $r_{ij}$ knowing input values described above?



enter image description here










share|cite|improve this question











$endgroup$












  • $begingroup$
    What exactly is it that you want to know?
    $endgroup$
    – amd
    Jan 16 at 0:48










  • $begingroup$
    @amd I update question - now it is more clear?
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:08










  • $begingroup$
    This is described in almost all tutorials and books on the subject.
    $endgroup$
    – lightxbulb
    Jan 16 at 6:45










  • $begingroup$
    @lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:55










  • $begingroup$
    Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
    $endgroup$
    – lightxbulb
    Jan 16 at 7:29
















0












$begingroup$


In ray-tracing technique critical point is to calculate rays which came out from eye $E$ to target $T$ through pixel $P_{ij}$ on viewport. The "viewport" is represented as rectangle divided to square pixels - this rectangle is perpendicular to line which go through points $E$, $C$ (viewport center) and $T$. The ray (red line on image) is represented by point $E$ and unit vector $r_{ij}$ (not shown in picture but it lay on red line) - below is picture which show "geometry" - but what are the formulas to calculate $r_{ij}$?



The given input values are:




  • eye position $E$,

  • target position $T$,

  • field of view $theta$ (angle, for human eye $approx 90^circ$),

  • number of square pixels $k$ (horizontal direction) and $m$ (vertical direction).

  • we also know vertical $w$ vector usually equal to $w=[wx,wy,wz]=[0,1,0]$ (not shown on picture) which indicate where is up and where is down


The orthogonal vectors $v$ and $b$ (and $t$) on picture are determined by $w$ and $t=T-E$ and maybe will useful in $r_{ij}$ calculations. The $d$ and pixel size is arbitrary and don't change the result because of fixed $theta$.



Question: How to calculate unit vector $r_{ij}$ knowing input values described above?



enter image description here










share|cite|improve this question











$endgroup$












  • $begingroup$
    What exactly is it that you want to know?
    $endgroup$
    – amd
    Jan 16 at 0:48










  • $begingroup$
    @amd I update question - now it is more clear?
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:08










  • $begingroup$
    This is described in almost all tutorials and books on the subject.
    $endgroup$
    – lightxbulb
    Jan 16 at 6:45










  • $begingroup$
    @lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:55










  • $begingroup$
    Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
    $endgroup$
    – lightxbulb
    Jan 16 at 7:29














0












0








0





$begingroup$


In ray-tracing technique critical point is to calculate rays which came out from eye $E$ to target $T$ through pixel $P_{ij}$ on viewport. The "viewport" is represented as rectangle divided to square pixels - this rectangle is perpendicular to line which go through points $E$, $C$ (viewport center) and $T$. The ray (red line on image) is represented by point $E$ and unit vector $r_{ij}$ (not shown in picture but it lay on red line) - below is picture which show "geometry" - but what are the formulas to calculate $r_{ij}$?



The given input values are:




  • eye position $E$,

  • target position $T$,

  • field of view $theta$ (angle, for human eye $approx 90^circ$),

  • number of square pixels $k$ (horizontal direction) and $m$ (vertical direction).

  • we also know vertical $w$ vector usually equal to $w=[wx,wy,wz]=[0,1,0]$ (not shown on picture) which indicate where is up and where is down


The orthogonal vectors $v$ and $b$ (and $t$) on picture are determined by $w$ and $t=T-E$ and maybe will useful in $r_{ij}$ calculations. The $d$ and pixel size is arbitrary and don't change the result because of fixed $theta$.



Question: How to calculate unit vector $r_{ij}$ knowing input values described above?



enter image description here










share|cite|improve this question











$endgroup$




In ray-tracing technique critical point is to calculate rays which came out from eye $E$ to target $T$ through pixel $P_{ij}$ on viewport. The "viewport" is represented as rectangle divided to square pixels - this rectangle is perpendicular to line which go through points $E$, $C$ (viewport center) and $T$. The ray (red line on image) is represented by point $E$ and unit vector $r_{ij}$ (not shown in picture but it lay on red line) - below is picture which show "geometry" - but what are the formulas to calculate $r_{ij}$?



The given input values are:




  • eye position $E$,

  • target position $T$,

  • field of view $theta$ (angle, for human eye $approx 90^circ$),

  • number of square pixels $k$ (horizontal direction) and $m$ (vertical direction).

  • we also know vertical $w$ vector usually equal to $w=[wx,wy,wz]=[0,1,0]$ (not shown on picture) which indicate where is up and where is down


The orthogonal vectors $v$ and $b$ (and $t$) on picture are determined by $w$ and $t=T-E$ and maybe will useful in $r_{ij}$ calculations. The $d$ and pixel size is arbitrary and don't change the result because of fixed $theta$.



Question: How to calculate unit vector $r_{ij}$ knowing input values described above?



enter image description here







analytic-geometry






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 23 at 12:36







Kamil Kiełczewski

















asked Jan 15 at 23:04









Kamil KiełczewskiKamil Kiełczewski

1267




1267












  • $begingroup$
    What exactly is it that you want to know?
    $endgroup$
    – amd
    Jan 16 at 0:48










  • $begingroup$
    @amd I update question - now it is more clear?
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:08










  • $begingroup$
    This is described in almost all tutorials and books on the subject.
    $endgroup$
    – lightxbulb
    Jan 16 at 6:45










  • $begingroup$
    @lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:55










  • $begingroup$
    Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
    $endgroup$
    – lightxbulb
    Jan 16 at 7:29


















  • $begingroup$
    What exactly is it that you want to know?
    $endgroup$
    – amd
    Jan 16 at 0:48










  • $begingroup$
    @amd I update question - now it is more clear?
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:08










  • $begingroup$
    This is described in almost all tutorials and books on the subject.
    $endgroup$
    – lightxbulb
    Jan 16 at 6:45










  • $begingroup$
    @lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
    $endgroup$
    – Kamil Kiełczewski
    Jan 16 at 6:55










  • $begingroup$
    Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
    $endgroup$
    – lightxbulb
    Jan 16 at 7:29
















$begingroup$
What exactly is it that you want to know?
$endgroup$
– amd
Jan 16 at 0:48




$begingroup$
What exactly is it that you want to know?
$endgroup$
– amd
Jan 16 at 0:48












$begingroup$
@amd I update question - now it is more clear?
$endgroup$
– Kamil Kiełczewski
Jan 16 at 6:08




$begingroup$
@amd I update question - now it is more clear?
$endgroup$
– Kamil Kiełczewski
Jan 16 at 6:08












$begingroup$
This is described in almost all tutorials and books on the subject.
$endgroup$
– lightxbulb
Jan 16 at 6:45




$begingroup$
This is described in almost all tutorials and books on the subject.
$endgroup$
– lightxbulb
Jan 16 at 6:45












$begingroup$
@lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
$endgroup$
– Kamil Kiełczewski
Jan 16 at 6:55




$begingroup$
@lightxbulb In all sources that I know - there are similar description to above, but no explicite formulas
$endgroup$
– Kamil Kiełczewski
Jan 16 at 6:55












$begingroup$
Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
$endgroup$
– lightxbulb
Jan 16 at 7:29




$begingroup$
Scratchapixel, pbrt-book, ray tracing in one weekend are all free access, and all of those explain how this is done.
$endgroup$
– lightxbulb
Jan 16 at 7:29










1 Answer
1






active

oldest

votes


















0












$begingroup$

IDEA: lets find position of center of each pixel $P_{ij}$ which allows us to easily find ray which starts at $E$ and go thought that pixel. To do it we find first $P_{1m}$ and find others by move on vievports plane.



ASSUMPTION: Below we introduce formulas which includes distance $d$ between eye and viewport however this value will be reduced during ray $r_{ij}$ normalization (so you might as well accept that $d=1$ and remove it from calculations).



PRECALCULATIONS: First we calculate normalized vectors $v_n, b_n$ from picutre (which are parallel to viewport plane and give as direction for shifting)



$$t = T-E, qquad b = wtimes t $$



$$
t_n = frac{t}{||t||}, qquad
b_n = frac{b}{||b||}, qquad
v_n = t_ntimes b_n \
$$



notice: $C=E+t_nd$, then we calculate viewport size divided by 2 and including aspect ratio $frac{m}{k}$



$$g_x=frac{h_x}{2} =d tan frac{theta}{2}, qquad g_y =frac{h_y}{2} = g_x frac{m}{k}$$



and then we calculate shifting vectors $q_x,q_y$ on viewport $x,y$ direction and viewport left upper pixel



$$ q_x = frac{2g_x}{k-1}b_n, qquad
q_y = frac{2g_y}{m-1}v_n, qquad
p_{1m} = t_n d - g_xb_n - g_yv_n$$



CALCULATIONS: notice that $P_{ij} = E + p_{ij}$ and ray $R_{ij} = P_{ij} -E = p_{ij}$ so normalized ray $r_{ij}$ is



$$ p_{ij} = p_{1m} + q_x(i-1) + q_y(j-1)$$
$$ r_{ij} = frac{p_{ij}}{||p_{ij}||} $$



TEST: above formulas wast tested here (works in browser)



SUMMARY: The above form is convenient to use it in shaders where in shader kernel we perform only final calculation based on prcarculated $q_x,q_y$ and $p_{1m}$. Wiki here.






share|cite|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3075098%2fhow-to-calculate-ray%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    IDEA: lets find position of center of each pixel $P_{ij}$ which allows us to easily find ray which starts at $E$ and go thought that pixel. To do it we find first $P_{1m}$ and find others by move on vievports plane.



    ASSUMPTION: Below we introduce formulas which includes distance $d$ between eye and viewport however this value will be reduced during ray $r_{ij}$ normalization (so you might as well accept that $d=1$ and remove it from calculations).



    PRECALCULATIONS: First we calculate normalized vectors $v_n, b_n$ from picutre (which are parallel to viewport plane and give as direction for shifting)



    $$t = T-E, qquad b = wtimes t $$



    $$
    t_n = frac{t}{||t||}, qquad
    b_n = frac{b}{||b||}, qquad
    v_n = t_ntimes b_n \
    $$



    notice: $C=E+t_nd$, then we calculate viewport size divided by 2 and including aspect ratio $frac{m}{k}$



    $$g_x=frac{h_x}{2} =d tan frac{theta}{2}, qquad g_y =frac{h_y}{2} = g_x frac{m}{k}$$



    and then we calculate shifting vectors $q_x,q_y$ on viewport $x,y$ direction and viewport left upper pixel



    $$ q_x = frac{2g_x}{k-1}b_n, qquad
    q_y = frac{2g_y}{m-1}v_n, qquad
    p_{1m} = t_n d - g_xb_n - g_yv_n$$



    CALCULATIONS: notice that $P_{ij} = E + p_{ij}$ and ray $R_{ij} = P_{ij} -E = p_{ij}$ so normalized ray $r_{ij}$ is



    $$ p_{ij} = p_{1m} + q_x(i-1) + q_y(j-1)$$
    $$ r_{ij} = frac{p_{ij}}{||p_{ij}||} $$



    TEST: above formulas wast tested here (works in browser)



    SUMMARY: The above form is convenient to use it in shaders where in shader kernel we perform only final calculation based on prcarculated $q_x,q_y$ and $p_{1m}$. Wiki here.






    share|cite|improve this answer











    $endgroup$


















      0












      $begingroup$

      IDEA: lets find position of center of each pixel $P_{ij}$ which allows us to easily find ray which starts at $E$ and go thought that pixel. To do it we find first $P_{1m}$ and find others by move on vievports plane.



      ASSUMPTION: Below we introduce formulas which includes distance $d$ between eye and viewport however this value will be reduced during ray $r_{ij}$ normalization (so you might as well accept that $d=1$ and remove it from calculations).



      PRECALCULATIONS: First we calculate normalized vectors $v_n, b_n$ from picutre (which are parallel to viewport plane and give as direction for shifting)



      $$t = T-E, qquad b = wtimes t $$



      $$
      t_n = frac{t}{||t||}, qquad
      b_n = frac{b}{||b||}, qquad
      v_n = t_ntimes b_n \
      $$



      notice: $C=E+t_nd$, then we calculate viewport size divided by 2 and including aspect ratio $frac{m}{k}$



      $$g_x=frac{h_x}{2} =d tan frac{theta}{2}, qquad g_y =frac{h_y}{2} = g_x frac{m}{k}$$



      and then we calculate shifting vectors $q_x,q_y$ on viewport $x,y$ direction and viewport left upper pixel



      $$ q_x = frac{2g_x}{k-1}b_n, qquad
      q_y = frac{2g_y}{m-1}v_n, qquad
      p_{1m} = t_n d - g_xb_n - g_yv_n$$



      CALCULATIONS: notice that $P_{ij} = E + p_{ij}$ and ray $R_{ij} = P_{ij} -E = p_{ij}$ so normalized ray $r_{ij}$ is



      $$ p_{ij} = p_{1m} + q_x(i-1) + q_y(j-1)$$
      $$ r_{ij} = frac{p_{ij}}{||p_{ij}||} $$



      TEST: above formulas wast tested here (works in browser)



      SUMMARY: The above form is convenient to use it in shaders where in shader kernel we perform only final calculation based on prcarculated $q_x,q_y$ and $p_{1m}$. Wiki here.






      share|cite|improve this answer











      $endgroup$
















        0












        0








        0





        $begingroup$

        IDEA: lets find position of center of each pixel $P_{ij}$ which allows us to easily find ray which starts at $E$ and go thought that pixel. To do it we find first $P_{1m}$ and find others by move on vievports plane.



        ASSUMPTION: Below we introduce formulas which includes distance $d$ between eye and viewport however this value will be reduced during ray $r_{ij}$ normalization (so you might as well accept that $d=1$ and remove it from calculations).



        PRECALCULATIONS: First we calculate normalized vectors $v_n, b_n$ from picutre (which are parallel to viewport plane and give as direction for shifting)



        $$t = T-E, qquad b = wtimes t $$



        $$
        t_n = frac{t}{||t||}, qquad
        b_n = frac{b}{||b||}, qquad
        v_n = t_ntimes b_n \
        $$



        notice: $C=E+t_nd$, then we calculate viewport size divided by 2 and including aspect ratio $frac{m}{k}$



        $$g_x=frac{h_x}{2} =d tan frac{theta}{2}, qquad g_y =frac{h_y}{2} = g_x frac{m}{k}$$



        and then we calculate shifting vectors $q_x,q_y$ on viewport $x,y$ direction and viewport left upper pixel



        $$ q_x = frac{2g_x}{k-1}b_n, qquad
        q_y = frac{2g_y}{m-1}v_n, qquad
        p_{1m} = t_n d - g_xb_n - g_yv_n$$



        CALCULATIONS: notice that $P_{ij} = E + p_{ij}$ and ray $R_{ij} = P_{ij} -E = p_{ij}$ so normalized ray $r_{ij}$ is



        $$ p_{ij} = p_{1m} + q_x(i-1) + q_y(j-1)$$
        $$ r_{ij} = frac{p_{ij}}{||p_{ij}||} $$



        TEST: above formulas wast tested here (works in browser)



        SUMMARY: The above form is convenient to use it in shaders where in shader kernel we perform only final calculation based on prcarculated $q_x,q_y$ and $p_{1m}$. Wiki here.






        share|cite|improve this answer











        $endgroup$



        IDEA: lets find position of center of each pixel $P_{ij}$ which allows us to easily find ray which starts at $E$ and go thought that pixel. To do it we find first $P_{1m}$ and find others by move on vievports plane.



        ASSUMPTION: Below we introduce formulas which includes distance $d$ between eye and viewport however this value will be reduced during ray $r_{ij}$ normalization (so you might as well accept that $d=1$ and remove it from calculations).



        PRECALCULATIONS: First we calculate normalized vectors $v_n, b_n$ from picutre (which are parallel to viewport plane and give as direction for shifting)



        $$t = T-E, qquad b = wtimes t $$



        $$
        t_n = frac{t}{||t||}, qquad
        b_n = frac{b}{||b||}, qquad
        v_n = t_ntimes b_n \
        $$



        notice: $C=E+t_nd$, then we calculate viewport size divided by 2 and including aspect ratio $frac{m}{k}$



        $$g_x=frac{h_x}{2} =d tan frac{theta}{2}, qquad g_y =frac{h_y}{2} = g_x frac{m}{k}$$



        and then we calculate shifting vectors $q_x,q_y$ on viewport $x,y$ direction and viewport left upper pixel



        $$ q_x = frac{2g_x}{k-1}b_n, qquad
        q_y = frac{2g_y}{m-1}v_n, qquad
        p_{1m} = t_n d - g_xb_n - g_yv_n$$



        CALCULATIONS: notice that $P_{ij} = E + p_{ij}$ and ray $R_{ij} = P_{ij} -E = p_{ij}$ so normalized ray $r_{ij}$ is



        $$ p_{ij} = p_{1m} + q_x(i-1) + q_y(j-1)$$
        $$ r_{ij} = frac{p_{ij}}{||p_{ij}||} $$



        TEST: above formulas wast tested here (works in browser)



        SUMMARY: The above form is convenient to use it in shaders where in shader kernel we perform only final calculation based on prcarculated $q_x,q_y$ and $p_{1m}$. Wiki here.







        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        edited Jan 24 at 10:35

























        answered Jan 21 at 21:37









        Kamil KiełczewskiKamil Kiełczewski

        1267




        1267






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3075098%2fhow-to-calculate-ray%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mario Kart Wii

            What does “Dominus providebit” mean?

            Antonio Litta Visconti Arese