OSDN Git Service

922988e82f17e6fb66f547500ce56b890784f9a9
[dtxmania/dtxmania.git] / FDK17プロジェクト / コード / 01.フレームワーク / Utilities / Camera.cs
1 /*\r
2 * Copyright (c) 2007-2009 SlimDX Group\r
3\r
4 * Permission is hereby granted, free of charge, to any person obtaining a copy\r
5 * of this software and associated documentation files (the "Software"), to deal\r
6 * in the Software without restriction, including without limitation the rights\r
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
8 * copies of the Software, and to permit persons to whom the Software is\r
9 * furnished to do so, subject to the following conditions:\r
10\r
11 * The above copyright notice and this permission notice shall be included in\r
12 * all copies or substantial portions of the Software.\r
13\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
20 * THE SOFTWARE.\r
21 */\r
22 using SharpDX;\r
23 \r
24 namespace SampleFramework\r
25 {\r
26     /// <summary>\r
27     /// Represents a view onto a 3D scene.\r
28     /// </summary>\r
29     public class Camera\r
30     {\r
31         Vector3 location;\r
32         Vector3 target;\r
33         float fieldOfView;\r
34         float aspectRatio;\r
35         float nearPlane;\r
36         float farPlane;\r
37         Matrix viewMatrix;\r
38         Matrix projectionMatrix;\r
39         bool viewDirty = true;\r
40         bool projectionDirty = true;\r
41 \r
42         /// <summary>\r
43         /// Gets or sets the location of the camera eye point.\r
44         /// </summary>\r
45         /// <value>The location of the camera eye point.</value>\r
46         public Vector3 Location\r
47         {\r
48             get { return location; }\r
49             set\r
50             {\r
51                 if (location == value)\r
52                     return;\r
53 \r
54                 location = value;\r
55                 viewDirty = true;\r
56             }\r
57         }\r
58 \r
59         /// <summary>\r
60         /// Gets or sets the view target point.\r
61         /// </summary>\r
62         /// <value>The view target point.</value>\r
63         public Vector3 Target\r
64         {\r
65             get { return target; }\r
66             set\r
67             {\r
68                 if (target == value)\r
69                     return;\r
70 \r
71                 target = value;\r
72                 viewDirty = true;\r
73             }\r
74         }\r
75 \r
76         /// <summary>\r
77         /// Gets or sets the field of view.\r
78         /// </summary>\r
79         /// <value>The field of view.</value>\r
80         public float FieldOfView\r
81         {\r
82             get { return fieldOfView; }\r
83             set\r
84             {\r
85                 if (fieldOfView == value)\r
86                     return;\r
87 \r
88                 fieldOfView = value;\r
89                 projectionDirty = true;\r
90             }\r
91         }\r
92 \r
93         /// <summary>\r
94         /// Gets or sets the aspect ratio.\r
95         /// </summary>\r
96         /// <value>The aspect ratio.</value>\r
97         public float AspectRatio\r
98         {\r
99             get { return aspectRatio; }\r
100             set\r
101             {\r
102                 if (aspectRatio == value)\r
103                     return;\r
104 \r
105                 aspectRatio = value;\r
106                 projectionDirty = true;\r
107             }\r
108         }\r
109 \r
110         /// <summary>\r
111         /// Gets or sets the near plane.\r
112         /// </summary>\r
113         /// <value>The near plane.</value>\r
114         public float NearPlane\r
115         {\r
116             get { return nearPlane; }\r
117             set\r
118             {\r
119                 if (nearPlane == value)\r
120                     return;\r
121 \r
122                 nearPlane = value;\r
123                 projectionDirty = true;\r
124             }\r
125         }\r
126 \r
127         /// <summary>\r
128         /// Gets or sets the far plane.\r
129         /// </summary>\r
130         /// <value>The far plane.</value>\r
131         public float FarPlane\r
132         {\r
133             get { return farPlane; }\r
134             set\r
135             {\r
136                 if (farPlane == value)\r
137                     return;\r
138 \r
139                 farPlane = value;\r
140                 projectionDirty = true;\r
141             }\r
142         }\r
143 \r
144         /// <summary>\r
145         /// Gets the view matrix.\r
146         /// </summary>\r
147         /// <value>The view matrix.</value>\r
148         public Matrix ViewMatrix\r
149         {\r
150             get\r
151             {\r
152                 if (viewDirty)\r
153                     RebuildViewMatrix();\r
154                 return viewMatrix;\r
155             }\r
156         }\r
157 \r
158         /// <summary>\r
159         /// Gets the projection matrix.\r
160         /// </summary>\r
161         /// <value>The projection matrix.</value>\r
162         public Matrix ProjectionMatrix\r
163         {\r
164             get\r
165             {\r
166                 if (projectionDirty)\r
167                     RebuildProjectionMatrix();\r
168                 return projectionMatrix;\r
169             }\r
170         }\r
171 \r
172         /// <summary>\r
173         /// Initializes a new instance of the <see cref="Camera"/> class.\r
174         /// </summary>\r
175         public Camera()\r
176         {\r
177         }\r
178 \r
179         /// <summary>\r
180         /// Rebuilds the view matrix.\r
181         /// </summary>\r
182         protected virtual void RebuildViewMatrix()\r
183         {\r
184             viewMatrix = Matrix.LookAtLH(Location, Target, Vector3.UnitY);\r
185             viewDirty = false;\r
186         }\r
187 \r
188         /// <summary>\r
189         /// Rebuilds the projection matrix.\r
190         /// </summary>\r
191         protected virtual void RebuildProjectionMatrix()\r
192         {\r
193             projectionMatrix = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, NearPlane, FarPlane);\r
194             projectionDirty = false;\r
195         }\r
196     }\r
197 }\r