Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>This answer is not correct, see the other answers and comments. Original answer left below for posterity.</em></p> <hr> <p><strike>You can't display it directly.</strike> You'll need to convert it to an RGB texture. As you may have gathered from <a href="http://en.wikipedia.org/wiki/YUV" rel="nofollow noreferrer">Wikipedia</a>, there are a bunch of variations on the YUV color space. Make sure you're using the right one.</p> <p>For each pixel, the conversion from YUV to RGB is a straightforward linear transformation. You just do the same thing to each pixel independently.</p> <p>Once you've converted the image to RGB, you can display it by creating a texture. You need to call <a href="http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/gentextures.html" rel="nofollow noreferrer"><code>glGenTextures()</code></a> to allocate a texture handle, <a href="http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/bindtexture.html" rel="nofollow noreferrer"><code>glBindTexture()</code></a> to bind the texture to the render context, and <a href="http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html" rel="nofollow noreferrer"><code>glTexImage2D()</code></a> to upload the texture data to the GPU. To render it, you again call <code>glBindTexture()</code>, followed by the rendering of a quad with texture coordinates set up properly.</p> <pre><code>// parameters: image: pointer to raw YUV input data // width: image width (must be a power of 2) // height: image height (must be a power of 2) // returns: a handle to the resulting RGB texture GLuint makeTextureFromYUV(const float *image, int width, int height) { float *rgbImage = (float *)malloc(width * height * 3 * sizeof(float)); // check for NULL float *rgbImagePtr = rgbImage; // convert from YUV to RGB (floats used here for simplicity; it's a little // trickier with 8-bit ints) int y, x; for(y = 0; y &lt; height; y++) { for(x = 0; x &lt; width; x++) { float Y = *image++; float U = *image++; float V = *image++; *rgbImagePtr++ = Y + 1.13983f * V; // R *rgbImagePtr++ = Y - 0.39465f * U - 0.58060f * V; // G *rgbImagePtr++ = Y + 2.03211f * U; // B } } // create texture GLuint texture; glGenTextures(1, &amp;texture); // bind texture to render context glBindTexture(GL_TEXTURE_2D, texture); // upload texture data glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_FLOAT, rgbImage); // don't use mipmapping (since we're not creating any mipmaps); the default // minification filter uses mipmapping. Use linear filtering for minification // and magnification. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // free data (it's now been copied onto the GPU) and return texture handle free(rgbImage); return texture; } </code></pre> <p>To render:</p> <pre><code>glBindTexture(GL_TEXTURE_2D, texture); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f( 0.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(64.0f, 0.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(64.0f, 64.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 64.0f, 0.0f); glEnd(); </code></pre> <p>And don't forget to call <code>glEnable(GL_TEXTURE_2D)</code> at some point during initialization, and call <code>glDeleteTextures(1, &amp;texture)</code> during shutdown.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload