// constructors /// <summary>Creates a new instance of this class.</summary> /// <remarks>Make sure to call the Reset method once you are finished rendering with this state.</remarks> internal OpenGlState() { this.AlphaFunctionComparison = Gl.GL_ALWAYS; this.AlphaFunctionValue = 0.0f; this.AlphaTest = false; this.Blend = false; this.BlendSourceFactor = Gl.GL_SRC_ALPHA; this.BlendDestinationFactor = Gl.GL_ONE_MINUS_SRC_ALPHA; this.EmissiveColor = OpenBveApi.Color.ColorRGB.Black; this.DepthMask = true; this.DepthTest = true; this.Texture = 0; this.Texturing = false; }
private static void UpdateLighting(double secondsSinceMidnight) { if (RoundTheClockLighting) { double hour = (secondsSinceMidnight % 86400.0) / 3600.0; LightAmbientColor = OpenBveApi.Color.ColorRGB.Black; LightDiffuseColor = OpenBveApi.Color.ColorRGB.Black; double sunIntensity; double moonIntensity; /* The sun */ { double sunAngle = secondsSinceMidnight * 2.0 * Math.PI / 86400.0; SunPosition = new OpenBveApi.Math.Vector3(Math.Sin(sunAngle), -Math.Cos(sunAngle), 0.0); double r = 0.6 * Math.Max(0.0, Math.Min(1.0, SunPosition.Y + 0.18)); double g = 0.6 * Math.Max(0.0, Math.Min(1.0, SunPosition.Y + 0.09)); double b = 0.6 * Math.Max(0.0, Math.Min(1.0, SunPosition.Y + 0.03)); sunIntensity = (r + g + b) / 3.0; LightAmbientColor += new OpenBveApi.Color.ColorRGB((float)r, (float)g, (float)b); LightDiffuseColor += new OpenBveApi.Color.ColorRGB((float)r, (float)g, (float)b); } /* The moon */ { double moonAngle = secondsSinceMidnight * 2.0 * Math.PI / 86400.0 + 0.9 * Math.PI; MoonPosition = new OpenBveApi.Math.Vector3(0.0, -Math.Cos(moonAngle), Math.Sin(moonAngle)); double r = Math.Max(0.0, Math.Min(1.0, 0.05 * MoonPosition.Y + 0.12)); double g = Math.Max(0.0, Math.Min(1.0, 0.07 * MoonPosition.Y + 0.12)); double b = Math.Max(0.0, Math.Min(1.0, 0.13 * MoonPosition.Y + 0.12)); moonIntensity = (r + g + b) / 3.0; LightAmbientColor += new OpenBveApi.Color.ColorRGB((float)r, (float)g, (float)b); LightDiffuseColor += new OpenBveApi.Color.ColorRGB((float)r, (float)g, (float)b); } double sunFactor = sunIntensity / (sunIntensity + moonIntensity); double moonFactor = moonIntensity / (sunIntensity + moonIntensity); LightPosition = SunPosition * sunFactor + MoonPosition * moonFactor; LightPosition.Normalize(); } }
/// <summary>Resets those OpenGL states that have changed from the default values.</summary> internal void Reset() { if (this.AlphaTest) { Gl.glDisable(Gl.GL_ALPHA_TEST); this.AlphaTest = false; } if (this.Blend) { Gl.glDisable(Gl.GL_BLEND); this.Blend = false; } if (this.EmissiveColor != OpenBveApi.Color.ColorRGB.Black) { Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_EMISSION, new float[] { 0.0f, 0.0f, 0.0f, 1.0f }); this.EmissiveColor = OpenBveApi.Color.ColorRGB.Black; } if (!this.DepthMask) { Gl.glDepthMask(Gl.GL_TRUE); this.DepthMask = true; } if (!this.DepthTest) { Gl.glEnable(Gl.GL_DEPTH_TEST); this.DepthTest = true; } if (this.Texturing) { Gl.glDisable(Gl.GL_TEXTURE_2D); this.Texture = 0; this.Texturing = false; } }
internal void SetEmissiveColor(OpenBveApi.Color.ColorRGB color) { if (color != this.EmissiveColor) { Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_EMISSION, new float[] { color.R, color.G, color.B, 1.0f }); this.EmissiveColor = color; } }
/// <summary>Creates or updates the display list for the static objects. If the display list already exists, it is recreated.</summary> internal void CreateOrUpdateDisplayList() { /* * Load all textures used by the objects in this leaf node. * */ this.LoadTextures(); /* * Begin rendering to the display list. * */ Renderer.OpenGlState state; this.DisplayList.Begin(out state); /* * Render all attached static objects. * */ Renderer.RenderStaticOpaqueObjects(this.StaticOpaqueObjects, this.StaticOpaqueObjectCount, ref state); if (Program.CurrentOptions.ShowGrid) { /* * Render the rectangle and bounding rectangle for debugging purposes. * */ OpenBveApi.Color.ColorRGB brightColor = new OpenBveApi.Color.ColorRGB( (float)Program.RandomNumberGenerator.NextDouble(), (float)Program.RandomNumberGenerator.NextDouble(), (float)Program.RandomNumberGenerator.NextDouble() ); OpenBveApi.Color.ColorRGB darkColor = new OpenBveApi.Color.ColorRGB( 0.5f * brightColor.R, 0.5f * brightColor.G, 0.5f * brightColor.B ); { /* Render the rectangle. */ double x = 0.5 * (this.Rectangle.Right - this.Rectangle.Left); double z = 0.5 * (this.Rectangle.Far - this.Rectangle.Near); OpenBveApi.Math.Vector3[] vertices = new OpenBveApi.Math.Vector3[] { new OpenBveApi.Math.Vector3(-x, -1.1, -z), new OpenBveApi.Math.Vector3(-x, -1.1, z), new OpenBveApi.Math.Vector3(x, -1.1, z), new OpenBveApi.Math.Vector3(x, -1.1, -z) }; Renderer.RenderPolygonFromVertices(vertices, darkColor, OpenBveApi.Color.ColorRGB.Black, ref state); } { /* Render the bounding rectangle. */ OpenBveApi.Math.Vector2 center = new OpenBveApi.Math.Vector2( 0.5 * (this.Rectangle.Left + this.Rectangle.Right), 0.5 * (this.Rectangle.Near + this.Rectangle.Far) ); OpenBveApi.Math.Vector2 nearLeft = new OpenBveApi.Math.Vector2( this.BoundingRectangle.Left - center.X, this.BoundingRectangle.Near - center.Y ); OpenBveApi.Math.Vector2 nearRight = new OpenBveApi.Math.Vector2( this.BoundingRectangle.Right - center.X, this.BoundingRectangle.Near - center.Y ); OpenBveApi.Math.Vector2 farLeft = new OpenBveApi.Math.Vector2( this.BoundingRectangle.Left - center.X, this.BoundingRectangle.Far - center.Y ); OpenBveApi.Math.Vector2 farRight = new OpenBveApi.Math.Vector2( this.BoundingRectangle.Right - center.X, this.BoundingRectangle.Far - center.Y ); OpenBveApi.Math.Vector3[] vertices = new OpenBveApi.Math.Vector3[] { new OpenBveApi.Math.Vector3(nearLeft.X, -1.0, nearLeft.Y), new OpenBveApi.Math.Vector3(farLeft.X, -1.0, farLeft.Y), new OpenBveApi.Math.Vector3(farRight.X, -1.0, farRight.Y), new OpenBveApi.Math.Vector3(nearRight.X, -1.0, nearRight.Y) }; Renderer.RenderPolygonFromVertices(vertices, brightColor, OpenBveApi.Color.ColorRGB.Black, ref state); } } /* * End rendering to the display list. * */ this.DisplayList.End(ref state); }
// --- functions --- internal static void InitializeLighting(OpenBveApi.Route.DirectionalLight light) { LightPosition = -light.LightDirection; LightAmbientColor = light.AmbientLight; LightDiffuseColor = light.DiffuseLight; }