179 lines
4.3 KiB
C++
179 lines
4.3 KiB
C++
#include <assert.h>
|
|
#include "render/texture2d.h"
|
|
#include "render/texturesmanager.h"
|
|
#include "render/gl_shared.h"
|
|
|
|
GLint GetGlWrap(TextureWrap wrap);
|
|
GLint GetGlTexFilter(TextureFilter filter);
|
|
|
|
Texture2D* Texture2D::Create()
|
|
{
|
|
return new Texture2D;
|
|
}
|
|
|
|
Texture2D::Texture2D()
|
|
{
|
|
m_pf = PF_UNKNOWN;
|
|
m_width = 0;
|
|
m_height = 0;
|
|
m_channels = 0;
|
|
m_handle = -1;
|
|
}
|
|
|
|
Texture2D::~Texture2D()
|
|
{
|
|
m_pf = PF_UNKNOWN;
|
|
m_width = 0;
|
|
m_height = 0;
|
|
m_channels = 0;
|
|
m_handle = -1;
|
|
}
|
|
|
|
void Texture2D::CreateBlackTexture(int width, int height, int channels)
|
|
{
|
|
size_t textureSize = width * height * channels;
|
|
byte* data = new byte[textureSize];
|
|
assert(data);
|
|
|
|
for (int i = 0; i < (int)textureSize; i++) {
|
|
data[i] = 0;
|
|
}
|
|
|
|
CreateFromExistedData(data, width, height, channels);
|
|
|
|
delete[] data;
|
|
}
|
|
|
|
void Texture2D::CreateWhiteTexture(int width, int height, int channels)
|
|
{
|
|
size_t textureSize = width * height * channels;
|
|
byte* data = new byte[textureSize];
|
|
assert(data);
|
|
|
|
for (int i = 0; i < (int)textureSize; i++) {
|
|
data[i] = 255;
|
|
}
|
|
|
|
CreateFromExistedData(data, width, height, channels);
|
|
|
|
delete[] data;
|
|
}
|
|
|
|
void Texture2D::CreateGrayTexture(int width, int height, int channels)
|
|
{
|
|
size_t textureSize = width * height * channels;
|
|
byte* data = new byte[textureSize];
|
|
assert(data);
|
|
|
|
for (int i = 0; i < (int)textureSize; i++) {
|
|
data[i] = 255 / 2;
|
|
}
|
|
|
|
CreateFromExistedData(data, width, height, channels);
|
|
|
|
delete[] data;
|
|
}
|
|
|
|
void Texture2D::CreateTexture_Generator(int width, int height, int channels, int color)
|
|
{
|
|
size_t textureSize = width * height * channels;
|
|
byte* data = new byte[textureSize];
|
|
assert(data);
|
|
|
|
m_textureFileName = "$generator_texture$";
|
|
|
|
for (int i = 0; i < (int)textureSize; i++) {
|
|
data[i] = color;
|
|
}
|
|
|
|
CreateFromExistedData(data, width, height, channels);
|
|
|
|
delete[] data;
|
|
}
|
|
|
|
void Texture2D::CreateFromExistedData(void* data, int width, int height, int channels)
|
|
{
|
|
//assert(data);
|
|
|
|
m_width = width;
|
|
m_height = height;
|
|
m_channels = channels;
|
|
|
|
glGenTextures(1, &m_handle);
|
|
glBindTexture(GL_TEXTURE_2D, m_handle);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, (channels == 3) ? GL_RGB : GL_RGBA, width, height, 0, (channels == 3) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
void Texture2D::CreateRaw(void* data, int width, int height, PixelFormat pf)
|
|
{
|
|
m_width = width;
|
|
m_height = height;
|
|
m_channels = (pf == PF_R8G8B8) ? 3 : 4;
|
|
m_pf = pf;
|
|
|
|
glGenTextures(1, &m_handle);
|
|
glBindTexture(GL_TEXTURE_2D, m_handle);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GetGLInternalPF(pf), width, height, 0, GetGLInternalPF(pf), GL_UNSIGNED_BYTE, data);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
|
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
|
|
|
|
void Texture2D::GenerateMipmaps()
|
|
{
|
|
assert(0 && "Not Implemented");
|
|
|
|
#if 0
|
|
glBindTexture(GL_TEXTURE_2D, m_handle);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
//if (g_texAnisoFilter.getValueB()) {
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, g_texAnisoLevel.getValueI());
|
|
//}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
#endif
|
|
}
|
|
|
|
void Texture2D::Bind()
|
|
{
|
|
glBindTexture(GL_TEXTURE_2D, m_handle);
|
|
}
|
|
|
|
void Texture2D::SetWrapS(TextureWrap wrap)
|
|
{
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GetGlWrap(wrap));
|
|
}
|
|
|
|
void Texture2D::SetWrapT(TextureWrap wrap)
|
|
{
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GetGlWrap(wrap));
|
|
}
|
|
|
|
void Texture2D::SetMin(TextureFilter filter)
|
|
{
|
|
GLint param = 0;
|
|
param = GetGlTexFilter(filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);
|
|
}
|
|
|
|
void Texture2D::SetMag(TextureFilter filter)
|
|
{
|
|
GLint param = 0;
|
|
param = GetGlTexFilter(filter);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
|
|
}
|