+++ /dev/null
-#include "stdafx.h"\r
-/*\r
-* Copyright (c) 2007-2010 SlimDX Group\r
-* \r
-* Permission is hereby granted, free of charge, to any person obtaining a copy\r
-* of this software and associated documentation files (the "Software"), to deal\r
-* in the Software without restriction, including without limitation the rights\r
-* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
-* copies of the Software, and to permit persons to whom the Software is\r
-* furnished to do so, subject to the following conditions:\r
-* \r
-* The above copyright notice and this permission notice shall be included in\r
-* all copies or substantial portions of the Software.\r
-* \r
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
-* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
-* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
-* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
-* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
-* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
-* THE SOFTWARE.\r
-*/\r
-\r
-#include <d3dx9.h>\r
-\r
-#include "../SlimDXException.h"\r
-\r
-#include "../direct3d9/Enums.h"\r
-#include "../direct3d9/PaletteEntry.h"\r
-#include "../direct3d9/CubeTexture.h"\r
-\r
-#include "Color3.h"\r
-#include "Color4.h"\r
-#include "Matrix.h"\r
-#include "Vector3.h"\r
-#include "SHVector.h"\r
-\r
-using namespace System;\r
-using namespace System::Text;\r
-using namespace System::Globalization;\r
-\r
-namespace SlimDX\r
-{\r
- SHVector::SHVector( int order )\r
- {\r
- coefficients = gcnew array<float>( order * order );\r
- this->order = order;\r
- }\r
-\r
- SHVector::SHVector( int order, array<float>^ coefficients )\r
- {\r
- this->coefficients = coefficients;\r
- this->order = order;\r
-\r
- if( order * order != coefficients->Length )\r
- throw gcnew InvalidOperationException( "The number of coefficients must be equal to the order squared." );\r
- }\r
-\r
- SHVector^ SHVector::Add( SHVector^ left, SHVector^ right )\r
- {\r
- if( left->Order != right->Order )\r
- throw gcnew InvalidOperationException( "The order of each vector must be identical." );\r
-\r
- array<float>^ output = gcnew array<float>( left->Coefficients->Length );\r
- for( int i = 0; i < left->Coefficients->Length; i++ )\r
- output[i] = left[i] + right[i];\r
-\r
- return gcnew SHVector( left->Order, output );\r
- }\r
-\r
- SHVector^ SHVector::Rotate( SHVector^ vector, Matrix rotationMatrix )\r
- {\r
- array<float>^ output = gcnew array<float>( vector->Coefficients->Length );\r
-\r
- pin_ptr<float> pinnedOutput = &output[0];\r
- pin_ptr<float> pinnedInput = &vector->Coefficients[0];\r
-\r
- D3DXSHRotate( pinnedOutput, vector->Order, reinterpret_cast<D3DXMATRIX*>( &rotationMatrix ), pinnedInput );\r
-\r
- return gcnew SHVector( vector->Order, output );\r
- }\r
-\r
- SHVector^ SHVector::RotateZ( SHVector^ vector, float angle )\r
- {\r
- array<float>^ output = gcnew array<float>( vector->Coefficients->Length );\r
-\r
- pin_ptr<float> pinnedOutput = &output[0];\r
- pin_ptr<float> pinnedInput = &vector->Coefficients[0];\r
-\r
- D3DXSHRotateZ( pinnedOutput, vector->Order, angle, pinnedInput );\r
-\r
- return gcnew SHVector( vector->Order, output );\r
- }\r
-\r
- SHVector^ SHVector::Scale( SHVector^ vector, float scale )\r
- {\r
- array<float>^ output = gcnew array<float>( vector->Coefficients->Length );\r
-\r
- pin_ptr<float> pinnedOutput = &output[0];\r
- pin_ptr<float> pinnedInput = &vector->Coefficients[0];\r
-\r
- D3DXSHScale( pinnedOutput, vector->Order, pinnedInput, scale );\r
-\r
- return gcnew SHVector( vector->Order, output );\r
- }\r
-\r
- float SHVector::Dot( SHVector^ left, SHVector^ right )\r
- {\r
- if( left->Order != right->Order )\r
- throw gcnew InvalidOperationException( "The order of each vector must be identical." );\r
-\r
- pin_ptr<float> pinnedLeft = &left->Coefficients[0];\r
- pin_ptr<float> pinnedRight = &right->Coefficients[0];\r
-\r
- return D3DXSHDot( left->Order, pinnedLeft, pinnedRight );\r
- }\r
-\r
- SHVector^ SHVector::EvaluateDirection( int order, Vector3 direction )\r
- {\r
- array<float>^ output = gcnew array<float>( order * order );\r
- pin_ptr<float> pinnedOutput = &output[0];\r
-\r
- D3DXSHEvalDirection( pinnedOutput, order, reinterpret_cast<D3DXVECTOR3*>( &direction ) );\r
-\r
- return gcnew SHVector( order, output );\r
- }\r
-\r
- Result SHVector::EvaluateConeLight( int order, Vector3 direction, float radius, Color3 color, [Out] SHVector^% red, [Out] SHVector^% green, [Out] SHVector^% blue )\r
- {\r
- array<float>^ redOutput = gcnew array<float>( order * order );\r
- array<float>^ greenOutput = gcnew array<float>( order * order );\r
- array<float>^ blueOutput = gcnew array<float>( order * order );\r
-\r
- pin_ptr<float> pinnedRed = &redOutput[0];\r
- pin_ptr<float> pinnedGreen = &greenOutput[0];\r
- pin_ptr<float> pinnedBlue = &blueOutput[0];\r
-\r
- HRESULT hr = D3DXSHEvalConeLight( order, reinterpret_cast<D3DXVECTOR3*>( &direction ), radius, color.Red, color.Green, color.Blue, pinnedRed, pinnedGreen, pinnedBlue );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- {\r
- red = nullptr;\r
- green = nullptr;\r
- blue = nullptr;\r
- return Result::Last;\r
- }\r
-\r
- red = gcnew SHVector( order, redOutput );\r
- green = gcnew SHVector( order, greenOutput );\r
- blue = gcnew SHVector( order, blueOutput );\r
-\r
- return Result::Last;\r
- }\r
-\r
- Result SHVector::EvaluateDirectionalLight( int order, Vector3 direction, Color3 color, [Out] SHVector^% red, [Out] SHVector^% green, [Out] SHVector^% blue )\r
- {\r
- array<float>^ redOutput = gcnew array<float>( order * order );\r
- array<float>^ greenOutput = gcnew array<float>( order * order );\r
- array<float>^ blueOutput = gcnew array<float>( order * order );\r
-\r
- pin_ptr<float> pinnedRed = &redOutput[0];\r
- pin_ptr<float> pinnedGreen = &greenOutput[0];\r
- pin_ptr<float> pinnedBlue = &blueOutput[0];\r
-\r
- HRESULT hr = D3DXSHEvalDirectionalLight( order, reinterpret_cast<D3DXVECTOR3*>( &direction ), color.Red, color.Green, color.Blue, pinnedRed, pinnedGreen, pinnedBlue );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- {\r
- red = nullptr;\r
- green = nullptr;\r
- blue = nullptr;\r
- return Result::Last;\r
- }\r
-\r
- red = gcnew SHVector( order, redOutput );\r
- green = gcnew SHVector( order, greenOutput );\r
- blue = gcnew SHVector( order, blueOutput );\r
-\r
- return Result::Last;\r
- }\r
-\r
- Result SHVector::EvaluateSphericalLight( int order, Vector3 direction, float radius, Color3 color, [Out] SHVector^% red, [Out] SHVector^% green, [Out] SHVector^% blue )\r
- {\r
- array<float>^ redOutput = gcnew array<float>( order * order );\r
- array<float>^ greenOutput = gcnew array<float>( order * order );\r
- array<float>^ blueOutput = gcnew array<float>( order * order );\r
-\r
- pin_ptr<float> pinnedRed = &redOutput[0];\r
- pin_ptr<float> pinnedGreen = &greenOutput[0];\r
- pin_ptr<float> pinnedBlue = &blueOutput[0];\r
-\r
- HRESULT hr = D3DXSHEvalSphericalLight( order, reinterpret_cast<D3DXVECTOR3*>( &direction ), radius, color.Red, color.Green, color.Blue, pinnedRed, pinnedGreen, pinnedBlue );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- {\r
- red = nullptr;\r
- green = nullptr;\r
- blue = nullptr;\r
- return Result::Last;\r
- }\r
-\r
- red = gcnew SHVector( order, redOutput );\r
- green = gcnew SHVector( order, greenOutput );\r
- blue = gcnew SHVector( order, blueOutput );\r
-\r
- return Result::Last;\r
- }\r
-\r
- Result SHVector::EvaluateHemisphereLight( int order, Vector3 direction, Color4 top, Color4 bottom, [Out] SHVector^% red, [Out] SHVector^% green, [Out] SHVector^% blue )\r
- {\r
- array<float>^ redOutput = gcnew array<float>( order * order );\r
- array<float>^ greenOutput = gcnew array<float>( order * order );\r
- array<float>^ blueOutput = gcnew array<float>( order * order );\r
-\r
- pin_ptr<float> pinnedRed = &redOutput[0];\r
- pin_ptr<float> pinnedGreen = &greenOutput[0];\r
- pin_ptr<float> pinnedBlue = &blueOutput[0];\r
-\r
- HRESULT hr = D3DXSHEvalHemisphereLight( order, reinterpret_cast<D3DXVECTOR3*>( &direction ), top.ToUnmanaged(), \r
- bottom.ToUnmanaged(), pinnedRed, pinnedGreen, pinnedBlue );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- {\r
- red = nullptr;\r
- green = nullptr;\r
- blue = nullptr;\r
- return Result::Last;\r
- }\r
-\r
- red = gcnew SHVector( order, redOutput );\r
- green = gcnew SHVector( order, greenOutput );\r
- blue = gcnew SHVector( order, blueOutput );\r
-\r
- return Result::Last;\r
- }\r
-\r
- Result SHVector::ProjectCubeMap( int order, SlimDX::Direct3D9::CubeTexture^ cubeMap, [Out] SHVector^% red, [Out] SHVector^% green, [Out] SHVector^% blue )\r
- {\r
- array<float>^ redOutput = gcnew array<float>( order * order );\r
- array<float>^ greenOutput = gcnew array<float>( order * order );\r
- array<float>^ blueOutput = gcnew array<float>( order * order );\r
-\r
- pin_ptr<float> pinnedRed = &redOutput[0];\r
- pin_ptr<float> pinnedGreen = &greenOutput[0];\r
- pin_ptr<float> pinnedBlue = &blueOutput[0];\r
-\r
- HRESULT hr = D3DXSHProjectCubeMap( order, cubeMap->InternalPointer, pinnedRed, pinnedGreen, pinnedBlue );\r
- if( RECORD_SDX( hr ).IsFailure )\r
- {\r
- red = nullptr;\r
- green = nullptr;\r
- blue = nullptr;\r
- return Result::Last;\r
- }\r
-\r
- red = gcnew SHVector( order, redOutput );\r
- green = gcnew SHVector( order, greenOutput );\r
- blue = gcnew SHVector( order, blueOutput );\r
-\r
- return Result::Last;\r
- }\r
-\r
- SHVector^ SHVector::operator + ( SHVector^ left, SHVector^ right )\r
- {\r
- return Add( left, right );\r
- }\r
-\r
- SHVector^ SHVector::operator * ( SHVector^ value, float scale )\r
- {\r
- return Scale( value, scale );\r
- }\r
- \r
- SHVector^ SHVector::operator * ( float scale, SHVector^ vec )\r
- {\r
- return Scale( vec, scale );\r
- }\r
-\r
- bool SHVector::operator == ( SHVector^ left, SHVector^ right )\r
- {\r
- if( ReferenceEquals( left, nullptr ) )\r
- return ReferenceEquals( right, nullptr );\r
-\r
- return SHVector::Equals( left, right );\r
- }\r
- \r
- bool SHVector::operator != ( SHVector^ left, SHVector^ right )\r
- {\r
- return !( left == right );\r
- }\r
- \r
- String^ SHVector::ToString()\r
- {\r
- StringBuilder^ output = gcnew StringBuilder();\r
- output->AppendFormat( "Order:{0}", order );\r
- for each( float f in coefficients )\r
- output->AppendFormat( " {1}", f );\r
-\r
- return output->ToString();\r
- }\r
-\r
- int SHVector::GetHashCode()\r
- {\r
- int value = 0;\r
- for each( float f in coefficients )\r
- value += f.GetHashCode();\r
-\r
- return value;\r
- }\r
-\r
- bool SHVector::Equals( Object^ value )\r
- {\r
- if( value == nullptr )\r
- return false;\r
-\r
- if( value->GetType() != GetType() )\r
- return false;\r
-\r
- return Equals( safe_cast<SHVector^>( value ) );\r
- }\r
-\r
- bool SHVector::Equals( SHVector^ value )\r
- {\r
- if( value == nullptr )\r
- return false;\r
-\r
- if( ReferenceEquals( this, value ) )\r
- return true;\r
-\r
- if( value->Order != order )\r
- return false;\r
-\r
- for( int i = 0; i < coefficients->Length; i++ )\r
- if( value[i] != coefficients[i] )\r
- return false;\r
-\r
- return true;\r
- }\r
-\r
- bool SHVector::Equals( SHVector^ value1, SHVector^ value2 )\r
- {\r
- return value1->Equals( value2 );\r
- }\r
-}
\ No newline at end of file