Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to 'manually' create image data for OpenGL texture use?

I'm learning about textures in the OpenGL environment. I see there's no conventional way of acquiring the actual image data before passing it to video memory.

Out of curiosity, is it possible to create my own matrix of pixel data and fill it in with arbitrary information instead of actually reading a bitmap or an image file?

like image 931
niebula Avatar asked Oct 19 '12 08:10

niebula


2 Answers

I see there's no conventional way of acquiring the actual image data before passing it to video memory.

What do you mean by "conventional"?

Out of curiosity, is it possible to create my own matrix of pixel data and fill it in with arbitrary information instead of actually reading a bitmap or an image file?

Well have a look at the signature of, hmm, let's say glTexImage2D:

   void glTexImage2D( GLenum target,
                      GLint level,
                      GLint internalformat,
                      GLsizei width,
                      GLsizei height,
                      GLint border,
                      GLenum format,
                      GLenum type,
                      const GLvoid *pixels )

The last parameter take a pointer into some memory. This memory has to be filled by you before it can be passed to OpenGL. Filling this memory happens either by reading and decoding a image file, or by procedurally generating the image. As a matter of fact, most of the examples in the official OpenGL Programming Guide (the Red Book) generate the image data procedurally. For example this is from the chapter 9 of the 1st edition (be warned that this is not a very good programming style and should not be followed):

#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];

static GLuint texName;

void makeCheckImage(void)
{
   int i, j, c;

   for (i = 0; i < checkImageHeight; i++) {
      for (j = 0; j < checkImageWidth; j++) {
         c = ((((i&0x8)==0)^((j&0x8))==0))*255;
         checkImage[i][j][0] = (GLubyte) c;
         checkImage[i][j][1] = (GLubyte) c;
         checkImage[i][j][2] = (GLubyte) c;
         checkImage[i][j][3] = (GLubyte) 255;
      }
   }
}

void init(void)
{    

   /* ... */

   makeCheckImage();
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   glGenTextures(1, &texName);
   glBindTexture(GL_TEXTURE_2D, texName);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
                   GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
                   GL_NEAREST);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, 
                checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
                checkImage);
}
like image 130
datenwolf Avatar answered Oct 20 '22 19:10

datenwolf


OpenGL doesn't know what a file is. It has no idea about file image formats and so forth. It has no functions to load things from files into textures.

The various pixel transfer functions take pointer to memory (or buffer objects containing memory). How you generate that data is entirely up to you. You can load it from a file, create it from an algorithm, whatever you want to do.

like image 42
Nicol Bolas Avatar answered Oct 20 '22 18:10

Nicol Bolas