1 | /**
|
---|
2 | \file
|
---|
3 | TerrainContentGenerator.cpp
|
---|
4 | \brief
|
---|
5 | Creates content for the terrain.
|
---|
6 | */
|
---|
7 | #include "TerrainContentGenerator.h"
|
---|
8 | #include <windows.h>
|
---|
9 | /********************************************/
|
---|
10 | /* TerrainContentGenerator implementation */
|
---|
11 | /********************************************/
|
---|
12 | //-----------------------------------------------------------------------
|
---|
13 | TerrainContentGenerator::TerrainContentGenerator(SceneManager *sm)
|
---|
14 | {
|
---|
15 | mSceneMgr = sm;
|
---|
16 | mRayQueryExecutor = new RayQueryExecutor(sm);
|
---|
17 |
|
---|
18 | mMinPos = Vector3(-500.0f, 5000.0f, -500.0f); |
---|
19 | mMaxPos = Vector3(500.0f, 5000.0f, 500.0f); |
---|
20 | |
---|
21 | mMinAngle = 0; |
---|
22 | mMaxAngle = 360;
|
---|
23 |
|
---|
24 | mCount = 0;
|
---|
25 | }
|
---|
26 | //-----------------------------------------------------------------------
|
---|
27 | TerrainContentGenerator::~TerrainContentGenerator()
|
---|
28 | {
|
---|
29 | delete mRayQueryExecutor;
|
---|
30 | }
|
---|
31 | //----------------------------------------------------------------------- |
---|
32 | void TerrainContentGenerator::generateScene(int numObjects, const String &meshName) |
---|
33 | { |
---|
34 | srand (time(0)); |
---|
35 | |
---|
36 | Vector3 rotationRatio; |
---|
37 | Vector3 translationRatio; |
---|
38 | |
---|
39 | for(int i=0; i < numObjects; ++i) |
---|
40 | { |
---|
41 | rotationRatio.x = rand() / (float) RAND_MAX; |
---|
42 | rotationRatio.y = rand() / (float) RAND_MAX; |
---|
43 | rotationRatio.z = rand() / (float) RAND_MAX; |
---|
44 | |
---|
45 | translationRatio.x = rand() / (float) RAND_MAX; |
---|
46 | translationRatio.y = rand() / (float) RAND_MAX; |
---|
47 | translationRatio.z = rand() / (float) RAND_MAX; |
---|
48 | |
---|
49 | generateSceneObject(translationRatio, rotationRatio, i, meshName); |
---|
50 | } |
---|
51 | }
|
---|
52 | //-----------------------------------------------------------------------
|
---|
53 | void TerrainContentGenerator::generateSceneObject(const Vector3 &translationRatio, |
---|
54 | const Vector3 &rotationRatio, const int idx, const String& entName)
|
---|
55 | {
|
---|
56 | // Setup the ray scene query
|
---|
57 | Real rotation = mMinAngle + rotationRatio.x * (mMaxAngle - mMinAngle); |
---|
58 | Vector3 position = mMinPos + translationRatio * (mMaxPos - mMinPos);
|
---|
59 |
|
---|
60 | Vector3 queryResult;
|
---|
61 |
|
---|
62 | char msg[100];
|
---|
63 | sprintf(msg, "pos: %3.3f %3.3f %3.3f\n", position.x, position.y, position.z);
|
---|
64 |
|
---|
65 | if (mRayQueryExecutor->executeRayQuery(&queryResult, position, Vector3::NEGATIVE_UNIT_Y))
|
---|
66 | {
|
---|
67 | char name[16];
|
---|
68 | sprintf(name, "%s%d", entName.c_str(), mCount++);
|
---|
69 | OutputDebugString("Creating new object "); OutputDebugString(msg);
|
---|
70 |
|
---|
71 | Entity *ent = mSceneMgr->createEntity(name, entName);
|
---|
72 | SceneNode *currentObject = mSceneMgr->getRootSceneNode()->
|
---|
73 | createChildSceneNode(String(name) + "Node", queryResult);
|
---|
74 |
|
---|
75 | currentObject->attachObject(ent);
|
---|
76 | currentObject->setScale(0.1f, 0.1f, 0.1f);
|
---|
77 | currentObject->setPosition(position);
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | OutputDebugString(msg);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | /****************************************/
|
---|
85 | /* RayQueryExecutor implementation */
|
---|
86 | /****************************************/
|
---|
87 | //-----------------------------------------------------------------------
|
---|
88 | RayQueryExecutor::RayQueryExecutor(SceneManager *sm)
|
---|
89 | {
|
---|
90 | mRaySceneQuery = sm->createRayQuery(Ray());
|
---|
91 | }
|
---|
92 | //-----------------------------------------------------------------------
|
---|
93 | RayQueryExecutor::~RayQueryExecutor()
|
---|
94 | {
|
---|
95 | delete mRaySceneQuery;
|
---|
96 | }
|
---|
97 | //-----------------------------------------------------------------------
|
---|
98 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Vector3 &pos, const Vector3 &dir)
|
---|
99 | {
|
---|
100 | return executeRayQuery(result, Ray(pos, dir));
|
---|
101 | }
|
---|
102 |
|
---|
103 | bool RayQueryExecutor::executeRayQuery(Vector3 *result, const Ray &ray)
|
---|
104 | {
|
---|
105 | mRaySceneQuery->setRay(ray);
|
---|
106 |
|
---|
107 | // Perform the scene query
|
---|
108 | RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
|
---|
109 | RaySceneQueryResult::iterator it = queryResult.begin();
|
---|
110 |
|
---|
111 | if (it != queryResult.end() && it->worldFragment)
|
---|
112 | {
|
---|
113 | SceneQuery::WorldFragment* wf = it->worldFragment;
|
---|
114 |
|
---|
115 | *result = wf->singleIntersection;
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return false;
|
---|
120 | }
|
---|