topsu's picture

Moving text on -z axis

Hello, I have rendered text on my screen using this code:

ebun.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;
 
 
 
 
namespace Topsuasd
{
    class Topsu : GameWindow
    {
        private float tripos1;
        private float tripos2;
        private float tripos3;
        private float triRot;
        private float fontti;
        private int timer;
 
 
        OpenTK.Graphics.TextPrinter textPrinter = new OpenTK.Graphics.TextPrinter();
        Font font = new Font("Comic Sans MS", 20, FontStyle.Regular);
        /// <summary>Creates a 800x600 window with the specified title.</summary>  
        public Topsu()
            : base(800, 600, new GraphicsMode(32, 24, 0, 8), "Test")
        {
            tripos1 = -1.0f;
            tripos2 = 0.0f;
            tripos3 = 1.0f;
            triRot = 0.0f;
            fontti = 20.0f;
 
        }
        /// <summary>Load resources here.</summary>  
        /// <param name="e">Not used.</param>  
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GL.ClearColor(Color.Crimson);
            GL.Enable(EnableCap.DepthTest);
        }
        /// <summary>  
        /// Called when your window is resized. Set your viewport here. It is also  
        /// a good place to set up your projection matrix (which probably changes  
        /// along when the aspect ratio of your window).  
        /// </summary>  
        /// <param name="e">Not used.</param>  
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
            Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
        }
        /// <summary>  
        /// Called when it is time to setup the next frame. Add you game logic here.  
        /// </summary>  
        /// <param name="e">Contains timing information for framerate independent logic.</param>  
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            if (Keyboard[Key.Escape])
                Exit();
            if (Keyboard[Key.F] && this.WindowState != WindowState.Fullscreen)
                this.WindowState = WindowState.Fullscreen;
            else if (Keyboard[Key.F] && this.WindowState == WindowState.Fullscreen)
                this.WindowState = WindowState.Normal;
            timer++;
 
            if (triRot >= 360)
            {
                if (tripos1 > -1.99f)
                {
                    tripos1 = tripos1 - 0.01f;
                }
                if (tripos2 > -0.99f)
                {
                    tripos2 = tripos2 - 0.01f;
                }
 
                if (tripos3 > 0.001f)
                {
                    tripos3 = tripos3 - 0.01f;
                }
            }
 
 
            if (triRot < 360)
                triRot += 1f;
 
 
            fontti = fontti - 1;
 
        }
        /// <summary>  
        /// Called when it is time to render the next frame. Add your rendering code here.  
        /// </summary>  
        /// <param name="e">Contains timing information.</param>  
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            //typically starts with  
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            //do stuff  
            //GL.LoadIdentity();  
            Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview); 
            GL.Translate(1.0f, 1.0f, 50.0f);
            GL.Rotate(triRot, 0.0f, 1.0f, 0.0f); 
            GL.Begin(BeginMode.Triangles);
 
            GL.Color3(Color.Gold);
            GL.Vertex2(tripos1, -1.3f);
            GL.Vertex2(tripos2, 0.4f);
            GL.Vertex2(tripos3, -1.3f);
 
            GL.Vertex2(-1.0f, -3.0f);
            GL.Vertex2(0.0f, -1.3f);
            GL.Vertex2(1.0f, -3.0f);
 
            GL.Vertex2(-3.0f, -3.0f);
            GL.Vertex2(-2.0f, -1.3f);
            GL.Vertex2(-1.0f, -3.0f);
 
 
            GL.End();
            //typically ends with  
            textPrinter.Begin();
 
            // For multiple lines GL.Translate same as the font height
 
            // You can do OpenGL tricks also
            GL.Translate(100f, 200f, 1f);
            GL.Rotate(triRot, Vector3.UnitZ);
            textPrinter.Print("Newfags can't triforce", font, Color.DarkRed);
 
            textPrinter.End();
            SwapBuffers();
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Topsuasd
{
    static class Program
    {
        /// <summary>  
        /// The main entry point for the application.  
        /// </summary>  
        [STAThread]
        static void Main()
        {
            // The 'using' idiom guarantees proper resource cleanup.  
            // We request 30 UpdateFrame events per second, and unlimited  
            // RenderFrame events (as fast as the computer can handle).  
            using (Topsu game = new Topsu())
            {
                game.Run(60, 60);
            }
        }
    }
}

If I change GL.Translate(100f, 200f, 1f) float in the Z-axis to anything else than numbers between -1 and 1 the text will not render, otherwise it will render fine except there is no difference between GL.Translate(100f, 200f, -0.1f), GL.Translate(100f, 200f, -1f), GL.Translate(100f, 200f, 1f) or so on.

I am trying to make the text appear smaller in the window but I cannot do that via font because I want to make it animated.

Also when running the code I get error: Warning 1       'OpenTK.Graphics.TextPrinter' is obsolete       C:\Users\xxxxx\Documents\Visual Studio 2013\Projects\Ebin\Ebin\ebun.cs  29      37      Ebin

Even while the text appears normally, how do I fix that?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

TextPrinter only supports 2d text printed using an orthographic projection. You can scale it using GL.Scale().

It is part of OpenTK.Compatibility.dll, which means it is no longer developed or supported. I would suggest copying the source code directly into your application, and modifying that to fit your needs (e.g. change the orthographic projection with a perspective projection, so you can use the z-axis to scale.)

Note that scaling 2d text will look *terrible*. It might be better to use signed distance fields instead.