Add image loader

This commit is contained in:
Mylloon 2023-05-21 08:16:16 +02:00
parent 40b1fd6a2a
commit 1b0b3fed8e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 25 additions and 1 deletions

View file

@ -2,6 +2,7 @@
#define DEMO_UTILS_H 1 #define DEMO_UTILS_H 1
#include <GL4D/gl4duw_SDL2.h> #include <GL4D/gl4duw_SDL2.h>
#include <SDL_image.h>
#include <string.h> #include <string.h>
// Récupère un delta-temps // Récupère un delta-temps
@ -25,7 +26,10 @@ void bindAndLoadf(const char *name);
* `-z` => move away */ * `-z` => move away */
void move(GLfloat x, GLfloat y, GLfloat z); void move(GLfloat x, GLfloat y, GLfloat z);
/* Rotate a vector XYZ */ // Rotate a vector XYZ
void rotate_vec(GLfloat *, GLfloat); void rotate_vec(GLfloat *, GLfloat);
// Load an image into a GL texture
void load_img(const char *filename, GLuint texture);
#endif #endif

View file

@ -26,3 +26,23 @@ void rotate_vec(GLfloat *vec, GLfloat angle) {
vec[1] = y * cosTheta - z * sinTheta; vec[1] = y * cosTheta - z * sinTheta;
vec[2] = y * sinTheta + z * cosTheta; vec[2] = y * sinTheta + z * cosTheta;
} }
void load_img(const char *filename, GLuint 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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
SDL_Surface *s = NULL;
if (!(s = IMG_Load(filename))) {
fprintf(stderr, "Erreur de chargement de l'image (IMG_Load)\n");
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, s->pixels);
SDL_FreeSurface(s);
glBindTexture(GL_TEXTURE_2D, 0);
}