entity's picture

2D-Text at world coodinates

Hello,

I need text annotations in 3d space. Using the startup tutorial, I calculated my screen coordinates from world coordinates using Glu.Project.

But unfortunately the triangle and text rotate in opposite directions. What is wrong here? Is this approach efficient for a decent number of strings, say 100?

      float angle = 0;
 
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit |
                     ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
 
            angle += 0.1f;
 
            Glu.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
 
            GL.Rotate(angle, Vector3.UnitZ);
 
 
            GL.Begin(BeginMode.Triangles);
 
            GL.Color3(Color.LightYellow);  GL.Vertex3(-1.0f, -1.0f, 4.0f);
            GL.Color3(Color.LightYellow); GL.Vertex3(1.0f, -1.0f, 4.0f);
            GL.Color3(Color.LightSkyBlue); GL.Vertex3(0.0f, 1.0f, 4.0f);
 
            GL.End();
 
            int[] Viewport = new int[4];
            double[] ModelViewMatrix = new double[16];
            double[] ProjectionMatrix = new double[16];
 
            GL.GetInteger(GetPName.Viewport, Viewport);
            GL.GetDouble(GetPName.ModelviewMatrix, ModelViewMatrix);
            GL.GetDouble(GetPName.ProjectionMatrix, ProjectionMatrix);
 
            Vector3 winPos;
            Glu.Project(new Vector3(-1.0f,-1.0f,4.0f),
                ModelViewMatrix,
                ProjectionMatrix,
                Viewport,
                out winPos);
 
            printer.Begin();
 
            GL.Translate(winPos);            
 
            printer.Print("Vertex 1", sans_serif, Color.SpringGreen);
 
            printer.End();
 
            SwapBuffers();
        }

Thanks,
entity


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.Begin() sets up an ortho projection with the Y-axis inverted: (0, 0) is the top-left corner of the screen. This was done to match the coordinate system used by GDI+ (and almost every other 2d library out there) and simplify the process of text layout.

You could either take this into account, or you could simply avoid TextPrinter.Begin()/End() and set up your own projection. If you opt for the latter approach, note that you have to scale text by 2.0f / Width and 2.0f / Height to make it render with the correct size.

Kamujin's picture

If TextPrinter.Begin() / End() are optional and only modify the projection matrix, maybe they should have names that communicate this more directly?

the Fiddler's picture

I'm open to suggestions.

objarni's picture

If Begin()/End() is not used, where will the text go?

I mean, is it placed in the xy plane, centered around origo, or where?

the Fiddler's picture

If you don't use Begin()/End(), this becomes your responsibility. The TextPrinter will then use the projection, modelview and texture matrices you have set up.

objarni's picture

But the text will go somewhere.

If I print an 'A' given Identity matrix in projection/modelview, what will it's coordinates be (x,y,z)?