1 | #include "OgreTerrainContentGenerator.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | namespace Ogre {
|
---|
5 |
|
---|
6 | /*************************************************/
|
---|
7 | /* RayQueryExecutor implementation */
|
---|
8 | /*************************************************/
|
---|
9 |
|
---|
10 | //-----------------------------------------------------------------------
|
---|
11 | RayQueryExecutor::RayQueryExecutor(SceneManager *sm)
|
---|
12 | {
|
---|
13 | mRaySceneQuery = sm->createRayQuery(Ray());
|
---|
14 | }
|
---|
15 | //-----------------------------------------------------------------------
|
---|
16 | RayQueryExecutor::~RayQueryExecutor()
|
---|
17 | {
|
---|
18 | OGRE_DELETE(mRaySceneQuery);
|
---|
19 | }
|
---|
20 | //-----------------------------------------------------------------------
|
---|
21 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Vector3 &pos,
|
---|
22 | const Vector3 &dir)
|
---|
23 | {
|
---|
24 | return executeRayQuery(result, Ray(pos, dir));
|
---|
25 | }
|
---|
26 | //-----------------------------------------------------------------------
|
---|
27 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Ray &ray)
|
---|
28 | {
|
---|
29 | mRaySceneQuery->setRay(ray);
|
---|
30 |
|
---|
31 | // Perform the scene query
|
---|
32 | RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
|
---|
33 | RaySceneQueryResult::iterator it = queryResult.begin();
|
---|
34 |
|
---|
35 | if (it != queryResult.end() && it->worldFragment)
|
---|
36 | {
|
---|
37 | SceneQuery::WorldFragment* wf = it->worldFragment;
|
---|
38 |
|
---|
39 | *result = wf->singleIntersection;
|
---|
40 | return true;
|
---|
41 | }
|
---|
42 |
|
---|
43 | return false;
|
---|
44 | }
|
---|
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 | if (clampToTerrain)
|
---|
73 | return GenerateSceneObject(position, rotation, objName);
|
---|
74 |
|
---|
75 | return SceneContentGenerator::GenerateSceneObject(position, rotation, objName);
|
---|
76 | }
|
---|
77 | //-----------------------------------------------------------------------
|
---|
78 | SceneNode *TerrainContentGenerator::GenerateSceneObject(const Vector3 &position, |
---|
79 | const Vector3 &rotation, const String& objName)
|
---|
80 | {
|
---|
81 | Vector3 queryResult;
|
---|
82 | // set y to max height so we are sure to be over terrain
|
---|
83 | Vector3 pos(position.x, MAX_HEIGHT, position.z);
|
---|
84 |
|
---|
85 | if (mRayQueryExecutor->executeRayQuery(&queryResult, position, Vector3::NEGATIVE_UNIT_Y))
|
---|
86 | {
|
---|
87 | // reject if height is too high
|
---|
88 | if (queryResult.y < mMaxPos.y)
|
---|
89 | {
|
---|
90 | // add offset so object is over terrain
|
---|
91 | queryResult.y += mOffset;
|
---|
92 |
|
---|
93 | return SceneContentGenerator::GenerateSceneObject(queryResult, rotation, objName);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | return NULL;
|
---|
98 | }
|
---|
99 | //-----------------------------------------------------------------------
|
---|
100 | void TerrainContentGenerator::SetOffset(Real offset)
|
---|
101 | {
|
---|
102 | mOffset = offset;
|
---|
103 | }
|
---|
104 |
|
---|
105 | } // namespace Ogre |
---|