Loading images (using their RGB (A) data) in openGL textures

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 pixel data of an image by storing it in a dynamically created "data" matrix. Now create textures and bind 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 at runtime the window displays a square that I made, but not the image that I was trying to convert to a texture.

How do I achieve loading any image into a texture using the pixel data I am extracting with openCV. I have printed the RGB values ​​and they seem legal enough and the number of triplets printed will be as expected.

+2


source to share


1 answer


glBindTexture

should be called before calls glTexParemeteri

... glTexImage2D

so openGL knows what texture you are 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 your variable data

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 reorder the color components and / or convert them to the range 0..1, I don't know how openCV loads images.

+1


source







All Articles