49 lines
1.2 KiB
C++
49 lines
1.2 KiB
C++
#include "TextureBean.h"
|
|
#include "../global.h"
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include "../external/stb/stb_image.h"
|
|
|
|
TextureBean::TextureBean()
|
|
:m_PData(nullptr)
|
|
{
|
|
m_Glu = new GLuint[1];
|
|
}
|
|
|
|
|
|
TextureBean::~TextureBean()
|
|
{
|
|
delete[] m_Glu;
|
|
if (m_PData)delete[] m_PData;
|
|
}
|
|
|
|
|
|
|
|
void TextureBean::Load()
|
|
{
|
|
if (!m_PData)return;
|
|
unsigned char *data = stbi_load_from_memory(m_PData, m_DataLen, &m_Width, &m_Height, &m_Comp, 0);
|
|
if (m_Comp == 4) {
|
|
m_Bpp = GL_RGBA;
|
|
}
|
|
else{
|
|
m_Bpp = GL_RGB;
|
|
}
|
|
glGenTextures(1, m_Glu);
|
|
glBindTexture(GL_TEXTURE_2D, m_Glu[0]);
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
//even better quality, but this will do for now.
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
//to the edge of our shape.
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
//Generate the texture
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_Bpp, m_Width, m_Height, 0, m_Bpp, GL_UNSIGNED_BYTE, data);
|
|
stbi_image_free(data);
|
|
delete[] m_PData;
|
|
m_PData = nullptr;
|
|
} |