How to use dynamic texture array with glTexImage2D?

I can currently load the static size texture that I created. In this case, it is 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 is the use of glTexImage2D:

glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGBA,
    TEXTURE_WIDTH, TEXTURE_HEIGHT,
    0, GL_RGBA, GL_UNSIGNED_BYTE, textureArray);

And this is how I fill the array (an example, not an 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 this so that there is no need for TEXTURE_WIDTH and TEXTURE_HEIGHT? Perhaps I could use an array of pointer style and dynamically allocate memory ...

Edit:

I think I see a problem, in C ++ this cannot be done. The work that Budric talked about is to use a one-dimensional array, but use all 3 dimensions multiplied to represent the indices:

GLbyte *array = new GLbyte[xMax * yMax * zMax];

And for access, for example, x / y / z 1/2/3, you will need:

GLbyte byte = array[1 * 2 * 3];

, , glTexImage2D . - , OpenGL?

2:

OpenGL, , ...

[0]: column 0 > [1]: 0 > [2]: 0... n > [n]: 1... n > [n]: 1.. n

... . , 3- , -, ++.

+3
3

int width = 1024;
int height = 1024;
GLubyte * texture = new GLubyte[4*width*height];
...
glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGBA,
    width, height,
    0, GL_RGBA, GL_UNSIGNED_BYTE, textureArray);
delete [] texture;         //remove the un-needed local copy of the texture;

OpenGL glTexImage2D. , OpenGL. , , , , , OpenGL.

Edit: C/++ . , [a] [b], . [a * cols + b].

, , .

, , :

int rows = 16, cols = 16;
char * storage = new char[rows * cols];
char ** accessor2D = new char *[rows];
for (int i = 0; i < rows; i++)
{
    accessor2D[i] = storage + i*cols;
}
accessor2D[5][5] = 2;
assert(storage[5*cols + 5] == accessor2D[5][5]);
delete [] accessor2D;
delete [] storage;

, 1D . . . 2D- . . .

+4

, , , :

, OpenGL (9-1, p373, 5th Ed.) .

:

static GLubyte checkImage[checkImageHeight][checkImageWidth][4];

3- , , . Someth. , :

GLubyte***checkImage;
checkImage = new GLubyte**[HEIGHT];

for (int i = 0; i < HEIGHT; ++i)
{
  checkImage[i] = new GLubyte*[WIDTH];

  for (int j = 0; j < WIDTH; ++j)
    checkImage[i][j] = new GLubyte[DEPTH];
}

:

unsigned int depth = 4;

GLubyte *checkImage = new GLubyte[height * width * depth];

:

for(unsigned int ix = 0; ix < height; ++ix)
{
  for(unsigned int iy = 0; iy < width; ++iy)
  {
    int c = (((ix&0x8) == 0) ^ ((iy&0x8)) == 0) * 255;

    checkImage[ix * width * depth + iy * depth + 0] = c;   //red
    checkImage[ix * width * depth + iy * depth + 1] = c;   //green
    checkImage[ix * width * depth + iy * depth + 2] = c;   //blue
    checkImage[ix * width * depth + iy * depth + 3] = 255; //alpha
  }
}

:

delete [] checkImage;

, ...

+5

You can always wrap it in class. If you load an image from a file, you get the height and width along with the rest of the data (how else can you use this file?), You can save them in a class that wraps the file loading rather than using a preprocessor, Something like:

class ImageLoader
{
...
  ImageLoader(const char* filename, ...);
...
  int GetHeight();
  int GetWidth();
  void* GetDataPointer();
...
};

Even better, you can hide the glTexImage2d function calls there.

class GLImageLoader
{
...
  ImageLoader(const char* filename, ...);
...
  GLuint LoadToTexture2D(); // returns texture id
...
};
+1
source

Source: https://habr.com/ru/post/1705288/


All Articles