57 #include "TextureNode.hh" 71 const std::string& _name,
73 GLint _texture_filter )
77 texture_repeat_( _texture_repeat ),
78 tex_mode_( GL_MODULATE ),
79 texture_filter_( _texture_filter ),
80 mipmapping_globally_active_(true),
81 last_mipmapping_status_(true),
84 open_volume_mesh_texture_draw_modes_(DrawModes::getDrawMode(
"Faces (textured)") | DrawModes::getDrawMode(
"Faces (textured and shaded)") )
96 for (std::vector<TextureInfo>::iterator texturesIt = textures_.begin(); texturesIt != textures_.end(); ++texturesIt) {
97 delete texturesIt->tex;
112 if ( !image.load( _filename ) )
114 std::cerr <<
"Cannot load texture " << _filename <<
"\n";
131 glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );
132 glPixelStorei( GL_UNPACK_SKIP_ROWS, 0 );
133 glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
134 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
135 glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
136 glPixelStorei( GL_PACK_SKIP_ROWS, 0 );
137 glPixelStorei( GL_PACK_SKIP_PIXELS, 0 );
138 glPixelStorei( GL_PACK_ALIGNMENT, 1 );
144 if ( texture_repeat_ ) {
145 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
146 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
148 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
149 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
152 const float borderColor[4] = {1.0, 1.0, 1.0, 1.0};
153 glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
156 if((mipmapping_globally_active_ && mipmapping_ && textures_[_id].mipmapAvailable) &&
157 (texture_filter_ == GL_LINEAR_MIPMAP_NEAREST ||
158 texture_filter_ == GL_LINEAR_MIPMAP_LINEAR ||
159 texture_filter_ == GL_NEAREST_MIPMAP_LINEAR ||
160 texture_filter_ == GL_NEAREST_MIPMAP_NEAREST)) {
162 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
163 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture_filter_ );
164 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
166 }
else if(texture_filter_ == GL_LINEAR) {
168 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
169 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
173 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
200 void TextureNode::setTextureDataGL ( GLuint _textureId,
206 const void * _data) {
210 Texture2D* tex = textures_[_textureId].tex;
215 textures_[_textureId].mipmapAvailable =
true;
217 textures_[_textureId].mipmapAvailable =
false;
221 bool mipmaps = mipmapping_globally_active_ && mipmapping_;
224 tex->autogenerateMipMaps();
249 for(
unsigned int i = 1; i < textures_.size(); ++i) {
257 size_t bufferSize = textures_[i].tex->getWidth() * textures_[i].tex->getHeight()*4;
259 if(_mipmap && bufferSize) {
262 GLubyte* buffer =
new GLubyte[bufferSize];
264 tex->getData(0, buffer);
267 tex->autogenerateMipMaps();
268 tex->setData(0, tex->getInternalFormat(), tex->getWidth(), tex->getHeight(),
269 tex->getFormat(), tex->getType(), buffer);
275 textures_[i].mipmapAvailable = _mipmap;
291 if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
292 std::cerr <<
"Can't load texture";
296 setTextureDataGL(
activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_UNSIGNED_BYTE,_image);
310 int tex_w, w( _image.width() );
311 int tex_h, h( _image.height() );
313 for (tex_w=1; tex_w < w; tex_w <<= 1) {};
314 for (tex_h=1; tex_h < h; tex_h <<= 1) {};
315 if (5 * tex_w > 7 *w)
317 if (5 * tex_h > 7 *h)
321 bool isNPOT = ( tex_w != w ) || ( tex_h != h );
330 textureGL = QGLWidget::convertToGLFormat ( _image.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
337 textureGL = QGLWidget::convertToGLFormat ( _image );
341 if ( !Texture2D::checkTextureMem(GL_RGBA, tex_w, tex_h, GL_RGBA) ) {
342 std::cerr <<
"Can't load texture TextureNode::set_texture" << std::endl;
347 setTextureDataGL(
activeTexture_ ,GL_TEXTURE_2D,tex_w,tex_h,GL_RGBA,GL_UNSIGNED_BYTE,textureGL.bits());
359 if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
360 std::cerr <<
"Can't load texture TextureNode::set_texture" << std::endl;
365 setTextureDataGL(
activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_FLOAT,_image);
373 if ( textures_.empty() ) {
388 for ( uint i = 0 ; i < textures_.size(); ++i )
389 if ( textures_[i].tex->id() == _id )
400 return read(_filename);
402 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
421 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
438 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
455 std::cerr <<
"Texture with id " << _id <<
" not handled by this Node!!!" << std::endl;
469 textures_.resize(textures_.size()+1);
480 return textures_.back().tex->id();
498 if(_state.compatibilityProfile())
514 if ( !textures_.empty() ) {
518 if(_state.compatibilityProfile())
519 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_ );
541 if(_state.compatibilityProfile())
580 if ( search == -1 ) {
581 std::cerr <<
"Texture to activate not found!" << std::endl;
static void enable(GLenum _cap, bool _warnRemoved=true)
replaces glEnable, but supports locking
DrawMode SOLID_2DTEXTURED_FACE
draw per halfedge textured faces
bool read(const char *_filename)
Uses a QImage to load the texture from the given file.
DrawMode SOLID_ENV_MAPPED
draw environment mapped
Namespace providing different geometric functions concerning angles.
TextureNode(BaseNode *_parent=0, const std::string &_name="<TextureNode>", bool _texture_repeat=true, GLint _texture_filter=GL_LINEAR)
Default constructor. Applies all properties.
DrawModes::DrawMode open_volume_mesh_texture_draw_modes_
OpenVolumeMesh DrawModes using textures.
static void bindTexture(GLenum _target, GLuint _buffer)
replaces glBindTexture, supports locking
DrawMode SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED
draw per halfedge texture faces modulated with face colors with smooth shading
DrawMode SOLID_2DTEXTURED_FACE_SHADED
draw per halfedge textured faces
void checkEmpty()
Check if a texture is already generated by this Node.
int available(GLuint _id)
check this node for a texture
PickTarget
What target to use for picking.
void enable_mipmapping()
Enable mipmapping.
int activeTexture_
currently active texture
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode) override
Unbind Texture.
DrawMode SOLID_TEXTURED
draw textured faces
void updateMipmaps(bool _mipmap)
Build mip maps of textures that don't have one yet.
void leavePick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode) override
Do nothing in picking.
bool openGLVersion(const int _major, const int _minor, bool _verbose)
void enterPick(GLState &_state, PickTarget _target, const DrawModes::DrawMode &_drawMode) override
Do nothing in picking.
void applyTextureParameters(int _id)
bool activateTexture(GLuint _id)
Set active Texture.
void set_texture(const QImage &_image)
Uses a QImage to set the texture.
virtual ~TextureNode()
Destructor.
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode) override
set default texture and states for the nodes children
GLuint activeTexture()
Get active Texture.
void disable_mipmapping()
Disable mipmapping.
DrawMode SOLID_TEXTURED_SHADED
draw smooth shaded textured faces
bool mipmapping_allowed() const
Get current global mipmapping state.
GLuint add_texture(const QImage &_image)
Add a texture to this node.