1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Modification History
*
* 2001-September-15 Jason Rohrer
* Created.
*
* 2005-February-21 Jason Rohrer
* Added comments about channel ordering.
* Created a cpp file for static init and longer function definitions.
*/
#ifndef SINGLE_TEXTURE_GL_INCLUDED
#define SINGLE_TEXTURE_GL_INCLUDED
#include <GL/gl.h>
#include <stdio.h>
#include <string.h>
#include "minorGems/graphics/Image.h"
#include "minorGems/graphics/RGBAImage.h"
/**
* A single-layer OpenGL 32-bit texture map.
*
* This is essentially a simplified version of TextureGL.
*
* @author Jason Rohrer
*/
class SingleTextureGL {
public:
/**
* Constructs a texture map, specifying texture data.
*
* Note that this constructor can only be called
* after a GL context has been setup.
*
* @param inImage the image to use as a source
* for texture data. The image should have
* 4 channels. Any extra channels are ignored,
* and black is set into additional texture
* channels if inImage has too few channels.
* Must be destroyed by caller, and can be
* destroyed as soon as this constructor returns.
* Channels are ordered as RGBA.
* Must be destroyed by caller.
*
* Note that width and height must each be powers of 2 and
* not larger than 256 (for 3DFX compatability). Additionally,
* for 3DFX compatability, the aspect ratio should not exceed 8:1.
*/
SingleTextureGL( Image *inImage );
/**
* The OpenGL texture is deleted when the SingleTextureGL object is
* destroyed.
*/
~SingleTextureGL();
/**
* Sets the data for this texture.
*
* @param inImage the image to use as a source
* for texture data. The image should have
* 4 channels. Any extra channels are ignored,
* and black is set into additional texture
* channels if inImage has too few channels.
* Must be destroyed by caller, and can be
* destroyed as soon as this constructor returns.
* Channels are ordered as RGBA.
* Must be destroyed by caller.
*
* Note that width and height must each be powers of 2 and
* not larger than 256 (for 3DFX compatability). Additionally,
* for 3DFX compatability, the aspect ratio should not exceed 8:1.
*/
void setTextureData( Image *inImage );
/**
* Enables this texture.
*/
void enable();
/**
* Disables this texture and all of its layers.
*/
void disable();
private:
unsigned int mTextureID;
};
inline SingleTextureGL::SingleTextureGL( Image *inImage ) {
glGenTextures( 1, &mTextureID );
setTextureData( inImage );
}
inline SingleTextureGL::~SingleTextureGL() {
unsigned int *textureIDs = new unsigned int[1];
textureIDs[0] = mTextureID;
glDeleteTextures( 1, textureIDs );
delete [] textureIDs;
}
inline void SingleTextureGL::enable() {
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, mTextureID );
int error = glGetError();
if( error != GL_NO_ERROR ) { // error
printf( "Error binding texture id %d, error = %d\n",
mTextureID, error );
}
}
inline void SingleTextureGL::disable() {
glDisable( GL_TEXTURE_2D );
}
#endif
|