source: trunk/VUT/work/TestCullingTerrain/TerrainContentGenerator.cpp @ 61

Revision 61, 2.9 KB checked in by mattausch, 20 years ago (diff)
Line 
1/**
2    \file
3        TerrainContentGenerator.cpp
4    \brief
5        Creates content for the terrain,
6*/
7#include "TerrainContentGenerator.h"
8
9//-----------------------------------------------------------------------
10TerrainContentGenerator::TerrainContentGenerator( SceneManager *sm, RayQueryExecutor *rayQueryEx )
11{
12        mSceneMgr = sm;
13        mRayQueryExecutor = rayQueryEx;
14
15        mMinTranslation = Vector3(-70.0f, -70.0f, 0.0f);
16        mMaxTranslation = Vector3(70.0f, 70.0f, 600.0f);
17
18        mMinAngle = 0;
19        mMaxAngle = 360;
20
21        mCount = 0;
22}
23//-----------------------------------------------------------------------
24void TerrainContentGenerator::generateScene( int numObjects )
25{
26        srand (time (0));
27       
28        Vector3 rotationRatio;
29        Vector3 translationRatio;
30
31        for(int i=0; i < numObjects; i++)
32        {
33                rotationRatio.x = rand() / (float) RAND_MAX;
34                rotationRatio.y = rand() / (float) RAND_MAX;
35                rotationRatio.z = rand() / (float) RAND_MAX;
36
37                translationRatio.x = rand() / (float) RAND_MAX;
38                translationRatio.y = rand() / (float) RAND_MAX;
39                translationRatio.z = rand() / (float) RAND_MAX;
40
41                generateSceneObject(translationRatio, rotationRatio, i, "sphere.mesh");
42        }
43}
44//-----------------------------------------------------------------------
45void TerrainContentGenerator::generateSceneObject(const Vector3 &translationRatio,
46                                         const Vector3 &rotationRatio, const int idx, const String& entName)
47{
48        // Setup the ray scene query
49        Real rotation = mMinAngle + rotationRatio.x * (mMaxAngle - mMinAngle);
50        Vector3 translation = mMinTranslation + translationRatio * (mMaxTranslation - mMinTranslation);
51
52        translation.y = 5000;
53   
54        Vector3 queryResult;
55
56        if(mRayQueryExecutor->executeRayQuery(&queryResult, translation, Vector3::NEGATIVE_UNIT_Y))
57        {
58                char name[16];
59                sprintf( name, "%s%d", entName.c_str(), mCount++);
60
61                Entity *ent = mSceneMgr->createEntity(name, entName);
62                SceneNode *currentObject = mSceneMgr->getRootSceneNode( )->createChildSceneNode( String(name) + "Node", queryResult);
63                currentObject->attachObject(ent);
64                currentObject->setScale(0.1f, 0.1f, 0.1f);
65        }
66}
67//-----------------------------------------------------------------------
68RayQueryExecutor::RayQueryExecutor( SceneManager *sm )
69{
70        mRaySceneQuery = sm->createRayQuery(Ray());
71}
72//-----------------------------------------------------------------------
73RayQueryExecutor::~RayQueryExecutor()
74{
75        delete mRaySceneQuery;
76}
77//-----------------------------------------------------------------------
78bool RayQueryExecutor::executeRayQuery( Vector3 *result, const Vector3 &pos, const Vector3 &dir )
79{
80        Ray ray(pos, dir);
81    mRaySceneQuery->setRay(ray);
82
83    // Perform the scene query
84    RaySceneQueryResult &queryResult = mRaySceneQuery->execute();
85    RaySceneQueryResult::iterator it = queryResult.begin( );
86
87        if (it != queryResult.end() && it->worldFragment)
88        {
89                SceneQuery::WorldFragment* wf = it->worldFragment;
90
91                *result = wf->singleIntersection;
92                return true;
93        }
94
95        return false;
96}
Note: See TracBrowser for help on using the repository browser.