View difference between Paste ID: F3ydkjPy and 1dmi9ixG
SHOW: | | - or go back to the newest paste.
1
bool Texture::loadTexture2D(std::string _file, bool generateMipmaps)
2
{
3
	Logger::getInstance()->write(StringUtils::format("Loading texture '%s'", _file.c_str()));
4
5
	is3D = false;
6
7
	FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(_file.c_str(), 0);
8
	FIBITMAP *dib = FreeImage_Load(fifmt, _file.c_str(),0);
9
	FreeImage_SetTransparent(dib,true);
10
	dib = FreeImage_ConvertTo32Bits(dib); 
11
	
12
    if( dib != NULL )
13
	{
14
        glGenTextures(1, &textureId);
15
		glBindTexture(GL_TEXTURE_2D, textureId);
16
17
		BYTE *bytes = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 4];
18
19
		width = FreeImage_GetWidth(dib);
20
		height = FreeImage_GetHeight(dib);
21
22
		BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);
23
24
		for(unsigned int pixel = 0; pixel < FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib); pixel++)
25
		{
26
			bytes[pixel*3+0] = pixels[pixel*3+2];
27
			bytes[pixel*3+1] = pixels[pixel*3+1];
28
			bytes[pixel*3+2] = pixels[pixel*3+0];
29
			bytes[pixel*3+3] = pixels[pixel*3+3];
30
		}
31
	     
32
		glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0,
33
				GL_RGBA, GL_UNSIGNED_BYTE, bytes);
34
35
		bitsPerPixel = FreeImage_GetBPP(dib);
36
37
		if(generateMipmaps)
38
		{
39
			glGenerateMipmap(GL_TEXTURE_2D);
40
			mipMapsGenerated = true;
41
		}
42
43
		glGenSamplers(1, &samplerId);
44
		
45
		FreeImage_Unload(dib);
46
		delete bytes;
47
48
		setFiltering(TextureFiltering::TEXTURE_FILTER_MAG_BILINEAR, TextureFiltering::TEXTURE_FILTER_MIN_BILINEAR);
49
		setSamplerParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
50
		setSamplerParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
51
52
		Logger::getInstance()->write(StringUtils::format("Loaded texture '%s'", _file.c_str()));
53
54
		return true;
55
	}
56
57
	Logger::getInstance()->write(StringUtils::format("Error loading texture '%s'", _file.c_str()));
58
59
	return false;
60
}
61
62
void Texture::setFiltering(TextureFiltering::_TextureFiltering _tfMagnification, TextureFiltering::_TextureFiltering _tfMinification)
63
{
64
	if(_tfMagnification == TextureFiltering::TEXTURE_FILTER_MAG_NEAREST)
65
	{
66
		glSamplerParameteri(samplerId, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
67
	}
68
	else if(_tfMagnification == TextureFiltering::TEXTURE_FILTER_MAG_BILINEAR)
69
	{
70
		glSamplerParameteri(samplerId, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71
	}
72
73
	if(_tfMinification == TextureFiltering::TEXTURE_FILTER_MIN_NEAREST)
74
	{
75
		glSamplerParameteri(samplerId, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
76
	}
77
	else if(_tfMinification == TextureFiltering::TEXTURE_FILTER_MIN_BILINEAR)
78
	{
79
		glSamplerParameteri(samplerId, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
80
	}
81
	else if(_tfMinification == TextureFiltering::TEXTURE_FILTER_MIN_NEAREST_MIPMAP)
82
	{
83
		glSamplerParameteri(samplerId, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
84
	}
85
	else if(_tfMinification == TextureFiltering::TEXTURE_FILTER_MIN_BILINEAR_MIPMAP)
86
	{
87
		glSamplerParameteri(samplerId, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
88
	}
89
	else if(_tfMinification == TextureFiltering::TEXTURE_FILTER_MIN_TRILINEAR)
90
	{
91
		glSamplerParameteri(samplerId, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
92
	}
93
94
	tfMinification = _tfMinification;
95
	tfMagnification = _tfMagnification;
96
}
97
98
void Texture::setSamplerParameter(GLenum parameter, GLenum value)
99
{
100
	glSamplerParameteri(samplerId, parameter, value);
101
}