-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
60 lines (47 loc) · 1.86 KB
/
Texture.cpp
File metadata and controls
60 lines (47 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <SFML/Graphics.hpp>
#include "ShaderProgram.hpp"
#include "Texture.hpp"
std::map< const char *, Texture * > Texture::textures; // for static texturs var
Texture::Texture( const char * aName )
: name( aName )
{
//ctor
}
Texture::~Texture()
{
glDeleteTextures( 1, &id ); // frees gpu memory
}
// importer for textures
Texture * Texture::load( const char * aName )
{
// check if in cache
std::map< const char *, Texture * >::iterator textureIterator = textures.find( aName );
if ( textureIterator != textures.end() ) {
std::cout << "Done loading texture form cache " << aName << std::endl;
return textureIterator->second;// key 2 exists, do something with iter->second (the value)
} else { // load from file and store in cache
sf::Image image;
if ( image.loadFromFile( aName ) ) {
image.flipVertically();
Texture * texture = new Texture( aName );
glGenTextures( 1, &texture->id );
glBindTexture( GL_TEXTURE_2D, texture->id );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr() );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
// glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
//glGenerateMipmap( GL_TEXTURE_2D ); // for mipmapping
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0 ); // for mipmapping
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4 ); // for mipmapping
std::cout << "Done loading texture " << aName << " with id " << texture->id << std::endl;
textures[aName] = texture; // stores mesh in cache for reuse
return texture;
} else {
std::cout << "Error loading texture image " << aName << std::endl;
return NULL;
}
}
}
GLuint Texture::getId() {
return id;
}