]> git.mxchange.org Git - simgear.git/blob - simgear/screen/texture.hxx
cygwin and mingw 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 <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 public:
65
66     SGTexture();
67     SGTexture(unsigned int width, unsigned int height);
68     ~SGTexture();
69
70     /* Copyright (c) Mark J. Kilgard, 1997.  */
71     void read_alpha_texture(const char *name);
72     void read_rgb_texture(const char *name);
73     void read_raw_texture(const char *name);
74     void read_r8_texture(const char *name);
75
76     inline bool usable() { return texture_data ? true : false; }
77
78     inline GLuint id() { return texture_id; }
79     inline GLubyte *texture() { return texture_data; }
80
81     inline int width() { return texture_width; }
82     inline int height() { return texture_height; }
83
84     // dynamic texture functions.
85     // everything drawn to the OpenGL screen after prepare is
86     // called and before finish is called will be included
87     // in the new texture.
88     void prepare(unsigned int width = 256, unsigned int height = 256);
89     void finish(unsigned int width, unsigned int height);
90
91     // texture pixel manipulation functions.
92     void set_pixel(GLuint x, GLuint y, sgVec3 &c);
93     sgVec3 *get_pixel(GLuint x, GLuint y);
94
95     void bind();
96     inline void select() {
97         // if (texture_data)
98             glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
99                           texture_width, texture_height, 0,
100                           GL_RGB, GL_UNSIGNED_BYTE, texture_data );
101     }
102 };
103
104 #endif
105