istereo2

diff src/tex.c @ 24:9d53a4938ce8

port to android mostly complete, ads not done, and needs some polishing
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Oct 2015 08:15:24 +0300
parents 81d35769f546
children
line diff
     1.1 --- a/src/tex.c	Sat Oct 03 06:10:30 2015 +0300
     1.2 +++ b/src/tex.c	Sun Oct 04 08:15:24 2015 +0300
     1.3 @@ -1,6 +1,6 @@
     1.4  /*
     1.5  Stereoscopic tunnel for iOS.
     1.6 -Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     1.7 +Copyright (C) 2011-2015 John Tsiombikas <nuclear@member.fsf.org>
     1.8  
     1.9  This program is free software: you can redistribute it and/or modify
    1.10  it under the terms of the GNU General Public License as published by
    1.11 @@ -24,29 +24,63 @@
    1.12  #include "tex.h"
    1.13  #include "config.h"
    1.14  #include "imago2.h"
    1.15 +#include "assman.h"
    1.16 +
    1.17 +static size_t ioread(void *buf, size_t bytes, void *uptr);
    1.18 +static long ioseek(long offs, int whence, void *uptr);
    1.19  
    1.20  unsigned int load_texture(const char *fname)
    1.21  {
    1.22 -	int xsz, ysz;
    1.23  	unsigned int tex;
    1.24 -	void *pixels;
    1.25 +	ass_file *fp;
    1.26 +	struct img_io io;
    1.27 +	struct img_pixmap img;
    1.28  
    1.29  	if(!fname) {
    1.30  		return 0;
    1.31  	}
    1.32 -	if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
    1.33 -		fprintf(stderr, "failed to load image: %s\n", fname);
    1.34 +	if(!(fp = ass_fopen(fname, "rb"))) {
    1.35 +		fprintf(stderr, "failed to open texture file: %s: %s\n", fname, strerror(errno));
    1.36  		return 0;
    1.37  	}
    1.38 +	io.uptr = fp;
    1.39 +	io.read = ioread;
    1.40 +	io.write = 0;
    1.41 +	io.seek = ioseek;
    1.42 +
    1.43 +	img_init(&img);
    1.44 +	if(img_read(&img, &io) == -1) {
    1.45 +		fprintf(stderr, "failed to load image: %s\n", fname);
    1.46 +		ass_fclose(fp);
    1.47 +		return 0;
    1.48 +	}
    1.49 +	ass_fclose(fp);
    1.50 +	img_convert(&img, IMG_FMT_RGBA32);
    1.51  
    1.52  	glGenTextures(1, &tex);
    1.53  	glBindTexture(GL_TEXTURE_2D, tex);
    1.54  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    1.55  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    1.56 -	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.57 +	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    1.58  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.59 -	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    1.60 -	img_free_pixels(pixels);
    1.61 +
    1.62 +#ifdef __GLEW_H__
    1.63 +	if(GLEW_SGIS_generate_mipmap) {
    1.64 +		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
    1.65 +#endif
    1.66 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0,
    1.67 +				GL_RGBA, GL_UNSIGNED_BYTE, img.pixels);
    1.68 +#ifdef __GLEW_H__
    1.69 +	} else {
    1.70 +		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, img.width, img.height,
    1.71 +				GL_RGBA, GL_UNSIGNED_BYTE, img.pixels);
    1.72 +	}
    1.73 +#endif
    1.74 +
    1.75 +#ifdef GL_ES_VERSION_2_0
    1.76 +	glGenerateMipmap(GL_TEXTURE_2D);
    1.77 +#endif
    1.78 +	img_destroy(&img);
    1.79  
    1.80  	return tex;
    1.81  }
    1.82 @@ -55,7 +89,7 @@
    1.83  {
    1.84  	glActiveTexture(GL_TEXTURE0 + unit);
    1.85  
    1.86 -#ifndef IPHONE
    1.87 +#ifndef GL_ES_VERSION_2_0
    1.88  	if(tex) {
    1.89  		glEnable(GL_TEXTURE_2D);
    1.90  	} else {
    1.91 @@ -65,3 +99,13 @@
    1.92  
    1.93  	glBindTexture(GL_TEXTURE_2D, tex);
    1.94  }
    1.95 +
    1.96 +static size_t ioread(void *buf, size_t bytes, void *uptr)
    1.97 +{
    1.98 +	return ass_fread(buf, 1, bytes, uptr);
    1.99 +}
   1.100 +
   1.101 +static long ioseek(long offs, int whence, void *uptr)
   1.102 +{
   1.103 +	return ass_fseek(uptr, offs, whence);
   1.104 +}