3 * Texture manipulation routines
5 * Copyright (c) Mark J. Kilgard, 1997.
6 * Code added in april 2003 by Erik Hofman
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.
13 #ifndef __SG_TEXTURE_HXX
14 #define __SG_TEXTURE_HXX 1
16 #include <simgear/compiler.h>
23 * A class to encapsulate all the info surrounding an OpenGL texture
24 * and also query various info and load various file formats
31 GLubyte *texture_data;
33 GLsizei texture_width;
34 GLsizei texture_height;
37 void resize(unsigned int width = 256, unsigned int height = 256);
44 typedef struct _ImageRec {
45 _ImageRec(void) : tmp(0), rowStart(0), rowSize(0) {}
46 unsigned short imagic;
49 unsigned short xsize, ysize, zsize;
50 unsigned int min, max;
51 unsigned int wasteBytes;
53 unsigned long colorMap;
57 unsigned int *rowStart;
61 void ConvertUint(unsigned *array, unsigned int length);
62 void ConvertShort(unsigned short *array, unsigned int length);
63 void rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n);
64 void rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
67 ImageRec *ImageOpen(const char *fileName);
68 ImageRec *ImageWriteOpen(const char *fileName);
69 ImageRec *RawImageOpen(const char *fileName);
70 void ImageClose(ImageRec *image);
71 void ImageGetRow(ImageRec *image, GLubyte *buf, int y, int z);
72 void ImagePutRow(ImageRec *image, GLubyte *buf, int y, int z);
74 inline void free_id() {
76 glDeleteTextures(1, &texture_id);
78 glDeleteTexturesEXT(1, &texture_id);
87 SGTexture(unsigned int width, unsigned int height);
90 /* Copyright (c) Mark J. Kilgard, 1997. */
91 void read_alpha_texture(const char *name);
92 void read_rgb_texture(const char *name);
93 void read_rgba_texture(const char *name);
94 void read_raw_texture(const char *name);
95 void read_r8_texture(const char *name);
96 void write_texture(const char *name);
98 inline bool usable() { return (texture_id > 0) ? true : false; }
100 inline GLuint id() { return texture_id; }
101 inline GLubyte *texture() { return texture_data; }
102 inline void set_data(GLubyte *data) { texture_data = data; }
103 // inline void set_colors(int c) { num_colors = c; }
105 inline int width() { return texture_width; }
106 inline int height() { return texture_height; }
107 inline int colors() { return num_colors; }
109 // dynamic texture functions.
110 // everything drawn to the OpenGL screen after prepare is
111 // called and before finish is called will be included
112 // in the new texture.
113 void prepare(unsigned int width = 256, unsigned int height = 256);
114 void finish(unsigned int width, unsigned int height);
116 // texture pixel manipulation functions.
117 void set_pixel(GLuint x, GLuint y, GLubyte *c);
118 GLubyte *get_pixel(GLuint x, GLuint y);
121 inline void select(bool keep_data = false) {
122 glTexImage2D( GL_TEXTURE_2D, 0, num_colors,
123 texture_width, texture_height, 0,
124 (num_colors==1)?GL_LUMINANCE:(num_colors==3)?GL_RGB:GL_RGBA, GL_UNSIGNED_BYTE, texture_data );
127 delete[] texture_data;
132 // Nowhere does it say that resident textures have to be in video memory!
133 // NVidia's OpenGL drivers implement glAreTexturesResident() correctly,
134 // but the Matrox G400, for example, doesn't.
135 inline bool is_resident() {
137 glAreTexturesResident(1, &texture_id, &is_res);
141 inline const char *err_str() { return errstr; }
142 inline void clear_err_str() { errstr = ""; }
144 void make_maxcolorwindow();
145 void make_grayscale(float contrast = 1.0);
146 void make_monochrome(float contrast = 1.0,
147 GLubyte r=255, GLubyte g=255, GLubyte b=255);
148 void make_normalmap(float brightness = 1.0, float contrast = 1.0);
149 void make_bumpmap(float brightness = 1.0, float contrast = 1.0);