Solar System OpenGL v1.0.0
Loading...
Searching...
No Matches
texture_loader.cpp
Go to the documentation of this file.
1
9#include "texture_loader.h"
10
20GLuint loadTexture(const char *filename)
21{
22 GLuint textureID;
23 int width, height, nrChannels;
24
25 // Load image using STB image library
26 unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
27 if (data)
28 {
29 // Generate and bind texture
30 glGenTextures(1, &textureID);
31 glBindTexture(GL_TEXTURE_2D, textureID);
32
33 // Set texture parameters and upload texture data
34 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
35 glGenerateMipmap(GL_TEXTURE_2D);
36
37 // Free image data
38 stbi_image_free(data);
39 }
40 else
41 {
42 std::cerr << "Failed to load texture: " << filename << std::endl;
43 }
44
45 // Set texture wrapping and filtering parameters
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
48 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
50
51 return textureID;
52};
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
STBIDEF stbi_uc * stbi_load(char const *filename, int *x, int *y, int *channels_in_file, int desired_channels)
GLuint loadTexture(const char *filename)
Loads a texture from a file and creates an OpenGL texture object.
Definition texture_loader.cpp:20
Provides texture loading functionality.