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
22 * A class to encapsulate all the info surrounding an OpenGL texture
23 * and also query various info and load various file formats
30 GLubyte *texture_data;
32 GLsizei texture_width;
33 GLsizei texture_height;
36 void resize(unsigned int width = 256, unsigned int height = 256);
40 typedef struct _ImageRec {
41 unsigned short imagic;
44 unsigned short xsize, ysize, zsize;
45 unsigned int min, max;
46 unsigned int wasteBytes;
48 unsigned long colorMap;
52 unsigned int *rowStart;
56 void ConvertUint(unsigned *array, unsigned int length);
57 void ConvertShort(unsigned short *array, unsigned int length);
58 void rgbtorgb(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *l, int n);
59 void rgbatorgba(GLubyte *r, GLubyte *g, GLubyte *b, GLubyte *a,
62 ImageRec *ImageOpen(const char *fileName);
63 ImageRec *RawImageOpen(const char *fileName);
64 void ImageClose(ImageRec *image);
65 void ImageGetRow(ImageRec *image, GLubyte *buf, int y, int z);
67 inline void free_id() {
69 glDeleteTextures(1, &texture_id);
71 glDeleteTexturesEXT(1, &texture_id);
80 SGTexture(unsigned int width, unsigned int height);
83 /* Copyright (c) Mark J. Kilgard, 1997. */
84 void read_alpha_texture(const char *name);
85 void read_rgb_texture(const char *name);
86 void read_rgba_texture(const char *name);
87 void read_raw_texture(const char *name);
88 void read_r8_texture(const char *name);
90 inline bool usable() { return (texture_id > 0) ? true : false; }
92 inline GLuint id() { return texture_id; }
93 inline GLubyte *texture() { return texture_data; }
94 inline void set_data(GLubyte *data) { texture_data = data; }
96 inline int width() { return texture_width; }
97 inline int height() { return texture_height; }
99 // dynamic texture functions.
100 // everything drawn to the OpenGL screen after prepare is
101 // called and before finish is called will be included
102 // in the new texture.
103 void prepare(unsigned int width = 256, unsigned int height = 256);
104 void finish(unsigned int width, unsigned int height);
106 // texture pixel manipulation functions.
107 void set_pixel(GLuint x, GLuint y, sgVec3 &c);
108 float *get_pixel(GLuint x, GLuint y);
111 inline void select(bool keep_data = false) {
112 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
113 texture_width, texture_height, 0,
114 GL_RGB, GL_UNSIGNED_BYTE, texture_data );
117 delete[] texture_data;
122 // Nowhere does it say that resident textures have to be in video memory!
123 // NVidia's OpenGL drivers implement glAreTexturesResident() correctly,
124 // but the Matrox G400, for example, doesn't.
125 inline bool is_resident() {
127 glAreTexturesResident(1, &texture_id, &is_res);