Texture3DNode.cc 6.69 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
Jan Möbius's avatar
Jan Möbius committed
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
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
37
 *   $Revision$                                                       *
Jan Möbius's avatar
Jan Möbius committed
38 39 40 41
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
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




//=============================================================================
//
//  CLASS Texture3DNode - IMPLEMENTATION
//
//=============================================================================

//=============================================================================


#include "Texture3DNode.hh"
#include "DrawModes.hh"

//=============================================================================

namespace ACG {
namespace SceneGraph {

//=============================================================================


Texture3DNode::Texture3DNode( BaseNode          * _parent, 
			      const std::string & _name )
  : BaseNode( _parent, _name ),
    texture_( 0 ), 
    tex_mode_( GL_MODULATE ),
    border_color_( 0, 0, 0, 0 ),
    wrap_mode_( GL_CLAMP ),
    filter_( GL_LINEAR )
{}

    
//----------------------------------------------------------------------------


Texture3DNode::~Texture3DNode()
{
  if ( glIsTexture( texture_ ) )
    glDeleteTextures( 1, &texture_ );
}


//----------------------------------------------------------------------------
  

void
91
Texture3DNode::enter( GLState &    /*_state */, const DrawModes::DrawMode& /* _drawmode */ ) 
Jan Möbius's avatar
 
Jan Möbius committed
92 93 94
{
  if ( glIsTexture( texture_ ) )
  {
95
    ACG::GLState::bindTexture( GL_TEXTURE_3D, texture_ );
Jan Möbius's avatar
 
Jan Möbius committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_);
    glTexParameterfv( GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, border_color_.data() );

    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, wrap_mode_ );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, wrap_mode_ );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, wrap_mode_ );

    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, filter_ );
    glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, filter_ );

  }
}


//----------------------------------------------------------------------------


void
114
Texture3DNode::leave( GLState &    /* _state */ , const DrawModes::DrawMode& /* _drawmode */  )
Jan Möbius's avatar
 
Jan Möbius committed
115
{
116
  ACG::GLState::bindTexture( GL_TEXTURE_3D, 0 );
Jan Möbius's avatar
 
Jan Möbius committed
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 156 157 158 159 160 161 162 163
}


//----------------------------------------------------------------------------


void
Texture3DNode::set_texture( unsigned int    _width,
			    unsigned int    _height,
			    unsigned int    _depth,
			    unsigned char * _data )
{
  glTexImage3D( GL_PROXY_TEXTURE_3D, 0, GL_RGBA,
 		_width, _height, _depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );

  GLint test_width;
  glGetTexLevelParameteriv( GL_PROXY_TEXTURE_3D, 0,
			    GL_TEXTURE_WIDTH, & test_width );

  GLint test_height;
  glGetTexLevelParameteriv( GL_PROXY_TEXTURE_3D, 0,
			    GL_TEXTURE_HEIGHT, & test_height );

  GLint test_depth;
  glGetTexLevelParameteriv( GL_PROXY_TEXTURE_3D, 0,
			    GL_TEXTURE_DEPTH, & test_depth );

  if ( ! test_width || ! test_height || ! test_depth ) 
  {
    std::cerr << "Can't load texture.\n";
    return;
  }

  glPixelStorei( GL_UNPACK_ROW_LENGTH,  0 );
  glPixelStorei( GL_UNPACK_SKIP_ROWS,   0 );
  glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
  glPixelStorei( GL_UNPACK_ALIGNMENT,   1 );
  glPixelStorei( GL_PACK_ROW_LENGTH,    0 );
  glPixelStorei( GL_PACK_SKIP_ROWS,     0 );
  glPixelStorei( GL_PACK_SKIP_PIXELS,   0 );
  glPixelStorei( GL_PACK_ALIGNMENT,     1 );    


  if ( glIsTexture( texture_ ) )
    glDeleteTextures( 1, & texture_ );

  glGenTextures( 1, & texture_ );
164
  ACG::GLState::bindTexture( GL_TEXTURE_3D, texture_ );
Jan Möbius's avatar
 
Jan Möbius committed
165 166 167 168

  glTexImage3D( GL_TEXTURE_3D, 0, GL_RGBA,
 		_width, _height, _depth, 0, GL_RGBA, GL_UNSIGNED_BYTE, _data );

169
  ACG::GLState::bindTexture( GL_TEXTURE_3D, 0 );
Jan Möbius's avatar
 
Jan Möbius committed
170 171 172 173 174 175 176 177

}

 
//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================