stb-master/stb_imag可以将本地图片读取早内存转换为GL纹理,最后通过ImGui::Image显示在界面上;
下面是代码:
头文件:
/*需要的头文件*/
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
函数实现:
void LoadTexture(const char* path, GLuint* texture,int& w,int& h)
{
int width, height, channels;
// 垂直文件
// stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(path, &width, &height, &channels, 4);
if (data)
{
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
w = width;
h = height;
}
}
函数调用调用读取png图片成纹理
GLuint texture;
int w = 0, h = 0;
LoadTexture("G:\\wallhaven-4yypy7_1920x1080.png",&texture, w, h);
界面层显示:
ImGui::Image((void*)(intptr_t)texture, ImVec2(1024, 720));
ps:其中宽度和高度可以任意变换;