Search code examples
c++pointersopengltexturesartifacts

GLUT textures artefacts


I'm traying to render texture to plane using:

unsigned char image[HEIGHT][WIDTH][3];
...
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 
             0,                         
             GL_RGB,                      
             WIDTH, HEIGHT, 
             0,                       
             GL_RGB,
             GL_UNSIGNED_BYTE, 
             image);
...
draw();

and that code ran smothly, but wher I'm traying to do this on dynamicly alocated array GLut is rendering artefacts. shorted code:

unsigned char ***image;
image = new unsigned char**[HEIGHT];
  for (int i = 0; i < HEIGHT; i++ )
  {
    image[i] = new unsigned char*[WIDTH];
    for (int j = 0; j < WIDTH ; j++ )
    {
        image[i][j] = new unsigned char[3];
    }
  }
...
GLuint textureId;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 
             0,                         
             GL_RGB,                      
             WIDTH, HEIGHT, 
             0,                       
             GL_RGB,
             GL_UNSIGNED_BYTE, 
             image);
...
draw();

both arrays has identical content (checked bit by bit). full code: main.cpp

http://pastebin.com/dzDbNgMa

TEXT_PLANE.hpp (using headers, to ensure inlinement):

http://pastebin.com/0HxcAnkW

I'm sory for the mess in code, but it's only a blasting side. I would be very greatfull for any help.


Solution

  • What you're using as your texture is the WIDTH * HEIGHT * 3 bytes of memory starting at image.

    For this, you need contiguous data like in the first example.
    Your second example is not an array of array of arrays, it's an array of pointers to arrays of pointers. These pointers can point anywhere.
    (An array is not a pointer, and a pointer is not an array.)

    If you need dynamic allocation, use

    unsigned char image* = new unsigned char [WIDTH * HEIGHT * 3];
    

    and do your own indexing arithmetic; the components would be

    image[row * WIDTH + 3 * column]
    image[row * WIDTH + 3 * column + 1]
    image[row * WIDTH + 3 * column + 2]
    

    (or

    image[column * HEIGHT + 3 * row], etc.
    

    Pick one.)