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