exoide's picture

Problem generating fonts

Hi there,

I'm porting some code from Tao Framework to OpenTK to create bitmaps fonts.

Here is the initialization code.

public static void Initialize(IntPtr hDC, string fontName, int size, int weight)
{
   IntPtr font;
 
   if (weight > 1000)
      weight = 1000;
   b = GL.GenLists(96);				// Storage for 96 Characters
   font = Gdi.CreateFont(-size,	// Height Of Font
      0,  // Width Of Font  (0=use default)
      0,  // Angle Of Escapement
      0,  // Orientation Angle
      weight,	// Font Weight (0=default, or use values from 0 to 1000, 700=Bold)
      false,	// Italic
      false,	// Underline
      false,	// Strikeout
      Gdi.ANSI_CHARSET,  // Character Set Identifier  (Use SYMBOL_CHARACTERSET for Wingdings or Webdings)
      Gdi.OUT_TT_PRECIS,  // Output Precision
      Gdi.CLIP_DEFAULT_PRECIS,  // Clipping Precision
      Gdi.ANTIALIASED_QUALITY,  // Output Quality
      Gdi.FF_DONTCARE | Gdi.DEFAULT_PITCH,  // Family And Pitch
      fontName); // Font Name
 
   IntPtr asd = Gdi.SelectObject(hDC, font);   // (1)
   bool a = WGL.UseFontBitmapsA(hDC, 32, 96, b); // Builds 96 Characters Starting At Character 32
}

Using Tao Framework it works nice however when I run it using OpenTK I get zero when I change the font in the context.

Here's the initialization code

private void Form1_Load(object sender, EventArgs e)
{
   IntPtr hDC = (glControl1.Context as IGraphicsContextInternal).Context.Handle;
   Font2D.Initialize(hDC, "courier", 14, 400);
 
   GL.ClearColor(Color.Bisque);
   int w = glControl1.Width;
   int h = glControl1.Height;
   GL.MatrixMode(MatrixMode.Projection);
   GL.LoadIdentity();
   GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
   GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
}

And here's the way I render a text

private void glControl1_Paint(object sender, PaintEventArgs e)
{
   glControl1.MakeCurrent();
   GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
   GL.MatrixMode(MatrixMode.Modelview);
   GL.LoadIdentity();
   GL.Color3(Color.Blue);
   Font2D.DrawString("test", new Vector3d(10, 10, 0));
   glControl1.SwapBuffers();
}

Can someone tell me why it doesn't run using OpenTK????

Thank you.

PD: I think the problem is when I get the GLControl's context.


Comments

Comment viewing options

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

This line:

(glControl1.Context as IGraphicsContextInternal).Context.Handle

returns the OpenGL context, not the device context. You need to use Gdi.GetDC to get the device context for your window handle.

exoide's picture

Hi Fiddler,

I did what you suggest me and it works now.

Thank you very much.

exoide's picture

So getting the OpenGL's DC in this way

(glControl1.Context as IGraphicsContextInternal).Context.Handle

is the equivalent to this one?

[DllImport("opengl32.dll")]
extern static IntPtr wglGetCurrentDC();

If yes then I think the OpenTK's developers should add some property in the GLControl to get the GL's DC easier. Because I found that way of getting the DC watching the OpenTK's sources and it tooks me some time.

the Fiddler's picture

This

(glControl1.Context as IGraphicsContextInternal).Context.Handle

returns the OpenGL RC for glControl1 (no p/invoke involved).

This

(GraphicsContext.CurrentContext as IGraphicsContextInternal).Context.Handle

returns the current OpenGL RC (equivalent to wglGetCurrentContext).

wglGetCurrentDC returns the GDI DC associated with the current OpenGL RC. OpenTK does *not* expose this function, since it is inherently non-portable. (Feel free to use a p/invoke if necessary, but keep in mind this will affect the 'write-once-run-everywhere' aspect of your program).

Who's online

There are currently 0 users and 9 guests online.