Added textures comp

This commit is contained in:
Gorbunov 2024-05-15 12:56:11 +03:00
parent 1521d9f0b3
commit 960a4ce7ed
2 changed files with 81 additions and 0 deletions

19
include/texture.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <glad/glad.h>
class Texture
{
public:
Texture() = default;
Texture(std::string path) { load(path); };
void bind(int slot);
void load(std::string path);
void clear();
private:
GLuint id = 0;
};

62
src/texture.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "texture.h"
#include "stb_image/stb_image.h"
#include <fstream>
#include <iostream>
void Texture::bind(int slot)
{
glBindTexture(GL_TEXTURE_2D, id);
glActiveTexture(GL_TEXTURE0 + slot);
}
void Texture::load(std::string path)
{
std::ifstream f(path, std::ios::binary);
if (!f.is_open())
{
throw(std::string( "err loading ") + path + "\n");
}
//most vexing parse here yay love cpp
std::string ret{ std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>() };
stbi_set_flip_vertically_on_load(true);
int width = 0;
int height = 0;
int channels = 0;
const unsigned char *decodedImage = stbi_load_from_memory((unsigned char*)ret.c_str(),
ret.size(), &width, &height, &channels, 4);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glActiveTexture(GL_TEXTURE0);
//minecraft is a pixel game so free optimizations, no bilinear filtey yay
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, decodedImage);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
this->id = id;
stbi_image_free((void*)decodedImage);
}
void Texture::clear()
{
glDeleteTextures(1, &id);
id = 0;
}