Wednesday, May 4, 2011

glTexImage2D example C/C++

An example of glTextImage2D() function in C/C++ language. Collected from http://stackoverflow.com/questions/679210/how-can-i-use-a-dynamically-sized-texture-array-with-glteximage2d
glTexImage2D() is used to load an image to GPU memory.

Currently, I'm able to load in a static sized texture which I have created. In this case it's 512 x 512.
This code is from the header:
#define TEXTURE_WIDTH 512
#define TEXTURE_HEIGHT 512
GLubyte textureArray[TEXTURE_HEIGHT][TEXTURE_WIDTH][4];
Here's the usage of glTexImage2D:
glTexImage2D(
        GL_TEXTURE_2D, 0, GL_RGBA,
        TEXTURE_WIDTH, TEXTURE_HEIGHT,
        0, GL_RGBA, GL_UNSIGNED_BYTE, textureArray);
And here's how I'm populating the array (rough example, not exact copy from my code):
for (int i = 0; i < getTexturePixelCount(); i++)
{
    textureArray[column][row][0] = (GLubyte)pixelValue1;
    textureArray[column][row][1] = (GLubyte)pixelValue2;
    textureArray[column][row][2] = (GLubyte)pixelValue3;
    textureArray[column][row][3] = (GLubyte)pixelValue4;
}
How do I change that so that there's no need for TEXTURE_WIDTH and TEXTURE_HEIGHT? Perhaps I could use a pointer style array and dynamically allocate the memory...