1 | // tCamera.cpp  |
2 | //  |
3 | // The camera tScene object. The primary members are a 4x4 homogeneous projection matrix and a type member that  |
4 | // indicates the type of projection matrix being stored. We could add depth-of-field members at a later time.  |
5 | //  |
6 | // Copyright (c) 2006, 2017 Tristan Grimmer.  |
7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
9 | //  |
10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
14 | // PERFORMANCE OF THIS SOFTWARE.  |
15 |   |
16 | #include "Scene/tCamera.h"  |
17 | namespace tScene  |
18 | {  |
19 |   |
20 |   |
21 | void tCamera::Scale(float scale)  |
22 | {  |
23 | switch (ProjectionMode)  |
24 | {  |
25 | case tProjectionMode::Perspective:  |
26 | case tProjectionMode::PerspectiveOblique:  |
27 | {  |
28 | float fovV, fovH, aspect, nearPlane, farPlane, offsetX, offsetY;  |
29 | Projection.ExtractPerspective(fovV, fovH, aspect, nearPlane, farPlane, offsetX, offsetY);  |
30 | nearPlane *= scale;  |
31 | farPlane *= scale;  |
32 | Projection.MakeProjPerspFovVOffset(fovV, aspect, nearPlane, farPlane, offsetX, offsetY);  |
33 | break;  |
34 | }  |
35 |   |
36 | case tProjectionMode::Parallel:  |
37 | {  |
38 | tMath::tMatrix4 scaleMat;  |
39 | scaleMat.MakeScale(scale);  |
40 | Projection = scale*Projection;  |
41 | break;  |
42 | }  |
43 | }  |
44 | }  |
45 |   |
46 |   |
47 | void tCamera::Save(tChunkWriter& chunk) const  |
48 | {  |
49 | chunk.Begin(tChunkID::Scene_Camera);  |
50 | {  |
51 | chunk.Begin(tChunkID::Scene_Object);  |
52 | tObject::Save(chunk);  |
53 | chunk.End();  |
54 |   |
55 | chunk.Begin(tChunkID::Scene_CameraParameters);  |
56 | chunk.Write(ProjectionMode);  |
57 | chunk.End();  |
58 |   |
59 | chunk.Begin(tChunkID::Scene_Projection);  |
60 | chunk.Write(Projection);  |
61 | chunk.End();  |
62 | }  |
63 | chunk.End();  |
64 | }  |
65 |   |
66 |   |
67 | void tCamera::Load(const tChunk& camChunk)  |
68 | {  |
69 | tAssert(camChunk.ID() == tChunkID::Scene_Camera);  |
70 | Clear();  |
71 |   |
72 | for (tChunk chunk = camChunk.First(); chunk.Valid(); chunk = chunk.Next())  |
73 | {  |
74 | switch (chunk.ID())  |
75 | {  |
76 | case tChunkID::Scene_Object:  |
77 | tObject::Load(chunk);  |
78 | break;  |
79 |   |
80 | case tChunkID::Scene_CameraParameters:  |
81 | chunk.GetItem(ProjectionMode);  |
82 | break;  |
83 |   |
84 | case tChunkID::Scene_Projection:  |
85 | chunk.GetItem(Projection);  |
86 | break;  |
87 | }  |
88 | }  |
89 | }  |
90 |   |
91 |   |
92 | }  |
93 | |