Tuesday, March 10, 2015

OpenTK Sprites Example

A new example is available on the GitHub repository:


This example features 2D drawing of textures, with motion and alpha blending. 30,000 sprites are added by default, but you can add more by hitting the + key. Most of the sprites are off-screen (and not drawn to help keep things running smoothly), but you can move the view with the arrow keys (and speed up with the shift key) to pan around the world. Clicking on one of the sprites will change its texture! 

UPDATE 3-11-15: New feature: multiple shaders! Hit V to change which shader is used, or hit M to toggle a mode where the shader is chosen based on the texture.


  1. Is it possible to use multiple shaders at once?
    How would I go about making some sprites take color data (like the shader from the 2nd tutorial) and other sprites use textures?

    1. I tried making the shader class initialize itself, so i could call ShaderProgram.Use and have it generate the buffers and use the program, but it either simply doesn't work or I was missing something.

    2. Hi David,

      Yes, you can use multiple shaders at once. I've uploaded a new version of the example with three shaders. Hit V to change which shader is used, or hit M to toggle a mode where the shader is chosen based on the texture.

      The only catch is that to use shaders with different sets of parameters (for example, one with only texture coordinates and one with only color values), you'll need to change how you send data to the buffers. The example uses three variations on the same shader, so they can work from the same buffered data without issues.

      The easiest solution to this might be to just add a "tint" uniform to the shader you use, so you don't need to change programs at all.

    3. Thanks for the help!

      In past programs, you always call EnableVertexAttribArrays() on the shader before drawing anything, and you do this on line 107 of TKSprites.cs for the currentshader.

      My understanding was that this "turns on" the attributes and uniforms for that shader - is this correct?

      If so, why do you not call it again after line 114? What happens if the shader you enable there accepts a uniform or attribute that wasn't enabled in line 107's shader?

      Thanks again for your help. These tutorials have been very informative.

    4. I re-read your reply after looking at the code some more and realized I was asking about what you'd just answered.

      What, then, would the overall process for enabling different shaders like that be?

    5. Hi David,

      The only major change you'll need is to make sure all the data is sent to all the buffers for the shaders you'll be using.

      You can see what happens if you don't send it by setting currentShader's default to 1, which will make it use the white shader with only coordinates and the transformation matrix. If you enable multi-shader mode without switching to another shader first, it should crash immediately because the texture data wasn't sent ("v_texcoord" and "mytexture" are included in the white shader, but they aren't used so the graphics card doesn't include them when we compile/link the shader, so we weren't actually sending that data anywhere).