Tangents are not yet used, so the normals are currently only correct for the ground.
73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
//
|
|
// Created by karl on 14.01.20.
|
|
//
|
|
|
|
#ifndef ECSGAME_TEXTURE_H
|
|
#define ECSGAME_TEXTURE_H
|
|
|
|
#include "../../Util/stb_image.h"
|
|
|
|
struct Texture {
|
|
uint id;
|
|
uint normal_id;
|
|
bool render_transparent = false;
|
|
|
|
struct Settings {
|
|
Settings(bool mipmaps) : mipmaps(mipmaps) {}
|
|
|
|
bool mipmaps = true;
|
|
};
|
|
|
|
// Bind a texture from the given path and return its ID
|
|
uint loadTexture(const std::string& path, Settings settings) {
|
|
uint gl_id;
|
|
|
|
glGenTextures(1, &gl_id);
|
|
glBindTexture(GL_TEXTURE_2D, gl_id);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
if (settings.mipmaps) {
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
} else {
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
}
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
int width, height, nrChannels;
|
|
unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
|
|
|
|
if (data) {
|
|
// Alpha channel?
|
|
unsigned int glChannels = GL_RGB;
|
|
if (nrChannels == 4) {
|
|
glChannels = GL_RGBA;
|
|
}
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, glChannels, width, height, 0, glChannels, GL_UNSIGNED_BYTE, data);
|
|
|
|
if (settings.mipmaps) {
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
}
|
|
} else {
|
|
std::cout << "Failed to load texture" << std::endl;
|
|
}
|
|
|
|
stbi_image_free(data);
|
|
|
|
return gl_id;
|
|
}
|
|
|
|
explicit Texture(const std::string& path, Settings settings, bool transparent) : id(loadTexture(path, settings)), render_transparent(transparent) {}
|
|
|
|
void addNormalmap(const std::string& path, Settings settings) {
|
|
normal_id = loadTexture(path, settings);
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
#endif //ECSGAME_TEXTURE_H
|