thready's picture

OpenTK fonts on Xamarin

Hi,

I'm grasping at straws a little now. I've tried about 20 different font APIs, not really sure what to use, these APIs typically included System.Drawing, but my project includes Mono.Android, OpenTK-1.0 and using code I found that loads an image into a texture... I always get a problem with names existing in both dlls (Microsoft System.Drawing vs the one in mono.droid)...

I know I'm not 100% in the right place here, but hoping someone has drawn some text with OpenTK-1.0 and Xamarin.

Thanks a lot for any help,
Mike


Comments

Comment viewing options

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

The simplest way would be to draw text on a canvas and upload that canvas to an OpenGL texture. (Canvas is the Android equivalent to System.Drawing.Bitmap)

Example code from stackoverflow:

// Create an empty, mutable bitmap
Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444);
// get a canvas to paint over the bitmap
Canvas canvas = new Canvas(bitmap);
bitmap.EraseColor(0);
 
// get a background image from resources
// note the image format must match the bitmap format
Drawable background = context.GetResources().GetDrawable(R.drawable.background);
background.SetBounds(0, 0, 256, 256);
background.Draw(canvas); // draw the background to our bitmap
 
// Draw the text
Paint textPaint = new Paint();
textPaint.TextSize = 32;
textPaint.AntiAlias = true;
textPaint.SetARGB(0xff, 0x00, 0x00, 0x00);
// draw the text centered
canvas.drawText("Hello World", 16,112, textPaint);
 
//Generate one texture pointer...
int texture;
GL.GenTextures(1, out texture);
//...and bind it to our array
GL.BindTexture(All.Texture2d, texture);
 
//Create Nearest Filtered Texture
GL.TexParameter(All.Texture2d, All.TextureMinFilter, All.Nearest);
GL.TexParameter(All.Texture2d, All.TextureMagFilter, All.Nearest);
 
//Different possible texture parameters, e.g. All.ClampToEdge
GL.TexParameter(All.Texture2d, All.TextureWrapS, All.Repeat);
GL.TexParameter(All.Texture2d, All.TextureWrapT, All.Repeat);
 
//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
Android.Opengl.GLUtils.TexImage2D((int)All.Texture2d, 0, bitmap, 0);
 
//Clean up
bitmap.recycle();

(Note: I haven't tested this code with a compiler, so you might need to fix a couple of things to get it to compile. The basic idea is very simple though: paint your text on a canvas and upload the canvas to an OpenGL texture. Then you can bind this texture to a quad and display it in your scene as any other OpenGL texture.)