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.
glBindTexture
should be called before theglTexParemeteri
...glTexImage2D
calls, so openGL knows which texture you're setting up.More importantly, you are not setting up your
data
variable correctly: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.