Full working examples for each platform:
typedef struct {
uint8_t r, g, b;
} pixel_t;
struct {
int32_t width, height;
pixel_t *pixels;
} pixel_buffer;
I've created these data structures to illustrate very clearly how this pixel drawing works. The pixel_buffer dimensions will always match the size of the window. Pixels are 3 bytes representing red, green, and blue. Each element can be a value from 0 to 255. When drawing a frame buffer, we only need the colour elements. When drawing overlapping textures, you may also want an alpha (transparency) channel.
glEnable (GL_TEXTURE_2D);
GLuint tex;
glGenTextures (1, &tex);
glBindTexture (GL_TEXTURE_2D, tex);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
OnResize ();
In order to use texturing with OpenGL's fixed function pipeline, we must enable it with glEnable (GL_TEXTURE_2D). Next, we create a texture object with glGenTextures, and bind it to the GL_TEXTURE_2D target.
The glTexParameteri line is very important - it assigns the filter to use when scaling down the texture. Even though our texture matches the size of the window, if this is not set, the texture probably will not be displayed because, since it is missing the expected minification mipmaps, the texture is considered "incomplete". This is because the default setting of GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR. Note that we then immediately call OnResize()...
static void OnResize () {
glViewport (0, 0, window.w, window.h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
pixel_buffer.width = window.w > 0 ? window.w : 1;
pixel_buffer.height = window.h > 0 ? window.h : 1;
pixel_buffer.pixels = (pixel_t*)realloc (pixel_buffer.pixels, pixel_buffer.width * pixel_buffer.height * sizeof (pixel_buffer.pixels[0]));
assert (pixel_buffer.pixels);
memset (pixel_buffer.pixels, 0, pixel_buffer.width * pixel_buffer.height * sizeof (pixel_buffer.pixels[0]));
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, pixel_buffer.width, pixel_buffer.height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
}
Whenever the screen is resized, we resize the pixel buffer to match. We also clear the pixel buffer - in this program, clearing is unnecessary, since we redraw every pixel every frame. But I left this here just for demonstration. We also call glTexImage2D to reallocate the GPU memory for the texture based on the new size. We call glPixelStorei before glTexImage2D
static uint8_t r = 0;
static int8_t rsign = 1;
r += rsign;
if (r == 255) rsign = -1;
else if (r == 0) rsign = 1;
for (int y = 0; y < pixel_buffer.height; ++y) {
uint8_t g = y * 255 / pixel_buffer.height;
for (int x = 0; x < pixel_buffer.width; ++x) {
uint8_t b = x * 255 / pixel_buffer.width;
const auto p = &pixel_buffer.pixels[x + y * pixel_buffer.width];
p->r = r;
p->g = g;
p->b = b;
}
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, pixel_buffer.width, pixel_buffer.height, GL_RGB, GL_UNSIGNED_BYTE, pixel_buffer.pixels);
All the code before the last line is just filling in the texture pixels with a nice colour effect. The pixels are greener toward the top, bluer toward the right, and more or less red over time. Then we use glTexSubImage2D to overwrite the pixel data of our texture with the new frame buffer. I've also included the glPixelStorei call here. In this program, as written, it only needs to be called once before the first call to glTexImage2D, but I make it a habit to call it right before it's needed in case I add code to a project later which changes it somewhere else.
glBegin (GL_TRIANGLE_STRIP);
glTexCoord2i (0, 1);
glVertex3f (-1, 1, 0);
glTexCoord2i (0, 0);
glVertex3f (-1, -1, 0);
glTexCoord2i (1, 1);
glVertex3f ( 1, 1, 0);
glTexCoord2i (1, 0);
glVertex3f ( 1, -1, 0);
glEnd ();
We map the texture to the vertices using texture XY coordinates from 0 (bottom-left) to 1 (top-right). Feel free to play with these numbers to get a better idea of how the texture mapping works. You might also try setting some pixels in a specific part of the texture to see where they show up on the screen.
glDrawPixels is OpenGL's specific function to draw pixels to the screen - so why would we go through all this flim-flam, mapping a texture to a triangle strip? Compatibility and capability. glDrawPixels was rarely used back in its day, and therefore, received little attention from driver programmers. While glDrawPixels is a part of the OpenGL 1.0 standard, its driver code path is likely to be suboptimal. There are also various things you're likely to want to do with pixels beyond just copying them to the screen. The OpenGL imaging subset would give you various capabilities, but it is an optional part of OpenGL, and is often not available, in my experience. Therefore, if you want to do anything beyond a simple pixel copy, you'll have to do it with textures anyway, so it's better to just use them from the beginning, in my opinion.
What we're doing in this example is drawing a 2d frame buffer on the CPU, then transferring it wholesale to the GPU (as a texture) to be rendered to the screen. We're essentially bypassing the GPU to do pixel rendering on the CPU instead. This is fine for low resolutions (like 240p), but is quite slow at 1080p or 4K. When you hit those kinds of resolutions, you're much better off sending your individual sprites to the GPU as textures once, then using the GPU to compose your sprites into a complete image.