imGui学习笔记:stb-master/stb_image实现显示本地图片

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:其中宽度和高度可以任意变换;

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值