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