2

In the main function:

    img = cvLoadImage("test.JPG");

//openCV functions to load and create matrix
    CvMat *mat = cvCreateMat(img->height,img->width,CV_8SC3 );
    cvGetMat( img, mat,0,1);
//creating the 3-dimensional array during runtime
data = new float**[img->height];
       for(int i=0;i<img->height;i++){
       data[i] = new float*[img->width];
       }

       for(int i=0;i<img->height;i++){
         for(int j=0;j<img->width;j++)
            data[i][j] = new float[3];
    }
//setting RGB values
for(int i=0;i<img->height;i++)
    {
        for(int j=0;j<img->width;j++)
        {
            CvScalar scal = cvGet2D( mat,i,j);
                    data[i][j][0] = scal.val[0];
            data[i][j][1] = scal.val[1];
            data[i][j][2] = scal.val[2];
        }
        }

I am using openCV to get the image pixel data, storing it in the dynamically created matrix "data".Now generating textures and binding them:

glGenTextures(1,&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);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0 ,GL_RGB, img->width,img->height,0,GL_RGB,GL_FLOAT,data);
glBindTexture(GL_TEXTURE_2D, texName);

glBegin(GL_QUADS);
glTexCoord2f(0,0); 
glVertex3f(-1,-1,0);
glTexCoord2f(0,1); 
glVertex3f(-1,1,0);
glTexCoord2f(1,1); 
glVertex3f(1,1,0);
glTexCoord2f(1,0); 
glVertex3f(1,-1,0);
glEnd();

There are no compilation errors but during the runtime, the window displays the square I made, but not the image that I tried to convert into texture.

How do I achieve loading any image into texture using the pixel data that I extract using openCV. I have printed the RGB values and they seem legit enough and the number of triplets printed are as expected.

1 Answers1

1

glBindTexture should be called before the glTexParemeteri...glTexImage2D calls, so openGL knows which texture you're setting up.

glEnable(GL_TEXTURE_2D);
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);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0 ,GL_RGB, img->width,img->height,0,GL_RGB,GL_FLOAT,data);

More importantly, you are not setting up your data variable correctly:

float* data = new float[img->height * img->width * 3];
for (int i = 0; i < img->height; i++)
{
    for (int j = 0; j < img->width; j++)
    {
        CvScalar scal = cvGet2D(mat, i, j);
        data[(i * img->width + j) + 0] = scal.val[0];
        data[(i * img->width + j) + 1] = scal.val[1];
        data[(i * img->width + j) + 2] = scal.val[2];
    }
}

Also, you might need to swap the order of color components and/or convert them to 0..1 range, I don't know how openCV loads images.

riv
  • 6,846
  • 2
  • 34
  • 63
  • I tried this. It gives me a white quadrilateral as I drew with glBegin()..glEnd() sequence. The "data" variable is a float, any problem with that(though the glTexImage2D has a valid enum as GL_FLOAT) – Chinmay Jindal May 27 '13 at 14:11
  • Oh I just noticed, your `data` variable is incorrect. It should be a linear array, not an array-of-arrays-of-arrays. You're lucky it doesn't crash. – riv May 28 '13 at 20:50