MCPcopy Create free account
hub / github.com/chrxh/alien / loadTexture

Method loadTexture

source/Gui/OpenGLHelper.cpp:8–31  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#include <stb_image.h>
7
8auto OpenGLHelper::loadTexture(std::filesystem::path const& filename) -> TextureData
9{
10 unsigned int textureId;
11 glGenTextures(1, &textureId);
12 glBindTexture(GL_TEXTURE_2D, textureId);
13 glTexParameteri(
14 GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
15 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
16 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
17 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
18 int width, height, nrChannels;
19// stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
20 // The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
21 unsigned char* data =
22 stbi_load(filename.string().c_str(), &width, &height, &nrChannels, 0);
23 if (data) {
24 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
25 glGenerateMipmap(GL_TEXTURE_2D);
26 } else {
27 throw std::runtime_error("Failed to load texture");
28 }
29 stbi_image_free(data);
30 return {textureId, width, height};
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected