//------------------------------------------------------------------------------------------------
void DebugLines::draw()
{
    if (_drawn || _points.empty()) 
        return;
    else 
        _drawn = true;

    // Initialization stuff
    mRenderOp.vertexData->vertexCount = _points.size();

    Ogre::VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
    Ogre::VertexBufferBinding *bind = mRenderOp.vertexData->vertexBufferBinding;

	HardwareVertexBufferSharedPtr vbuf;
	if(decl->getElementCount() == 0)
	{
		decl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
		decl->addElement(0, 12, VET_FLOAT1, VES_TEXTURE_COORDINATES);
		if ( mHasColours )
			decl->addElement(0, 16, VET_COLOUR, VES_DIFFUSE);


		vbuf = HardwareBufferManager::getSingleton().createVertexBuffer(
			decl->getVertexSize(0),
			mRenderOp.vertexData->vertexCount,
			HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);

		bind->setBinding(0, vbuf);
	}
	else
	{
		bind->unsetAllBindings();

		 vbuf = HardwareBufferManager::getSingleton().createVertexBuffer(
			decl->getVertexSize(0),
			mRenderOp.vertexData->vertexCount,
			HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);

		bind->setBinding(0, vbuf); 
	}

    // Drawing stuff
    unsigned int size = (unsigned int)_points.size();
    Ogre::Vector3 vaabMin = _points[0];
    Ogre::Vector3 vaabMax = _points[0];

    float *prPos = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));

    for(unsigned int i = 0; i < size; i++)
    {
        *prPos++ = _points[i].x;
        *prPos++ = _points[i].y;
        *prPos++ = _points[i].z;
		*prPos++ = 0;
		
		if ( mHasColours ) {
			unsigned int* colorPtr = reinterpret_cast<unsigned int*>(prPos++);
			Ogre::ColourValue col = _colours[i];
			//col.setHSB( (float)(i%256)/256.0f, 1.0f, 1.0f );
			Ogre::Root::getSingleton().convertColourValue(col, colorPtr);
		}
			
        if (_points[i].x < vaabMin.x)
			vaabMin.x = _points[i].x;
		else if (_points[i].x > vaabMax.x)
			vaabMax.x = _points[i].x;

        if (_points[i].y < vaabMin.y)
			vaabMin.y = _points[i].y;
		else if (_points[i].y > vaabMax.y)
			vaabMax.y = _points[i].y;

        if (_points[i].z < vaabMin.z)
			vaabMin.z = _points[i].z;
		else if (_points[i].z > vaabMax.z)
			vaabMax.z = _points[i].z;
    }

    vbuf->unlock();

    mBox.setInfinite();
	//mBox.Extents(vaabMin, vaabMax);
}
//------------------------------------------------------------------------------------------------
void DebugLines::draw()
{
    if (_lines.empty())
    {
        mRenderOp.vertexData->vertexCount = 0;
        return;
    }
    
    // Initialization stuff
    mRenderOp.vertexData->vertexCount = _lines.size() * 2;

    Ogre::VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration;
    Ogre::VertexBufferBinding *bind = mRenderOp.vertexData->vertexBufferBinding;

    if (_vbuf.isNull())
    {
        decl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
        decl->addElement(0, 12, VET_COLOUR, VES_DIFFUSE);

        _vbuf = HardwareBufferManager::getSingleton().createVertexBuffer(
            decl->getVertexSize(0),
            mRenderOp.vertexData->vertexCount,
            HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);

        bind->setBinding(0, _vbuf);
    }
    else
    {
        if (_vbuf->getNumVertices() != mRenderOp.vertexData->vertexCount)
        {
            bind->unsetAllBindings();

            _vbuf = HardwareBufferManager::getSingleton().createVertexBuffer(
                decl->getVertexSize(0),
                mRenderOp.vertexData->vertexCount,
                HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);

            bind->setBinding(0, _vbuf);
        }
    }

    // Drawing stuff
    unsigned int size = (unsigned int)_lines.size();
    Ogre::Vector3 vaabMin = _lines[0]._start;
    Ogre::Vector3 vaabMax = _lines[0]._start;

    float *prPos = static_cast<float*>(_vbuf->lock(HardwareBuffer::HBL_DISCARD));

    Ogre::RenderSystem* rs = Root::getSingleton().getRenderSystem();
    Ogre::VertexElementType vet = VET_COLOUR_ARGB;
    if (rs)
        vet = rs->getColourVertexElementType();

    for(unsigned int i = 0; i < size; i++)
    {
        const DebugLine& line = _lines[i];
        uint32 packedColor;
        if (vet == VET_COLOUR_ARGB)
            packedColor = line._color.getAsARGB();
        else
            packedColor = line._color.getAsABGR();
        
        *prPos++ = line._start.x;
        *prPos++ = line._start.y;
        *prPos++ = line._start.z;
        *((uint32*)prPos) = packedColor;
        prPos++;
        *prPos++ = line._end.x;
        *prPos++ = line._end.y;
        *prPos++ = line._end.z;
        *((uint32*)prPos) = packedColor;
        prPos++;
        
        if (line._start.x < vaabMin.x)
            vaabMin.x = line._start.x;
        else if (line._start.x > vaabMax.x)
            vaabMax.x = line._start.x;

        if (line._start.y < vaabMin.y)
            vaabMin.y = line._start.y;
        else if (line._start.y > vaabMax.y)
            vaabMax.y = line._start.y;

        if (line._start.z < vaabMin.z)
            vaabMin.z = line._start.z;
        else if (line._start.z > vaabMax.z)
            vaabMax.z = line._start.z;
        
        if (line._end.x < vaabMin.x)
            vaabMin.x = line._end.x;
        else if (line._end.x > vaabMax.x)
            vaabMax.x = line._end.x;

        if (line._end.y < vaabMin.y)
            vaabMin.y = line._end.y;
        else if (line._end.y > vaabMax.y)
            vaabMax.y = line._end.y;

        if (line._end.z < vaabMin.z)
            vaabMin.z = line._end.z;
        else if (line._end.z > vaabMax.z)
            vaabMax.z = line._end.z;
    }

    _vbuf->unlock();

    mBox.setInfinite();
    //mBox.Extents(vaabMin, vaabMax);
    
    clear();
}