~sladen/openbve/openbve2

« back to all changes in this revision

Viewing changes to OpenBve/Graphics/Camera.cs

  • Committer: Paul Sladen
  • Author(s): Michelle Boucquemont
  • Date: 2010-03-17 04:04:44 UTC
  • Revision ID: git-v1:c9050dde4c55fe2b9c257080653cee1a07c91303
import openbve2.zip (2010-03-14)

You will find a settings.cfg file where you can configure a series of
options and also which route or object to load. The options are
explained briefly via the comments in the settings.cfg file itself.
Select any route or object you want, but mind the following limitations:

* Background images and fog are not yet supported. Only a background
  color will be shown.
* Animated objects are not yet supported. This includes signals. These
  objects are not shown at all.
* Glow is not yet supported. All objects using glow will always show at
  full intensity instead of fading in.
* The legacy visibility system (where objects are disposed of as soon as
  the CSV/RW block the object was placed in has been passed by the camera)
  is not yet supported, only the new 2D grid is.

In-game, you can use the arrow, the WASD and the PageUp and PageDown keys to move and rotate around. Hold down the shift, control or alt key for faster movement. There is no way for you to jump to specific "track positions" as the new program does not have something like them internally. Also, you cannot jump to stations in any way at the moment. The initial position of the camera is the first stop point, though.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
using Tao.OpenGl;
3
3
 
4
4
namespace OpenBve {
5
 
 
6
5
        internal static class Camera {
7
6
 
8
7
                // members
9
8
                internal static OpenBveApi.Math.Vector3 Position = new OpenBveApi.Math.Vector3(0.0, 0.0, 0.0);
10
9
                internal static OpenBveApi.Math.Orientation3 Orientation = OpenBveApi.Math.Orientation3.Default;
 
10
                internal static OpenBveApi.Math.Vector3 GridLeafNodeCenter = new OpenBveApi.Math.Vector3(0.0, 0.0, 0.0);
 
11
                internal static ObjectGrid.GridLeafNode GridLeafNode = null;
 
12
                internal static ViewportOptions Viewport = new ViewportOptions(1.0, 1.0, 1.0, 2.0);
11
13
                
12
 
                // viewport
 
14
                // viewport options
13
15
                /// <summary>Stores options required to set up the viewport.</summary>
14
16
                internal struct ViewportOptions {
 
17
                        // members
15
18
                        /// <summary>The horizontal viewing angle in radians.</summary>
16
19
                        internal double HorizontalViewingAngle;
17
20
                        /// <summary>The vertical viewing angle in radians.</summary>
18
21
                        internal double VerticalViewingAngle;
19
22
                        internal double NearClippingPlane;
20
23
                        internal double FarClippingPlane;
 
24
                        // constructors
21
25
                        /// <summary>Creates a new instance of the ViewportOptions structure.</summary>
22
26
                        /// <param name="HorizontalViewingAngle">The horizontal viewing angle in radians.</param>
23
27
                        /// <param name="VerticalViewingAngle">The vertical viewing angle in radians.</param>
24
28
                        /// <param name="NearClippingPlane">The distance to the near clipping plane in meters.</param>
25
29
                        /// <param name="FarClippingPlane">The distance to the far clipping plane in meters.</param>
26
 
