-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
83 lines (72 loc) · 1.57 KB
/
Texture.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <FreeImagePlus.h>
#include "Texture.h"
Texture::Texture ()
{
glGenTextures (1, &m_textureId);
}
Texture::~Texture ()
{
glDeleteTextures (1, &m_textureId);
}
void
Texture::setSWrapMode (GLint sWrapMode)
{
this->bind ();
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
this->unbind ();
}
void
Texture::setTWrapMode (GLint tWrapMode)
{
this->bind ();
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
this->unbind ();
}
void
Texture::setMinFilter (GLint minFilter)
{
this->bind ();
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
this->unbind ();
}
void
Texture::setMagFilter (GLint magFilter)
{
this->bind ();
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
this->unbind ();
}
void
Texture::setData (const std::string& textureFile)
{
this->bind ();
fipImage image;
bool ok=image.load (textureFile.c_str ());
if(!ok){
std::cout<<"Failed to load texture from file\n";
exit(-1);
}
unsigned int width = image.getWidth ();
unsigned int height = image.getHeight ();
const GLint level = 0;
GLint internalFormat = GL_RGB;
const GLint border = 0;
GLenum format = GL_BGR;
GLenum type = GL_UNSIGNED_BYTE;
GLubyte* texData = image.accessPixels();
glTexImage2D (GL_TEXTURE_2D, level, internalFormat, width, height, border,
format, type, texData);
glGenerateMipmap (GL_TEXTURE_2D);
unbind ();
}
void
Texture::bind () const
{
glBindTexture (GL_TEXTURE_2D, m_textureId);
}
void
Texture::unbind () const
{
glBindTexture (GL_TEXTURE_2D, 0);
}