1 | #include "DynamicLines.h" |
---|
2 | #include <Ogre.h> |
---|
3 | #include <cassert> |
---|
4 | #include <cmath> |
---|
5 | |
---|
6 | using namespace Ogre; |
---|
7 | |
---|
8 | namespace orxonox |
---|
9 | { |
---|
10 | enum { |
---|
11 | POSITION_BINDING, |
---|
12 | TEXCOORD_BINDING |
---|
13 | }; |
---|
14 | |
---|
15 | DynamicLines::DynamicLines(OperationType opType) |
---|
16 | { |
---|
17 | initialize(opType,false); |
---|
18 | setMaterial("BaseWhiteNoLighting"); |
---|
19 | mDirty = true; |
---|
20 | } |
---|
21 | |
---|
22 | DynamicLines::~DynamicLines() |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | void DynamicLines::setOperationType(OperationType opType) |
---|
27 | { |
---|
28 | mRenderOp.operationType = opType; |
---|
29 | } |
---|
30 | |
---|
31 | RenderOperation::OperationType DynamicLines::getOperationType() const |
---|
32 | { |
---|
33 | return mRenderOp.operationType; |
---|
34 | } |
---|
35 | |
---|
36 | void DynamicLines::addPoint(const Vector3 &p) |
---|
37 | { |
---|
38 | mPoints.push_back(p); |
---|
39 | mDirty = true; |
---|
40 | } |
---|
41 | void DynamicLines::addPoint(Real x, Real y, Real z) |
---|
42 | { |
---|
43 | mPoints.push_back(Vector3(x,y,z)); |
---|
44 | mDirty = true; |
---|
45 | } |
---|
46 | const Vector3& DynamicLines::getPoint(unsigned short index) const |
---|
47 | { |
---|
48 | assert(index < mPoints.size() && "Point index is out of bounds!!"); |
---|
49 | return mPoints[index]; |
---|
50 | } |
---|
51 | unsigned short DynamicLines::getNumPoints(void) const |
---|
52 | { |
---|
53 | return (unsigned short)mPoints.size(); |
---|
54 | } |
---|
55 | void DynamicLines::setPoint(unsigned short index, const Vector3 &value) |
---|
56 | { |
---|
57 | assert(index < mPoints.size() && "Point index is out of bounds!!"); |
---|
58 | |
---|
59 | mPoints[index] = value; |
---|
60 | mDirty = true; |
---|
61 | } |
---|
62 | void DynamicLines::clear() |
---|
63 | { |
---|
64 | mPoints.clear(); |
---|
65 | mDirty = true; |
---|
66 | } |
---|
67 | |
---|
68 | void DynamicLines::update() |
---|
69 | { |
---|
70 | if (mDirty) fillHardwareBuffers(); |
---|
71 | } |
---|
72 | |
---|
73 | void DynamicLines::createVertexDeclaration() |
---|
74 | { |
---|
75 | VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration; |
---|
76 | decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION); |
---|
77 | } |
---|
78 | |
---|
79 | void DynamicLines::fillHardwareBuffers() |
---|
80 | { |
---|
81 | int size = mPoints.size(); |
---|
82 | |
---|
83 | prepareHardwareBuffers(size,0); |
---|
84 | |
---|
85 | if (!size) { |
---|
86 | mBox.setExtents(Vector3::ZERO,Vector3::ZERO); |
---|
87 | mDirty=false; |
---|
88 | return; |
---|
89 | } |
---|
90 | |
---|
91 | Vector3 vaabMin = mPoints[0]; |
---|
92 | Vector3 vaabMax = mPoints[0]; |
---|
93 | |
---|
94 | HardwareVertexBufferSharedPtr vbuf = |
---|
95 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(0); |
---|
96 | |
---|
97 | Real *prPos = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD)); |
---|
98 | { |
---|
99 | for(int i = 0; i < size; i++) |
---|
100 | { |
---|
101 | *prPos++ = mPoints[i].x; |
---|
102 | *prPos++ = mPoints[i].y; |
---|
103 | *prPos++ = mPoints[i].z; |
---|
104 | |
---|
105 | if(mPoints[i].x < vaabMin.x) |
---|
106 | vaabMin.x = mPoints[i].x; |
---|
107 | if(mPoints[i].y < vaabMin.y) |
---|
108 | vaabMin.y = mPoints[i].y; |
---|
109 | if(mPoints[i].z < vaabMin.z) |
---|
110 | vaabMin.z = mPoints[i].z; |
---|
111 | |
---|
112 | if(mPoints[i].x > vaabMax.x) |
---|
113 | vaabMax.x = mPoints[i].x; |
---|
114 | if(mPoints[i].y > vaabMax.y) |
---|
115 | vaabMax.y = mPoints[i].y; |
---|
116 | if(mPoints[i].z > vaabMax.z) |
---|
117 | vaabMax.z = mPoints[i].z; |
---|
118 | } |
---|
119 | } |
---|
120 | vbuf->unlock(); |
---|
121 | |
---|
122 | mBox.setExtents(vaabMin, vaabMax); |
---|
123 | |
---|
124 | mDirty = false; |
---|
125 | } |
---|
126 | |
---|
127 | /* |
---|
128 | void DynamicLines::getWorldTransforms(Matrix4 *xform) const |
---|
129 | { |
---|
130 | // return identity matrix to prevent parent transforms |
---|
131 | *xform = Matrix4::IDENTITY; |
---|
132 | } |
---|
133 | */ |
---|
134 | /* |
---|
135 | const Quaternion &DynamicLines::getWorldOrientation(void) const |
---|
136 | { |
---|
137 | return Quaternion::IDENTITY; |
---|
138 | } |
---|
139 | |
---|
140 | const Vector3 &DynamicLines::getWorldPosition(void) const |
---|
141 | { |
---|
142 | return Vector3::ZERO; |
---|
143 | } |
---|
144 | */ |
---|
145 | } |
---|