本文介绍了在OpenGL纹理中加载图像(使用其RGB(A)像素数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主函数中:

    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];
        }
        }



我使用openCV获取图像像素数据,存储在动态创建的矩阵数据中。现在生成纹理并绑定它们:

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.

如何实现使用openCV提取的像素数据将任何图像加载到纹理中。我已经打印了RGB值,它们似乎足够合法,打印的三元组的数量是预期的。

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.

推荐答案

<$ c $在调用 glTexParemeteri ... glTexImage2D 之前调用c> glBindTexture 知道您要设置的纹理。

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);

更重要的是,您未设置数据 variable>

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];
    }
}

此外,您可能需要交换颜色的顺序组件和/或将它们转换为0..1范围,我不知道openCV如何加载图像。

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.

这篇关于在OpenGL纹理中加载图像(使用其RGB(A)像素数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:17