| 1 | /* |
| 2 | * \file texture.hxx |
| 3 | * Texture manipulation routines |
| 4 | * |
| 5 | * Copyright (c) Mark J. Kilgard, 1997. |
| 6 | * Code added in april 2003 by Erik Hofman |
| 7 | * |
| 8 | * This program is freely distributable without licensing fees |
| 9 | * and is provided without guarantee or warrantee expressed or |
| 10 | * implied. This program is -not- in the public domain. |
| 11 | */ |
| 12 | |
| 13 | #pragma once |
| 14 | |
| 15 | #include <simgear/compiler.h> |
| 16 | #include <osg/GL> |
| 17 | #include <zlib.h> |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * A class to encapsulate all the info surrounding an OpenGL texture |
| 22 | * and also query various info and load various file formats |
| 23 | */ |
| 24 | class SGTexture { |
| 25 | |
| 26 | private: |
| 27 | |
| 28 | GLuint texture_id; |
| 29 | GLubyte *texture_data; |
| 30 | |
| 31 | GLsizei texture_width; |
| 32 | GLsizei texture_height; |
| 33 | GLsizei num_colors; |
| 34 | |
| 35 | void resize(unsigned int width = 256, unsigned int height = 256); |
| 36 | |
| 37 | const char *errstr; |
| 38 | |
| 39 | protected: |
| 40 | |
| 41 | FILE *file; |
| 42 | typedef struct _ImageRec { |
| 43 | _ImageRec(void) : tmp(0), rowStart(0), rowSize(0) {} |
| 44 | unsigned short imagic; |
| 45 | unsigned short type; |
| 46 | unsigned short dim; |
| 47 | unsigned short xsize, ysize, zsize; |
| 48 | unsigned int min, max; |
| 49 | unsigned int wasteBytes; |
| 50 | char name[80]; |
| 51 | unsigned long colorMap; |
| 52 | gzFile file; |
| 53 | GLubyte *tmp; |
| 54 | unsigned long rleEnd; |
| 55 | unsigned int *rowStart; |
| 56 | int *rowSize; |
| 57 | } ImageRec; |
| 58 | |
| 59 | void ConvertUint(unsigned *array, unsigned int length); |
| 60 | void ConvertShort(unsigned short *array, unsigned int length); |
| 61 | void rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n); |
| 62 | void rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a, |
| 63 | GLubyte *l, int n); |
| 64 | |
| 65 | ImageRec *ImageOpen(const char *fileName); |
| 66 | ImageRec *ImageWriteOpen(const char *fileName); |
| 67 | ImageRec *RawImageOpen(const char *fileName); |
| 68 | void ImageClose(ImageRec *image); |
| 69 | void ImageGetRow(ImageRec *image, GLubyte *buf, int y, int z); |
| 70 | void ImagePutRow(ImageRec *image, GLubyte *buf, int y, int z); |
| 71 | |
| 72 | inline void free_id() { |
| 73 | #ifdef GL_VERSION_1_1 |
| 74 | glDeleteTextures(1, &texture_id); |
| 75 | #else |
| 76 | glDeleteTexturesEXT(1, &texture_id); |
| 77 | #endif |
| 78 | texture_id = 0; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | public: |
| 83 | |
| 84 | SGTexture(); |
| 85 | SGTexture(unsigned int width, unsigned int height); |
| 86 | ~SGTexture(); |
| 87 | |
| 88 | /* Copyright (c) Mark J. Kilgard, 1997. */ |
| 89 | void read_alpha_texture(const char *name); |
| 90 | void read_rgb_texture(const char *name); |
| 91 | void read_rgba_texture(const char *name); |
| 92 | void read_raw_texture(const char *name); |
| 93 | void read_r8_texture(const char *name); |
| 94 | void write_texture(const char *name); |
| 95 | |
| 96 | inline bool usable() { return (texture_id > 0) ? true : false; } |
| 97 | |
| 98 | inline GLuint id() { return texture_id; } |
| 99 | inline GLubyte *texture() { return texture_data; } |
| 100 | inline void set_data(GLubyte *data) { texture_data = data; } |
| 101 | inline void set_colors(int c) { num_colors = c; } |
| 102 | |
| 103 | inline int width() { return texture_width; } |
| 104 | inline int height() { return texture_height; } |
| 105 | inline int colors() { return num_colors; } |
| 106 | |
| 107 | // dynamic texture functions. |
| 108 | // everything drawn to the OpenGL screen after prepare is |
| 109 | // called and before finish is called will be included |
| 110 | // in the new texture. |
| 111 | void prepare(unsigned int width = 256, unsigned int height = 256); |
| 112 | void finish(unsigned int width, unsigned int height); |
| 113 | |
| 114 | // texture pixel manipulation functions. |
| 115 | void set_pixel(GLuint x, GLuint y, GLubyte *c); |
| 116 | GLubyte *get_pixel(GLuint x, GLuint y); |
| 117 | |
| 118 | void bind(); |
| 119 | inline void select(bool keep_data = false) { |
| 120 | glTexImage2D( GL_TEXTURE_2D, 0, num_colors, |
| 121 | texture_width, texture_height, 0, |
| 122 | (num_colors==1)?GL_LUMINANCE:(num_colors==3)?GL_RGB:GL_RGBA, GL_UNSIGNED_BYTE, texture_data ); |
| 123 | |
| 124 | if (!keep_data) { |
| 125 | delete[] texture_data; |
| 126 | texture_data = 0; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Nowhere does it say that resident textures have to be in video memory! |
| 131 | // NVidia's OpenGL drivers implement glAreTexturesResident() correctly, |
| 132 | // but the Matrox G400, for example, doesn't. |
| 133 | inline bool is_resident() { |
| 134 | GLboolean is_res; |
| 135 | glAreTexturesResident(1, &texture_id, &is_res); |
| 136 | return is_res != 0; |
| 137 | } |
| 138 | |
| 139 | inline const char *err_str() { return errstr; } |
| 140 | inline void clear_err_str() { errstr = ""; } |
| 141 | |
| 142 | void make_maxcolorwindow(); |
| 143 | void make_grayscale(float contrast = 1.0); |
| 144 | void make_monochrome(float contrast = 1.0, |
| 145 | GLubyte r=255, GLubyte g=255, GLubyte b=255); |
| 146 | void make_normalmap(float brightness = 1.0, float contrast = 1.0); |
| 147 | void make_bumpmap(float brightness = 1.0, float contrast = 1.0); |
| 148 | }; |