Skip to content
Navigation Menu
{{ message }}
This repository was archived by the owner on May 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 800
Expand file tree
/
Copy pathGLKTexture.mm
More file actions
742 lines (631 loc) · 23.1 KB
/
Copy pathGLKTexture.mm
File metadata and controls
742 lines (631 loc) · 23.1 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//******************************************************************************
//
// Copyright (c) Microsoft. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include <utility>
#import <StubReturn.h>
#import <Starboard.h>
#import <GLKit/GLKitExport.h>
#import <GLKit/GLKTexture.h>
#import "NSLogging.h"
#import "CGImageInternal.h"
#import "CGDataProviderInternal.h"
#import <Starboard/SmartTypes.h>
static const wchar_t* TAG = L"GLKTexture";
// TODO: BK: reorg, lots of cut & paste here.
NSString* const GLKTextureLoaderApplyPremultiplication = @"ApplyPremult";
NSString* const GLKTextureLoaderGenerateMipmaps = @"Mips";
NSString* const GLKTextureLoaderOriginBottomLeft = @"BottomLeft";
NSString* const GLKTextureLoaderGrayscaleAsAlpha = @"AlphaGrayscale";
NSString* const GLKTextureLoaderSRGB = @"SRGB";
NSString* const GLKTextureLoaderErrorDomain = @"GLKTextureLoaderErrorDomain";
NSString* const GLKTextureLoaderErrorKey = @"GLKTextureLoaderErrorKey";
NSString* const GLKTextureLoaderGLErrorKey = @"GLKTextureLoaderGLErrorKey";
namespace {
template <int C>
class PixelByteChannel {
public:
static const int sizeBytes = C;
static const int channels = C;
template <int CHANNEL>
inline float getCh() const {
return static_cast<float>(bytes[CHANNEL]) / 255.f;
}
template <int CHANNEL>
inline void setChan(float* vals) {
setChan<CHANNEL - 1>(vals);
bytes[CHANNEL - 1] = (unsigned char)(vals[CHANNEL - 1] * 255.f);
}
template <>
inline void setChan<0>(float* vals) {
}
inline void setCh(float* vals) {
setChan<C>(vals);
}
inline void setCh0(float val) {
bytes[0] = (unsigned char)(255.f * val);
}
private:
unsigned char bytes[sizeBytes];
};
class PixelRGB16 {
public:
static const int sizeBytes = 2;
static const int channels = 3;
template <int C>
inline float getCh() const {
}
template <>
inline float getCh<0>() const {
return static_cast<float>((val >> 11) & 0x1F) / 31.f;
}
template <>
inline float getCh<1>() const {
return static_cast<float>((val >> 5) & 0x3F) / 63.f;
}
template <>
inline float getCh<2>() const {
return static_cast<float>(val & 0x1F) / 31.f;
}
inline void setCh(float* vals) {
val = ((unsigned short)(vals[0] * 31.f) << 11) | ((unsigned short)(vals[1] * 63.f) << 5) | ((unsigned short)(vals[2] * 31.f));
}
private:
unsigned short int val;
};
template <class PSRC>
void GreyscaleConvert(PixelByteChannel<1>* pdst, PSRC* psrc, int pixels) {
for (int i = 0; i < pixels; i++) {
float grey = (psrc[i].template getCh<0>() * 0.6f) + (psrc[i].template getCh<1>() * 0.3f) + (psrc[i].template getCh<2>() * 0.1f);
pdst[i].setCh0(grey);
}
}
bool getOpt(NSDictionary* d, NSString* opt) {
if (d) {
NSNumber* n = [d objectForKey:opt];
if (n) {
return ([n intValue] > 0);
}
}
return false;
}
void swapRows(unsigned char* bytes, size_t rowSize, size_t h) {
auto tmp = (unsigned char*)_alloca(rowSize);
auto end = bytes + (rowSize * (h - 1));
while (end > bytes) {
memcpy(tmp, bytes, rowSize);
memcpy(bytes, end, rowSize);
memcpy(end, tmp, rowSize);
end -= rowSize;
bytes += rowSize;
}
}
bool getBitmapFormat(GLint& fmt, GLint& type, GLKTextureInfoAlphaState& as, int bpp) {
switch (bpp) {
case 8:
fmt = GL_ALPHA;
type = GL_UNSIGNED_BYTE;
as = GLKTextureInfoAlphaStateNonPremultiplied;
break;
case 16:
fmt = GL_RGB;
type = GL_UNSIGNED_SHORT_5_6_5;
as = GLKTextureInfoAlphaStateNone;
break;
case 24:
fmt = GL_RGB;
type = GL_UNSIGNED_BYTE;
as = GLKTextureInfoAlphaStateNone;
break;
case 32:
fmt = GL_RGBA;
type = GL_UNSIGNED_BYTE;
as = GLKTextureInfoAlphaStateNonPremultiplied;
break;
default:
NSTraceWarning(TAG, @"Unrecognized image format - %d bpp.", bpp);
return false;
}
return true;
}
template <typename PIX>
class BoxGen {
template <int C>
struct BoxCalc {
inline static void apply(const PIX& s0, const PIX& s1, const PIX& s2, const PIX& s3, float* out) {
out[C] = 0.25f * (s0.template getCh<C>() + s1.template getCh<C>() + s2.template getCh<C>() + s3.template getCh<C>());
BoxCalc<C + 1>::apply(s0, s1, s2, s3, out);
}
};
template <>
struct BoxCalc<PIX::channels> {
inline static void apply(const PIX&, const PIX&, const PIX&, const PIX&, float*) {
}
};
public:
inline static void calculate(const PIX* in, PIX* out, size_t w, size_t mipW, size_t x, size_t y) {
size_t ix = x << 1;
size_t iy = y << 1;
const PIX& src0 = in[iy * w + ix];
const PIX& src1 = in[iy * w + ix + 1];
const PIX& src2 = in[(iy + 1) * w + ix];
const PIX& src3 = in[(iy + 1) * w + ix + 1];
float outChans[PIX::channels];
BoxCalc<0>::apply(src0, src1, src2, src3, outChans);
out[y * mipW + x].setCh(outChans);
}
};
template <typename PIX, typename FC>
void boxMipGen(GLenum targ, GLint fmt, GLint type, GLint level, size_t w, size_t h, const PIX* input) {
size_t nw = w / 2;
size_t nh = h / 2;
if (nw == 0 || nh == 0) {
return;
}
PIX* output = new PIX[nw * nh];
for (size_t y = 0; y < nh; y++) {
for (size_t x = 0; x < nw; x++) {
FC::calculate(input, output, w, nw, x, y);
}
}
glTexImage2D(targ, level, fmt, nw, nh, 0, fmt, type, output);
GLint err = glGetError();
if (err) {
NSTraceError(TAG, @"Problem %d generating mip.", err);
}
boxMipGen<PIX, FC>(targ, fmt, type, level + 1, nw, nh, output);
delete[] output;
}
void createMipmaps(GLenum targ, GLint fmt, GLint type, size_t w, size_t h, unsigned char* bytes) {
if (fmt == GL_ALPHA) {
const PixelByteChannel<1>* pix = (PixelByteChannel<1>*)bytes;
boxMipGen<PixelByteChannel<1>, BoxGen<PixelByteChannel<1>>>(targ, fmt, type, 1, w, h, pix);
} else if (fmt == GL_RGB) {
if (type == GL_UNSIGNED_SHORT_5_6_5) {
const PixelRGB16* pix = (PixelRGB16*)bytes;
boxMipGen<PixelRGB16, BoxGen<PixelRGB16>>(targ, fmt, type, 1, w, h, pix);
} else {
const PixelByteChannel<3>* pix = (PixelByteChannel<3>*)bytes;
boxMipGen<PixelByteChannel<3>, BoxGen<PixelByteChannel<3>>>(targ, fmt, type, 1, w, h, pix);
}
} else if (fmt == GL_RGBA) {
const PixelByteChannel<4>* pix = (PixelByteChannel<4>*)bytes;
boxMipGen<PixelByteChannel<4>, BoxGen<PixelByteChannel<4>>>(targ, fmt, type, 1, w, h, pix);
}
}
}
@implementation GLKTextureInfo
/**
@Status Interoperable
*/
- (id)initWith:(GLuint)tex target:(GLuint)targ width:(GLuint)width height:(GLuint)height alphaState:(GLKTextureInfoAlphaState)as {
_name = tex;
_target = targ;
_width = width;
_height = height;
_alphaState = as;
_containsMipmaps = FALSE;
_textureOrigin = GLKTextureInfoOriginTopLeft;
return self;
}
/**
@Status Stub
@Notes
*/
- (id)copyWithZone:(NSZone*)zone {
UNIMPLEMENTED();
return StubReturn();
}
@end
@implementation GLKTextureLoader {
}
/**
@Status Interoperable
*/
+ (GLKTextureInfo*)textureWithContentsOfFile:(NSString*)fname options:(NSDictionary*)opts error:(NSError**)err {
woc::StrongCF<CGImageRef> image{ woc::MakeStrongCF(
_CGImageCreateFromFileWithWICFormat(static_cast<CFStringRef>(fname), GUID_WICPixelFormat32bppPRGBA)) };
RETURN_NULL_IF(!image);
return [self textureWithCGImage:image options:opts error:err];
}
/**
@Status Stub
*/
+ (GLKTextureInfo*)textureWithContentsOfData:(NSData*)data options:(NSDictionary*)opts error:(NSError**)err {
UNIMPLEMENTED();
return nil;
}
/**
@Status Interoperable
*/
+ (GLKTextureInfo*)textureWithCGImage:(CGImageRef)image options:(NSDictionary*)opts error:(NSError**)err {
woc::StrongCF<CGImageRef> img = { woc::MakeStrongCF(_CGImageCreateCopyWithPixelFormat(image, GUID_WICPixelFormat32bppPRGBA)) };
RETURN_NULL_IF(!img);
size_t w = CGImageGetWidth(img);
size_t h = CGImageGetHeight(img);
size_t bpp = CGImageGetBitsPerPixel(img);
size_t rowSize = CGImageGetBytesPerRow(img);
size_t expectedRowSize = (bpp / 8) * w;
if (rowSize != expectedRowSize) {
NSTraceWarning(TAG, @"WARNING - Image (%dx%d) - expected row size %d, got row size %d\n", w, h, expectedRowSize, rowSize);
}
unsigned char* bytesIn = static_cast<unsigned char*>(const_cast<void*>(_CGDataProviderGetData(CGImageGetDataProvider(img))));
RETURN_NULL_IF(!bytesIn);
if (getOpt(opts, GLKTextureLoaderOriginBottomLeft)) {
swapRows(bytesIn, rowSize, h);
}
GLuint tex;
glGenTextures(1, &tex);
bool genMips = getOpt(opts, GLKTextureLoaderGenerateMipmaps);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (!genMips) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
}
GLint fmt, type;
GLKTextureInfoAlphaState as;
if (!getBitmapFormat(fmt, type, as, bpp)) {
return nil;
}
if (bpp == 32 && getOpt(opts, GLKTextureLoaderApplyPremultiplication)) {
size_t pixelCount = w * h;
PixelByteChannel<4>* pixels = (PixelByteChannel<4>*)bytesIn;
for (size_t i = 0; i < pixelCount; i++) {
float alpha = pixels[i].template getCh<3>();
float vals[3] = { pixels[i].template getCh<0>() * alpha,
pixels[i].template getCh<1>() * alpha,
pixels[i].template getCh<2>() * alpha };
pixels[i].template setChan<3>(vals);
}
}
auto bytes = bytesIn;
bool deleteBytes = false;
if (getOpt(opts, GLKTextureLoaderGrayscaleAsAlpha) && fmt != GL_ALPHA) {
deleteBytes = true;
int pixels = w * h;
bytes = new unsigned char[pixels];
if (bpp == 16) {
GreyscaleConvert<PixelRGB16>((PixelByteChannel<1>*)bytes, (PixelRGB16*)bytesIn, pixels);
} else if (bpp == 24) {
GreyscaleConvert<PixelByteChannel<3>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<3>*)bytesIn, pixels);
} else if (bpp == 32) {
GreyscaleConvert<PixelByteChannel<4>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<4>*)bytesIn, pixels);
}
bpp = 8;
fmt = GL_ALPHA;
type = GL_UNSIGNED_BYTE;
}
NSTraceVerbose(TAG, @"Creating %dx%d texture, %d bpp, fmt 0x%x type 0x%x.", w, h, bpp, fmt, type);
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, type, bytes);
GLint i = glGetError();
if (i != 0) {
NSTraceError(TAG, @"Error %d creating texture.", i);
}
if (genMips) {
createMipmaps(GL_TEXTURE_2D, fmt, type, w, h, bytes);
}
if (deleteBytes) {
delete[] bytes;
}
return [[GLKTextureInfo alloc] initWith:tex target:GL_TEXTURE_2D width:w height:h alphaState:as];
}
/**
@Status Interoperable
*/
+ (GLKTextureInfo*)cubeMapWithContentsOfFile:(NSString*)fname options:(NSDictionary*)opts error:(NSError**)err {
woc::StrongCF<CGImageRef> img = { woc::MakeStrongCF(
_CGImageCreateFromFileWithWICFormat(static_cast<CFStringRef>(fname), GUID_WICPixelFormat32bppPRGBA)) };
RETURN_NULL_IF(!img);
size_t w = CGImageGetWidth(img);
size_t h = CGImageGetHeight(img);
size_t bpp = CGImageGetBitsPerPixel(img);
size_t rowSize = CGImageGetBytesPerRow(img);
size_t expectedRowSize = (bpp / 8) * w;
if (rowSize != expectedRowSize) {
NSTraceWarning(TAG, @"WARNING - Image (%dx%d) - expected row size %d, got row size %d\n", w, h, expectedRowSize, rowSize);
}
if (h != 6 * w) {
NSTraceError(TAG, @"ERROR - Unexpected cube map format, expected 6 square textures aligned vertically.");
return nil;
}
unsigned char* bytesIn = static_cast<unsigned char*>(const_cast<void*>(_CGDataProviderGetData(CGImageGetDataProvider(img))));
RETURN_NULL_IF(!bytesIn);
if (getOpt(opts, GLKTextureLoaderOriginBottomLeft)) {
swapRows(bytesIn, rowSize, h);
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
bool genMips = getOpt(opts, GLKTextureLoaderGenerateMipmaps);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (!genMips) {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLint fmt, type;
GLKTextureInfoAlphaState as;
if (!getBitmapFormat(fmt, type, as, bpp)) {
return nil;
}
if (bpp == 32 && getOpt(opts, GLKTextureLoaderApplyPremultiplication)) {
size_t pixelCount = w * h;
PixelByteChannel<4>* pixels = (PixelByteChannel<4>*)bytesIn;
for (size_t i = 0; i < pixelCount; i++) {
float alpha = pixels[i].template getCh<3>();
float vals[3] = { pixels[i].template getCh<0>() * alpha,
pixels[i].template getCh<1>() * alpha,
pixels[i].template getCh<2>() * alpha };
pixels[i].template setChan<3>(vals);
}
}
auto bytes = bytesIn;
bool deleteBytes = false;
if (getOpt(opts, GLKTextureLoaderGrayscaleAsAlpha) && fmt != GL_ALPHA) {
deleteBytes = true;
int pixels = w * h;
bytes = new unsigned char[pixels];
if (bpp == 16) {
GreyscaleConvert<PixelRGB16>((PixelByteChannel<1>*)bytes, (PixelRGB16*)bytesIn, pixels);
} else if (bpp == 24) {
GreyscaleConvert<PixelByteChannel<3>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<3>*)bytesIn, pixels);
} else if (bpp == 32) {
GreyscaleConvert<PixelByteChannel<4>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<4>*)bytesIn, pixels);
}
bpp = 8;
fmt = GL_ALPHA;
type = GL_UNSIGNED_BYTE;
bpp = 1;
}
NSTraceVerbose(TAG, @"Creating %dx%d cube map texture, %d bpp, fmt 0x%x type 0x%x.", w, h, bpp, fmt, type);
size_t sideh = h / 6;
for (int i = 0; i < 6; i++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, fmt, w, sideh, 0, fmt, type, bytes);
GLint err = glGetError();
if (err != 0) {
NSTraceError(TAG, @"Error %d creating cube face %d.", err, i);
}
if (genMips) {
createMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, fmt, type, w, h, bytes);
}
bytes += rowSize * sideh;
}
if (deleteBytes) {
delete[] bytes;
}
return [[GLKTextureInfo alloc] initWith:tex target:GL_TEXTURE_2D width:w height:sideh alphaState:as];
}
/**
@Status Interoperable
*/
+ (GLKTextureInfo*)cubeMapWithContentsOfFiles:(NSArray*)fnames options:(NSDictionary*)opts error:(NSError**)err {
if ([fnames count] != 6) {
return nil;
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
bool genMips = getOpt(opts, GLKTextureLoaderGenerateMipmaps);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (!genMips) {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
bool fmtInited = false;
size_t sideW = 0, sideH = 0, imgBpp = 0, imgRowSize = 0;
size_t curSide = 0;
GLKTextureInfoAlphaState as;
for (NSString* fn in fnames) {
woc::StrongCF<CGImageRef> img = { woc::MakeStrongCF(
_CGImageCreateFromFileWithWICFormat(static_cast<CFStringRef>(fn), GUID_WICPixelFormat32bppPRGBA)) };
if (!img) {
NSTraceWarning(TAG, @"Unable to open cube side texture %@", fn);
curSide++;
continue;
}
unsigned char* bytesIn = static_cast<unsigned char*>(const_cast<void*>(_CGDataProviderGetData(CGImageGetDataProvider(img))));
if (!bytesIn) {
NSTraceWarning(TAG, @"Unable to obtain bytes of texture %@", fn);
curSide++;
continue;
}
size_t w = CGImageGetWidth(img);
size_t h = CGImageGetHeight(img);
size_t bpp = CGImageGetBitsPerPixel(img);
size_t rowSize = CGImageGetBytesPerRow(img);
size_t expectedRowSize = (bpp / 8) * w;
if (w != h || rowSize != expectedRowSize) {
NSTraceWarning(TAG, @"WARNING - Image %@ (%dx%d) - is in an invalid format.", fn, w, h);
curSide++;
continue;
}
if (getOpt(opts, GLKTextureLoaderOriginBottomLeft)) {
swapRows(bytesIn, rowSize, h);
}
if (!fmtInited) {
fmtInited = true;
sideW = w;
sideH = h;
imgBpp = bpp;
imgRowSize = rowSize;
} else {
if ((w != sideW) || (h != sideH) || (imgBpp != bpp)) {
NSTraceWarning(TAG, @"WARNING - Image %@ (%dx%d) - does not match existing format.", fn, w, h);
curSide++;
continue;
}
}
GLint fmt, type;
if (!getBitmapFormat(fmt, type, as, bpp)) {
return nil;
}
if (bpp == 32 && getOpt(opts, GLKTextureLoaderApplyPremultiplication)) {
size_t pixelCount = w * h;
PixelByteChannel<4>* pixels = (PixelByteChannel<4>*)bytesIn;
for (size_t i = 0; i < pixelCount; i++) {
float alpha = pixels[i].template getCh<3>();
float vals[3] = { pixels[i].template getCh<0>() * alpha,
pixels[i].template getCh<1>() * alpha,
pixels[i].template getCh<2>() * alpha };
pixels[i].template setChan<3>(vals);
}
}
auto bytes = bytesIn;
bool deleteBytes = false;
if (getOpt(opts, GLKTextureLoaderGrayscaleAsAlpha) && fmt != GL_ALPHA) {
deleteBytes = true;
int pixels = w * h;
bytes = new unsigned char[pixels];
if (bpp == 16) {
GreyscaleConvert<PixelRGB16>((PixelByteChannel<1>*)bytes, (PixelRGB16*)bytesIn, pixels);
} else if (bpp == 24) {
GreyscaleConvert<PixelByteChannel<3>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<3>*)bytesIn, pixels);
} else if (bpp == 32) {
GreyscaleConvert<PixelByteChannel<4>>((PixelByteChannel<1>*)bytes, (PixelByteChannel<4>*)bytesIn, pixels);
}
bpp = 8;
fmt = GL_ALPHA;
type = GL_UNSIGNED_BYTE;
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + curSide, 0, fmt, w, h, 0, fmt, type, bytes);
GLint err = glGetError();
if (err != 0) {
NSTraceError(TAG, @"Error %d creating cube face %d.", err, curSide);
}
if (genMips) {
createMipmaps(GL_TEXTURE_CUBE_MAP_POSITIVE_X + curSide, fmt, type, w, h, bytes);
}
if (deleteBytes) {
delete[] bytes;
}
curSide++;
}
if (!fmtInited) {
NSTraceWarning(TAG, @"Unable to create cube map.");
return nil;
}
return [[GLKTextureInfo alloc] initWith:tex target:GL_TEXTURE_CUBE_MAP width:sideW height:sideH alphaState:as];
}
/**
@Status Stub
*/
- (id)initWithShareContext:(NSOpenGLContext*)context {
UNIMPLEMENTED();
return self;
}
/**
@Status Stub
@Notes
*/
- (instancetype)initWithSharegroup:(EAGLSharegroup*)sharegroup {
UNIMPLEMENTED();
return self;
}
/**
@Status Stub
@Notes
*/
- (void)textureWithContentsOfFile:(NSString*)fileName
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
+ (GLKTextureInfo*)textureWithContentsOfURL:(NSURL*)filePath options:(NSDictionary*)textureOperations error:(NSError* _Nullable*)outError {
UNIMPLEMENTED();
return StubReturn();
}
/**
@Status Stub
@Notes
*/
- (void)textureWithContentsOfURL:(NSURL*)filePath
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
- (void)textureWithContentsOfData:(NSData*)data
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
- (void)textureWithCGImage:(CGImageRef)cgImage
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
- (void)cubeMapWithContentsOfFile:(NSString*)fileName
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
- (void)cubeMapWithContentsOfFiles:(NSArray*)filePaths
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
/**
@Status Stub
@Notes
*/
+ (GLKTextureInfo*)cubeMapWithContentsOfURL:(NSURL*)filePath options:(NSDictionary*)textureOperations error:(NSError* _Nullable*)outError {
UNIMPLEMENTED();
return StubReturn();
}
/**
@Status Stub
@Notes
*/
- (void)cubeMapWithContentsOfURL:(NSURL*)filePath
options:(NSDictionary*)textureOperations
queue:(dispatch_queue_t)queue
completionHandler:(GLKTextureLoaderCallback)block {
UNIMPLEMENTED();
}
@end
You can’t perform that action at this time.
