opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27  
28 using System;
29 using System.Drawing;
30 using OpenMetaverse;
31  
32 namespace OpenSim.Region.CoreModules.World.Warp3DMap
33 {
34 public class Viewport
35 {
36 private const float DEG_TO_RAD = (float)Math.PI / 180f;
37 private static readonly Vector3 UP_DIRECTION = Vector3.UnitZ;
38  
39 public Vector3 Position;
40 public Vector3 LookDirection;
41 public float FieldOfView;
42 public float NearPlaneDistance;
43 public float FarPlaneDistance;
44 public int Width;
45 public int Height;
46 public bool Orthographic;
47 public float OrthoWindowWidth;
48 public float OrthoWindowHeight;
49  
50 public Viewport(Vector3 position, Vector3 lookDirection, float fieldOfView, float farPlaneDist, float nearPlaneDist, int width, int height)
51 {
52 // Perspective projection mode
53 Position = position;
54 LookDirection = lookDirection;
55 FieldOfView = fieldOfView;
56 FarPlaneDistance = farPlaneDist;
57 NearPlaneDistance = nearPlaneDist;
58 Width = width;
59 Height = height;
60 }
61  
62 public Viewport(Vector3 position, Vector3 lookDirection, float farPlaneDist, float nearPlaneDist, int width, int height, float orthoWindowWidth, float orthoWindowHeight)
63 {
64 // Orthographic projection mode
65 Position = position;
66 LookDirection = lookDirection;
67 FarPlaneDistance = farPlaneDist;
68 NearPlaneDistance = nearPlaneDist;
69 Width = width;
70 Height = height;
71 OrthoWindowWidth = orthoWindowWidth;
72 OrthoWindowHeight = orthoWindowHeight;
73 Orthographic = true;
74 }
75  
76 public Point VectorToScreen(Vector3 v)
77 {
78 Matrix4 m = GetWorldToViewportMatrix();
79 Vector3 screenPoint = v * m;
80 return new Point((int)screenPoint.X, (int)screenPoint.Y);
81 }
82  
83 public Matrix4 GetWorldToViewportMatrix()
84 {
85 Matrix4 result = GetViewMatrix();
86 result *= GetPerspectiveProjectionMatrix();
87 result *= GetViewportMatrix();
88  
89 return result;
90 }
91  
92 public Matrix4 GetViewMatrix()
93 {
94 Vector3 zAxis = -LookDirection;
95 zAxis.Normalize();
96  
97 Vector3 xAxis = Vector3.Cross(UP_DIRECTION, zAxis);
98 xAxis.Normalize();
99  
100 Vector3 yAxis = Vector3.Cross(zAxis, xAxis);
101  
102 Vector3 position = Position;
103 float offsetX = -Vector3.Dot(xAxis, position);
104 float offsetY = -Vector3.Dot(yAxis, position);
105 float offsetZ = -Vector3.Dot(zAxis, position);
106  
107 return new Matrix4(
108 xAxis.X, yAxis.X, zAxis.X, 0f,
109 xAxis.Y, yAxis.Y, zAxis.Y, 0f,
110 xAxis.Z, yAxis.Z, zAxis.Z, 0f,
111 offsetX, offsetY, offsetZ, 1f);
112 }
113  
114 public Matrix4 GetPerspectiveProjectionMatrix()
115 {
116 float aspectRatio = (float)Width / (float)Height;
117  
118 float hFoV = FieldOfView * DEG_TO_RAD;
119 float zn = NearPlaneDistance;
120 float zf = FarPlaneDistance;
121  
122 float xScale = 1f / (float)Math.Tan(hFoV / 2f);
123 float yScale = aspectRatio * xScale;
124 float m33 = (zf == double.PositiveInfinity) ? -1 : (zf / (zn - zf));
125 float m43 = zn * m33;
126  
127 return new Matrix4(
128 xScale, 0f, 0f, 0f,
129 0f, yScale, 0f, 0f,
130 0f, 0f, m33, -1f,
131 0f, 0f, m43, 0f);
132 }
133  
134 public Matrix4 GetOrthographicProjectionMatrix(float aspectRatio)
135 {
136 float w = Width;
137 float h = Height;
138 float zn = NearPlaneDistance;
139 float zf = FarPlaneDistance;
140  
141 float m33 = 1 / (zn - zf);
142 float m43 = zn * m33;
143  
144 return new Matrix4(
145 2f / w, 0f, 0f, 0f,
146 0f, 2f / h, 0f, 0f,
147 0f, 0f, m33, 0f,
148 0f, 0f, m43, 1f);
149 }
150  
151 public Matrix4 GetViewportMatrix()
152 {
153 float scaleX = (float)Width * 0.5f;
154 float scaleY = (float)Height * 0.5f;
155 float offsetX = 0f + scaleX;
156 float offsetY = 0f + scaleY;
157  
158 return new Matrix4(
159 scaleX, 0f, 0f, 0f,
160 0f, -scaleY, 0f, 0f,
161 0f, 0f, 1f, 0f,
162 offsetX, offsetY, 0f, 1f);
163 }
164 }
165 }