-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGLPTexture.m
160 lines (130 loc) · 3.62 KB
/
GLPTexture.m
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#import "GLPTexture.h"
@interface GLPTexture ()
{
BOOL freeWhenDone;
}
@end
@implementation GLPTexture
@synthesize GLTarget = target;
@synthesize GLTexture = texture;
+ (instancetype)texture
{
return [[self alloc] init];
}
+ (instancetype)textureWithTarget:(GLenum)target
{
return [[self alloc] initWithTarget:target];
}
+ (instancetype)textureWithFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height
{
return [[self alloc] initWithFormat:format width:width height:height];
}
+ (instancetype)textureWithFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels
{
return [[self alloc] initWithFormat:format width:width height:height pixels:pixels];
}
- (instancetype)init
{
return [self initWithTarget:GL_TEXTURE_2D];
}
- (instancetype)initWithTarget:(GLenum)target_
{
return [self initWithTarget:target_ texture:glpTextureCreate(target_) freeWhenDone:YES];
}
- (instancetype)initWithTarget:(GLenum)target_ texture:(GLuint)texture_ freeWhenDone:(BOOL)freeWhenDone_
{
self = [super init];
if(self != nil)
{
if(texture_ == 0)
{
return nil;
}
target = target_;
texture = texture_;
freeWhenDone = freeWhenDone_;
}
return self;
}
- (instancetype)initWithFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height
{
self = [self initWithFormat:format width:width height:height pixels:NULL];
if(self != nil)
{
}
return self;
}
- (instancetype)initWithFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels
{
self = [self init];
if(self != nil)
{
[self setFormat:format width:width height:height pixels:pixels];
}
return self;
}
- (void)dealloc
{
if(freeWhenDone)
{
glpTextureDelete(texture), texture = 0;
}
}
- (void)setFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height
{
[self setFormat:format width:width height:height pixels:NULL target:target level:0];
}
- (void)setFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels
{
[self setFormat:format width:width height:height pixels:pixels target:target level:0];
}
- (void)setFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels level:(GLint)level
{
[self setFormat:format width:width height:height pixels:pixels target:target level:level];
}
- (void)setFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels target:(GLenum)target_
{
[self setFormat:format width:width height:height pixels:pixels target:target_ level:0];
}
- (void)setFormat:(GLPPixelformat)format width:(GLsizei)width height:(GLsizei)height pixels:(const void *)pixels target:(GLenum)target_ level:(GLint)level
{
glpTextureSetData(target_, format, width, height, pixels, level);
}
- (void)setWrap:(GLenum)wrap
{
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap);
if(target == GL_TEXTURE_CUBE_MAP)
{
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrap);
}
}
- (void)setFilter:(GLenum)filter
{
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
}
- (void)setMinFilter:(GLenum)minFilter magFilter:(GLenum)magFilter
{
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
- (void)generateMipmap
{
glGenerateMipmap(target);
}
#pragma mark - EEGLTexture protocol
- (void)bind
{
glBindTexture(target, texture);
}
- (void)bindToUnit:(GLenum)unit
{
glpActiveTexture(unit);
[self bind];
}
- (void)unbind
{
glBindTexture(target, 0);
}
@end