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