-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.cpp
executable file
·109 lines (86 loc) · 2.2 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Texture.h"
Texture::Texture(void)
{}
Texture::~Texture(void)
{}
void Texture::loadTexture( const char* fileName )
{
SDL_Surface* tex = IMG_Load( fileName );
if( tex == NULL )
{
fprintf( stderr, "Could not load texture: %s, %s\n", fileName, IMG_GetError() );
return;
}
if( ( tex->w & ( tex->w - 1 ) ) != 0 )
{
fprintf( stderr, "Error loading texture: Texture is not a valid power of two-image!\n" );
SDL_FreeSurface( tex );
return;
}
if( ( tex->h & ( tex->h - 1 ) ) != 0 )
{
fprintf( stderr, "Error loading texture: Texture is not a valid power of two-image!\n" );
SDL_FreeSurface( tex );
return;
}
GLenum texFmt;
GLint Bpp = tex->format->BytesPerPixel;
if( Bpp == 4 )
{
if( tex->format->Rmask == 0x000000ff )
texFmt = GL_RGBA;
else
texFmt = GL_BGRA;
}
else if( Bpp == 3 )
{
if( tex->format->Rmask == 0x0000ff )
texFmt = GL_RGB;
else
texFmt = GL_BGR;
}
else
{
fprintf( stderr, "Error loading texture: Image is not true-color!\n" );
SDL_FreeSurface( tex );
return;
}
w = tex->w;
h = tex->h;
glGenTextures( 1, &texID );
bind();
int OGLVer;
SDL_GL_GetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, &OGLVer );
float maxA;
glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxA );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex->w, tex->h, 0, texFmt, GL_UNSIGNED_BYTE, tex->pixels );
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_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxA );
if( OGLVer == 3 )
glGenerateMipmap( GL_TEXTURE_2D );
SDL_FreeSurface( tex );
}
void Texture::bind()
{
glBindTexture( GL_TEXTURE_2D, texID );
}
void Texture::unbind()
{
glBindTexture( GL_TEXTURE_2D, 0 );
}
void Texture::destroy()
{
glDeleteTextures( 1, &texID );
}
int Texture::getWidth()
{
return w;
}
int Texture::getHeight()
{
return h;
}