How to draw pixels as a texture into a polygon in OpenGL?

In C ++ OpenGL, I want to draw each pixel manually (like the texture I assume) onto a simple square primitive, or even two polygons that make up a square.

I don’t know where to start, or what phrases to look for. Am I looking for texture mapping or texture creation? Most examples are loading from a file, but I do not want to do this.

I tried reading my book on OpenGL programming, but it's just a maze, as I'm completely new to OpenGL. Please, help.

+4
source share
6 answers

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

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

Once you have created the texture, you can snap it to the state so that your triangles use this texture.

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

+6
source

If the square you draw is two-dimensional and doesn't rotate, you can look for glDrawPixels . It allows you to draw a rectangular area of ​​pixels directly into the buffer without using any polygons.

+1
source

If you are a beginner, I would recommend starting with a tutorial one of NeHe .

0
source

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

This means that if the data changes, you need to either rerun the glTexImage2D () call, or use something like glTexSubImage2D() for a partial update. I would recommend testing and profiling to ensure that updating sub-textures is fast, not sure how to optimize the actual drivers for this case.

0
source

Thanks to Reed Copsi for pointing me to glTexImage2D . It turns out that it is very simple; just pass the GLubyte array to the glTexImage2D function (as well as all the functions needed to bind the texture, etc.). Did not try this exact piece of code, but it should work fine. Array elements are a sequential version of rows, columns, and pipes.

 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 read in an OpenGL book you can use a 2D array for monochrome images, so I assume you can also use a 3D array.

0
source

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

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

This tutorial is pretty old. I assume that some of these extensions are not really extensions in newer versions of OGL. Sorry, I can’t help anymore. I no longer work directly with the graphical API, and if I did, I would use D3D if I could (to avoid the extension soup, and for other reasons I won’t bore you here.)

-1
source

All Articles