1

In a private window manager/compositor Haskell repository I have come across the following datatype which I am trying to understand:

data TextureBlitter = TextureBlitter {
  _textureBlitterProgram :: Program,                  -- OpenGL Type
  _textureBlitterVertexCoordEntry :: AttribLocation,  -- OpenGL Type
  _textureBlitterTextureCoordEntry :: AttribLocation, -- OpenGL Type
  _textureBlitterMatrixLocation :: UniformLocation    -- OpenGL Type
  } deriving Eq

The types Program, AttribLocation, and UniformLocation are from this OpenGL library.

The Problem: I cannot find good information online about what the concept of a "texture blitter" is. So I'm hoping that people with more expertise might immediately have a good guess as to what this type is (probably) used for.

I'm assuming that the field _textureBlitterProgram :: Program is an OpenGL shader program. But what about the other entries? And what is a TextureBlitter as a whole supposed to represent?

EDIT: I have discovered in my repo shaders with the same name:

//textureblitter.vert
#version 300 es
precision highp float;

uniform highp mat4 matrix;
in highp vec3 vertexCoordEntry;
in highp vec2 textureCoordEntry;
out highp vec2 textureCoord;
void main() {
   textureCoord = textureCoordEntry;
   gl_Position = matrix * vec4(vertexCoordEntry, 1.);
}

and

//textureblitter.frag
#version 300 es
precision highp float;

uniform sampler2D uTexSampler;
in highp vec2 textureCoord;
out highp vec4 fragmentColor;

void main() {
   fragmentColor = texture2D(uTexSampler, textureCoord);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
George
  • 6,927
  • 4
  • 34
  • 67
  • Presumably a TextureBlitter is something that blits textures, and you could easily look up that a `Program` is an OpenGL shader program, and an `AttribLocation` is a reference to a vertex attribute in the program, and a `UniformLocation` is a reference to a uniform in the program. – user253751 May 26 '17 at 03:58

1 Answers1

0

I don't use haskell nor its OpenGL package. But the names and shaders you expose are pretty descriptive. I'll try to explain what a texture is in OpenGL parlance.

Let's say you have a picture of size width x height. Let's suppose it's saved in a two-dimension, [w,h] sized, matrix.
Instead of accesing a pixel in that matrix by its a,b coordinates let's use normalized coordinates (i.e. in [0-1] range): u= a/w and v= b/h. These formulas need u and v of type float so no rounding to integer is done.

Using u,v coordinates allows us to access any pixel in a "generic" matrix.

Now you want to show that picture on the screen. It's rectangle can be scaled, rotated or even deformed by a perspective projection. Somehow you know the final four coordinates of that rectangle.

If you use also normalized coordinates (again in [0-1] range) then a mapping between picture coordinates and rectangle coordinates makes the picture to adjust to the [likely deformed] rectangle.

This is how OpenGL works. You pass the vertices of the rectangle and compute their normalized final coordinates by the use of some matrix. You also pass the picture matrix (called texture) and map it to those final coordinates.

The programm where all of this computing and mapping is done is a shader, which usually is composed by two sub-shaders: a Vertex Shader that works vertex by vertex (the VS runs exactly once per vertex); and a Fragment Shader that works with fragments (interpolated points between vertices).

TextureBlitter or "an object that blits a picture onto the screen"

  • You set the program (shader) to use. You can have several shaders with different effects (e.g. modifying the colors of the picture). Just select one.
  • Set the vertices. The AttribLocation represents the point of connection between your vertices and the shader that uses it (attribute in shaders parlance).
  • Same for "picture" coordinates.
  • Set the matrix that transform the vertices. Because it's the same for all vertices, another type of connection is used: UniformLocation (an uniform in shaders parlance).

I suppose you can find a good tutorial with examples for how to set and use this "texture blitter".

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Doesn't "to blit" mean "to copy a framebuffer though"? Or does it also mean "to copy a picture onto a screen, via a shader (fed vertex coordinates + a texture) transformed by a matrix"? – George May 26 '17 at 05:36
  • The origin of the word "blit" can be read [here](https://en.wikipedia.org/wiki/Bit_blit). I suppose a broad meaning is "copy an image onto the screen, no matter how" – Ripi2 May 27 '17 at 00:10