99 lines
3.0 KiB
C++
99 lines
3.0 KiB
C++
#include "Texture.h"
|
|
#include <GL\glew.h>
|
|
#include <iostream>
|
|
|
|
Texture::Texture(const std::string& path) {
|
|
m_RendererId = 0;
|
|
m_FilePath = path;
|
|
m_LocalBuffer = nullptr;
|
|
m_Width = 0;
|
|
m_Height = 0;
|
|
m_BPP = 0;
|
|
|
|
stbi_set_flip_vertically_on_load(1);
|
|
|
|
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
|
|
|
|
glGenTextures(1, &m_RendererId);
|
|
glBindTexture(GL_TEXTURE_2D, m_RendererId);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
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_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -1.0f);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
if (m_LocalBuffer)
|
|
stbi_image_free(m_LocalBuffer);
|
|
else
|
|
std::cout << "Error: failed to load texture: " << path << std::endl;
|
|
}
|
|
|
|
// Empty texture not from file.
|
|
Texture::Texture(glm::vec2 size, GLenum format)
|
|
{
|
|
m_Format = format;
|
|
|
|
m_Width = size.x;
|
|
m_Height = size.y;
|
|
|
|
glGenTextures(1, &m_RendererId);
|
|
glBindTexture(GL_TEXTURE_2D, m_RendererId);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, format, GL_UNSIGNED_BYTE, NULL);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
}
|
|
|
|
|
|
Texture::Texture(glm::vec2 size, msdfgen::BitmapConstRef<unsigned char, 4>& bitmap, bool t)
|
|
{
|
|
m_Width = size.x;
|
|
m_Height = size.y;
|
|
|
|
glGenTextures(1, &m_RendererId);
|
|
glBindTexture(GL_TEXTURE_2D, m_RendererId);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
auto pixel = bitmap(0, bitmap.height - 0 - 1);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (unsigned char*)bitmap.pixels);
|
|
}
|
|
|
|
void Texture::Resize(glm::vec2 size)
|
|
{
|
|
glDeleteTextures(1, &m_RendererId);
|
|
m_Width = size.x;
|
|
m_Height = size.y;
|
|
glGenTextures(1, &m_RendererId);
|
|
glBindTexture(GL_TEXTURE_2D, m_RendererId);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_Format, size.x, size.y, 0, m_Format, GL_UNSIGNED_BYTE, NULL);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
}
|
|
|
|
Texture::~Texture() {
|
|
glDeleteTextures(1, &m_RendererId);
|
|
}
|
|
|
|
void Texture::AttachToFramebuffer(GLenum attachment)
|
|
{
|
|
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, attachment, GL_TEXTURE_2D, m_RendererId, 0);
|
|
}
|
|
|
|
|
|
void Texture::Bind(unsigned int slot) const {
|
|
glActiveTexture(GL_TEXTURE0 + slot);
|
|
glBindTexture(GL_TEXTURE_2D, m_RendererId);
|
|
}
|
|
|
|
void Texture::Unbind() const {
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|