00001 #include "GLTexture1D.h"
00002
00003 #include <QDebug>
00004
00005 GLTexture1D::GLTexture1D()
00006 {
00007 texture = 0;
00008 width = 0;
00009 }
00010
00011 GLTexture1D::~GLTexture1D()
00012 {
00013 free();
00014 }
00015
00016 GLuint GLTexture1D::getTexture()
00017 {
00018 return texture;
00019 }
00020
00021 GLuint GLTexture1D::getWidth()
00022 {
00023 return width;
00024 }
00025
00026 void GLTexture1D::setTypes(GLuint textureColorType, GLuint filterType)
00027 {
00028 this->textureColorType = textureColorType;
00029 this->filterType = filterType;
00030 }
00031
00032 void GLTexture1D::create(GLuint width)
00033 {
00034 free();
00035
00036 GLenum glError;
00037
00038
00039 GLint cs1DTexture;
00040 glGetIntegerv(GL_TEXTURE_BINDING_1D, &cs1DTexture);
00041
00042 glGenTextures(1, &texture);
00043 glBindTexture(GL_TEXTURE_1D, texture);
00044
00045 glTexImage1D(GL_TEXTURE_1D, 0, textureColorType, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00046 glError = glGetError();
00047 while(glError != GL_NO_ERROR && width > 512) {
00048 qDebug() << "create GLTexture1D with " << width << " width failed";
00049 width /= 2;
00050 glTexImage1D(GL_TEXTURE_1D, 0, textureColorType, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
00051 glError = glGetError();
00052 }
00053
00054 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, filterType);
00055 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, filterType);
00056
00057 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00058
00059 glError = glGetError();
00060 if(glError != GL_NO_ERROR)
00061 {
00062 qDebug() << "create GLTexture1D error / generate texture: " << (char*)gluErrorString(glError);
00063 }
00064
00065
00066 glBindTexture(GL_TEXTURE_1D, cs1DTexture);
00067
00068 glError = glGetError();
00069 if(glError != GL_NO_ERROR)
00070 {
00071 qDebug() << "create GLTexture1D error / unbind: " << (char*)gluErrorString(glError);
00072 }
00073
00074 this->width = width;
00075 }
00076
00077 bool GLTexture1D::updateSubImage(GLuint xOffset, GLuint width, GLuint format, GLuint type, void* data)
00078 {
00079 bool ret = false;
00080
00081 if(texture != 0)
00082 {
00083
00084 GLint cs1DTexture;
00085 glGetIntegerv(GL_TEXTURE_BINDING_1D, &cs1DTexture);
00086
00087 glBindTexture(GL_TEXTURE_1D, texture);
00088 glTexSubImage1D(GL_TEXTURE_1D, 0, xOffset, width, format, type, data);
00089
00090
00091 glBindTexture(GL_TEXTURE_1D, cs1DTexture);
00092
00093 GLenum glError = glGetError();
00094 if(glError != GL_NO_ERROR)
00095 {
00096 qDebug() << "updateSubImage GLTexture1D error: " << (char*)gluErrorString(glError);
00097 }
00098
00099 ret = true;
00100 }
00101
00102 return ret;
00103 }
00104
00105 void GLTexture1D::free()
00106 {
00107 if(texture != 0)
00108 {
00109 glDeleteTextures(1, &texture);
00110 texture = 0;
00111
00112 GLenum glError = glGetError();
00113 if(glError != GL_NO_ERROR)
00114 {
00115 qDebug() << "free GLTexture1D error: " << (char*)gluErrorString(glError);
00116 }
00117 }
00118 }