root/OpenSceneGraph/trunk/src/osgPlugins/fbx/fbxRLight.cpp @ 13464

Revision 13464, 3.3 kB (checked in by robert, 3 days ago)

Fixed comment

  • Property svn:eol-style set to native
Line 
1#include <osg/LightSource>
2
3#include <osgDB/ReadFile>
4
5#if defined(_MSC_VER)
6#pragma warning( disable : 4505 )
7#pragma warning( default : 4996 )
8#endif
9#include <fbxsdk.h>
10
11#include "fbxReader.h"
12
13osgDB::ReaderWriter::ReadResult OsgFbxReader::readFbxLight(KFbxNode* pNode, int& nLightCount)
14{
15    const KFbxLight* fbxLight = KFbxCast<KFbxLight>(pNode->GetNodeAttribute());
16
17    if (!fbxLight)
18    {
19        return osgDB::ReaderWriter::ReadResult::ERROR_IN_READING_FILE;
20    }
21
22    osg::Light* osgLight = new osg::Light;
23    osg::LightSource* osgLightSource = new osg::LightSource;
24
25    osgLight->setLightNum(nLightCount++);
26    osgLightSource->setLight(osgLight);
27
28    KFbxLight::ELightType fbxLightType = fbxLight->LightType.IsValid() ?
29        fbxLight->LightType.Get() : KFbxLight::ePOINT;
30
31    osgLight->setPosition(osg::Vec4(0,0,0,fbxLightType != KFbxLight::eDIRECTIONAL));
32
33    if (fbxLightType == KFbxLight::eSPOT)
34    {
35        double coneAngle = fbxLight->ConeAngle.IsValid() ? fbxLight->ConeAngle.Get() : 45.0;
36        double hotSpot = fbxLight->HotSpot.IsValid() ? fbxLight->HotSpot.Get() : 45.0;
37        const float MIN_HOTSPOT = 0.467532f;
38
39        osgLight->setSpotCutoff(static_cast<float>(coneAngle));
40
41        //Approximate the hotspot using the GL light exponent.
42        //This formula maps a hotspot of 180 to exponent 0 (uniform light
43        // distribution) and a hotspot of 45 to exponent 1 (effective light
44        // intensity is attenuated by the cosine of the angle between the
45        // direction of the light and the direction from the light to the vertex
46        // being lighted). A hotspot close to 0 maps to exponent 128 (maximum).
47        float exponent = (180.0f / (std::max)(static_cast<float>(hotSpot),
48            MIN_HOTSPOT) - 1.0f) / 3.0f;
49        osgLight->setSpotExponent(exponent);
50    }
51
52    if (fbxLight->DecayType.IsValid() &&
53        fbxLight->DecayStart.IsValid())
54    {
55        double fbxDecayStart = fbxLight->DecayStart.Get();
56
57        switch (fbxLight->DecayType.Get())
58        {
59        case KFbxLight::eNONE:
60            break;
61        case KFbxLight::eLINEAR:
62            osgLight->setLinearAttenuation(fbxDecayStart);
63            break;
64        case KFbxLight::eQUADRATIC:
65        case KFbxLight::eCUBIC:
66            osgLight->setQuadraticAttenuation(fbxDecayStart);
67            break;
68        }
69    }
70
71    osg::Vec3f osgDiffuseSpecular(1.0f, 1.0f, 1.0f);
72    osg::Vec3f osgAmbient(0.0f, 0.0f, 0.0f);
73    if (fbxLight->Color.IsValid())
74    {
75        fbxDouble3 fbxColor = fbxLight->Color.Get();
76        osgDiffuseSpecular.set(
77            static_cast<float>(fbxColor[0]),
78            static_cast<float>(fbxColor[1]),
79            static_cast<float>(fbxColor[2]));
80    }
81    if (fbxLight->Intensity.IsValid())
82    {
83        osgDiffuseSpecular *= static_cast<float>(fbxLight->Intensity.Get()) * 0.01f;
84    }
85    if (fbxLight->ShadowColor.IsValid())
86    {
87        fbxDouble3 fbxShadowColor = fbxLight->ShadowColor.Get();
88        osgAmbient.set(
89            static_cast<float>(fbxShadowColor[0]),
90            static_cast<float>(fbxShadowColor[1]),
91            static_cast<float>(fbxShadowColor[2]));
92    }
93
94    osgLight->setDiffuse(osg::Vec4f(osgDiffuseSpecular, 1.0f));
95    osgLight->setSpecular(osg::Vec4f(osgDiffuseSpecular, 1.0f));
96    osgLight->setAmbient(osg::Vec4f(osgAmbient, 1.0f));
97
98    return osgDB::ReaderWriter::ReadResult(osgLightSource);
99}
Note: See TracBrowser for help on using the browser.