]> git.mxchange.org Git - simgear.git/blob - simgear/screen/texture.hxx
Frederic Bouvier:
[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 protected:
40
41     typedef struct _ImageRec {
42         unsigned short imagic;
43         unsigned short type;
44         unsigned short dim;
45         unsigned short xsize, ysize, zsize;
46         unsigned int min, max;
47         unsigned int wasteBytes;
48         char name[80];
49         unsigned long colorMap;
50         gzFile file;
51         GLubyte *tmp;
52         unsigned long rleEnd;
53         unsigned int *rowStart;
54         int *rowSize;
55     } ImageRec;
56
57     void ConvertUint(unsigned *array, unsigned int length);
58     void ConvertShort(unsigned short *array, unsigned int length);
59     void rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n);
60     void rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
61                     GLubyte *l, int n);
62
63     ImageRec *ImageOpen(const char *fileName);
64     ImageRec *RawImageOpen(const char *fileName);
65     void ImageClose(ImageRec *image);
66     void ImageGetRow(ImageRec *image, GLubyte *buf, int y, int z);
67
68     inline void free_id() {
69 #ifdef GL_VERSION_1_1
70         glDeleteTextures(1, &texture_id);
71 #else
72         glDeleteTexturesEXT(1, &texture_id);
73 #endif
74         texture_id = 0;
75     }
76
77
78 public:
79
80     SGTexture();
81     SGTexture(unsigned int width, unsigned int height);
82     ~SGTexture();
83
84     /* Copyright (c) Mark J. Kilgard, 1997.  */
85     void read_alpha_texture(const char *name);
86     void read_rgb_texture(const char *name);
87     void read_rgba_texture(const char *name);
88     void read_raw_texture(const char *name);
89     void read_r8_texture(const char *name);
90
91     inline bool usable() { return (texture_id > 0) ? true : false; }
92
93     inline GLuint id() { return texture_id; }
94     inline GLubyte *texture() { return texture_data; }
95     inline void set_data(GLubyte *data) { texture_data = data; }
96
97     inline int width() { return texture_width; }
98     inline int height() { return texture_height; }
99
100     // dynamic texture functions.
101     // everything drawn to the OpenGL screen after prepare is
102     // called and before finish is called will be included
103     // in the new texture.
104     void prepare(unsigned int width = 256, unsigned int height = 256);
105     void finish(unsigned int width, unsigned int height);
106
107     // texture pixel manipulation functions.
108     void set_pixel(GLuint x, GLuint y, sgVec3 &c);
109     float *get_pixel(GLuint x, GLuint y);
110
111     void bind();
112     inline void select(bool keep_data = false) {
113         glTexImage2D( GL_TEXTURE_2D, 0, num_colors,
114                       texture_width, texture_height, 0,
115                       (num_colors==1)?GL_LUMINANCE:(num_colors==3)?GL_RGB:GL_RGBA, GL_UNSIGNED_BYTE, texture_data );
116
117         if (!keep_data) {
118             delete[] texture_data;
119             texture_data = 0;
120         }
121     }
122
123     // Nowhere does it say that resident textures have to be in video memory!
124     // NVidia's OpenGL drivers implement glAreTexturesResident() correctly,
125     // but the Matrox G400, for example, doesn't.
126     inline bool is_resident() {
127         GLboolean is_res;
128         glAreTexturesResident(1, &texture_id, &is_res);
129         return is_res != 0;
130     }
131 };
132
133 #endif
134