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?
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With