OpenGL: Best way to precisely copy pixels of an image to screen?

Hi, I am attempting to write a simple 3D OpenGL 3.1 engine (older spec targeted for retro gaming rigs), and I would like to know the most efficient way to do a 2D copy of a texture directly to the screen (or back buffer)? I don't want to draw a quad and render the quad because, from what I understand, that involves texture transforms/sampling/filtering etc.

(In the past, in some school projects when rendering sprites to quads, I had instances where even using GL_NEAREST gave me 1-pixel-off results sometimes, whether that be the sprite being offset incorrectly by 1 pixel, or a vertical column of pixels on the sprite being squashed out of existence, or a vertical column of pixels being doubled/cloned.)

The reason I want a direct 2D pixel copy in a 3D engine, is mostly for HUD elements or bitmap font display. As far as I've read from searching, I should avoid "glCopyPixels" as it's inefficient?

So right now, I'm using glFramebufferTexture2D and glBlitFramebuffer every frame, but I feel like even this is not the best solution, especially for a HUD that doesn't necessarily need to be re-rendered every frame. glBlitFramebuffer also has a filtering field (I have GL_NEAREST again) and I'm wondering if there's a better way to do this with absolutely no filtering...just a direct copy.

Is there a better way? I'm doing this currently:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	GLuint my_fbo = 0;		// only once in Init
	glGenFramebuffers(1, &my_fbo);	// only once in Init


	// =========== CODE BELOW RUNS EVERY FRAME =======================
	glBindFramebuffer(GL_READ_FRAMEBUFFER, my_fbo);
	glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
	                       GL_TEXTURE_2D, _textures[0], 0);

	glBlitFramebuffer(0, 0, 128, 128,
	                  0, 0, 128, 128,
	                  GL_COLOR_BUFFER_BIT, GL_NEAREST);

	glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
	// =========== CODE ABOVE RUNS EVERY FRAME =======================

	
	glDeleteFramebuffers(1, &my_fbo);	// only once, on clean-up 


Thank you
the best way I ever found for this was to drop the image as a texture onto a rectangle that had proportional dimensions to the image's size. Texturing is designed to be quite efficient.
When you say rectangle do you mean a quad (2 tris)? That's the method I did for drawing 2D pixel sprites in openGL in school projects, but that method resulted in occasional 1-pixel-off visual errors. Like being rendered 1 pixel off of where it should be rendered when the sprite is in certain positions or during certain frames, or a row/col of pixels being squashed or cloned in certain positions or during certain frames. Undoubtedly due to floating point rounding problems after transform/sampling.

Or do you mean something else?
Its been a WHILE since I did gl, but I do believe it was 2 triangles. It may have been some other construct, but I also remember the textures having to be applied onto triangular sections as well.

the size of the objects governs whether you have the pixel problems; you can clear it up with a little manipulation of your space and object sizes etc. Or you could back when. I do recall the texture stuff being really picky about how what settings you used, which corner was which, the size of the object, etc but it was really fast (I used it to play a movie, and it was fine). If you mess up how and when you draw stuff, it can also flicker when you try to push higher speed playback. Lots of little details to work through, unfortunately. There may be a better way, but at the time I did this, it was the fastest/best looking result of several things I tried. I moved to bitblt / directx not long after this, though, when directx came out (for the NT side, so around dx 4 or 5 and whatever was after nt4, win 2k maybe? ). So as I said, a while back. Maybe gl or one of its buddy libraries supports rectangular textures now, though?
Last edited on
Topic archived. No new replies allowed.