~scrawl-deactivatedaccount/+junk/StuntRally

« back to all changes in this revision

Viewing changes to vdrift/roadstrip.cpp

  • Committer: Jannik Heller
  • Date: 2011-01-30 12:41:18 UTC
  • Revision ID: scrawl@scrawl-desktop-20110130124118-v5euo5nkmhjqqd9s
First commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "stdafx.h"
 
2
 
 
3
#include "roadstrip.h"
 
4
 
 
5
bool ROADSTRIP::ReadFrom(std::istream & openfile, std::ostream & error_output)
 
6
{
 
7
        patches.clear();
 
8
 
 
9
        assert(openfile);
 
10
 
 
11
        //number of patches in this road strip
 
12
        int num;
 
13
        openfile >> num;
 
14
 
 
15
        //add all road patches to this strip
 
16
        int badcount = 0;
 
17
        for (int i = 0; i < num; i++)
 
18
        {
 
19
                BEZIER * prevbezier = NULL;
 
20
                if (!patches.empty())
 
21
                        prevbezier = &patches.back().GetPatch();
 
22
 
 
23
                patches.push_back(ROADPATCH());
 
24
                patches.back().GetPatch().ReadFrom(openfile);
 
25
 
 
26
                if (prevbezier)
 
27
                        prevbezier->Attach(patches.back().GetPatch());
 
28
 
 
29
                if (patches.back().GetPatch().CheckForProblems())
 
30
                {
 
31
                        badcount++;
 
32
                        patches.pop_back();
 
33
                }
 
34
        }
 
35
 
 
36
        if (badcount > 0)
 
37
                error_output << "Rejected " << badcount << " bezier patch(es) from roadstrip due to errors" << std::endl;
 
38
 
 
39
        //close the roadstrip
 
40
        if (patches.size() > 2)
 
41
        {
 
42
                //only close it if it ends near where it starts
 
43
                if (((patches.back().GetPatch().GetFL() - patches.front().GetPatch().GetBL()).Magnitude() < 0.1) &&
 
44
                    ((patches.back().GetPatch().GetFR() - patches.front().GetPatch().GetBR()).Magnitude() < 0.1))
 
45
                {
 
46
                        patches.back().GetPatch().Attach(patches.front().GetPatch());
 
47
                        closed = true;
 
48
                }
 
49
        }
 
50
 
 
51
        GenerateSpacePartitioning();
 
52
 
 
53
        return true;
 
54
}
 
55
 
 
56
void ROADSTRIP::GenerateSpacePartitioning()
 
57
{
 
58
        aabb_part.Clear();
 
59
 
 
60
        for (std::list <ROADPATCH>::iterator i = patches.begin(); i != patches.end(); ++i)
 
61
        {
 
62
                ROADPATCH * rp = &(*i);
 
63
                aabb_part.Add(rp, i->GetPatch().GetAABB());
 
64
        }
 
65
 
 
66
        aabb_part.Optimize();
 
67
}
 
68
 
 
69
bool ROADSTRIP::Collide(const MATHVECTOR <float, 3> & origin, const MATHVECTOR <float, 3> & direction, float seglen, MATHVECTOR <float, 3> & outtri, const BEZIER * & colpatch, MATHVECTOR <float, 3> & normal) const
 
70
{
 
71
        std::list <ROADPATCH *> candidates;
 
72
        aabb_part.Query(AABB<float>::RAY(origin, direction, seglen), candidates);
 
73
        bool col = false;
 
74
        for (std::list <ROADPATCH *>::iterator i = candidates.begin(); i != candidates.end(); ++i)
 
75
        {
 
76
                MATHVECTOR <float, 3> coltri, colnorm;
 
77
                if ((*i)->Collide(origin, direction, seglen, coltri, colnorm))
 
78
                {
 
79
                        if (!col || (coltri-origin).Magnitude() < (outtri-origin).Magnitude())
 
80
                        {
 
81
                                outtri = coltri;
 
82
                                normal = colnorm;
 
83
                                colpatch = &(*i)->GetPatch();
 
84
                        }
 
85
 
 
86
                        col = true;
 
87
                }
 
88
        }
 
89
 
 
90
        return col;
 
91
}
 
92
 
 
93
void ROADSTRIP::Reverse()
 
94
{
 
95
        patches.reverse();
 
96
 
 
97
        for (std::list <ROADPATCH>::iterator i = patches.begin(); i != patches.end(); ++i)
 
98
        {
 
99
                i->GetPatch().Reverse();
 
100
                i->GetPatch().ResetDistFromStart();
 
101
        }
 
102
 
 
103
        //fix pointers to next patches for race placement
 
104
        for (std::list <ROADPATCH>::iterator i = patches.begin(); i != patches.end(); ++i)
 
105
        {
 
106
                std::list <ROADPATCH>::iterator n = i;
 
107
                n++;
 
108
                BEZIER * nextpatchptr = NULL;
 
109
                if (n != patches.end())
 
110
                {
 
111
                        nextpatchptr = &(n->GetPatch());
 
112
                        i->GetPatch().Attach(*nextpatchptr);
 
113
                }
 
114
                else
 
115
                {
 
116
                        i->GetPatch().ResetNextPatch();
 
117
                        i->GetPatch().Attach(patches.front().GetPatch());
 
118
                }
 
119
        }
 
120
}
 
121
 
 
122
/*void ROADSTRIP::CreateRacingLine(
 
123
        SCENENODE * parentnode, 
 
124
        TEXTURE_GL & racingline_texture,
 
125
        std::ostream & error_output)
 
126
{
 
127
        for (std::list <ROADPATCH>::iterator i = patches.begin(); i != patches.end(); ++i)
 
128
        {
 
129
                std::list <ROADPATCH>::iterator n = i;
 
130
                n++;
 
131
                ROADPATCH * nextpatch(NULL);
 
132
                if (n != patches.end())
 
133
                {
 
134
                        nextpatch = &(*n);
 
135
                }
 
136
                i->AddRacinglineScenenode(parentnode, nextpatch, racingline_texture, error_output);
 
137
        }
 
138
}
 
139
*/
 
 
b'\\ No newline at end of file'