Search code examples
arraysopenglfor-looppoint-clouds

Trouble with putting image into array while doing point cloud


Hi I'm having trouble putting image data into 3D arrays while doing point cloud with opengl.

These are my two 3D arrays:

float*** imgdata;
    imgdata = new float**[576];
    for(int a=0; a<576; a++)
    {
        *(imgdata + a) = new float*[352];
        for(int b=0; b<352; b++)
            *(*(imgdata + a) + b) = new float[1];
    }

float*** texture;
texture = new float**[576];
for(int a=0; a<576; a++)
{
    *(texture + a) = new float*[352];
    for(int b=0; b<352; b++)
        *(*(texture + a) + b) = new float[3];
}

and this is where I load in a depth map and a color image

GBmp bm0;
GBmp bm1;
bm0.load("depth.bmp");
bm1.load("color.bmp");

now I try to put the data of the two images into the 3D arrays using for loops , this is the part where the problem seems to be.

for (int i=0; i<bm0.w; i++)
{
    for (int j=0; j<bm0.h; j++)
    {
        for (int x=0; x<bm0.w; x++)
        { 
            for (int y=0; y<bm0.h; y++)
            {
                texture[i][j][0] = bm1.rgb[x*3+y*bm0.w*3];
                texture[i][j][1] = bm1.rgb[x*3+y*bm0.w*3+1];
                texture[i][j][2] = bm1.rgb[x*3+y*bm0.w*3+2];
            }
        }
    }
}

for (int i=0; i<bm0.w; i++)
{
    for (int j=0; j<bm0.h; j++)
    {
        for (int x=0; x<bm0.w; x++)
        { 
            for (int y=0; y<bm0.h; y++)
            {
                imgdata[i][j][0] = bm0.rgb[x+y*bm0.w];
            }
        }
    }
}

When I start debugging, I only get a blank window without any errors or unhandled exception so I have no idea what went wrong. Any help or advise is greatly appreciated.

Here's the other half of the code. I'm using those two arrays, imgdata & texture, to draw points on the 3D space.

glPointSize (1);
    glBegin(GL_POINTS);
    for (int i=0; i<bm0.w; i++)
        {
            for (int j=0; j<bm0.h; j++)
            {
                glColor3f(texture[i][j][0]/255, texture[i][j][1]/255, texture[i][j][2]/255);
                x=imgdata[i][j][0];
                y=imgdata[i][j][1];   
                z=imgdata[i][j][2];   
                glVertex3f(x,y,z);    
            }
        }
    glEnd(); 
glutSwapBuffers();

free(imgdata);
free(texture);

Solution

  • Your looping logic looks incorrect. Take a close look at what's happening here:

    for (int i=0; i<bm0.w; i++)
    {
        for (int j=0; j<bm0.h; j++)
        {
            for (int x=0; x<bm0.w; x++)
            { 
                for (int y=0; y<bm0.h; y++)
                {
                    texture[i][j][0] = bm1.rgb[x*3+y*bm0.w*3];
                    texture[i][j][1] = bm1.rgb[x*3+y*bm0.w*3+1];
                    texture[i][j][2] = bm1.rgb[x*3+y*bm0.w*3+2];
                }
            }
        }
    }
    

    It looks like you want to pretty much just copy the data into the arrays you allocated with your own memory layout. The outer two loops iterate over each pixel in your target array (texture). Once you're inside these two loops, I believe you just want to copy the corresponding pixel from the source array. Instead, you have another two loops that iterate over each pixel in the source array.

    So you overwrite texture[i][j] with each iteration of the inner two loops. The result of this is that all the pixels in the target array will have the same value, which is the last pixel in the source array.

    Instead, you need something like this:

    for (int i=0; i<bm0.w; i++)
    {
        for (int j=0; j<bm0.h; j++)
        {
            texture[i][j][0] = bm1.rgb[i*3+j*bm0.w*3];
            texture[i][j][1] = bm1.rgb[i*3+j*bm0.w*3+1];
            texture[i][j][2] = bm1.rgb[i*3+j*bm0.w*3+2];
        }
    }
    

    This simply loops over all pixels, and copies them from source to destination.

    Another problem is that you allocate and populate imgdata with [1] for the last dimension, but then access it with indices [0], [1] and [2] in the draw code.