00001 #include "DynamicLines.h"
00002 #include "Ogre.h"
00003 #include <cassert>
00004 #include <cmath>
00005
00006 using namespace Ogre;
00007
00008 namespace ASR
00009 {
00010
00011 enum {
00012 POSITION_BINDING,
00013 TEXCOORD_BINDING
00014 };
00015
00016 DynamicLines::DynamicLines(OperationType opType)
00017 {
00018 initialize(opType,false);
00019 setMaterial("BaseWhiteNoLighting");
00020 mDirty = true;
00021 }
00022
00023 DynamicLines::~DynamicLines()
00024 {
00025 }
00026
00027 void DynamicLines::setOperationType(OperationType opType)
00028 {
00029 mRenderOp.operationType = opType;
00030 }
00031
00032 RenderOperation::OperationType DynamicLines::getOperationType() const
00033 {
00034 return mRenderOp.operationType;
00035 }
00036
00037 void DynamicLines::addPoint(const Vector3 &p)
00038 {
00039 mPoints.push_back(p);
00040 mDirty = true;
00041 }
00042 void DynamicLines::addPoint(Real x, Real y, Real z)
00043 {
00044 mPoints.push_back(Vector3(x,y,z));
00045 mDirty = true;
00046 }
00047 const Vector3& DynamicLines::getPoint(unsigned short index) const
00048 {
00049 assert(index < mPoints.size() && "Point index is out of bounds!!");
00050 return mPoints[index];
00051 }
00052 unsigned short DynamicLines::getNumPoints(void) const
00053 {
00054 return (unsigned short)mPoints.size();
00055 }
00056 void DynamicLines::setPoint(unsigned short index, const Vector3 &value)
00057 {
00058 assert(index < mPoints.size() && "Point index is out of bounds!!");
00059
00060 mPoints[index] = value;
00061 mDirty = true;
00062 }
00063 void DynamicLines::clear()
00064 {
00065 mPoints.clear();
00066 mDirty = true;
00067 }
00068
00069 void DynamicLines::update()
00070 {
00071 if (mDirty) fillHardwareBuffers();
00072 }
00073
00074 void DynamicLines::createVertexDeclaration()
00075 {
00076 VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
00077 decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
00078 }
00079
00080 void DynamicLines::fillHardwareBuffers()
00081 {
00082 int size = mPoints.size();
00083
00084 prepareHardwareBuffers(size,0);
00085
00086 if (!size) {
00087 mBox.setExtents(Vector3::ZERO,Vector3::ZERO);
00088 mDirty=false;
00089 return;
00090 }
00091
00092 Vector3 vaabMin = mPoints[0];
00093 Vector3 vaabMax = mPoints[0];
00094
00095 HardwareVertexBufferSharedPtr vbuf =
00096 mRenderOp.vertexData->vertexBufferBinding->getBuffer(0);
00097
00098 Real *prPos = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
00099 {
00100 for(int i = 0; i < size; i++)
00101 {
00102 *prPos++ = mPoints[i].x;
00103 *prPos++ = mPoints[i].y;
00104 *prPos++ = mPoints[i].z;
00105
00106 if(mPoints[i].x < vaabMin.x)
00107 vaabMin.x = mPoints[i].x;
00108 if(mPoints[i].y < vaabMin.y)
00109 vaabMin.y = mPoints[i].y;
00110 if(mPoints[i].z < vaabMin.z)
00111 vaabMin.z = mPoints[i].z;
00112
00113 if(mPoints[i].x > vaabMax.x)
00114 vaabMax.x = mPoints[i].x;
00115 if(mPoints[i].y > vaabMax.y)
00116 vaabMax.y = mPoints[i].y;
00117 if(mPoints[i].z > vaabMax.z)
00118 vaabMax.z = mPoints[i].z;
00119 }
00120 }
00121 vbuf->unlock();
00122
00123 mBox.setExtents(vaabMin, vaabMax);
00124
00125 mDirty = false;
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 }