1 | #include "OgreTerrainContentGenerator.h"
|
---|
2 |
|
---|
3 | #define MAX_HEIGHT 5000 // maximal possible height
|
---|
4 |
|
---|
5 | namespace Ogre {
|
---|
6 |
|
---|
7 | /*************************************************/
|
---|
8 | /* RayQueryExecutor implementation */
|
---|
9 | /*************************************************/
|
---|
10 |
|
---|
11 | //-----------------------------------------------------------------------
|
---|
12 | RayQueryExecutor::RayQueryExecutor(SceneManager *sm)
|
---|
13 | {
|
---|
14 | mRaySceneQuery = sm->createRayQuery(Ray());
|
---|
15 | }
|
---|
16 | //-----------------------------------------------------------------------
|
---|
17 | RayQueryExecutor::~RayQueryExecutor()
|
---|
18 | {
|
---|
19 | delete mRaySceneQuery;
|
---|
20 | }
|
---|
21 | //-----------------------------------------------------------------------
|
---|
22 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Vector3 &pos,
|
---|
23 | const Vector3 &dir)
|
---|
24 | {
|
---|
25 | return executeRayQuery(result, Ray(pos, dir));
|
---|
26 | }
|
---|
27 | //-----------------------------------------------------------------------
|
---|
28 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Ray &ray)
|
---|
29 | {
|
---|
30 | mRaySceneQuery->setRay(ray);
|
---|
31 |
|
---|
32 | // Perform the scene query
|
---|
33 | RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
|
---|
34 | RaySceneQueryResult::iterator it = queryResult.begin();
|
---|
35 |
|
---|
36 | if (it != queryResult.end() && it->worldFragment)
|
---|
37 | {
|
---|
38 | SceneQuery::WorldFragment* wf = it->worldFragment;
|
---|
39 |
|
---|
40 | *result = wf->singleIntersection;
|
---|
41 | return true;
|
---|
42 | }
|
---|
43 |
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*******************************************************/
|
---|
48 | /* TerrainContentGenerator implementation */
|
---|
49 | /*******************************************************/
|
---|
50 |
|
---|
51 | //-----------------------------------------------------------------------
|
---|
52 | TerrainContentGenerator::TerrainContentGenerator(SceneManager *sm):
|
---|
53 | SceneContentGenerator(sm), mOffset(0)
|
---|
54 | {
|
---|
55 | mMinPos = Vector3(0.0f, MAX_HEIGHT, 0.0f);
|
---|
56 | mMaxPos = Vector3(3000.0f, MAX_HEIGHT, 3000.0f);
|
---|
57 |
|
---|
58 | mMinAngle = Vector3(0.0f, 0.0f, 0.0f);
|
---|
59 | mMaxAngle = Vector3(360.0f, 0.0f, 0.0f);
|
---|
60 |
|
---|
61 | mRayQueryExecutor = new RayQueryExecutor(sm);
|
---|
62 | }
|
---|
63 | //-----------------------------------------------------------------------
|
---|
64 | TerrainContentGenerator::~TerrainContentGenerator()
|
---|
65 | {
|
---|
66 | delete mRayQueryExecutor;
|
---|
67 | }
|
---|
68 | //-----------------------------------------------------------------------
|
---|
69 | SceneNode *TerrainContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
70 | const Vector3 &rotation, const String& objName, const bool clampToTerrain)
|
---|
71 | {
|
---|
72 |
|
---|
73 | if (clampToTerrain)
|
---|
74 | {
|
---|
75 | return GenerateSceneObject(position, rotation, objName);
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | return SceneContentGenerator::GenerateSceneObject(position, rotation, objName);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | //-----------------------------------------------------------------------
|
---|
83 | SceneNode *TerrainContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
84 | const Vector3 &rotation, const String& objName)
|
---|
85 | {
|
---|
86 | Vector3 queryResult;
|
---|
87 | // set y to max height so we are sure to be over terrain
|
---|
88 | Vector3 pos(position.x, MAX_HEIGHT, position.z);
|
---|
89 |
|
---|
90 | if (mRayQueryExecutor->executeRayQuery(&queryResult, position, Vector3::NEGATIVE_UNIT_Y))
|
---|
91 | {
|
---|
92 | // reject if height is too high
|
---|
93 | if (queryResult.y < mMaxPos.y)
|
---|
94 | {
|
---|
95 | // add offset so object is over terrain
|
---|
96 | queryResult.y += mOffset;
|
---|
97 |
|
---|
98 | return SceneContentGenerator::GenerateSceneObject(queryResult, rotation, objName);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 | //-----------------------------------------------------------------------
|
---|
105 | RayQueryExecutor *TerrainContentGenerator::GetRayQueryExecutor()
|
---|
106 | {
|
---|
107 | return mRayQueryExecutor;
|
---|
108 | }
|
---|
109 | //-----------------------------------------------------------------------
|
---|
110 | void TerrainContentGenerator::SetOffset(Real offset)
|
---|
111 | {
|
---|
112 | mOffset = offset;
|
---|
113 | }
|
---|
114 |
|
---|
115 | } // namespace Ogre |
---|