From 8db13a7eac3c20be83885638b352825c6359ce86 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 11 May 2009 10:32:15 +0000 Subject: [PATCH] Use managed Stack instead of the OpenGL matrix stack, as the latter may be broken in some driver implementations (e.g. S3 graphics twister). --- .../Graphics/Text/GL1TextOutputProvider.cs | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs index 8da65531..827c2170 100644 --- a/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs +++ b/Source/Utilities/Graphics/Text/GL1TextOutputProvider.cs @@ -41,11 +41,19 @@ namespace OpenTK.Graphics.Text Queue> inactive_lists = new Queue>(); #pragma warning disable 0649 - - struct Viewport { public float Left, Top, Right, Bottom; } - + struct Viewport { public int X, Y, Width, Height; } #pragma warning restore 0649 + // Used to save the current state in Begin() and restore it in End() + Stack projection_stack = new Stack(); + Stack modelview_stack = new Stack(); + Stack texture_stack = new Stack(); + Stack viewport_stack = new Stack(); + + // Used as temporary storage when saving / restoring the current state. + Viewport viewport = new Viewport(); + Matrix4 matrix = new Matrix4(); + bool disposed; #endregion @@ -179,27 +187,34 @@ namespace OpenTK.Graphics.Text GraphicsContext.Assert(); + // Save the state of everything we are going to modify: + // the current matrix mode, viewport state and the projection, modelview and texture matrices. + // All these will be restored in the TextPrinter.End() method. int current_matrix; GL.GetInteger(GetPName.MatrixMode, out current_matrix); - Viewport viewport = new Viewport(); - GL.GetFloat(GetPName.Viewport, out viewport.Left); + GL.GetInteger(GetPName.Viewport, out viewport.X); + viewport_stack.Push(viewport); + + GL.GetFloat(GetPName.ProjectionMatrix, out matrix.Row0.X); + projection_stack.Push(matrix); + GL.GetFloat(GetPName.ModelviewMatrix, out matrix.Row0.X); + modelview_stack.Push(matrix); + GL.GetFloat(GetPName.TextureMatrix, out matrix.Row0.X); + texture_stack.Push(matrix); // Prepare to draw text. We want pixel perfect precision, so we setup a 2D mode, // with size equal to the window (in pixels). // While we could also render text in 3D mode, it would be very hard to get // pixel-perfect precision. GL.MatrixMode(MatrixMode.Projection); - GL.PushMatrix(); GL.LoadIdentity(); - GL.Ortho(viewport.Left, viewport.Right, viewport.Bottom, viewport.Top, -1.0, 1.0); + GL.Ortho(viewport.X, viewport.Width, viewport.Height, viewport.Y, -1.0, 1.0); GL.MatrixMode(MatrixMode.Modelview); - GL.PushMatrix(); GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Texture); - GL.PushMatrix(); GL.LoadIdentity(); GL.MatrixMode((MatrixMode)current_matrix); @@ -219,14 +234,20 @@ namespace OpenTK.Graphics.Text int current_matrix; GL.GetInteger(GetPName.MatrixMode, out current_matrix); + viewport = viewport_stack.Pop(); + GL.Viewport(viewport.X, viewport.Y, viewport.Width, viewport.Height); + GL.MatrixMode(MatrixMode.Texture); - GL.PopMatrix(); + matrix = texture_stack.Pop(); + GL.LoadMatrix(ref matrix); GL.MatrixMode(MatrixMode.Modelview); - GL.PopMatrix(); + matrix = modelview_stack.Pop(); + GL.LoadMatrix(ref matrix); GL.MatrixMode(MatrixMode.Projection); - GL.PopMatrix(); + matrix = projection_stack.Pop(); + GL.LoadMatrix(ref matrix); GL.MatrixMode((MatrixMode)current_matrix); }