OpenGL加载图像常用几种方式
OpenCV方式
cv::Mat image = cv::imread("awesomeface.png",cv::IMREAD_COLOR);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
cv::Mat fp;
cv::cvtColor(image,fp,cv::COLOR_BGRA2RGBA);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,fp.cols,fp.rows,0, GL_RGBA, GL_UNSIGNED_BYTE, fp.data);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL2方式
int width, height;
unsigned char* image = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGBA);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
stb方式
int width, height, nrChannels;
unsigned char *data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
总结
类型 | 使用感受 |
---|
OpenCV | CV过于庞大,不推荐 |
SOIL2 | 功能很强大 |
stb | 推荐,只有头文件。精简好用 |