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

Revision 13466, 3.2 kB (checked in by robert, 12 days ago)

Moved widgets from VolumeEditorWidget? to TransferFunctionWidget?, and widget utilities into WidgetUtils?.

  • 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(FbxNode* pNode, int& nLightCount)
14{
15    const FbxLight* fbxLight = FbxCast<FbxLight>(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    osgLightSource->setLight(osgLight);
26    osgLight->setLightNum(nLightCount++);
27
28    FbxLight::EType fbxLightType = fbxLight->LightType.IsValid() ?
29        fbxLight->LightType.Get() : FbxLight::ePoint;
30
31    osgLight->setPosition(osg::Vec4(0,0,0,fbxLightType != FbxLight::eDirectional));
32
33    if (fbxLightType == FbxLight::eSpot)
34    {
35        double coneAngle = fbxLight->OuterAngle.Get();
36        double hotSpot = fbxLight->InnerAngle.Get();
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 FbxLight::eNone:
60            break;
61        case FbxLight::eLinear:
62            osgLight->setLinearAttenuation(fbxDecayStart);
63            break;
64        case FbxLight::eQuadratic:
65        case FbxLight::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.