istereo2

view src/tex.c @ 30:900651a2f401

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