forked from childhood/libSOIL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOIL.c
executable file
·2024 lines (1984 loc) · 57.4 KB
/
SOIL.c
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Jonathan Dummer
2007-07-26-10.36
Simple OpenGL Image Library
Public Domain
using Sean Barret's stb_image as a base
Thanks to:
* Sean Barret - for the awesome stb_image
* Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts
* everybody at gamedev.net
*/
#define SOIL_CHECK_FOR_GL_ERRORS 0
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wingdi.h>
#include <GL/gl.h>
#elif defined(__APPLE__) || defined(__APPLE_CC__)
/* I can't test this Apple stuff! */
#include <OpenGL/gl.h>
#include <Carbon/Carbon.h>
#define APIENTRY
#else
#include <GL/gl.h>
#include <GL/glx.h>
#endif
#include "SOIL.h"
#include "stb_image_aug.h"
#include "image_helper.h"
#include "image_DXT.h"
#include <stdlib.h>
#include <string.h>
/* error reporting */
char *result_string_pointer = "SOIL initialized";
/* for loading cube maps */
enum{
SOIL_CAPABILITY_UNKNOWN = -1,
SOIL_CAPABILITY_NONE = 0,
SOIL_CAPABILITY_PRESENT = 1
};
static int has_cubemap_capability = SOIL_CAPABILITY_UNKNOWN;
int query_cubemap_capability( void );
#define SOIL_TEXTURE_WRAP_R 0x8072
#define SOIL_CLAMP_TO_EDGE 0x812F
#define SOIL_NORMAL_MAP 0x8511
#define SOIL_REFLECTION_MAP 0x8512
#define SOIL_TEXTURE_CUBE_MAP 0x8513
#define SOIL_TEXTURE_BINDING_CUBE_MAP 0x8514
#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#define SOIL_PROXY_TEXTURE_CUBE_MAP 0x851B
#define SOIL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C
/* for non-power-of-two texture */
static int has_NPOT_capability = SOIL_CAPABILITY_UNKNOWN;
int query_NPOT_capability( void );
/* for texture rectangles */
static int has_tex_rectangle_capability = SOIL_CAPABILITY_UNKNOWN;
int query_tex_rectangle_capability( void );
#define SOIL_TEXTURE_RECTANGLE_ARB 0x84F5
#define SOIL_MAX_RECTANGLE_TEXTURE_SIZE_ARB 0x84F8
/* for using DXT compression */
static int has_DXT_capability = SOIL_CAPABILITY_UNKNOWN;
int query_DXT_capability( void );
#define SOIL_RGB_S3TC_DXT1 0x83F0
#define SOIL_RGBA_S3TC_DXT1 0x83F1
#define SOIL_RGBA_S3TC_DXT3 0x83F2
#define SOIL_RGBA_S3TC_DXT5 0x83F3
typedef void (APIENTRY * P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid * data);
P_SOIL_GLCOMPRESSEDTEXIMAGE2DPROC soilGlCompressedTexImage2D = NULL;
unsigned int SOIL_direct_load_DDS(
const char *filename,
unsigned int reuse_texture_ID,
int flags,
int loading_as_cubemap );
unsigned int SOIL_direct_load_DDS_from_memory(
const unsigned char *const buffer,
int buffer_length,
unsigned int reuse_texture_ID,
int flags,
int loading_as_cubemap );
/* other functions */
unsigned int
SOIL_internal_create_OGL_texture
(
const unsigned char *const data,
int width, int height, int channels,
unsigned int reuse_texture_ID,
unsigned int flags,
unsigned int opengl_texture_type,
unsigned int opengl_texture_target,
unsigned int texture_check_size_enum
);
/* and the code magic begins here [8^) */
unsigned int
SOIL_load_OGL_texture
(
const char *filename,
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels;
unsigned int tex_id;
/* does the user want direct uploading of the image as a DDS file? */
if( flags & SOIL_FLAG_DDS_LOAD_DIRECT )
{
/* 1st try direct loading of the image as a DDS file
note: direct uploading will only load what is in the
DDS file, no MIPmaps will be generated, the image will
not be flipped, etc. */
tex_id = SOIL_direct_load_DDS( filename, reuse_texture_ID, flags, 0 );
if( tex_id )
{
/* hey, it worked!! */
return tex_id;
}
}
/* try to load the image */
img = SOIL_load_image( filename, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* OK, make it a texture! */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
reuse_texture_ID, flags,
GL_TEXTURE_2D, GL_TEXTURE_2D,
GL_MAX_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_load_OGL_HDR_texture
(
const char *filename,
int fake_HDR_format,
int rescale_to_max,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels;
unsigned int tex_id;
/* no direct uploading of the image as a DDS file */
/* error check */
if( (fake_HDR_format != SOIL_HDR_RGBE) &&
(fake_HDR_format != SOIL_HDR_RGBdivA) &&
(fake_HDR_format != SOIL_HDR_RGBdivA2) )
{
result_string_pointer = "Invalid fake HDR format specified";
return 0;
}
/* try to load the image (only the HDR type) */
img = stbi_hdr_load_rgbe( filename, &width, &height, &channels, 4 );
/* channels holds the original number of channels, which may have been forced */
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* the load worked, do I need to convert it? */
if( fake_HDR_format == SOIL_HDR_RGBdivA )
{
RGBE_to_RGBdivA( img, width, height, rescale_to_max );
} else if( fake_HDR_format == SOIL_HDR_RGBdivA2 )
{
RGBE_to_RGBdivA2( img, width, height, rescale_to_max );
}
/* OK, make it a texture! */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
reuse_texture_ID, flags,
GL_TEXTURE_2D, GL_TEXTURE_2D,
GL_MAX_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_load_OGL_texture_from_memory
(
const unsigned char *const buffer,
int buffer_length,
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels;
unsigned int tex_id;
/* does the user want direct uploading of the image as a DDS file? */
if( flags & SOIL_FLAG_DDS_LOAD_DIRECT )
{
/* 1st try direct loading of the image as a DDS file
note: direct uploading will only load what is in the
DDS file, no MIPmaps will be generated, the image will
not be flipped, etc. */
tex_id = SOIL_direct_load_DDS_from_memory(
buffer, buffer_length,
reuse_texture_ID, flags, 0 );
if( tex_id )
{
/* hey, it worked!! */
return tex_id;
}
}
/* try to load the image */
img = SOIL_load_image_from_memory(
buffer, buffer_length,
&width, &height, &channels,
force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* OK, make it a texture! */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
reuse_texture_ID, flags,
GL_TEXTURE_2D, GL_TEXTURE_2D,
GL_MAX_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_load_OGL_cubemap
(
const char *x_pos_file,
const char *x_neg_file,
const char *y_pos_file,
const char *y_neg_file,
const char *z_pos_file,
const char *z_neg_file,
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels;
unsigned int tex_id;
/* error checking */
if( (x_pos_file == NULL) ||
(x_neg_file == NULL) ||
(y_pos_file == NULL) ||
(y_neg_file == NULL) ||
(z_pos_file == NULL) ||
(z_neg_file == NULL) )
{
result_string_pointer = "Invalid cube map files list";
return 0;
}
/* capability checking */
if( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )
{
result_string_pointer = "No cube map capability present";
return 0;
}
/* 1st face: try to load the image */
img = SOIL_load_image( x_pos_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, and create a texture ID if necessary */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
reuse_texture_ID, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_X,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image( x_neg_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image( y_pos_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image( y_neg_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image( z_pos_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image( z_neg_file, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_load_OGL_cubemap_from_memory
(
const unsigned char *const x_pos_buffer,
int x_pos_buffer_length,
const unsigned char *const x_neg_buffer,
int x_neg_buffer_length,
const unsigned char *const y_pos_buffer,
int y_pos_buffer_length,
const unsigned char *const y_neg_buffer,
int y_neg_buffer_length,
const unsigned char *const z_pos_buffer,
int z_pos_buffer_length,
const unsigned char *const z_neg_buffer,
int z_neg_buffer_length,
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels;
unsigned int tex_id;
/* error checking */
if( (x_pos_buffer == NULL) ||
(x_neg_buffer == NULL) ||
(y_pos_buffer == NULL) ||
(y_neg_buffer == NULL) ||
(z_pos_buffer == NULL) ||
(z_neg_buffer == NULL) )
{
result_string_pointer = "Invalid cube map buffers list";
return 0;
}
/* capability checking */
if( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )
{
result_string_pointer = "No cube map capability present";
return 0;
}
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
x_pos_buffer, x_pos_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, and create a texture ID if necessary */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
reuse_texture_ID, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_X,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
x_neg_buffer, x_neg_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
y_pos_buffer, y_pos_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
y_neg_buffer, y_neg_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
z_pos_buffer, z_pos_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* continue? */
if( tex_id != 0 )
{
/* 1st face: try to load the image */
img = SOIL_load_image_from_memory(
z_neg_buffer, z_neg_buffer_length,
&width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* upload the texture, but reuse the assigned texture ID */
tex_id = SOIL_internal_create_OGL_texture(
img, width, height, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP, SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
/* and nuke the image data */
SOIL_free_image_data( img );
}
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_load_OGL_single_cubemap
(
const char *filename,
const char face_order[6],
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels, i;
unsigned int tex_id = 0;
/* error checking */
if( filename == NULL )
{
result_string_pointer = "Invalid single cube map file name";
return 0;
}
/* does the user want direct uploading of the image as a DDS file? */
if( flags & SOIL_FLAG_DDS_LOAD_DIRECT )
{
/* 1st try direct loading of the image as a DDS file
note: direct uploading will only load what is in the
DDS file, no MIPmaps will be generated, the image will
not be flipped, etc. */
tex_id = SOIL_direct_load_DDS( filename, reuse_texture_ID, flags, 1 );
if( tex_id )
{
/* hey, it worked!! */
return tex_id;
}
}
/* face order checking */
for( i = 0; i < 6; ++i )
{
if( (face_order[i] != 'N') &&
(face_order[i] != 'S') &&
(face_order[i] != 'W') &&
(face_order[i] != 'E') &&
(face_order[i] != 'U') &&
(face_order[i] != 'D') )
{
result_string_pointer = "Invalid single cube map face order";
return 0;
};
}
/* capability checking */
if( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )
{
result_string_pointer = "No cube map capability present";
return 0;
}
/* 1st off, try to load the full image */
img = SOIL_load_image( filename, &width, &height, &channels, force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* now, does this image have the right dimensions? */
if( (width != 6*height) &&
(6*width != height) )
{
SOIL_free_image_data( img );
result_string_pointer = "Single cubemap image must have a 6:1 ratio";
return 0;
}
/* try the image split and create */
tex_id = SOIL_create_OGL_single_cubemap(
img, width, height, channels,
face_order, reuse_texture_ID, flags
);
/* nuke the temporary image data and return the texture handle */
SOIL_free_image_data( img );
return tex_id;
}
unsigned int
SOIL_load_OGL_single_cubemap_from_memory
(
const unsigned char *const buffer,
int buffer_length,
const char face_order[6],
int force_channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* img;
int width, height, channels, i;
unsigned int tex_id = 0;
/* error checking */
if( buffer == NULL )
{
result_string_pointer = "Invalid single cube map buffer";
return 0;
}
/* does the user want direct uploading of the image as a DDS file? */
if( flags & SOIL_FLAG_DDS_LOAD_DIRECT )
{
/* 1st try direct loading of the image as a DDS file
note: direct uploading will only load what is in the
DDS file, no MIPmaps will be generated, the image will
not be flipped, etc. */
tex_id = SOIL_direct_load_DDS_from_memory(
buffer, buffer_length,
reuse_texture_ID, flags, 1 );
if( tex_id )
{
/* hey, it worked!! */
return tex_id;
}
}
/* face order checking */
for( i = 0; i < 6; ++i )
{
if( (face_order[i] != 'N') &&
(face_order[i] != 'S') &&
(face_order[i] != 'W') &&
(face_order[i] != 'E') &&
(face_order[i] != 'U') &&
(face_order[i] != 'D') )
{
result_string_pointer = "Invalid single cube map face order";
return 0;
};
}
/* capability checking */
if( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )
{
result_string_pointer = "No cube map capability present";
return 0;
}
/* 1st off, try to load the full image */
img = SOIL_load_image_from_memory(
buffer, buffer_length,
&width, &height, &channels,
force_channels );
/* channels holds the original number of channels, which may have been forced */
if( (force_channels >= 1) && (force_channels <= 4) )
{
channels = force_channels;
}
if( NULL == img )
{
/* image loading failed */
result_string_pointer = stbi_failure_reason();
return 0;
}
/* now, does this image have the right dimensions? */
if( (width != 6*height) &&
(6*width != height) )
{
SOIL_free_image_data( img );
result_string_pointer = "Single cubemap image must have a 6:1 ratio";
return 0;
}
/* try the image split and create */
tex_id = SOIL_create_OGL_single_cubemap(
img, width, height, channels,
face_order, reuse_texture_ID, flags
);
/* nuke the temporary image data and return the texture handle */
SOIL_free_image_data( img );
return tex_id;
}
unsigned int
SOIL_create_OGL_single_cubemap
(
const unsigned char *const data,
int width, int height, int channels,
const char face_order[6],
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* variables */
unsigned char* sub_img;
int dw, dh, sz, i;
unsigned int tex_id;
/* error checking */
if( data == NULL )
{
result_string_pointer = "Invalid single cube map image data";
return 0;
}
/* face order checking */
for( i = 0; i < 6; ++i )
{
if( (face_order[i] != 'N') &&
(face_order[i] != 'S') &&
(face_order[i] != 'W') &&
(face_order[i] != 'E') &&
(face_order[i] != 'U') &&
(face_order[i] != 'D') )
{
result_string_pointer = "Invalid single cube map face order";
return 0;
};
}
/* capability checking */
if( query_cubemap_capability() != SOIL_CAPABILITY_PRESENT )
{
result_string_pointer = "No cube map capability present";
return 0;
}
/* now, does this image have the right dimensions? */
if( (width != 6*height) &&
(6*width != height) )
{
result_string_pointer = "Single cubemap image must have a 6:1 ratio";
return 0;
}
/* which way am I stepping? */
if( width > height )
{
dw = height;
dh = 0;
} else
{
dw = 0;
dh = width;
}
sz = dw+dh;
sub_img = (unsigned char *)malloc( sz*sz*channels );
/* do the splitting and uploading */
tex_id = reuse_texture_ID;
for( i = 0; i < 6; ++i )
{
int x, y, idx = 0;
unsigned int cubemap_target = 0;
/* copy in the sub-image */
for( y = i*dh; y < i*dh+sz; ++y )
{
for( x = i*dw*channels; x < (i*dw+sz)*channels; ++x )
{
sub_img[idx++] = data[y*width*channels+x];
}
}
/* what is my texture target?
remember, this coordinate system is
LHS if viewed from inside the cube! */
switch( face_order[i] )
{
case 'N':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_Z;
break;
case 'S':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
break;
case 'W':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_X;
break;
case 'E':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_X;
break;
case 'U':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_POSITIVE_Y;
break;
case 'D':
cubemap_target = SOIL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
break;
}
/* upload it as a texture */
tex_id = SOIL_internal_create_OGL_texture(
sub_img, sz, sz, channels,
tex_id, flags,
SOIL_TEXTURE_CUBE_MAP,
cubemap_target,
SOIL_MAX_CUBE_MAP_TEXTURE_SIZE );
}
/* and nuke the image and sub-image data */
SOIL_free_image_data( sub_img );
/* and return the handle, such as it is */
return tex_id;
}
unsigned int
SOIL_create_OGL_texture
(
const unsigned char *const data,
int width, int height, int channels,
unsigned int reuse_texture_ID,
unsigned int flags
)
{
/* wrapper function for 2D textures */
return SOIL_internal_create_OGL_texture(
data, width, height, channels,
reuse_texture_ID, flags,
GL_TEXTURE_2D, GL_TEXTURE_2D,
GL_MAX_TEXTURE_SIZE );
}
#if SOIL_CHECK_FOR_GL_ERRORS
void check_for_GL_errors( const char *calling_location )
{
/* check for errors */
GLenum err_code = glGetError();
while( GL_NO_ERROR != err_code )
{
printf( "OpenGL Error @ %s: %i", calling_location, err_code );
err_code = glGetError();
}
}
#else
void check_for_GL_errors( const char *calling_location )
{
/* no check for errors */
}
#endif
unsigned int
SOIL_internal_create_OGL_texture
(
const unsigned char *const data,
int width, int height, int channels,
unsigned int reuse_texture_ID,
unsigned int flags,
unsigned int opengl_texture_type,
unsigned int opengl_texture_target,
unsigned int texture_check_size_enum
)
{
/* variables */
unsigned char* img;
unsigned int tex_id;
unsigned int internal_texture_format = 0, original_texture_format = 0;
int DXT_mode = SOIL_CAPABILITY_UNKNOWN;
int max_supported_size;
/* If the user wants to use the texture rectangle I kill a few flags */
if( flags & SOIL_FLAG_TEXTURE_RECTANGLE )
{