Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ClipRegion.h
Go to the documentation of this file.
1/****************************************************************************/
2/* Copyright (c) 2011, Ola Olsson
3*
4* Permission is hereby granted, free of charge, to any person obtaining a copy
5* of this software and associated documentation files (the "Software"), to deal
6* in the Software without restriction, including without limitation the rights
7* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8* copies of the Software, and to permit persons to whom the Software is
9* furnished to do so, subject to the following conditions:
10*
11* The above copyright notice and this permission notice shall be included in
12* all copies or substantial portions of the Software.
13*
14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20* THE SOFTWARE.
21*/
22/****************************************************************************/
28// Original copyright notice:
29
30// Copyright 2010 Intel Corporation
31// All Rights Reserved
32//
33// Permission is granted to use, copy, distribute and Prepare derivative works
34// of this
35// software for any purpose and without fee, provided, that the above copyright
36// notice
37// and this statement appear in all copies. Intel makes no representations
38// about the
39// suitability of this software for any purpose. THIS SOFTWARE IS PROVIDED "AS
40// IS."
41// INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, AND ALL
42// LIABILITY,
43// INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THIS
44// SOFTWARE,
45// INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS, AND INCLUDING
46// THE
47// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Intel
48// does not
49// assume any responsibility for any errors which may appear in this software
50// nor any
51// responsibility to update it.
52
53#pragma once
54#ifndef DVD_CLIP_REGION_H_
55#define DVD_CLIP_REGION_H_
56
57namespace Divide {
58
59//--------------------------------------------------------------------------------------
60// Bounds computation utilities, similar to PointLightBounds.cpp
61inline
63 F32 nc, // Tangent plane x/y normal coordinate (view space)
64 F32 lc, // Light x/y coordinate (view space)
65 F32 lz, // Light z coordinate (view space)
66 F32 lightRadius,
67 F32 cameraScale, // Project scale for coordinate (_11 or _22 for x/y
68 // respectively)
69 F32 &clipMin, F32 &clipMax) {
70 F32 nz = (lightRadius - nc * lc) / lz;
71 F32 pz =
72 (lc * lc + lz * lz - lightRadius * lightRadius) / (lz - nz / nc * lc);
73
74 if (pz < 0.0f) {
75 F32 c = -nz * cameraScale / nc;
76 if (nc < 0.0f) {
77 // Left side boundary
78 clipMin = std::max(clipMin, c);
79 } else { // Right side boundary
80 clipMax = std::min(clipMax, c);
81 }
82 }
83}
84
85inline
86void updateClipRegion(F32 lc, // Light x/y coordinate (view space)
87 F32 lz, // Light z coordinate (view space)
88 F32 lightRadius,
89 F32 cameraScale, // Project scale for coordinate (_11 or
90 // _22 for x/y respectively)
91 F32 &clipMin, F32 &clipMax) {
92 F32 rSq = lightRadius * lightRadius;
93 F32 lcSqPluslzSq = lc * lc + lz * lz;
94 F32 d = rSq * lc * lc - lcSqPluslzSq * (rSq - lz * lz);
95
96 if (d >= 0.0f) {
97 F32 a = lightRadius * lc;
98 F32 b = sqrt(d);
99 F32 nx0 = (a + b) / lcSqPluslzSq;
100 F32 nx1 = (a - b) / lcSqPluslzSq;
101
102 updateClipRegionRoot(nx0, lc, lz, lightRadius, cameraScale, clipMin,
103 clipMax);
104 updateClipRegionRoot(nx1, lc, lz, lightRadius, cameraScale, clipMin,
105 clipMax);
106 }
107}
108
109// Returns bounding box [min.xy, max.xy] in clip [-1, 1] space.
110inline
111vec4<F32> computeClipRegion(const vec3<F32> &lightPosView, F32 lightRadius,
112 F32 cameraNear, const mat4<F32> &projection) {
113 // Early out with empty rectangle if the light is too far behind the view
114 // frustum
115 vec4<F32> clipRegion(1.0f, 1.0f, -1.0f, -1.0f);
116 if (lightPosView.z - lightRadius <= -cameraNear) {
117 vec2<F32> clipMin(-1.0f, -1.0f);
118 vec2<F32> clipMax(1.0f, 1.0f);
119
120 updateClipRegion(lightPosView.x, lightPosView.z, lightRadius,
121 projection.element(1, 1), clipMin.x, clipMax.x);
122
123 updateClipRegion(lightPosView.y, lightPosView.z, lightRadius,
124 projection.element(2, 2), clipMin.y, clipMax.y);
125
126 clipRegion.set(clipMin, clipMax);
127 }
128
129 return clipRegion;
130}
131}
132#endif // DVD_CLIP_REGION_H_
T & element(U8 row, U8 column) noexcept
void set(const T *v) noexcept
set the 4 components of the vector manually using a source pointer to a (large enough) array
Definition: MathVectors.h:1241
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
vec4< F32 > computeClipRegion(const vec3< F32 > &lightPosView, F32 lightRadius, F32 cameraNear, const mat4< F32 > &projection)
Definition: ClipRegion.h:111
void updateClipRegionRoot(F32 nc, F32 lc, F32 lz, F32 lightRadius, F32 cameraScale, F32 &clipMin, F32 &clipMax)
Definition: ClipRegion.h:62
void updateClipRegion(F32 lc, F32 lz, F32 lightRadius, F32 cameraScale, F32 &clipMin, F32 &clipMax)
Definition: ClipRegion.h:86