dbf-halloween2015

diff libs/imago/imago_gl.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/imago/imago_gl.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,223 @@
     1.4 +#include "imago2.h"
     1.5 +
     1.6 +
     1.7 +/* to avoid dependency to OpenGL, I'll define all the relevant GL macros manually */
     1.8 +#define GL_UNSIGNED_BYTE		0x1401
     1.9 +#define GL_FLOAT				0x1406
    1.10 +
    1.11 +#define GL_LUMINANCE			0x1909
    1.12 +#define GL_RGB					0x1907
    1.13 +#define GL_RGBA					0x1908
    1.14 +
    1.15 +#define GL_RGBA32F				0x8814
    1.16 +#define GL_RGB32F				0x8815
    1.17 +#define GL_LUMINANCE32F			0x8818
    1.18 +
    1.19 +#define GL_TEXTURE_2D			0x0de1
    1.20 +#define GL_TEXTURE_WRAP_S		0x2802
    1.21 +#define GL_TEXTURE_WRAP_T		0x2803
    1.22 +#define GL_TEXTURE_MAG_FILTER	0x2800
    1.23 +#define GL_TEXTURE_MIN_FILTER	0x2801
    1.24 +#define GL_LINEAR				0x2601
    1.25 +#define GL_REPEAT				0x2901
    1.26 +
    1.27 +
    1.28 +typedef unsigned int GLenum;
    1.29 +typedef unsigned int GLuint;
    1.30 +typedef int GLint;
    1.31 +typedef int GLsizei;
    1.32 +typedef void GLvoid;
    1.33 +
    1.34 +/* for the same reason I'll load GL functions dynamically */
    1.35 +typedef void (*gl_gen_textures_func)(GLsizei, GLuint*);
    1.36 +typedef void (*gl_bind_texture_func)(GLenum, GLuint);
    1.37 +typedef void (*gl_tex_parameteri_func)(GLenum, GLenum, GLint);
    1.38 +typedef void (*gl_tex_image2d_func)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*);
    1.39 +
    1.40 +static gl_gen_textures_func gl_gen_textures;
    1.41 +static gl_bind_texture_func gl_bind_texture;
    1.42 +static gl_tex_parameteri_func gl_tex_parameteri;
    1.43 +static gl_tex_image2d_func gl_tex_image2d;
    1.44 +
    1.45 +static int load_glfunc(void);
    1.46 +
    1.47 +unsigned int img_fmt_glfmt(enum img_fmt fmt)
    1.48 +{
    1.49 +	switch(fmt) {
    1.50 +	case IMG_FMT_GREY8:
    1.51 +	case IMG_FMT_GREYF:
    1.52 +		return GL_LUMINANCE;
    1.53 +
    1.54 +	case IMG_FMT_RGB24:
    1.55 +	case IMG_FMT_RGBF:
    1.56 +		return GL_RGB;
    1.57 +
    1.58 +	case IMG_FMT_RGBA32:
    1.59 +	case IMG_FMT_RGBAF:
    1.60 +		return GL_RGBA;
    1.61 +
    1.62 +	default:
    1.63 +		break;
    1.64 +	}
    1.65 +	return 0;
    1.66 +}
    1.67 +
    1.68 +unsigned int img_fmt_gltype(enum img_fmt fmt)
    1.69 +{
    1.70 +	switch(fmt) {
    1.71 +	case IMG_FMT_GREY8:
    1.72 +	case IMG_FMT_RGB24:
    1.73 +	case IMG_FMT_RGBA32:
    1.74 +		return GL_UNSIGNED_BYTE;
    1.75 +
    1.76 +	case IMG_FMT_GREYF:
    1.77 +	case IMG_FMT_RGBF:
    1.78 +	case IMG_FMT_RGBAF:
    1.79 +		return GL_FLOAT;
    1.80 +
    1.81 +	default:
    1.82 +		break;
    1.83 +	}
    1.84 +	return 0;
    1.85 +}
    1.86 +
    1.87 +unsigned int img_fmt_glintfmt(enum img_fmt fmt)
    1.88 +{
    1.89 +	switch(fmt) {
    1.90 +	case IMG_FMT_GREY8:
    1.91 +		return GL_LUMINANCE;
    1.92 +	case IMG_FMT_RGB24:
    1.93 +		return GL_RGB;
    1.94 +	case IMG_FMT_RGBA32:
    1.95 +		return GL_RGBA;
    1.96 +	case IMG_FMT_GREYF:
    1.97 +		return GL_LUMINANCE32F;
    1.98 +	case IMG_FMT_RGBF:
    1.99 +		return GL_RGB32F;
   1.100 +	case IMG_FMT_RGBAF:
   1.101 +		return GL_RGBA32F;
   1.102 +	default:
   1.103 +		break;
   1.104 +	}
   1.105 +	return 0;
   1.106 +}
   1.107 +
   1.108 +unsigned int img_glfmt(struct img_pixmap *img)
   1.109 +{
   1.110 +	return img_fmt_glfmt(img->fmt);
   1.111 +}
   1.112 +
   1.113 +unsigned int img_gltype(struct img_pixmap *img)
   1.114 +{
   1.115 +	return img_fmt_gltype(img->fmt);
   1.116 +}
   1.117 +
   1.118 +unsigned int img_glintfmt(struct img_pixmap *img)
   1.119 +{
   1.120 +	return img_fmt_glintfmt(img->fmt);
   1.121 +}
   1.122 +
   1.123 +unsigned int img_gltexture(struct img_pixmap *img)
   1.124 +{
   1.125 +	unsigned int tex;
   1.126 +	unsigned int intfmt, fmt, type;
   1.127 +
   1.128 +	if(!gl_gen_textures) {
   1.129 +		if(load_glfunc() == -1) {
   1.130 +			fprintf(stderr, "imago: failed to initialize the OpenGL helpers\n");
   1.131 +			return 0;
   1.132 +		}
   1.133 +	}
   1.134 +
   1.135 +	intfmt = img_glintfmt(img);
   1.136 +	fmt = img_glfmt(img);
   1.137 +	type = img_gltype(img);
   1.138 +
   1.139 +	gl_gen_textures(1, &tex);
   1.140 +	gl_bind_texture(GL_TEXTURE_2D, tex);
   1.141 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   1.142 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   1.143 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   1.144 +	gl_tex_parameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   1.145 +	gl_tex_image2d(GL_TEXTURE_2D, 0, intfmt, img->width, img->height, 0, fmt, type, img->pixels);
   1.146 +	return tex;
   1.147 +}
   1.148 +
   1.149 +unsigned int img_gltexture_load(const char *fname)
   1.150 +{
   1.151 +	struct img_pixmap img;
   1.152 +	unsigned int tex;
   1.153 +
   1.154 +	img_init(&img);
   1.155 +	if(img_load(&img, fname) == -1) {
   1.156 +		img_destroy(&img);
   1.157 +		return 0;
   1.158 +	}
   1.159 +
   1.160 +	tex = img_gltexture(&img);
   1.161 +	img_destroy(&img);
   1.162 +	return tex;
   1.163 +}
   1.164 +
   1.165 +unsigned int img_gltexture_read_file(FILE *fp)
   1.166 +{
   1.167 +	struct img_pixmap img;
   1.168 +	unsigned int tex;
   1.169 +
   1.170 +	img_init(&img);
   1.171 +	if(img_read_file(&img, fp) == -1) {
   1.172 +		img_destroy(&img);
   1.173 +		return 0;
   1.174 +	}
   1.175 +
   1.176 +	tex = img_gltexture(&img);
   1.177 +	img_destroy(&img);
   1.178 +	return tex;
   1.179 +}
   1.180 +
   1.181 +unsigned int img_gltexture_read(struct img_io *io)
   1.182 +{
   1.183 +	struct img_pixmap img;
   1.184 +	unsigned int tex;
   1.185 +
   1.186 +	img_init(&img);
   1.187 +	if(img_read(&img, io) == -1) {
   1.188 +		img_destroy(&img);
   1.189 +		return 0;
   1.190 +	}
   1.191 +
   1.192 +	tex = img_gltexture(&img);
   1.193 +	img_destroy(&img);
   1.194 +	return tex;
   1.195 +}
   1.196 +
   1.197 +#if defined(__unix__) || defined(__APPLE__)
   1.198 +#ifndef __USE_GNU
   1.199 +#define __USE_GNU
   1.200 +#endif
   1.201 +
   1.202 +#include <dlfcn.h>
   1.203 +#endif
   1.204 +#ifdef WIN32
   1.205 +#include <windows.h>
   1.206 +#endif
   1.207 +
   1.208 +static int load_glfunc(void)
   1.209 +{
   1.210 +#if defined(__unix__) || defined(__APPLE__)
   1.211 +	gl_gen_textures = (gl_gen_textures_func)dlsym(RTLD_DEFAULT, "glGenTextures");
   1.212 +	gl_bind_texture = (gl_bind_texture_func)dlsym(RTLD_DEFAULT, "glBindTexture");
   1.213 +	gl_tex_parameteri = (gl_tex_parameteri_func)dlsym(RTLD_DEFAULT, "glTexParameteri");
   1.214 +	gl_tex_image2d = (gl_tex_image2d_func)dlsym(RTLD_DEFAULT, "glTexImage2D");
   1.215 +#endif
   1.216 +
   1.217 +#ifdef WIN32
   1.218 +	HMODULE handle = GetModuleHandle(0);
   1.219 +	gl_gen_textures = (gl_gen_textures_func)GetProcAddress(handle, "glGenTextures");
   1.220 +	gl_bind_texture = (gl_bind_texture_func)GetProcAddress(handle, "glBindTexture");
   1.221 +	gl_tex_parameteri = (gl_tex_parameteri_func)GetProcAddress(handle, "glTexParameteri");
   1.222 +	gl_tex_image2d = (gl_tex_image2d_func)GetProcAddress(handle, "glTexImage2D");
   1.223 +#endif
   1.224 +
   1.225 +	return (gl_gen_textures && gl_bind_texture && gl_tex_parameteri && gl_tex_image2d) ? 0 : -1;
   1.226 +}