OglTexture.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <sofa/component/visualmodel/OglTexture.h>
00026 #include <sofa/core/ObjectFactory.h>
00027 #include <sofa/helper/system/FileRepository.h>
00028
00029 namespace sofa
00030 {
00031 namespace component
00032 {
00033
00034 namespace visualmodel
00035 {
00036
00037
00038 SOFA_DECL_CLASS(OglTexture2D)
00039
00040
00041 int OglTexture2DClass = core::RegisterObject("OglTexture2D")
00042 .add< OglTexture2D >()
00043 ;
00044
00045 unsigned short OglTexture::MAX_NUMBER_OF_TEXTURE_UNIT = 1;
00046
00047 OglTexture::OglTexture()
00048 :textureUnit(initData(&textureUnit, 1, "textureUnit", "Set the texture unit"))
00049 ,enabled(initData(&enabled, (bool) true, "enabled", "enabled ?"))
00050 {
00051
00052 }
00053
00054 OglTexture::~OglTexture()
00055 {
00056
00057 }
00058
00059 void OglTexture::setActiveTexture(unsigned short unit)
00060 {
00061 glActiveTexture(GL_TEXTURE0 + unit);
00062 }
00063
00064 void OglTexture::init()
00065 {
00066 OglShaderElement::init();
00067 }
00068
00069 void OglTexture::initVisual()
00070 {
00071 GLint maxTextureUnits;
00072 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
00073 MAX_NUMBER_OF_TEXTURE_UNIT = maxTextureUnits;
00074
00075 if (textureUnit.getValue() > MAX_NUMBER_OF_TEXTURE_UNIT)
00076 {
00077 std::cerr << "Unit Texture too high ; set it at the unit texture n°1" << std::endl;
00078 textureUnit.setValue(1);
00079 }
00080
00081
00082
00083
00084
00085
00086 }
00087
00088 void OglTexture::reinit()
00089 {
00090 if (textureUnit.getValue() > MAX_NUMBER_OF_TEXTURE_UNIT)
00091 {
00092 std::cerr << "Unit Texture too high ; set it at the unit texture n°1" << std::endl;
00093 textureUnit.setValue(1);
00094 }
00095
00096 }
00097 void OglTexture::fwdDraw(Pass)
00098 {
00099 if (enabled.getValue())
00100 {
00101 setActiveTexture(textureUnit.getValue());
00102 bind();
00103 forwardDraw();
00104 setActiveTexture(0);
00105 }
00106 }
00107 void OglTexture::bwdDraw(Pass)
00108 {
00109 if (enabled.getValue())
00110 {
00111 setActiveTexture(textureUnit.getValue());
00112 unbind();
00113 backwardDraw();
00115 setActiveTexture(0);
00116 }
00117 }
00118
00119 OglTexture2D::OglTexture2D()
00120 :texture2DFilename(initData(&texture2DFilename, (std::string) "", "texture2DFilename", "Texture2D Filename"))
00121 ,repeat(initData(&repeat, (bool) false, "repeat", "Repeat Texture ?"))
00122 {
00123
00124 }
00125
00126
00127 OglTexture2D::~OglTexture2D()
00128 {
00129 if (!texture2D)delete texture2D;
00130 }
00131
00132 void OglTexture2D::parse(core::objectmodel::BaseObjectDescription* arg)
00133 {
00134 helper::system::FileRepository fp;
00135 if (arg->getAttribute("texture2DFilename"))
00136 {
00137 texture2DFilename.setValue( arg->getAttribute("texture2DFilename") );
00138 }
00139 img = helper::io::Image::Create(texture2DFilename.getValue());
00140
00141 if (arg->getAttribute("id"))
00142 {
00143 id.setValue( arg->getAttribute("id") );
00144 }
00145
00146 if (arg->getAttribute("textureUnit"))
00147 {
00148 textureUnit.setValue( atoi(arg->getAttribute("textureUnit") ) );
00149 }
00150 }
00151
00152 void OglTexture2D::initVisual()
00153 {
00154 OglTexture::initVisual();
00155
00156 if (!img)
00157 {
00158 std::cerr << "OglTexture2D: Error : OglTexture2D file " << texture2DFilename.getValue() << " not found." << std::endl;
00159 return;
00160 }
00161
00162 texture2D = new helper::gl::Texture(img);
00163
00164 texture2D->init();
00165 setActiveTexture(textureUnit.getValue());
00166
00167 bind();
00168
00169 if (!repeat.getValue())
00170 {
00171 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00173 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP );
00174 }
00175 else
00176 {
00177 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
00178 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
00179 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT );
00180 }
00181
00182 unbind();
00183
00184 shader->setTexture(id.getValue().c_str(), textureUnit.getValue());
00185
00186 setActiveTexture(0);
00187 }
00188
00189 void OglTexture2D::bind()
00190 {
00191 if (!texture2D) initVisual();
00192 texture2D->bind();
00193 glEnable(GL_TEXTURE_2D);
00194 }
00195
00196 void OglTexture2D::unbind()
00197 {
00198 texture2D->unbind();
00199 glDisable(GL_TEXTURE_2D);
00200 }
00201
00202 void OglTexture2D::forwardDraw()
00203 {
00204
00205 }
00206
00207 void OglTexture2D::backwardDraw()
00208 {
00209
00210 }
00211
00212
00213 }
00214
00215 }
00216
00217 }