AaronCameron.net
Because you all make me very, very tired.
Not a Member? - Login or Create an Account
Monday the 6th of February 2012 @ 08:34am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cMaterial.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  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 "materials/cMaterial.h"
00026 
00027 #include "n2l/vfs.h"
00028 #include "n2l/dynVars.h"
00029 
00030 #include "materials/cGLTexture.h"
00031 
00032 #include <GL/gl.h>
00033 
00034 #include <iostream>
00035 using namespace std;
00036 
00037 namespace n2l
00038 {
00039 
00040     /*******************************************************************/
00041     cMaterial::cMaterial() :
00042         mAmbient( 0.2f, 0.2f, 0.2f, 1.0f ),
00043         mDiffuse( 0.8f, 0.8f, 0.8f, 1.0f ),
00044         mSpecular( 0.0f, 0.0f, 0.0f, 1.0f ),
00045         mEmission( 0.0f, 0.0f, 0.0f, 1.0f ),
00046         mShininess( 0 ),
00047         mTexture( 0 )
00048     {
00049     }
00050 
00051     /*******************************************************************/
00052     cMaterial::cMaterial( const cVfsNodeInterface & iNode )
00053     {
00054         cMaterial();
00055         load( iNode );
00056     }
00057 
00058     /*******************************************************************/
00059     cMaterial::cMaterial( const cDynVar & iDef )
00060     {
00061         cMaterial();
00062         load( iDef );
00063     }
00064 
00065     /*******************************************************************/
00066     cMaterial::~cMaterial()
00067     {
00068     }
00069 
00070     /*******************************************************************/
00071     void cMaterial::load( const cVfsNodeInterface & iNode )
00072     {
00073         if (!iNode.likeFile())
00074             throw cBadDataUseException( "cMaterial::load",
00075                                         "Provided node isn\'t like a file" );
00076         // Load it.
00077         if (iNode.firstLine()!="n2l::cMaterial")
00078             throw cParsingException("cMaterial::load",
00079                                     "This file isn\'t a cMaterial" );
00080 
00081         cDynVar def;
00082         if (iNode.buffer().size()<=(iNode.firstLine().size()+1))
00083             throw cParsingException("cMaterial::load",
00084                                     "No data after header!" );
00085         def.unserialize( iNode.buffer().c_str() + iNode.firstLine().size()+1 );
00086 
00087         load( def );
00088     }
00089 
00090     /*******************************************************************/
00091     void cMaterial::load( const cDynVar & iDef )
00092     {
00093         // Reset to base values
00094         setGLDefault();
00095 
00096         // Override as defined
00097         if (iDef.keyExists("b")) shininess( tSint(iDef["b"]) );
00098         
00099 
00100         if (iDef.keyExists("n")) name( iDef["n"] );
00101         
00102         if (iDef["a"])
00103             mAmbient.set(   iDef["a"][0],iDef["a"][1],
00104                             iDef["a"][2],iDef["a"][3] );
00105 
00106         if (iDef["d"])
00107             mDiffuse.set(   iDef["d"][0],iDef["d"][1],
00108                             iDef["d"][2],iDef["d"][3] );
00109 
00110         if (iDef["s"])
00111             mSpecular.set(  iDef["s"][0],iDef["s"][1],
00112                             iDef["s"][2],iDef["s"][3] );
00113 
00114         if (iDef["e"])
00115             mEmission.set(  iDef["e"][0],iDef["e"][1],
00116                             iDef["e"][2],iDef["e"][3] );
00117     }
00118     
00119     /*******************************************************************/
00120     void cMaterial::setGLDefault()
00121     {
00122         mAmbient.set( 0.2f, 0.2f, 0.2f, 1.0f );
00123         mDiffuse.set( 0.8f, 0.8f, 0.8f, 1.0f );
00124         mSpecular.set( 0.0f, 0.0f, 0.0f, 1.0f );
00125         mEmission.set( 0.0f, 0.0f, 0.0f, 1.0f );
00126         mShininess = 0;
00127         mTexture = 0;
00128     }
00129 
00130     /*******************************************************************/
00131     void cMaterial::name( const tString & iValue )
00132     {
00133         mName = iValue;
00134     }
00135 
00136     /*******************************************************************/
00137     void cMaterial::ambient( const cColour & iValue )
00138     {
00139         mAmbient = iValue;
00140     }
00141 
00142     /*******************************************************************/
00143     void cMaterial::diffuse( const cColour & iValue )
00144     {
00145         mDiffuse = iValue;
00146     }
00147 
00148     /*******************************************************************/
00149     void cMaterial::specular( const cColour & iValue )
00150     {
00151         mSpecular = iValue;
00152     }
00153 
00154     /*******************************************************************/
00155     void cMaterial::emission( const cColour & iValue )
00156     {
00157         mEmission = iValue;
00158     }
00159 
00160     /*******************************************************************/
00161     void cMaterial::shininess( const tSint iValue )
00162     {
00163         mShininess = n2lMax((tSint)(0),n2lMin((tSint)(128),iValue));
00164     }
00165 
00166     /*******************************************************************/
00167     void cMaterial::texture( const cAutoPtr<const cGLTexture> & iValue )
00168     {
00169         mTexture = iValue;
00170     }
00171 
00172     /*******************************************************************/
00173     void cMaterial::useMaterial( const tMaterialOptions iOptions ) const
00174     {
00175         static const cColour smZeroColour(0,0,0,1);
00176     
00177         if (iOptions & MatOpt_Disable) return;
00178 
00179         if (iOptions & MatOpt_ZeroDiffAmbEmis) {
00180             smZeroColour.glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT );
00181             smZeroColour.glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE );
00182             smZeroColour.glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION );
00183         } else {
00184             mAmbient.glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT );
00185             mDiffuse.glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE );
00186             mEmission.glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION );
00187         }
00188 
00189         if (iOptions & MatOpt_ZeroSpecular) {
00190             smZeroColour.glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR );
00191         } else {
00192             mSpecular.glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR );
00193             glMateriali( GL_FRONT_AND_BACK, GL_SHININESS, mShininess );
00194         }
00195     }
00196 
00197 }
©2012 Aaron Cameron