// Texture.h
#pragma once
#include <stb_image.h>
#include <glad/glad.h>
#include <iostream>
#include <string>
class Texture
{
unsigned int ID;
public:
Texture(std::string path = "res/horse-face.png") {
// load and create a texture
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
// 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.
unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
if (data) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else {
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
static void setActiveTexture(unsigned int glTexture) {
glActiveTexture(glTexture);
}
void bind() {
glBindTexture(GL_TEXTURE_2D, ID);
}
};
// main.cpp
// |>----------<>----------<>----------<>----------<>----------<|
// |> TEXTURE <|
// |>----------<>----------<>----------<>----------<>----------<|
Texture horse("res/horse-face.png"), face("res/tears-of-joy.png");
// default texture unit is 0 but we have minimum 16 unit for binding texture
Texture::setActiveTexture(GL_TEXTURE0);
face.bind();
shader.SetUniform1i("texture1", 0);
// GL_TEXTURE0 -> the unit 0
// GL_TEXTURE5 -> the unit 5
// ...
// in main loop between ImGui::Begin and ImGui::End
ImGui::Begin("test");
//...
ImGui::Text(selected.c_str());
if (ImGui::Button("face texture"))
face.bind();
if (ImGui::Button("horse texture"))
horse.bind();
ImGui::End();
Leave a Reply