看了2个晚上总算设计个一个简易的文理类 如下:
/* 19.02.2009 [email protected] www.gaimo.net */ /* how to use this class step is: 1. define instance 2. set img ptr 3. bind 4. set_border(in render) */ #ifndef G_TEXTURE_HPP #define G_TEXTURE_HPP #include <string> #include <windows.h> #include <gl/gl.h> struct img_info { unsigned char *data; int x; int y; std::string name; }; namespace g { namespace graphic { class texture { public: texture(); virtual ~texture(); texture& operator = (const texture&); texture(const texture&); public: //set data info from img file.. bool set_info(const img_info *&); void bind(); virtual void render(); //void set_geome(void_ptr ptr); void set_border(const int x,const int y, const int w, const int h); public: //get info //Point get_size()const{return Point(x,y);} std::string get_name()const{return name;} private: GLubyte *image[1]; int x; int y; GLuint tex[1]; std::string name; //void_ptr geome_ptr; }; } } #endif
cxx文件为
#include "texture.hpp" namespace g { namespace graphic { texture::texture():x(0),y(0),name("") { glEnable(GL_TEXTURE_2D); image[0] = NULL; tex[0] = 0; } texture::~texture() { glDisable(GL_TEXTURE_2D); } //texture& operator = (const texture&); //texture(const texture&); bool texture::set_info(const img_info* &_ptr) { if(_ptr != NULL) { image[0] = _ptr->data; x = _ptr->x; y = _ptr->y; name = _ptr->name; return true; } else return false; } void texture::bind() { glGenTextures(1,&tex[0]); glBindTexture(GL_TEXTURE_2D, tex[0]); } void texture::render() { glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, image[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } //void set_geome(void_ptr ptr); void texture::set_border(const int x,const int y, const int w, const int h) { glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(static_cast<GLfloat>(x), static_cast<GLfloat>(y), 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(static_cast<GLfloat>(w), static_cast<GLfloat>(y), 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(static_cast<GLfloat>(w), static_cast<GLfloat>(h), 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(static_cast<GLfloat>(x), static_cast<GLfloat>(h), 0.0f); glEnd(); } } }