Loading Images (using their RGB(A) pixel data) in openGL textures

霸气de小男生 提交于 2019-12-04 18:09:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!