objarni's picture

Texture Utility Library

Project Images

License: MIT (same as OpenTK; free for anything basically)

Overview
The Texture Utility Library (TexLib) for OpenTK is a collection of two classes to aid the OpenTK developer using OpenGL Textures.
Both classes are contained within one source code file called "TexLib.cs". Just include it in your project to use TexLib.

The classes are TexUtil, which handles texture initialization & creation, and TextureFont which allows rendering of bitmap fonts to simplify writing text using OpenTK.

Installation

  1. Download the .zip.
  2. Unzip it.
  3. Add "TexLib.cs" to your project.

TexUtil

This is a class to simplify the task of using textures with OpenTK.

It provides methods to create OpenGL textures from three possible sources:

  1. .NET Bitmap objects (System.Drawing.Bitmap)
  2. files, using .NETs built-in readers for example PNG, BMP and JPG file formats
  3. byte arrays in RGB or RGBA formats

It is released under MIT license, meaning you can use any way you like. It is the same license as OpenTK uses.

TexUtil Background
I blogged about the development of the TexUtil class in my OpenTK blog here and here.

TexUtil example code
This is an example of using TexUtil. It uses the OpenTK GameWindow class to display a textured quad. Note that the texture data is defined in code using a byte array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using TexLib;
using OpenTK.Graphics;
using OpenTK.Input;
using System.Drawing;
 
namespace ConsoleApplication1
{
  public class Demo : GameWindow
  {
    private int textureId;
 
    public Demo()
      : base(800, 600, GraphicsMode.Default, "OpenTK TexLib Demo")
    {
    }
 
    public override void OnLoad(EventArgs e)
    {
      //
      // Initialize alpha-blended texturing
      //
      TexUtil.InitTexturing();
      //
      // Creating a texture
      //
      textureId = TexUtil.CreateRGBTexture(2, 2,  
        new byte[] { 
          255,0,0, // Red
          0,255,0, // Green
          0,0,255, // Blue
          255,255,255 // White
        });
      OnResize(null);
      GL.ClearColor(Color.Blue);
    }
 
    public override void OnUpdateFrame(UpdateFrameEventArgs e)
    {
      if (Keyboard[Key.Escape])
        Exit();
    }
 
    protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
    {
      GL.Viewport(new System.Drawing.Size(Width, Height));
      GL.MatrixMode(MatrixMode.Projection);
      GL.LoadIdentity();
      GL.Ortho(0, Width, 0, Height, -1, 1);
    }
 
    public override void OnRenderFrame(RenderFrameEventArgs e)
    {
      GL.Clear(ClearBufferMask.ColorBufferBit);
      GL.MatrixMode(MatrixMode.Modelview);
      GL.LoadIdentity();
      GL.BindTexture(TextureTarget.Texture2D, textureId);
      GL.Begin(BeginMode.Quads);
        GL.TexCoord2(0, 0);      GL.Vertex2(10, 10);
        GL.TexCoord2(1, 0);      GL.Vertex2(100, 10);
        GL.TexCoord2(1, 1);      GL.Vertex2(100, 100);
        GL.TexCoord2(0, 1);      GL.Vertex2(10, 100);
      GL.End();
      SwapBuffers();
    }
  }
 
  class Program
  {
    static void Main(string[] args)
    {
      using (Demo demo = new Demo())
      {
        demo.Run(30.0, 0.0);
      }
    }
  }
}

TextureFont

The TextureFont class allows blitting of so called bitmap fonts using OpenTK.

TextureFont background
I blogged about the development of TextureFont here and here.

TextureFont example code

// Setup GL state for ordinary texturing.
TexUtil.InitTexturing();
// Load a bitmap from disc, and put it in a GL texture.
int tex = TexUtil.CreateTextureFromFile("mybitmapfont.png");
// Create a TextureFont object from the loaded texture.
TextureFont texFont = new TextureFont(tex);
// Write something centered in the viewport.
texFont.WriteStringAt("Center", 10, 50, 50, 0); // text, heightPercent, xPercent, yPercent, degreesCounterClockwise

Releases

Official releasesDateSizeLinksStatus
0.92009-Aug-113.09 KBRecommended for 1.xThis is currently the recommended release for 1.x.