PDA

View Full Version : Drawing a grid of pixels on a 2D screen



Bernd82
02-12-2015, 07:10 AM
I have a 2D situation:

gDC:=GetDC(Handle); {Windows handle}
RC:=CreateRenderingContext(gDC,[opDoubleBuffered],24,0,0,0,0,0);
ActivateRenderingContext(gDC,RC);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glViewport(0,0,ClientWidth,ClientHeight);
glOrtho(0,ClientWidth,ClientHeight,0,0,1);

Now I want to draw a grid of pixels on the screen. I already read that GL_POINTS would be rather slow for setting pixels. If I have a FullHD window and every 3rd pixel is set, I have to draw 230,000 pixel actions. Quite a lot :D

In reality the pixel pitch is not every 3rd pixel but let's say every 3.258th pixel. Has anyone got an idea for a better solution?

Thanks, Bernd

GClements
02-12-2015, 08:57 AM
Several options spring to mind:


Draw a screen-sized quad with a fragment shader which tests gl_FragCoord.
Draw full-height vertical lines every 3.258 pixels in the X direction, and full-width horizontal lines every 3.258 pixels in the Y direction, the latter using glLogicOp(GL_AND) (or blending, or stencilling).
Generate a 1920x1 texture and a 1x1080 texture containing the correct patterns, and draw a pair of screen-sized quads, each stretching the texture in one direction, the latter using glLogicOp(GL_AND) (or blending, or stencilling or multi-texture).
Create a 2x2 chequerboard texture, draw 4 screen-sized quads with the appropriate scaling, with and without 1-pixel offsets in each direction. Use logical/stencil/multi-texture operations to ensure that only the correct pixels are affected (I can't be bothered figuring out the details right now).


Option #1 is probably the simplest approach (it doesn't require that you start with a blank screen, or use additional buffers), but requires an OpenGL version which supports shaders.

Bernd82
02-12-2015, 09:30 AM
Hi GClements,

thanks for your 4 suggestions :) None of them sounds really easy. I tested and found out that drawing 230,000 GL_POINTS is rather quick, though. I have not been working with shaders, stencils and textures yet. So I will leave it the way it is for the moment.

Coming from GDI it is not that easy to get used to OpenGL. Thanks for your help and time. I see that shaders, stencils and textures are very powerful features. I will get back to them.

Bernd