• 0

[C++] OpenGL glDrawPixels() problem


Question

I have a buffer of raw RGB values representing an image, left to right, top to bottom. Like this:

RGBRGBRGBRGBRGB...

Each value (R, G, or B) is an unsigned byte. So each pixel is represented by 24 bits.

The image's size is exactly the same as the viewport, so I just want to copy-paste the pixels onto it. Note that the pixels will change on each refresh, so there is no point in using a textured quad. I thought glDrawPixels() would be perfect for that, so I call it like so:

glDrawPixels(Width, Height, GL_RGB, GL_UNSIGNED_BYTE, data);

Unfortunately the picture appears completely distorted and the colors are all off.

This is what the rgb buffer contains on the left, and on the right is the result of calling glDrawPixels.

973181774t.png222286194t.png

The image is flipped horizontal and vertical, skewed horizontally and the colors are messed up. I've tried reversing the buffer which fixed the vertical flip but none of the other issues. I don't know at this point, so any help?

Link to comment
https://www.neowin.net/forum/topic/989776-c-opengl-gldrawpixels-problem/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

First thing first, I thought OpenGL had increasing Y going from the bottom of the screen up, not top down like most traditional graphics. That would explain the vertical flip. Your skewing issue might be related to this:

  Quote

width ? height pixels are read from memory, starting at location data. By default, these pixels are taken from adjacent memory locations, except that after all width pixels are read, the read pointer is advanced to the next four-byte boundary. The four-byte row alignment is specified by glPixelStore with argument GL_UNPACK_ALIGNMENT, and it can be set to one, two, four, or eight bytes. Other pixel store parameters specify different read pointer advancements, both before the first pixel is read and after all width pixels are read. See the glPixelStore reference page for details on these options.

  • 0

Thank you, sir. Using glPixelStorei(GL_UNPACK_ALIGNMENT, 1) fixed the colors and the skewing.

I was incorrect when I said the image is horizontally flipped, it is not. Reversing the buffer fixes the vertical flip but introduces a horizontal flip. I guess that's a pretty easy fix: I should reverse the order of rows in the buffer, not the individual pixels. So my problem is practically solved.

:D

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.