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;
35 void resize(unsigned int width = 256, unsigned int height = 256);
39 typedef struct _ImageRec {
40 unsigned short imagic;
43 unsigned short xsize, ysize, zsize;
44 unsigned int min, max;
45 unsigned int wasteBytes;
47 unsigned long colorMap;
51 unsigned int *rowStart;
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);
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);
64 inline void free_id() {
66 glDeleteTextures(1, &texture_id);
68 glDeleteTexturesEXT(1, &texture_id);
77 SGTexture(unsigned int width, unsigned int height);
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);
86 inline bool usable() { return (texture_id > 0) ? true : false; }
88 inline GLuint id() { return texture_id; }
89 inline GLubyte *texture() { return texture_data; }
90 inline void set_data(GLubyte *data) { texture_data = data; }
92 inline int width() { return texture_width; }
93 inline int height() { return texture_height; }
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);
102 // texture pixel manipulation functions.
103 void set_pixel(GLuint x, GLuint y, sgVec3 &c);
104 float *get_pixel(GLuint x, GLuint y);
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 );
113 delete[] texture_data;
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() {
123 glAreTexturesResident(1, &texture_id, &is_res);