Drawing pixels in Checkerboard pattern

I’m trying to display pixels in Checkerboard pattern black and white. I understand that I need to find the end of the row and make offset 1 or 0 for each iteration. So that each time when the first loop ends the second starts with a white pixel or black pixel.
In my case pixels are in the same formation.

ofApp.h
ofTexture tex;
unsigned char pixels[200 * 200];
int OffSetX;
int OffSetY;

ofApp.cpp

[b]

void ofApp::setup(){
// fill the pixels array with white pixels
int size = sizeof(pixels) / sizeof(unsigned char);
//int row = (200 * 200) / 2;

for (int i = 0; i < size; i ++) {
	OffSetX = i + 1;
	for (int j = 0; j < size; j++) {
		OffSetY= (j + OffSetX)%2;
	
		if ((OffSetY+OffSetX) % 2 == 0) {

			pixels[j] = 255;

		}else{
			pixels[j] = 0;
			}


	}
}

tex.loadData(pixels, 200, 200, GL_LUMINANCE);

}
[/b]


void ofApp::draw(){
tex.draw(20, 20, 800, 800);
}

1 Like

Using ofPixels makes this slightly easier:

ofApp.h:

	ofPixels pixels;
	ofTexture texture;

setup:

pixels.allocate(10, 10, OF_PIXELS_GRAY);
for(auto line: pixels.getLines()){
	int x = 0;
	for(auto pixel: line.getPixels()){
		if (x % 2 != line.getLineNum() % 2){
			pixel[0] = 255;
		}else{
			pixel[0] = 0;
		}
		x += 1;
	}
}
texture.allocate(pixels);
texture.setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);

Note the last call that sets the texture filtering to nearest neighbour:

texture.setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);

which you need in order to get a sharp pattern instead of a blurry one from the filtering trying to soften the image when upscaling

3 Likes