86 lignes
2.4 KiB
C++
86 lignes
2.4 KiB
C++
/* gl_helpers.cpp */
|
|
|
|
// Eric Bachard 2024/11/05 17h01
|
|
#include "imgui.h"
|
|
#include "imgui_impl_sdl3.h"
|
|
#include "imgui_impl_opengl3.h"
|
|
#include <stdio.h>
|
|
#include <SDL3/SDL.h>
|
|
#if defined(IMGUI_IMPL_OPENGL_ES2)
|
|
#include <SDL_opengles2.h>
|
|
#else
|
|
#include <SDL3/SDL_opengl.h>
|
|
#endif
|
|
|
|
#include <iostream>
|
|
|
|
#define _CRT_SECURE_NO_WARNINGS
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "stb_image.h"
|
|
#include "gl_helpers.h"
|
|
|
|
// Simple helper function to load an image into a OpenGL texture with common settings
|
|
bool LoadTextureFromMemory(const void* data, size_t data_size, GLuint* out_texture, int* out_width, int* out_height)
|
|
{
|
|
// Load from file
|
|
int image_width = 0;
|
|
int image_height = 0;
|
|
unsigned char* image_data = stbi_load_from_memory((const unsigned char*)data, (int)data_size, &image_width, &image_height, NULL, 4);
|
|
|
|
if (image_data == NULL)
|
|
{
|
|
std::cout << "image_data == NULL" << "\n";
|
|
return false;
|
|
}
|
|
|
|
// Create a OpenGL texture identifier
|
|
GLuint image_texture;
|
|
glGenTextures(1, &image_texture);
|
|
glBindTexture(GL_TEXTURE_2D, image_texture);
|
|
|
|
// Setup filtering parameters for display
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
// Upload pixels into texture
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
|
|
stbi_image_free(image_data);
|
|
|
|
*out_texture = image_texture;
|
|
*out_width = image_width;
|
|
*out_height = image_height;
|
|
|
|
return true;
|
|
}
|
|
|
|
// Open and read a file, then forward to LoadTextureFromMemory()
|
|
bool LoadTextureFromFile(const char* file_name, GLuint* out_texture, int* out_width, int* out_height)
|
|
{
|
|
FILE* f = fopen(file_name, "rb");
|
|
|
|
if (f == NULL)
|
|
return false;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
size_t file_size = (size_t)ftell(f);
|
|
|
|
if (file_size < 0)
|
|
return false;
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
void* file_data = IM_ALLOC(file_size);
|
|
unsigned int taille_image = fread(file_data, 1, file_size, f);
|
|
|
|
if (0 >= taille_image)
|
|
{
|
|
fprintf(stdout, "Erreur avec le chargement de l'image en mémoire");
|
|
return false;
|
|
}
|
|
bool ret = LoadTextureFromMemory(file_data, file_size, out_texture, out_width, out_height);
|
|
IM_FREE(file_data);
|
|
|
|
return ret;
|
|
}
|
|
|