graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 970b896a440fa6ee8a394b8b5f1ace2e87752905
parent 08888d85f037d7bfe0d64c3130ecb8c336531cd5
Author: Brian Swetland <swetland@frotz.net>
Date:   Sat, 22 Jun 2013 23:58:25 -0700

textures/loadpng: option flags for grayscale, etc

Diffstat:
Mcommon/app.h | 8++++++--
Mcommon/buffers.cc | 18+++++++++++-------
Mcommon/loadpng.cc | 16+++++++++-------
Mcommon/texturefont.cc | 2+-
Mcommon/util.h | 7+++++--
5 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/common/app.h b/common/app.h @@ -92,14 +92,18 @@ struct Program { int load(const char *vsfn, const char *gsfn, const char *psfn); }; +#define OPT_TEX2D_GEN_MIPMAP 0x0001000 +#define OPT_TEX2D_RGBA 0x0000000 +#define OPT_TEX2D_GRAY 0x0000002 + struct Texture2D { unsigned id; unsigned width; unsigned height; Texture2D() : id(0), width(0), height(0) {}; ~Texture2D() { if (id) { glDeleteTextures(1, &id); } }; - int load(const char *fn, int genmips); - int load(void *data, unsigned w, unsigned h, int genmips); + int load(const char *fn, int options); + int load(void *data, unsigned w, unsigned h, int options); void use(unsigned index) { glActiveTexture(GL_TEXTURE0 + index); glBindTexture(GL_TEXTURE_2D, id); diff --git a/common/buffers.cc b/common/buffers.cc @@ -128,24 +128,28 @@ void UniformBuffer::load(void *data, unsigned size) { sz = size; } -int Texture2D::load(const char *fn, int genmips) { +int Texture2D::load(const char *fn, int options) { void *data; unsigned dw, dh; int r; - if (!(data = load_png_rgba(fn, &dw, &dh, 0))) + if (!(data = load_png_rgba(fn, &dw, &dh, options))) return error("cannot load '%s'", fn); - r = load(data, dw, dh, genmips); + r = load(data, dw, dh, options); free(data); return r; } -int Texture2D::load(void *data, unsigned w, unsigned h, int genmips) { +int Texture2D::load(void *data, unsigned w, unsigned h, int options) { if (id == 0) glGenTextures(1, &id); glBindTexture(GL_TEXTURE_2D, id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, - GL_RGBA, GL_UNSIGNED_BYTE, data); - if (genmips) + if (options & OPT_TEX2D_GRAY) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, + GL_RED, GL_UNSIGNED_BYTE, data); + else + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, data); + if (options & OPT_TEX2D_GEN_MIPMAP) glGenerateMipmap(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); diff --git a/common/loadpng.cc b/common/loadpng.cc @@ -21,14 +21,16 @@ #include "util.h" -void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int inverty) { +void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int options) { png_structp png; png_infop info; png_uint_32 w, h; - int depth, ctype, itype, i; + int depth, ctype, itype, ch, i; png_byte *data = 0; FILE *fp; + ch = (options & OPT_PNG_GRAY) ? 1 : 4; + if ((fp = fopen_asset(fn, "image")) == NULL) goto exit; @@ -90,7 +92,7 @@ void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int if (!(data = (png_byte*) malloc(w * h * ch))) png_error(png, "cannot allocate image buffer"); - if (inverty) + if (options & OPT_PNG_INVERTY) for (i = h-1; i >= 0; i--) png_read_row(png, data + (i * w * ch), NULL); else @@ -111,11 +113,11 @@ exit: return data; } -void *load_png_rgba(const char *fn, unsigned *_width, unsigned *_height, int texture) { - return _load_png(fn, _width, _height, 4, texture); +void *load_png_rgba(const char *fn, unsigned *_width, unsigned *_height, int options) { + return _load_png(fn, _width, _height, options); } -void *load_png_gray(const char *fn, unsigned *_width, unsigned *_height, int texture) { - return _load_png(fn, _width, _height, 1, texture); +void *load_png_gray(const char *fn, unsigned *_width, unsigned *_height, int options) { + return _load_png(fn, _width, _height, options | OPT_PNG_GRAY); } diff --git a/common/texturefont.cc b/common/texturefont.cc @@ -43,7 +43,7 @@ int TextureFont::init(App *app, const char *fontname) { next = data; sprintf(tmp, "%s.font.png", fontname); - if (glyphs.load(tmp, 0)) + if (glyphs.load(tmp, OPT_TEX2D_GRAY)) goto fail; sprintf(tmp, "%s.font.dat", fontname); diff --git a/common/util.h b/common/util.h @@ -25,8 +25,11 @@ typedef unsigned char u8; typedef signed char s8; /* file io helpers */ -void *load_png_rgba(const char *fn, unsigned *width, unsigned *height, int texture); -void *load_png_gray(const char *fn, unsigned *width, unsigned *height, int texture); +#define OPT_PNG_INVERTY 0x0001 +#define OPT_PNG_GRAY 0x0002 + +void *load_png_rgba(const char *fn, unsigned *width, unsigned *height, int options); +void *load_png_gray(const char *fn, unsigned *width, unsigned *height, int options); void *load_file(const char *fn, unsigned *sz); int file_get_mtime(const char *fn);