CSharpGL(4)设计和使用Camera

+BIT祝威+悄悄在此留下版了个权的信息说:

描述在OpenGL中Camera的概念和用处。

设计一个Camera以及操控Camera的SatelliteRotator。

以PyramidElement为例演示如何使用Camera和SatelliteRotator。

+BIT祝威+悄悄在此留下版了个权的信息说:

下载

您可以在(https://github.com/bitzhuwei/CSharpGL)找到最新的源码。欢迎感兴趣的同学fork之。

+BIT祝威+悄悄在此留下版了个权的信息说:

Camera

CSharpGL(4)设计和使用Camera

在OpenGL中的Camera概念可以方便我们设定projection矩阵和view矩阵。

定义

  1     /// <summary>
  2     /// 摄像机。
  3     /// </summary>
  4     public class Camera :
  5         ICamera,
  6         IPerspectiveViewCamera, IOrthoViewCamera,
  7         IViewCamera, IPerspectiveCamera, IOrthoCamera
  8     {
  9         /// <summary>
 10         /// 默认目标为vec3(0, 0, 0)
 11         /// </summary>
 12         public static readonly vec3 defaultTarget = new vec3(0, 0, 0);
 13 
 14         /// <summary>
 15         /// 默认位置为vec3(0, 0, 1)
 16         /// </summary>
 17         public static readonly vec3 defaultPosition = new vec3(0, 0, 1);
 18 
 19         /// <summary>
 20         /// 默认上方为vec3(0, 1, 0)
 21         /// </summary>
 22         public static readonly vec3 defaultUpVector = new vec3(0, 1, 0);
 23 
 24         internal Camera() { }
 25 
 26         /// <summary>
 27         /// 摄像机。
 28         /// </summary>
 29         /// <param name="cameraType">类型</param>
 30         /// <param name="width">OpenGL窗口的宽度</param>
 31         /// <param name="height">OpenGL窗口的高度</param>
 32         public Camera(CameraType cameraType, double width, double height)
 33         {
 34             this.lastHeight = width;
 35             this.lastHeight = height;
 36 
 37             IPerspectiveCamera perspectiveCamera = this;
 38             perspectiveCamera.FieldOfView = 60.0f;
 39             perspectiveCamera.AspectRatio = width / height;
 40             perspectiveCamera.Near = 0.01;
 41             perspectiveCamera.Far = 10000;
 42 
 43             const int factor = 100;
 44             IOrthoCamera orthoCamera = this;
 45             orthoCamera.Left = -width / 2 / factor;
 46             orthoCamera.Right = width / 2 / factor;
 47             orthoCamera.Bottom = -height / 2 / factor;
 48             orthoCamera.Top = height / 2 / factor;
 49             orthoCamera.Near = -10000;
 50             orthoCamera.Far = 10000;
 51 
 52             this.Target = defaultTarget;
 53             this.Position = defaultPosition;
 54             this.UpVector = defaultUpVector;
 55 
 56             this.CameraType = cameraType;
 57         }
 58 
 59         public void Resize(double width, double height)
 60         {
 61             double aspectRatio = width / height;
 62 
 63             IPerspectiveCamera perspectiveCamera = this;
 64             perspectiveCamera.AspectRatio = aspectRatio;
 65 
 66             IOrthoCamera orthoCamera = this;
 67 
 68             double lastAspectRatio = this.lastWidth / this.lastHeight;
 69             if (aspectRatio > lastAspectRatio)
 70             {
 71                 double top = orthoCamera.Top;
 72                 double newRight = top * aspectRatio;
 73                 orthoCamera.Left = -newRight;
 74                 orthoCamera.Right = newRight;
 75             }
 76             else if (aspectRatio < lastAspectRatio)
 77             {
 78                 double right = orthoCamera.Right;
 79                 double newTop = right / aspectRatio;
 80                 orthoCamera.Bottom = -newTop;
 81                 orthoCamera.Top = newTop;
 82             }
 83 
 84             //const int factor = 100;
 85             //if (width / 2 / factor != orthoCamera.Right)
 86             //{
 87             //    orthoCamera.Left = -width / 2 / factor;
 88             //    orthoCamera.Right = width / 2 / factor;
 89             //}
 90             //if (height / 2 / factor != orthoCamera.Top)
 91             //{
 92             //    orthoCamera.Bottom = -height / 2 / factor;
 93             //    orthoCamera.Top = height / 2 / factor;
 94             //}
 95         }
 96 
 97         double lastWidth;
 98         double lastHeight;
 99 
100         /// <summary>
101         /// Gets or sets the target.
102         /// </summary>
103         /// <value>
104         /// The target.
105         /// </value>
106         [Description("The target of the camera (the point it's looking at)"), Category("Camera")]
107         public vec3 Target { get; set; }
108 
109         /// <summary>
110         /// Gets or sets up vector.
111         /// </summary>
112         /// <value>
113         /// Up vector.
114         /// </value>
115         [Description("The up direction, relative to camera. (Controls tilt)."), Category("Camera")]
116         public vec3 UpVector { get; set; }
117 
118         /// <summary>
119         /// The camera position.
120         /// </summary>
121         private vec3 position = new vec3(0, 0, 0);
122 
123         /// <summary>
124         /// Every time a camera is used to project, the projection matrix calculated
125         /// and stored here.
126         /// </summary>
127         private mat4 projectionMatrix = mat4.identity();
128 
129         ///// <summary>
130         ///// The screen aspect ratio.
131         ///// </summary>
132         //private double aspectRatio = 1.0f;
133 
134         /// <summary>
135         /// Gets or sets the position.
136         /// </summary>
137         /// <value>
138         /// The position.
139         /// </value>
140         [Description("The position of the camera"), Category("Camera")]
141         public vec3 Position
142         {
143             get { return position; }
144             set { position = value; }
145         }
146 
147         /// <summary>
148         /// camera's perspective type.
149         /// </summary>
150         public CameraType CameraType { get; set; }
151 
152         #region IPerspectiveCamera 成员
153 
154         double IPerspectiveCamera.FieldOfView { get; set; }
155 
156         double IPerspectiveCamera.AspectRatio { get; set; }
157 
158         double IPerspectiveCamera.Near { get; set; }
159 
160         double IPerspectiveCamera.Far { get; set; }
161 
162         #endregion
163 
164         #region IOrthoCamera 成员
165 
166         double IOrthoCamera.Left { get; set; }
167 
168         double IOrthoCamera.Right { get; set; }
169 
170         double IOrthoCamera.Bottom { get; set; }
171 
172         double IOrthoCamera.Top { get; set; }
173 
174         double IOrthoCamera.Near { get; set; }
175 
176         double IOrthoCamera.Far { get; set; }
177 
178         #endregion
179     }
Camera

相关文章: