source: OGRE/trunk/ogrenew/Samples/Dot3Bump/src/Dot3Bump.cpp @ 692

Revision 692, 10.8 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10You may use this sample code for anything you like, it is not covered by the
11LGPL like the rest of the engine.
12-----------------------------------------------------------------------------
13*/
14
15/**
16    \file
17        Dot3Bump.cpp
18    \brief
19        Specialisation of OGRE's framework application to show the
20        dotproduct blending operation and normalization cube map usage
21                for achieving bump mapping effect
22        \par
23                Tangent space computations made through the guide of the
24                tutorial on bump mapping from http://users.ox.ac.uk/~univ1234
25                author : paul.baker@univ.ox.ac.uk
26**/
27
28#include "ExampleApplication.h"
29
30#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
31#define WIN32_LEAN_AND_MEAN
32#include "windows.h"
33#endif
34
35// entities we'll use
36#define NUM_ENTITIES 3
37Entity* mEntities[NUM_ENTITIES];
38String mEntityMeshes[NUM_ENTITIES] =
39{
40    "athene.mesh",
41    "knot.mesh",
42    "ogrehead.mesh"
43};
44size_t mCurrentEntity = 0;
45
46// Lights
47#define NUM_LIGHTS 3
48
49// the light
50Light *mLights[NUM_LIGHTS];
51// billboards for lights
52BillboardSet* mLightFlareSets[NUM_LIGHTS];
53Billboard* mLightFlares[NUM_LIGHTS];
54// Positions for lights
55Vector3 mLightPositions[NUM_LIGHTS] =
56{
57        Vector3(300,0,0),
58        Vector3(-300,50,0),
59        Vector3(0, -300, -100)
60};
61// Base orientations of the lights
62Radian mLightRotationAngles[NUM_LIGHTS] = { Degree(0), Degree(30), Degree(75) };
63Vector3 mLightRotationAxes[NUM_LIGHTS] = {
64    Vector3::UNIT_X,
65    Vector3::UNIT_Z,
66    Vector3::UNIT_Y
67};
68// Rotation speed for lights, degrees per second
69Real mLightSpeeds[NUM_LIGHTS] = { 30, 10, 50};
70
71// Colours for the lights
72ColourValue mDiffuseLightColours[NUM_LIGHTS] =
73{
74        ColourValue(1, 1, 1),
75        ColourValue(1, 0, 0),
76        ColourValue(1, 1, 0.5)
77};
78ColourValue mSpecularLightColours[NUM_LIGHTS] =
79{
80        ColourValue(1, 1, 1),
81        ColourValue(1, 0.8, 0.8),
82        ColourValue(1, 1, 0.8)
83};
84// Which lights are enabled
85bool mLightState[NUM_LIGHTS] =
86{
87        true,
88        true,
89        false
90};
91// The materials
92#define NUM_MATERIALS 4
93String mMaterialNames[NUM_ENTITIES][NUM_MATERIALS] =
94{
95    // athene
96    "Examples/Athene/Basic",
97    "Examples/Athene/NormalMapped",
98    "Examples/Athene/NormalMappedSpecular",
99    "Examples/Athene/NormalMapped",
100    // knot
101        "Examples/BumpMapping/SingleLight",
102        "Examples/BumpMapping/MultiLight",
103        "Examples/BumpMapping/MultiLightSpecular",
104    "Examples/OffsetMapping/Specular",
105    // ogre head
106    "Examples/BumpMapping/SingleLight",
107    "Examples/BumpMapping/MultiLight",
108    "Examples/BumpMapping/MultiLightSpecular",
109    "Examples/OffsetMapping/Specular"
110};
111size_t mCurrentMaterial = 1;
112
113// the scene node of the entity
114SceneNode *mMainNode;
115// the light nodes
116SceneNode* mLightNodes[NUM_LIGHTS];
117// the light node pivots
118SceneNode* mLightPivots[NUM_LIGHTS];
119
120OverlayElement* mObjectInfo;
121OverlayElement* mMaterialInfo;
122OverlayElement* mInfo;
123
124#define KEY_PRESSED(_key,_timeDelay, _macro) \
125{ \
126    if (mInputDevice->isKeyDown(_key) && timeDelay <= 0) \
127    { \
128                timeDelay = _timeDelay; \
129        _macro ; \
130    } \
131}
132
133
134
135// Event handler to add ability to change material
136class Dp3_Listener : public ExampleFrameListener
137{
138public:
139    Dp3_Listener(RenderWindow* win, Camera* cam)
140        : ExampleFrameListener(win, cam)
141    {
142    }
143
144    void flipLightState(size_t i)
145    {
146        mLightState[i] = !mLightState[i];
147        mLights[i]->setVisible(mLightState[i]);
148        mLightFlareSets[i]->setVisible(mLightState[i]);
149    }
150    bool frameStarted(const FrameEvent& evt)
151    {
152        if(!ExampleFrameListener::frameStarted(evt))
153            return false;
154       
155        static Real timeDelay = 0;
156
157        timeDelay -= evt.timeSinceLastFrame;
158
159                // switch meshes
160        KEY_PRESSED(KC_O, 1,
161            mEntities[mCurrentEntity]->setVisible(false);
162            mCurrentEntity = (++mCurrentEntity) % NUM_ENTITIES;
163            mEntities[mCurrentEntity]->setVisible(true);
164            mEntities[mCurrentEntity]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]);
165            mObjectInfo->setCaption("Current: " + mEntityMeshes[mCurrentEntity]);
166            mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]);
167        );
168
169                // switch materials
170                KEY_PRESSED(KC_M, 1,
171            mCurrentMaterial = (++mCurrentMaterial) % NUM_MATERIALS;
172            mEntities[mCurrentEntity]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]);
173            mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]);
174        );
175
176                // enable / disable lights
177                KEY_PRESSED(KC_1, 1, flipLightState(0));
178                // switch materials
179                KEY_PRESSED(KC_2, 1, flipLightState(1));
180                // switch materials
181                KEY_PRESSED(KC_3, 1, flipLightState(2));
182
183        // animate the lights
184        for (size_t i = 0; i < NUM_LIGHTS; ++i)
185            mLightPivots[i]->rotate(Vector3::UNIT_Z, Degree(mLightSpeeds[i] * evt.timeSinceLastFrame));
186
187                return true;
188    }
189
190};
191
192class Dp3_Application : public ExampleApplication
193{
194public:
195    Dp3_Application() {}
196       
197protected:
198        SceneNode *mpObjsNode; // the node wich will hold our entities
199
200        void createScene(void)
201    {
202        // First check that vertex programs and dot3 or fragment programs are supported
203                const RenderSystemCapabilities* caps = Root::getSingleton().getRenderSystem()->getCapabilities();
204        if (!caps->hasCapability(RSC_VERTEX_PROGRAM))
205        {
206            OGRE_EXCEPT(1, "Your card does not support vertex programs, so cannot "
207                "run this demo. Sorry!",
208                "Dot3Bump::createScene");
209        }
210        if (!(caps->hasCapability(RSC_FRAGMENT_PROGRAM)
211                        || caps->hasCapability(RSC_DOT3)) )
212        {
213            OGRE_EXCEPT(1, "Your card does not support dot3 blending or fragment programs, so cannot "
214                "run this demo. Sorry!",
215                "Dot3Bump::createScene");
216        }
217
218        // Set ambient light and fog
219        mSceneMgr->setAmbientLight(ColourValue(0.0, 0.0, 0.0));
220        /*
221                // Define a floor plane mesh
222        Plane p;
223        p.normal = Vector3::UNIT_Y;
224        p.d = 200;
225        MeshManager::getSingleton().createPlane("FloorPlane",p,2000,2000,1,1,true,1,5,5,Vector3::UNIT_Z);
226        // Create an entity (the floor)
227        Entity *floorEnt = mSceneMgr->createEntity("floor", "FloorPlane");
228        floorEnt->setMaterialName("Examples/DP3Terrain");
229        mSceneMgr->getRootSceneNode()->attachObject(floorEnt);
230        */
231
232        mMainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
233
234        // Load the meshes with non-default HBU options
235                for(int mn = 0; mn < NUM_ENTITIES; mn++) {
236                        MeshPtr pMesh = MeshManager::getSingleton().load(mEntityMeshes[mn],
237                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,   
238                HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
239                                HardwareBuffer::HBU_STATIC_WRITE_ONLY,
240                                true, true); //so we can still read it
241            // Build tangent vectors, all our meshes use only 1 texture coordset
242            unsigned short src, dest;
243            if (!pMesh->suggestTangentVectorBuildParams(src, dest))
244            {
245                pMesh->buildTangentVectors(src, dest);
246            }
247            // Create entity
248            mEntities[mn] = mSceneMgr->createEntity("Ent" + StringConverter::toString(mn),
249                mEntityMeshes[mn]);
250            // Attach to child of root node
251                mMainNode->attachObject(mEntities[mn]);
252            // Make invisible, except for index 0
253            if (mn == 0)
254                mEntities[mn]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]);
255            else
256                mEntities[mn]->setVisible(false);
257                }
258
259        for (unsigned int i = 0; i < NUM_LIGHTS; ++i)
260        {
261            mLightPivots[i] = mSceneMgr->getRootSceneNode()->createChildSceneNode();
262            mLightPivots[i]->rotate(mLightRotationAxes[i], mLightRotationAngles[i]);
263            // Create a light, use default parameters
264            mLights[i] = mSceneMgr->createLight("Light" + StringConverter::toString(i));
265                        mLights[i]->setPosition(mLightPositions[i]);
266                        mLights[i]->setDiffuseColour(mDiffuseLightColours[i]);
267                        mLights[i]->setSpecularColour(mSpecularLightColours[i]);
268                        mLights[i]->setVisible(mLightState[i]);
269            // Attach light
270            mLightPivots[i]->attachObject(mLights[i]);
271                        // Create billboard for light
272                        mLightFlareSets[i] = mSceneMgr->createBillboardSet("Flare" + StringConverter::toString(i));
273                        mLightFlareSets[i]->setMaterialName("Examples/Flare");
274                        mLightPivots[i]->attachObject(mLightFlareSets[i]);
275                        mLightFlares[i] = mLightFlareSets[i]->createBillboard(mLightPositions[i]);
276                        mLightFlares[i]->setColour(mDiffuseLightColours[i]);
277                        mLightFlareSets[i]->setVisible(mLightState[i]);
278        }
279        // move the camera a bit right and make it look at the knot
280                mCamera->moveRelative(Vector3(50, 0, 20));
281                mCamera->lookAt(0, 0, 0);
282                // show overlay
283                Overlay* pOver = OverlayManager::getSingleton().getByName("Example/DP3Overlay");   
284        mObjectInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/ObjectInfo");
285        mMaterialInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/MaterialInfo");
286        mInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/Info");
287
288        mObjectInfo->setCaption("Current: " + mEntityMeshes[mCurrentEntity]);
289        mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]);
290        if (!caps->hasCapability(RSC_FRAGMENT_PROGRAM))
291        {
292            mInfo->setCaption("NOTE: Light colours and specular highlights are not supported by your card.");
293        }
294                pOver->show();
295        }
296
297    // Create new frame listener
298    void createFrameListener(void)
299    {
300        mFrameListener= new Dp3_Listener(mWindow, mCamera);
301        mRoot->addFrameListener(mFrameListener);
302    }
303};
304
305#ifdef __cplusplus
306extern "C" {
307#endif
308
309#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
310INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
311#else
312int main(int argc, char **argv)
313#endif
314{
315    // Create application object
316    Dp3_Application app;
317
318    try {
319        app.go();
320    } catch( Exception& e ) {
321#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
322        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
323#else
324        std::cerr << "An exception has occured: " << e.getFullDescription();
325#endif
326    }
327
328    return 0;
329}
330
331#ifdef __cplusplus
332}
333#endif
Note: See TracBrowser for help on using the repository browser.