views:

1440

answers:

7

In C++ OpenGL, I want to draw each pixel manually (as a texture I assume) on to a simple square primitive, or indeed 2 polygons forming a square.

I have no idea where to start, or what phrases to look for. Am I looking for texture mapping or creating textures? Most examples are to load from a file, but I dont want to do that.

I've tried reading my OpenGL Programming Guide book, but its just a maze as I'm quite new to OpenGL. Please help.

+4  A: 

Take a close look at glTexImage2D. This is the call that loads the image in OpenGL for a 2D Texture.

glTexImage2D actually takes a raw pointer to the pixel data in the image. You can allocate memory yourself, and set the image data directly (however you want), then call this function to pass the image to OpenGL.

Once you've created the texture, you can bind it to the state so that your triangles use that texture.

There is nothing in OpenGL that directly requires images used as textures to be loaded from files. A good resource for textures can be found in this GameDev article.

Reed Copsey
A: 

If you are new then I would recommend starting from tutorial one on from NeHe.

Ketan
A: 

What you are looking for is generally called "render to texture."

http://developer.nvidia.com/object/gdc_oglrtt.html

That tutorial is pretty old. I'd guess that some of those extensions aren't actually extensions in newer versions of OGL. Sorry I can't be more help. I don't work directly with graphics API's much any more, and if I did I'd use D3D if I could (to avoid the extension soup, and for other reasons I won't bore you with here.)

BigSandwich
A: 

Do note that while glTexImage2D() takes a pointer to the pixels to load, it does not care about any changes to that data after the call. That is, the data is really loaded from the memory area you specify, and OpenGL creates a copy of its own.

This means that if the data changes, you will need to either re-do the glTexImage2D() call, or use something like glTexSubImage2D() to do a partial update. I would recommend testing and profiling to see if sub-texture updating is quick, not sure how actual drivers optimize for that case.

unwind
A: 

If the square you are drawing to is 2 dimensional and not rotated, you may be looking for glDrawPixels. It allows you to draw a rectangular region of pixels directly to the buffer without using any polygons.

cobbal
A: 

Thanks to Reed Copsey for pointing me towards glTexImage2D. Turns out this is very simple; just pass an array of GLubyte to the glTexImage2D function (as well as all the functions needed to bind the texture, etc). Haven't tried this exact snippet of code, but it should work fine. The array elements represent a serial version of the rows, columns and channels.

int pixelIndex = 0;
GLubyte pixels[400];

for (int x = 0; x < 10; x++)
{
    for (int y = 0; y < 10; x++)
    {
         for (int channel = 0; channel < 4; channel++)
         {
             // 0 = black, 255 = white
             pixels[pixelIndex++] = 255;
         }
    }
}

glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGBA, SIZE, SIZE, 0,
    GL_RGBA, GL_UNSIGNED_BYTE, pixels);

I've read in the OpenGL book you can use a 2D array for monochrome images, so I assume you could use a 3D array also.

nbolton
A: 
z.sadeghian
Please post as a new question.
nbolton