I'm trying to process a 2D array in a fragment shader - so to learn that, I started building my array:
int const _s = 512;
std::array<GLubyte,_s*_s*4> hitmap;
for(unsigned j = 0; j < _s; j++) {
for(unsigned i = 0; i < _s; i+=4) {
hitmap[i+j*_s] = j%256; //R
if(j>33 && j < 45) hitmap[i+j*_s] = 0; //R
hitmap[i+j*_s+1] = i%256; //G
if(i>33 && i < 45) hitmap[i+j*_s+1] = 0; //G
hitmap[i+j*_s+2] = 0; //B
hitmap[i+j*_s+3] = 255; //A
}
}
And as a first step, just push that as a texture and display that on a surface.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _s, _s, 0, GL_RGBA, GL_UNSIGNED_BYTE, hitmap.data());
I'm drawing two triangles with following coordinates to get a square:
// positions // texture coords
1.f, 1.f, 0.0f, 1.0f, 1.0f, // top right
1.f, -1.f, 0.0f, 1.0f, 0.0f, // bottom right
-1.f, -1.f, 0.0f, 0.0f, 0.0f, // bottom left
-1.f, 1.f, 0.0f, 0.0f, 1.0f // top left
And my fragment shader is pretty stupid:
#version 150 core
uniform sampler2D ourTexture;
out vec4 FragColor;
in vec2 TexCoord;
void main()
{
FragColor = texture(ourTexture, TexCoord);
};
(yes, I'm using a pretty old version here :/)
The result I get is the following:
There is some weird stuff right at the top, and the texture seems to repeat (although, not mirrored). I seriously can't figure out what I'm doing wrong - it looks like I don't provide enough data. I expect to get a 2x2 square pattern, given I'm mapping 512 px into the range of 256 via mod divisions.
Edit: Like this: