![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cGuiModelView.cppGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2003 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 #include "gui/cGuiModelView.h" 00026 00027 #include "n2l/dynVars.h" 00028 #include "n2l/resourceManagement.h" 00029 #include "n2l/materials.h" 00030 #include "n2l/renderObjects.h" 00031 #include "n2l/renderTools.h" 00032 #include "n2l/video.h" 00033 00034 #include "gui/cGuiFill.h" 00035 00036 /******************************************************************************/ 00037 namespace n2l 00038 { 00039 00040 00041 /**************************************************************************/ 00042 cGuiModelView::cGuiModelView() 00043 { 00044 init(); 00045 } 00046 00047 00048 /**************************************************************************/ 00049 cGuiModelView::~cGuiModelView() 00050 { 00051 init(); 00052 } 00053 00054 00055 /**************************************************************************/ 00056 cGuiModelView::cGuiModelView( const cVfsNodeInterface & iNode ) 00057 { 00058 init(); 00059 load( iNode ); 00060 } 00061 00062 00063 /**************************************************************************/ 00064 cGuiModelView::cGuiModelView( const cDynVar & iDefinition ) 00065 { 00066 init(); 00067 load( iDefinition ); 00068 } 00069 00070 /**************************************************************************/ 00071 void cGuiModelView::load( const cVfsNodeInterface &iNode ) 00072 { 00073 cDynVar def; 00074 validateAndDecode( def, iNode, "n2l::cGuiModelView" ); 00075 load( def ); 00076 } 00077 00078 /**************************************************************************/ 00079 void cGuiModelView::load( const cDynVar &iDef ) 00080 { 00081 // Load parent properties 00082 cGuiElement::load( iDef ); 00083 00084 if (iDef.keyExists("fill")) { 00085 const cDynVar &Tmp = iDef["fill"]; 00086 if (Tmp.isArray()) 00087 mFill = new cGuiFill( Tmp ); 00088 else 00089 mFill = new cGuiFill( 00090 *cResourceManager::get<cGuiFill>( Tmp ) ); 00091 } 00092 00093 if (iDef["model"]) { 00094 if (iDef["model"].isArray()) 00095 mesh( new cRVisualTriMesh(iDef["model"]) ); 00096 else 00097 mesh( cResourceManager::get<cRVisualTriMesh>(iDef["model"]) ); 00098 } 00099 00100 if (iDef["texture"]) 00101 mTexture = cResourceManager::get<cGLTexture>(iDef["texture"]); 00102 00103 if (iDef["modelTrans"].isArray()) 00104 for ( tSint i = 0; i!=16; ++i ) 00105 mMeshTrans[i] = iDef["modelTrans"][i]; 00106 00107 mNeedsUpdate = true; 00108 } 00109 00110 /**************************************************************************/ 00111 void cGuiModelView::mesh( const cAutoPtr<const cRVisualTriMesh> &iMesh ) 00112 { 00113 mMesh = iMesh; 00114 if (mMesh.isSet()) { 00115 mMeshDim = mMesh->dimensions(); 00116 if (mCamMode==CameraMode_Auto) { 00117 const tFloat Radius = mMesh->boundingRadius(); 00118 00119 const tFloat FovY = 50.0f; 00120 const tFloat Near = 1.0f; 00121 mPerProj = new cRPerspectiveProjection( FovY, 1.0f, 00122 Near,1000.0f ); 00123 // Find the closest visible point on the intersection of 00124 // the near and top clipping planes 00125 00126 tVector3f p( 0.0f, 2.14f, 1.0f ); 00127 00128 // Set the distance between the object location and point p 00129 // to the bounding radius. 00130 tVector3f o( 0.0f, 0.0f, /* z */ 0.0f ); 00131 // So o-p == Radius, then solve for z 00132 // Radius = sqrt( (0-0)^2 + (p.y-0)^2 + (z-1)^2 ) 00133 // Radius^2 = z^2 + 2z + 1 + 4.58 00134 // 0 = z^2 + 2z + p.y^2-Radius^2 00135 tFloat c = (p.y()*p.y())-(Radius*Radius); 00136 // z^2 - 2z + c 00137 // z = ( 2+sqrt(4 - 4c) )/2 00138 // Finally, reverse for the camera 00139 o.z() = -1.f * (2.0f+sqrt(4.0f-4.0f*c)); 00140 00141 mCamera->set( o, 00142 tVector3f(0.0f,0.0f,1.0f), tVector3f(0.0f,-1.0f,0.0f) ); 00143 } 00144 } 00145 00146 mNeedsUpdate = true; 00147 } 00148 00149 /**************************************************************************/ 00150 void cGuiModelView::texture( const cAutoPtr<const cGLTexture> &iTexture ) 00151 { 00152 mTexture = iTexture; 00153 mNeedsUpdate = true; 00154 } 00155 00156 00157 /**************************************************************************/ 00158 void cGuiModelView::meshTrans( const tMatrix44f &iTrans ) 00159 { 00160 mMeshTrans = iTrans; 00161 mNeedsUpdate = true; 00162 } 00163 00164 /**************************************************************************/ 00165 void cGuiModelView::update( const tUint iTimePassed ) 00166 { 00167 cGuiElement::update( iTimePassed ); 00168 00169 if (!mMesh.isSet()) return; 00170 00171 if (mRender.isSet() && mCamVersion==mCamera->version() && 00172 mNeedsUpdate==false) 00173 { 00174 return; 00175 } 00176 00177 glPushAttrib( GL_ALL_ATTRIB_BITS ); 00178 00179 glViewport( 0,0, 512,512 ); 00180 00181 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); 00182 glClearDepth( 1.0f ); 00183 glDepthFunc( GL_LESS ); 00184 glEnable( GL_DEPTH_TEST ); 00185 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00186 00187 glCullFace( GL_BACK ); 00188 glEnable( GL_CULL_FACE ); 00189 glEnable( GL_TEXTURE_2D ); 00190 00191 glMatrixMode( GL_PROJECTION ); 00192 00193 glPushMatrix(); 00194 glLoadIdentity(); 00195 00196 mPerProj->render(); 00197 00198 glMatrixMode( GL_MODELVIEW ); 00199 glPushMatrix(); 00200 glLoadIdentity(); 00201 00202 mCamera->render(); 00203 00204 cColour( 0.2f, 0.2f, 0.2f, 1.0f ).glLightfv( GL_LIGHT0, GL_AMBIENT ); 00205 cColour( 1.0f, 1.0f, 1.0f, 1.0f ).glLightfv( GL_LIGHT0, GL_DIFFUSE ); 00206 cColour( 1.0f, 1.0f, 1.0f, 1.0f ).glLightfv( GL_LIGHT0, GL_SPECULAR ); 00207 cColour( 0.0f, 20.0f, -1000.0f, 0.0f ).glLightfv( GL_LIGHT0, GL_POSITION ); 00208 glEnable( GL_LIGHTING ); 00209 glEnable( GL_LIGHT0 ); 00210 00211 glMultMatrixf( mMeshTrans.data() ); 00212 00213 mMesh->render( mTexture, R_Normal ); // R_ShowNormals | R_FaceLighting 00214 00215 glPopMatrix(); 00216 00217 glMatrixMode( GL_PROJECTION ); 00218 glPopMatrix(); 00219 00220 if (!mRender.isSet()) 00221 mRender = new cGLTexture( GL_RGBA, tVector2u(512,512) ); 00222 00223 mRender->bindTexture2D(); 00224 00225 glReadBuffer( GL_BACK ); 00226 glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 0,0,512,512, 0 ); 00227 00228 dynamic_cast<cOpenGLDisplay*>(cOpenGLDisplay::currentDisplay())-> 00229 resetViewport(); 00230 00231 glPopAttrib(); 00232 00233 mNeedsUpdate = false; 00234 mCamVersion = mCamera->version(); 00235 } 00236 00237 /**************************************************************************/ 00238 void cGuiModelView::draw() const 00239 { 00240 00241 if (!mRender.isSet()) return; 00242 00243 mFill->texture( mRender ); 00244 // I don't want to think about why right now. 00245 // mFill->draw( pos()+tVector2f(0.0f,size().y()), 00246 // size()*tVector2f(1.0f,-1.0f) ); 00247 mFill->draw( pos(), size() ); 00248 00249 } 00250 00251 /**************************************************************************/ 00252 void cGuiModelView::prop( const tString &iName, const cDynVar &iVal, 00253 const tString &iInnerKey ) 00254 { 00255 if (iName == "fill") { 00256 if (iInnerKey.empty()) { 00257 if (iVal.isArray()) 00258 mFill = new cGuiFill( iVal ); 00259 else 00260 mFill = new cGuiFill( 00261 *cResourceManager::get<cGuiFill>(iVal) ); 00262 } else 00263 mFill->prop( iInnerKey, iVal ); 00264 00265 } 00266 00267 else cGuiElement::prop( iName, iVal ); 00268 } 00269 00270 /**************************************************************************/ 00271 const cDynVar cGuiModelView::prop( const tString &iName, 00272 const tString &iInnerKey ) const 00273 { 00274 cDynVar rVal; 00275 00276 if (iName == "fill" && !iInnerKey.empty()) 00277 return mFill->prop( iInnerKey ); 00278 00279 return cGuiElement::prop( iName ); 00280 } 00281 00282 00283 /**************************************************************************/ 00284 const cAutoPtr<cGuiElement> cGuiModelView::clone() const 00285 { 00286 cAutoPtr<cGuiElement> newElement( new cGuiModelView ); 00287 cloneInto( newElement ); 00288 return newElement; 00289 } 00290 00291 /**************************************************************************/ 00292 void cGuiModelView::cloneInto( 00293 const cAutoPtr<cGuiModelView> &i_ioElement ) const 00294 { 00295 // Parent Properties first 00296 cGuiElement::cloneInto( i_ioElement ); 00297 00298 // Now our properties. 00299 i_ioElement->mMeshTrans = mMeshTrans; 00300 00301 i_ioElement->mMeshDim = mMeshDim; 00302 00303 i_ioElement->mTexture = mTexture; 00304 00305 i_ioElement->mPerProj = mPerProj->clone(); 00306 i_ioElement->mCamera = new cRFreeCamera( *mCamera ); 00307 00308 i_ioElement->mNeedsUpdate = true; 00309 00310 i_ioElement->mFill = new cGuiFill( *mFill ); 00311 00312 i_ioElement->mCamMode = mCamMode; 00313 00314 i_ioElement->mesh( mMesh ); 00315 } 00316 00317 /**************************************************************************/ 00318 void cGuiModelView::init() 00319 { 00320 mPerProj = new cRPerspectiveProjection( 50.0, 1.0, 1.0, 1000.0 ); 00321 mCamera = new cRFreeCamera( tVector3f(0.0f,0.0f,-50.0f), 00322 tVector3f(0.0f,0.0f,1.0f), tVector3f(0.0f,-1.0f,0.0f) ); 00323 mCamVersion = 0; 00324 mMeshTrans.identity(); 00325 mNeedsUpdate = false; 00326 mCamMode = CameraMode_Auto; 00327 mFill = new cGuiFill; 00328 } 00329 00330 /**************************************************************************/ 00331 const cAutoPtr<cGuiElement> cGuiModelView::loadNew( const cDynVar &iDef ) 00332 { 00333 return new cGuiModelView(iDef); 00334 } 00335 00336 } // namespace n2l |