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-2006 Torus Knot Software Ltd |
---|
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 | \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 |
---|
37 | Entity* mEntities[NUM_ENTITIES]; |
---|
38 | String mEntityMeshes[NUM_ENTITIES] = |
---|
39 | { |
---|
40 | "athene.mesh", |
---|
41 | "knot.mesh", |
---|
42 | "ogrehead.mesh" |
---|
43 | }; |
---|
44 | size_t mCurrentEntity = 0; |
---|
45 | |
---|
46 | // Lights |
---|
47 | #define NUM_LIGHTS 3 |
---|
48 | |
---|
49 | // the light |
---|
50 | Light *mLights[NUM_LIGHTS]; |
---|
51 | // billboards for lights |
---|
52 | BillboardSet* mLightFlareSets[NUM_LIGHTS]; |
---|
53 | Billboard* mLightFlares[NUM_LIGHTS]; |
---|
54 | // Positions for lights |
---|
55 | Vector3 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 |
---|
62 | Radian mLightRotationAngles[NUM_LIGHTS] = { Degree(0), Degree(30), Degree(75) }; |
---|
63 | Vector3 mLightRotationAxes[NUM_LIGHTS] = { |
---|
64 | Vector3::UNIT_X, |
---|
65 | Vector3::UNIT_Z, |
---|
66 | Vector3::UNIT_Y |
---|
67 | }; |
---|
68 | // Rotation speed for lights, degrees per second |
---|
69 | Real mLightSpeeds[NUM_LIGHTS] = { 30, 10, 50}; |
---|
70 | |
---|
71 | // Colours for the lights |
---|
72 | ColourValue mDiffuseLightColours[NUM_LIGHTS] = |
---|
73 | { |
---|
74 | ColourValue(1, 1, 1), |
---|
75 | ColourValue(1, 0, 0), |
---|
76 | ColourValue(1, 1, 0.5) |
---|
77 | }; |
---|
78 | ColourValue 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 |
---|
85 | bool mLightState[NUM_LIGHTS] = |
---|
86 | { |
---|
87 | true, |
---|
88 | true, |
---|
89 | false |
---|
90 | }; |
---|
91 | // The materials |
---|
92 | #define NUM_MATERIALS 4 |
---|
93 | String 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 | }; |
---|
111 | size_t mCurrentMaterial = 1; |
---|
112 | |
---|
113 | // the scene node of the entity |
---|
114 | SceneNode *mMainNode; |
---|
115 | // the light nodes |
---|
116 | SceneNode* mLightNodes[NUM_LIGHTS]; |
---|
117 | // the light node pivots |
---|
118 | SceneNode* mLightPivots[NUM_LIGHTS]; |
---|
119 | |
---|
120 | OverlayElement* mObjectInfo; |
---|
121 | OverlayElement* mMaterialInfo; |
---|
122 | OverlayElement* mInfo; |
---|
123 | |
---|
124 | #define KEY_PRESSED(_key,_timeDelay, _macro) \ |
---|
125 | { \ |
---|
126 | if (mKeyboard->isKeyDown(_key) && timeDelay <= 0) \ |
---|
127 | { \ |
---|
128 | timeDelay = _timeDelay; \ |
---|
129 | _macro ; \ |
---|
130 | } \ |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | // Event handler to add ability to change material |
---|
136 | class Dp3_Listener : public ExampleFrameListener |
---|
137 | { |
---|
138 | public: |
---|
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 | using namespace OIS; |
---|
153 | |
---|
154 | if(!ExampleFrameListener::frameStarted(evt)) |
---|
155 | return false; |
---|
156 | |
---|
157 | static Real timeDelay = 0; |
---|
158 | |
---|
159 | timeDelay -= evt.timeSinceLastFrame; |
---|
160 | |
---|
161 | // switch meshes |
---|
162 | KEY_PRESSED(KC_O, 1, |
---|
163 | mEntities[mCurrentEntity]->setVisible(false); |
---|
164 | mCurrentEntity = (++mCurrentEntity) % NUM_ENTITIES; |
---|
165 | mEntities[mCurrentEntity]->setVisible(true); |
---|
166 | mEntities[mCurrentEntity]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
167 | mObjectInfo->setCaption("Current: " + mEntityMeshes[mCurrentEntity]); |
---|
168 | mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
169 | ); |
---|
170 | |
---|
171 | // switch materials |
---|
172 | KEY_PRESSED(KC_M, 1, |
---|
173 | mCurrentMaterial = (++mCurrentMaterial) % NUM_MATERIALS; |
---|
174 | mEntities[mCurrentEntity]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
175 | mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
176 | ); |
---|
177 | |
---|
178 | // enable / disable lights |
---|
179 | KEY_PRESSED(KC_1, 1, flipLightState(0)); |
---|
180 | // switch materials |
---|
181 | KEY_PRESSED(KC_2, 1, flipLightState(1)); |
---|
182 | // switch materials |
---|
183 | KEY_PRESSED(KC_3, 1, flipLightState(2)); |
---|
184 | |
---|
185 | // animate the lights |
---|
186 | for (size_t i = 0; i < NUM_LIGHTS; ++i) |
---|
187 | mLightPivots[i]->rotate(Ogre::Vector3::UNIT_Z, Degree(mLightSpeeds[i] * evt.timeSinceLastFrame)); |
---|
188 | |
---|
189 | return true; |
---|
190 | } |
---|
191 | |
---|
192 | }; |
---|
193 | |
---|
194 | class Dp3_Application : public ExampleApplication |
---|
195 | { |
---|
196 | public: |
---|
197 | Dp3_Application() {} |
---|
198 | |
---|
199 | protected: |
---|
200 | SceneNode *mpObjsNode; // the node wich will hold our entities |
---|
201 | |
---|
202 | void createScene(void) |
---|
203 | { |
---|
204 | // First check that vertex programs and dot3 or fragment programs are supported |
---|
205 | const RenderSystemCapabilities* caps = Root::getSingleton().getRenderSystem()->getCapabilities(); |
---|
206 | if (!caps->hasCapability(RSC_VERTEX_PROGRAM)) |
---|
207 | { |
---|
208 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Your card does not support vertex programs, so cannot " |
---|
209 | "run this demo. Sorry!", |
---|
210 | "Dot3Bump::createScene"); |
---|
211 | } |
---|
212 | if (!(caps->hasCapability(RSC_FRAGMENT_PROGRAM) |
---|
213 | || caps->hasCapability(RSC_DOT3)) ) |
---|
214 | { |
---|
215 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "Your card does not support dot3 blending or fragment programs, so cannot " |
---|
216 | "run this demo. Sorry!", |
---|
217 | "Dot3Bump::createScene"); |
---|
218 | } |
---|
219 | |
---|
220 | // Set ambient light and fog |
---|
221 | mSceneMgr->setAmbientLight(ColourValue(0.0, 0.0, 0.0)); |
---|
222 | /* |
---|
223 | // Define a floor plane mesh |
---|
224 | Plane p; |
---|
225 | p.normal = Vector3::UNIT_Y; |
---|
226 | p.d = 200; |
---|
227 | MeshManager::getSingleton().createPlane("FloorPlane",p,2000,2000,1,1,true,1,5,5,Vector3::UNIT_Z); |
---|
228 | // Create an entity (the floor) |
---|
229 | Entity *floorEnt = mSceneMgr->createEntity("floor", "FloorPlane"); |
---|
230 | floorEnt->setMaterialName("Examples/DP3Terrain"); |
---|
231 | mSceneMgr->getRootSceneNode()->attachObject(floorEnt); |
---|
232 | */ |
---|
233 | |
---|
234 | mMainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
235 | |
---|
236 | // Load the meshes with non-default HBU options |
---|
237 | for(int mn = 0; mn < NUM_ENTITIES; mn++) { |
---|
238 | MeshPtr pMesh = MeshManager::getSingleton().load(mEntityMeshes[mn], |
---|
239 | ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, |
---|
240 | HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY, |
---|
241 | HardwareBuffer::HBU_STATIC_WRITE_ONLY, |
---|
242 | true, true); //so we can still read it |
---|
243 | // Build tangent vectors, all our meshes use only 1 texture coordset |
---|
244 | // Note we can build into VES_TANGENT now (SM2+) |
---|
245 | unsigned short src, dest; |
---|
246 | if (!pMesh->suggestTangentVectorBuildParams(VES_TANGENT, src, dest)) |
---|
247 | { |
---|
248 | pMesh->buildTangentVectors(VES_TANGENT, src, dest); |
---|
249 | } |
---|
250 | // Create entity |
---|
251 | mEntities[mn] = mSceneMgr->createEntity("Ent" + StringConverter::toString(mn), |
---|
252 | mEntityMeshes[mn]); |
---|
253 | // Attach to child of root node |
---|
254 | mMainNode->attachObject(mEntities[mn]); |
---|
255 | // Make invisible, except for index 0 |
---|
256 | if (mn == 0) |
---|
257 | mEntities[mn]->setMaterialName(mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
258 | else |
---|
259 | mEntities[mn]->setVisible(false); |
---|
260 | } |
---|
261 | |
---|
262 | for (unsigned int i = 0; i < NUM_LIGHTS; ++i) |
---|
263 | { |
---|
264 | mLightPivots[i] = mSceneMgr->getRootSceneNode()->createChildSceneNode(); |
---|
265 | mLightPivots[i]->rotate(mLightRotationAxes[i], mLightRotationAngles[i]); |
---|
266 | // Create a light, use default parameters |
---|
267 | mLights[i] = mSceneMgr->createLight("Light" + StringConverter::toString(i)); |
---|
268 | mLights[i]->setPosition(mLightPositions[i]); |
---|
269 | mLights[i]->setDiffuseColour(mDiffuseLightColours[i]); |
---|
270 | mLights[i]->setSpecularColour(mSpecularLightColours[i]); |
---|
271 | mLights[i]->setVisible(mLightState[i]); |
---|
272 | // Attach light |
---|
273 | mLightPivots[i]->attachObject(mLights[i]); |
---|
274 | // Create billboard for light |
---|
275 | mLightFlareSets[i] = mSceneMgr->createBillboardSet("Flare" + StringConverter::toString(i)); |
---|
276 | mLightFlareSets[i]->setMaterialName("Examples/Flare"); |
---|
277 | mLightPivots[i]->attachObject(mLightFlareSets[i]); |
---|
278 | mLightFlares[i] = mLightFlareSets[i]->createBillboard(mLightPositions[i]); |
---|
279 | mLightFlares[i]->setColour(mDiffuseLightColours[i]); |
---|
280 | mLightFlareSets[i]->setVisible(mLightState[i]); |
---|
281 | } |
---|
282 | // move the camera a bit right and make it look at the knot |
---|
283 | mCamera->moveRelative(Vector3(50, 0, 20)); |
---|
284 | mCamera->lookAt(0, 0, 0); |
---|
285 | // show overlay |
---|
286 | Overlay* pOver = OverlayManager::getSingleton().getByName("Example/DP3Overlay"); |
---|
287 | mObjectInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/ObjectInfo"); |
---|
288 | mMaterialInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/MaterialInfo"); |
---|
289 | mInfo = OverlayManager::getSingleton().getOverlayElement("Example/DP3/Info"); |
---|
290 | |
---|
291 | mObjectInfo->setCaption("Current: " + mEntityMeshes[mCurrentEntity]); |
---|
292 | mMaterialInfo->setCaption("Current: " + mMaterialNames[mCurrentEntity][mCurrentMaterial]); |
---|
293 | if (!caps->hasCapability(RSC_FRAGMENT_PROGRAM)) |
---|
294 | { |
---|
295 | mInfo->setCaption("NOTE: Light colours and specular highlights are not supported by your card."); |
---|
296 | } |
---|
297 | pOver->show(); |
---|
298 | } |
---|
299 | |
---|
300 | // Create new frame listener |
---|
301 | void createFrameListener(void) |
---|
302 | { |
---|
303 | mFrameListener= new Dp3_Listener(mWindow, mCamera); |
---|
304 | mRoot->addFrameListener(mFrameListener); |
---|
305 | } |
---|
306 | }; |
---|
307 | |
---|
308 | #ifdef __cplusplus |
---|
309 | extern "C" { |
---|
310 | #endif |
---|
311 | |
---|
312 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
313 | INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT ) |
---|
314 | #else |
---|
315 | int main(int argc, char **argv) |
---|
316 | #endif |
---|
317 | { |
---|
318 | // Create application object |
---|
319 | Dp3_Application app; |
---|
320 | |
---|
321 | try { |
---|
322 | app.go(); |
---|
323 | } catch( Exception& e ) { |
---|
324 | #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 |
---|
325 | MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL); |
---|
326 | #else |
---|
327 | std::cerr << "An exception has occured: " << e.getFullDescription(); |
---|
328 | #endif |
---|
329 | } |
---|
330 | |
---|
331 | return 0; |
---|
332 | } |
---|
333 | |
---|
334 | #ifdef __cplusplus |
---|
335 | } |
---|
336 | #endif |
---|