                        internal ViewportOptions(double HorizontalViewingAngle, double VerticalViewingAngle, double NearClippingPlane, double FarClippingPlane) {
27
 
                                this.HorizontalViewingAngle = HorizontalViewingAngle;
28
 
                                this.VerticalViewingAngle = VerticalViewingAngle;
29
 
                                this.NearClippingPlane = NearClippingPlane;
30
 
                                this.FarClippingPlane = FarClippingPlane;
 
30
                        internal ViewportOptions(double horizontalViewingAngle, double verticalViewingAngle, double nearClippingPlane, double farClippingPlane) {
 
31
                                this.HorizontalViewingAngle = horizontalViewingAngle;
 
32
                                this.VerticalViewingAngle = verticalViewingAngle;
 
33
                                this.NearClippingPlane = nearClippingPlane;
 
34
                                this.FarClippingPlane = farClippingPlane;
31
35
                        }
32
36
                        /// <summary>Creates a new instance of the ViewportOptions structure.</summary>
33
37
                        /// <param name="VerticalViewingAngle">The vertical viewing angle in radians.</param>
34
38
                        /// <param name="NearClippingPlane">The distance to the near clipping plane in meters.</param>
35
39
                        /// <param name="FarClippingPlane">The distance to the far clipping plane in meters.</param>
36
40
                        /// <remarks>The horizontal viewing angle is determined automatically from the vertical viewing angle and the window's aspect ratio.</remarks>
37
 
                        internal ViewportOptions(double VerticalViewingAngle, double NearClippingPlane, double FarClippingPlane) {
38
 
                                this.HorizontalViewingAngle = 2.0 * Math.Atan(Math.Tan(0.5 * VerticalViewingAngle) * Window.Properties.AspectRatio);
39
 
                                this.VerticalViewingAngle = VerticalViewingAngle;
40
 
                                this.NearClippingPlane = NearClippingPlane;
41
 
                                this.FarClippingPlane = FarClippingPlane;
 
41
                        internal ViewportOptions(double verticalViewingAngle, double nearClippingPlane, double farClippingPlane) {
 
42
                                this.HorizontalViewingAngle = 2.0 * Math.Atan(Math.Tan(0.5 * verticalViewingAngle) * Screen.Properties.AspectRatio);
 
43
                                this.VerticalViewingAngle = verticalViewingAngle;
 
44
                                this.NearClippingPlane = nearClippingPlane;
 
45
                                this.FarClippingPlane = farClippingPlane;
42
46
                        }
43
47
                }
 
48
                
 
49
                // set viewport
44
50
                /// <summary>Sets the viewport.</summary>
45
51
                /// <param name="Options">The options to set up the viewport.</param>
46
 
                internal static void SetViewport(ViewportOptions Options) {
47
 
                        Gl.glViewport(0, 0, Window.Properties.Width, Window.Properties.Height);
 
52
                internal static void SetViewport(ViewportOptions options) {
 
53
                        Gl.glViewport(0, 0, Screen.Properties.Width, Screen.Properties.Height);
48
54
                        Gl.glMatrixMode(Gl.GL_PROJECTION);
49
55
                        Gl.glLoadIdentity();
50
56
                        const double inverseDegrees = 57.295779513082320877;
51
 
                        double aspectRatio = Math.Tan(0.5 * Options.HorizontalViewingAngle) / Math.Tan(0.5 * Options.VerticalViewingAngle);
52
 
                        Glu.gluPerspective(Options.VerticalViewingAngle * inverseDegrees, -aspectRatio, Options.NearClippingPlane, Options.FarClippingPlane);
 
57
                        double aspectRatio = Math.Tan(0.5 * options.HorizontalViewingAngle) / Math.Tan(0.5 * options.VerticalViewingAngle);
 
58
                        Glu.gluPerspective(options.VerticalViewingAngle * inverseDegrees, -aspectRatio, options.NearClippingPlane, options.FarClippingPlane);
53
59
                        Gl.glMatrixMode(Gl.GL_MODELVIEW);
54
60
                        Gl.glLoadIdentity();
 
61
                        Viewport = options;
 
62
                }
 
63
                
 
64
                // update grid leaf node
 
65
                internal static void UpdateGridLeafNode() {
 
66
                        /*
 
67
                         * Find the leaf node the camera is currently in.
 
68
                         * */
 
69
                        ObjectGrid.GridLeafNode leaf;
 
70
                        if (ObjectGrid.Grid.GetLeafNode(Camera.Position, out leaf)) {
 
71
                                double x = 0.5 * (leaf.Rectangle.Left + leaf.Rectangle.Right);
 
72
                                double z = 0.5 * (leaf.Rectangle.Near + leaf.Rectangle.Far);
 
73
                                Camera.GridLeafNodeCenter = new OpenBveApi.Math.Vector3(x, 0.0, z);
 
74
                        } else {
 
75
                                leaf = null;
 
76
                                Camera.GridLeafNodeCenter = new OpenBveApi.Math.Vector3(0.0, 0.0, 0.0);
 
77
                        }
 
78
                        /*
 
79
                         * Check if the leaf node the camera is in has changed.
 
80
                         * */
 
81
                        if (leaf != Camera.GridLeafNode) {
 
82
                                if (leaf != null) {
 
83
                                        /*
 
84
                                         * The camera is within the bounds of a leaf node.
 
85
                                         * */
 
86
                                        ObjectGrid.GridPopulatedLeafNode[] oldLeafNodes;
 
87
                                        if (Camera.GridLeafNode != null) {
 
88
                                                oldLeafNodes = Camera.GridLeafNode.VisibleLeafNodes;
 
89
                                        } else {
 
90
                                                oldLeafNodes = null;
 
91
                                        }
 
92
                                        ObjectGrid.GridPopulatedLeafNode[] newLeafNodes = leaf.VisibleLeafNodes;
 
93
                                        /*
 
94
                                         * Find leaf nodes that were visible before but are not any longer.
 
95
                                         * */
 
96
                                        if (oldLeafNodes != null) {
 
97
                                                for (int i = 0; i < oldLeafNodes.Length; i++) {
 
98
                                                        bool remove = true;
 
99
                                                        for (int j = 0; j < newLeafNodes.Length; j++) {
 
100
                                                                if (oldLeafNodes[i] == newLeafNodes[j]) {
 
101
                                                                        remove = false;
 
102
                                                                        break;
 
103
                                                                }
 
104
                                                        }
 
105
                                                        if (remove) {
 
106
                                                                /*
 
107
                                                                 * This leaf node is not visible any longer. Remove its
 
108
                                                                 * associated transparent faces from the renderer.
 
109
                                                                 * */
 
110
                                                                for (int j = 0; j < oldLeafNodes[i].TransparentFaceCount; j++) {
 
111
                                                                        Renderer.TransparentWorldFaces.Remove(oldLeafNodes[i].TransparentFaces[j]);
 
112
                                                                }
 
113
                                                                oldLeafNodes[i].TransparentFaceCount = 0;
 
114
                                                        }
 
115
                                                }
 
116
                                        }
 
117
                                        /*
 
118
                                         * Find leaf nodes that are visible now but were not before.
 
119
                                         * */
 
120
                                        for (int i = 0; i < newLeafNodes.Length; i++) {
 
121
                                                bool add = true;
 
122
                                                if (oldLeafNodes != null) {
 
123
                                                        for (int j = 0; j < oldLeafNodes.Length; j++) {
 
124
                                                                if (newLeafNodes[i] == oldLeafNodes[j]) {
 
125
                                                                        add = false;
 
126
                                                                        break;
 
127
                                                                }
 
128
                                                        }
 
129
                                                }
 
130
                                                if (add) {
 
131
                                                        /*
 
132
                                                         * This leaf node has become visible. Add all
 
133
                                                         * its transparent faces to the renderer.
 
134
                                                         * */
 
135
                                                        ObjectGrid.GridPopulatedLeafNode visibleLeaf = newLeafNodes[i];
 
136
                                                        if (visibleLeaf.TransparentFaceCount != 0) {
 
137
                                                                throw new InvalidOperationException("#65517: A bug in the transparency management occured.");
 
138
                                                        }
 
139
                                                        visibleLeaf.LoadTextures();
 
140
                                                        double x = 0.5 * (visibleLeaf.Rectangle.Left + visibleLeaf.Rectangle.Right);
 
141
                                                        double z = 0.5 * (visibleLeaf.Rectangle.Near + visibleLeaf.Rectangle.Far);
 
142
                                                        OpenBveApi.Math.Vector3 offset = new OpenBveApi.Math.Vector3(x, 0.0, z);
 
143
                                                        for (int j = 0; j < visibleLeaf.StaticOpaqueObjectCount; j++) {
 
144
                                                                int libraryIndex = visibleLeaf.StaticOpaqueObjects[j].LibraryIndex;
 
145
                                                                OpenBveApi.Geometry.FaceVertexMesh mesh = ObjectLibrary.Library.Objects[libraryIndex] as OpenBveApi.Geometry.FaceVertexMesh;
 
146
                                                                if (mesh != null) {
 
147
                                                                        for (int k = 0; k < mesh.Faces.Length; k++) {
 
148
                                                                                add = false;
 
149
                                                                                if (mesh.Materials[mesh.Faces[k].Material].BlendMode == OpenBveApi.Geometry.BlendMode.Additive) {
 
150
                                                                                        add = true;
 
151
                                                                                }
 
152
                                                                                if (!add) {
 
153
                                                                                        Textures.ApiHandle apiHandle = mesh.Materials[mesh.Faces[k].Material].DaytimeTexture as Textures.ApiHandle;
 
154
                                                                                        if (apiHandle != null) {
 
155
                                                                                                Textures.TextureType type = Textures.RegisteredTextures[apiHandle.TextureIndex].Type;
 
156
                                                                                                if (type == Textures.TextureType.Unknown) {
 
157
                                                                                                        throw new InvalidOperationException("#31596: A bug in the texture management occured.");
 
158
                                                                                                } else if (type == Textures.TextureType.Alpha) {
 
159
                                                                                                        add = true;
 
160
                                                                                                }
 
161
                                                                                        }
 
162
                                                                                }
 
163
                                                                                if (!add) {
 
164
                                                                                        for (int h = 0; h < mesh.Faces[k].Vertices.Length; h++) {
 
165
                                                                                                if (mesh.Vertices[mesh.Faces[k].Vertices[h]].ReflectiveColor.A != 1.0f) {
 
166
                                                                                                        add = true;
 
167
                                                                                                        break;
 
168
                                                                                                }
 
169
                                                                                        }
 
170
                                                                                }
 
171
                                                                                if (add) {
 
172
                                                                                        OpenBveApi.Math.Vector3 position = visibleLeaf.StaticOpaqueObjects[j].GridPosition + offset;
 
173
                                                                                        OpenBveApi.Math.Orientation3 orientation = visibleLeaf.StaticOpaqueObjects[j].GridOrientation;
 
174
                                                                                        if (visibleLeaf.TransparentFaceCount == visibleLeaf.TransparentFaces.Length) {
 
175
                                                                                                Array.Resize<object>(ref visibleLeaf.TransparentFaces, visibleLeaf.TransparentFaces.Length << 1);
 
176
                                                                                        }
 
177
                                                                                        visibleLeaf.TransparentFaces[visibleLeaf.TransparentFaceCount] = Renderer.TransparentWorldFaces.Add(libraryIndex, k, position, orientation);
 
178
                                                                                        visibleLeaf.TransparentFaceCount++;
 
179
                                                                                }
 
180
                                                                        }
 
181
                                                                }
 
182
                                                        }
 
183
                                                }
 
184
                                        }
 
185
                                } else if (Camera.GridLeafNode != null) {
 
186
                                        /*
 
187
                                         * Before, the camera was inside the bounds of
 
188
                                         * a leaf node, but now, it is not anymore.
 
189
                                         * Remove the transparent faces associated
 
190
                                         * to the old leaf node from the renderer.
 
191
                                         * */
 
192
                                        ObjectGrid.GridPopulatedLeafNode[] oldLeafNodes = Camera.GridLeafNode.VisibleLeafNodes;
 
193
                                        for (int i = 0; i < oldLeafNodes.Length; i++) {
 
194
                                                for (int j = 0; j < oldLeafNodes[i].TransparentFaceCount; j++) {
 
195
                                                        Renderer.TransparentWorldFaces.Remove(oldLeafNodes[i].TransparentFaces[j]);
 
196
                                                }
 
197
                                                oldLeafNodes[i].TransparentFaceCount = 0;
 
198
                                        }
 
199
                                }
 
200
                        }
 
201
                        /*
 
202
                         * Apply the found leaf node.
 
203
                         * */
 
204
                        Camera.GridLeafNode = leaf;
55
205
                }
56
206
                
57
207
        }
58
 
        
59
208
}
 
 
b'\\ No newline at end of file'