[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | You may use this sample code for anything you like, it is not covered by the
|
---|
| 11 | LGPL like the rest of the engine.
|
---|
| 12 | -----------------------------------------------------------------------------
|
---|
| 13 | */
|
---|
| 14 | /*
|
---|
| 15 | -----------------------------------------------------------------------------
|
---|
| 16 | Filename: CelShading.cpp
|
---|
| 17 | Description: Demo of cel-shaded graphics using vertex & fragment programs
|
---|
| 18 | -----------------------------------------------------------------------------
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | #include "ExampleApplication.h"
|
---|
| 23 |
|
---|
| 24 | SceneNode* rotNode;
|
---|
| 25 |
|
---|
| 26 | // Listener class for frame updates
|
---|
| 27 | class CelShadingListener : public ExampleFrameListener
|
---|
| 28 | {
|
---|
| 29 | protected:
|
---|
| 30 | public:
|
---|
| 31 | CelShadingListener(RenderWindow* win, Camera* cam)
|
---|
| 32 | : ExampleFrameListener(win, cam)
|
---|
| 33 | {
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | bool frameStarted(const FrameEvent& evt)
|
---|
| 37 | {
|
---|
| 38 | rotNode->yaw(Degree(evt.timeSinceLastFrame * 30));
|
---|
| 39 | // Call superclass
|
---|
| 40 | return ExampleFrameListener::frameStarted(evt);
|
---|
| 41 | }
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | // Custom parameter bindings
|
---|
| 45 | #define CUSTOM_SHININESS 1
|
---|
| 46 | #define CUSTOM_DIFFUSE 2
|
---|
| 47 | #define CUSTOM_SPECULAR 3
|
---|
| 48 |
|
---|
| 49 | class CelShadingApplication : public ExampleApplication
|
---|
| 50 | {
|
---|
| 51 | public:
|
---|
| 52 | CelShadingApplication() {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | ~CelShadingApplication() { }
|
---|
| 56 |
|
---|
| 57 | protected:
|
---|
| 58 |
|
---|
| 59 | void createFrameListener(void)
|
---|
| 60 | {
|
---|
| 61 | // This is where we instantiate our own frame listener
|
---|
| 62 | mFrameListener= new CelShadingListener(mWindow, mCamera);
|
---|
| 63 | mRoot->addFrameListener(mFrameListener);
|
---|
| 64 |
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | // Just override the mandatory create scene method
|
---|
| 69 | void createScene(void)
|
---|
| 70 | {
|
---|
| 71 | // Check capabilities
|
---|
| 72 | const RenderSystemCapabilities* caps = Root::getSingleton().getRenderSystem()->getCapabilities();
|
---|
| 73 | if (!caps->hasCapability(RSC_VERTEX_PROGRAM) || !(caps->hasCapability(RSC_FRAGMENT_PROGRAM)))
|
---|
| 74 | {
|
---|
| 75 | OGRE_EXCEPT(1, "Your card does not support vertex and fragment programs (or you selected D3D7), so cannot "
|
---|
| 76 | "run this demo. Sorry!",
|
---|
| 77 | "CelShading::createScene");
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // Create a point light
|
---|
| 81 | Light* l = mSceneMgr->createLight("MainLight");
|
---|
| 82 | // Accept default settings: point light, white diffuse, just set position
|
---|
| 83 | // Add light to the scene node
|
---|
| 84 | rotNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
|
---|
| 85 | rotNode->createChildSceneNode(Vector3(20,40,50))->attachObject(l);
|
---|
| 86 |
|
---|
| 87 | Entity *ent = mSceneMgr->createEntity("head", "ogrehead.mesh");
|
---|
| 88 |
|
---|
| 89 | mCamera->setPosition(20, 0, 100);
|
---|
| 90 | mCamera->lookAt(0,0,0);
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | // Set common material, but define custom parameters to change colours
|
---|
| 94 | // See Example-Advanced.material for how these are finally bound to GPU parameters
|
---|
| 95 | SubEntity* sub;
|
---|
| 96 | // eyes
|
---|
| 97 | sub = ent->getSubEntity(0);
|
---|
| 98 | sub->setMaterialName("Examples/CelShading");
|
---|
| 99 | sub->setCustomParameter(CUSTOM_SHININESS, Vector4(35.0f, 0.0f, 0.0f, 0.0f));
|
---|
| 100 | sub->setCustomParameter(CUSTOM_DIFFUSE, Vector4(1.0f, 0.3f, 0.3f, 1.0f));
|
---|
| 101 | sub->setCustomParameter(CUSTOM_SPECULAR, Vector4(1.0f, 0.6f, 0.6f, 1.0f));
|
---|
| 102 | // skin
|
---|
| 103 | sub = ent->getSubEntity(1);
|
---|
| 104 | sub->setMaterialName("Examples/CelShading");
|
---|
| 105 | sub->setCustomParameter(CUSTOM_SHININESS, Vector4(10.0f, 0.0f, 0.0f, 0.0f));
|
---|
| 106 | sub->setCustomParameter(CUSTOM_DIFFUSE, Vector4(0.0f, 0.5f, 0.0f, 1.0f));
|
---|
| 107 | sub->setCustomParameter(CUSTOM_SPECULAR, Vector4(0.3f, 0.5f, 0.3f, 1.0f));
|
---|
| 108 | // earring
|
---|
| 109 | sub = ent->getSubEntity(2);
|
---|
| 110 | sub->setMaterialName("Examples/CelShading");
|
---|
| 111 | sub->setCustomParameter(CUSTOM_SHININESS, Vector4(25.0f, 0.0f, 0.0f, 0.0f));
|
---|
| 112 | sub->setCustomParameter(CUSTOM_DIFFUSE, Vector4(1.0f, 1.0f, 0.0f, 1.0f));
|
---|
| 113 | sub->setCustomParameter(CUSTOM_SPECULAR, Vector4(1.0f, 1.0f, 0.7f, 1.0f));
|
---|
| 114 | // teeth
|
---|
| 115 | sub = ent->getSubEntity(3);
|
---|
| 116 | sub->setMaterialName("Examples/CelShading");
|
---|
| 117 | sub->setCustomParameter(CUSTOM_SHININESS, Vector4(20.0f, 0.0f, 0.0f, 0.0f));
|
---|
| 118 | sub->setCustomParameter(CUSTOM_DIFFUSE, Vector4(1.0f, 1.0f, 0.7f, 1.0f));
|
---|
| 119 | sub->setCustomParameter(CUSTOM_SPECULAR, Vector4(1.0f, 1.0f, 1.0f, 1.0f));
|
---|
| 120 |
|
---|
| 121 | // Add entity to the root scene node
|
---|
| 122 | mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
|
---|
| 123 |
|
---|
| 124 | mWindow->getViewport(0)->setBackgroundColour(ColourValue::White);
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 131 | #define WIN32_LEAN_AND_MEAN
|
---|
| 132 | #include "windows.h"
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
| 135 | #ifdef __cplusplus
|
---|
| 136 | extern "C" {
|
---|
| 137 | #endif
|
---|
| 138 |
|
---|
| 139 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 140 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
|
---|
| 141 | #else
|
---|
| 142 | int main(int argc, char **argv)
|
---|
| 143 | #endif
|
---|
| 144 | {
|
---|
| 145 | // Create application object
|
---|
| 146 | CelShadingApplication app;
|
---|
| 147 |
|
---|
| 148 | try {
|
---|
| 149 | app.go();
|
---|
| 150 | } catch( Exception& e ) {
|
---|
| 151 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
|
---|
| 152 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
|
---|
| 153 | #else
|
---|
| 154 | std::cerr << "An exception has occured: " << e.getFullDescription();
|
---|
| 155 | #endif
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 | return 0;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | #ifdef __cplusplus
|
---|
| 163 | }
|
---|
| 164 | #endif
|
---|