rev |
line source |
nuclear@2
|
1 /*
|
nuclear@2
|
2 Stereoscopic tunnel for iOS.
|
nuclear@24
|
3 Copyright (C) 2011-2015 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@2
|
4
|
nuclear@2
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@2
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@2
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@2
|
8 (at your option) any later version.
|
nuclear@2
|
9
|
nuclear@2
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@2
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@2
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@2
|
13 GNU General Public License for more details.
|
nuclear@2
|
14
|
nuclear@2
|
15 You should have received a copy of the GNU General Public License
|
nuclear@2
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@2
|
17 */
|
nuclear@2
|
18
|
nuclear@2
|
19 #include <stdio.h>
|
nuclear@2
|
20 #include <stdlib.h>
|
nuclear@2
|
21 #include <string.h>
|
nuclear@2
|
22 #include <errno.h>
|
nuclear@2
|
23 #include "opengl.h"
|
nuclear@2
|
24 #include "tex.h"
|
nuclear@2
|
25 #include "config.h"
|
nuclear@2
|
26 #include "imago2.h"
|
nuclear@24
|
27 #include "assman.h"
|
nuclear@24
|
28
|
nuclear@24
|
29 static size_t ioread(void *buf, size_t bytes, void *uptr);
|
nuclear@24
|
30 static long ioseek(long offs, int whence, void *uptr);
|
nuclear@2
|
31
|
nuclear@2
|
32 unsigned int load_texture(const char *fname)
|
nuclear@2
|
33 {
|
nuclear@2
|
34 unsigned int tex;
|
nuclear@24
|
35 ass_file *fp;
|
nuclear@24
|
36 struct img_io io;
|
nuclear@24
|
37 struct img_pixmap img;
|
nuclear@2
|
38
|
nuclear@2
|
39 if(!fname) {
|
nuclear@2
|
40 return 0;
|
nuclear@2
|
41 }
|
nuclear@24
|
42 if(!(fp = ass_fopen(fname, "rb"))) {
|
nuclear@24
|
43 fprintf(stderr, "failed to open texture file: %s: %s\n", fname, strerror(errno));
|
nuclear@2
|
44 return 0;
|
nuclear@2
|
45 }
|
nuclear@24
|
46 io.uptr = fp;
|
nuclear@24
|
47 io.read = ioread;
|
nuclear@24
|
48 io.write = 0;
|
nuclear@24
|
49 io.seek = ioseek;
|
nuclear@24
|
50
|
nuclear@24
|
51 img_init(&img);
|
nuclear@24
|
52 if(img_read(&img, &io) == -1) {
|
nuclear@24
|
53 fprintf(stderr, "failed to load image: %s\n", fname);
|
nuclear@24
|
54 ass_fclose(fp);
|
nuclear@24
|
55 return 0;
|
nuclear@24
|
56 }
|
nuclear@24
|
57 ass_fclose(fp);
|
nuclear@24
|
58 img_convert(&img, IMG_FMT_RGBA32);
|
nuclear@2
|
59
|
nuclear@2
|
60 glGenTextures(1, &tex);
|
nuclear@2
|
61 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@2
|
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
nuclear@2
|
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
nuclear@24
|
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
nuclear@2
|
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@24
|
66
|
nuclear@24
|
67 #ifdef __GLEW_H__
|
nuclear@24
|
68 if(GLEW_SGIS_generate_mipmap) {
|
nuclear@24
|
69 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
nuclear@24
|
70 #endif
|
nuclear@24
|
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0,
|
nuclear@24
|
72 GL_RGBA, GL_UNSIGNED_BYTE, img.pixels);
|
nuclear@24
|
73 #ifdef __GLEW_H__
|
nuclear@24
|
74 } else {
|
nuclear@24
|
75 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img.width, img.height,
|
nuclear@24
|
76 GL_RGBA, GL_UNSIGNED_BYTE, img.pixels);
|
nuclear@24
|
77 }
|
nuclear@24
|
78 #endif
|
nuclear@24
|
79
|
nuclear@24
|
80 #ifdef GL_ES_VERSION_2_0
|
nuclear@24
|
81 glGenerateMipmap(GL_TEXTURE_2D);
|
nuclear@24
|
82 #endif
|
nuclear@24
|
83 img_destroy(&img);
|
nuclear@2
|
84
|
nuclear@2
|
85 return tex;
|
nuclear@2
|
86 }
|
nuclear@2
|
87
|
nuclear@2
|
88 void bind_texture(unsigned int tex, int unit)
|
nuclear@2
|
89 {
|
nuclear@2
|
90 glActiveTexture(GL_TEXTURE0 + unit);
|
nuclear@2
|
91
|
nuclear@24
|
92 #ifndef GL_ES_VERSION_2_0
|
nuclear@2
|
93 if(tex) {
|
nuclear@2
|
94 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
95 } else {
|
nuclear@2
|
96 glDisable(GL_TEXTURE_2D);
|
nuclear@2
|
97 }
|
nuclear@2
|
98 #endif
|
nuclear@2
|
99
|
nuclear@2
|
100 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@2
|
101 }
|
nuclear@24
|
102
|
nuclear@24
|
103 static size_t ioread(void *buf, size_t bytes, void *uptr)
|
nuclear@24
|
104 {
|
nuclear@24
|
105 return ass_fread(buf, 1, bytes, uptr);
|
nuclear@24
|
106 }
|
nuclear@24
|
107
|
nuclear@24
|
108 static long ioseek(long offs, int whence, void *uptr)
|
nuclear@24
|
109 {
|
nuclear@24
|
110 return ass_fseek(uptr, offs, whence);
|
nuclear@24
|
111 }
|