1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 /*
47  * camera.cpp - This file contains all of the functions for doing camera work.
48  */
49 
50 #include "machine.hpp"
51 #include "types.hpp"
52 #include "macros.hpp"
53 #include "vector.hpp"
54 #include "camera.hpp"
55 #include "util.hpp"
56 
camray(scenedef * scene,int x,int y)57 ray camray(scenedef *scene, int x, int y) {
58     ray ray1, newray;
59     vector projcent;
60     vector projpixel;
61     flt px, py, sx, sy;
62 
63     sx = (flt)scene->hres;
64     sy = (flt)scene->vres;
65 
66     /* calculate the width and height of the image plane given   */
67     /* the aspect ratio, image resolution, and zoom factor       */
68 
69     px = ((sx / sy) / scene->aspectratio) / scene->camzoom;
70     py = 1.0 / scene->camzoom;
71 
72     /* assuming viewvec is a unit vector, then the center of the */
73     /* image plane is the camera center + vievec                 */
74     projcent.x = scene->camcent.x + scene->camviewvec.x;
75     projcent.y = scene->camcent.y + scene->camviewvec.y;
76     projcent.z = scene->camcent.z + scene->camviewvec.z;
77 
78     /* starting from the center of the image plane, we move the   */
79     /* center of the pel we're calculating, to                    */
80     /* projcent + (rightvec * x distance)                         */
81     ray1.o = projcent;
82     ray1.d = scene->camrightvec;
83     projpixel = Raypnt(&ray1, ((x * px / sx) - (px / 2.0)));
84 
85     /* starting from the horizontally translated pel, we move the */
86     /* center of the pel we're calculating, to                    */
87     /* projcent + (upvec * y distance)                            */
88     ray1.o = projpixel;
89     ray1.d = scene->camupvec;
90     projpixel = Raypnt(&ray1, ((y * py / sy) - (py / 2.0)));
91 
92     /* now that we have the exact pel center in the image plane */
93     /* we create the real primary ray that will be used by the  */
94     /* rest of the system.                                      */
95     /* The ray is expected to be re-normalized elsewhere, we're */
96     /* only really concerned about getting its direction right. */
97     newray.o = scene->camcent;
98     VSub(&projpixel, &scene->camcent, &newray.d);
99     newray.depth = scene->raydepth;
100     newray.flags = RT_RAY_REGULAR; /* camera only generates primary rays */
101 
102     return newray;
103 }
104