2 #include "mof/VertexBuffer.hpp"
3 #include "mof/private/GraphicsDeviceImpl.hpp"
5 #include "mof/VertexTypes.hpp"
6 #include "mof/private/VertexFVF.hpp"
9 template class mof::VertexBuffer<mof::VertexXYZRHWCUV>;
10 template class mof::VertexBuffer<mof::VertexXYZRHWC>;
11 template class mof::VertexBuffer<mof::VertexXYZCUV>;
12 template class mof::VertexBuffer<mof::VertexXYZNUV>;
13 template class mof::VertexBuffer<mof::VertexXYZC>;
17 struct mof::VertexBuffer<T>::Impl{
18 IDirect3DVertexBuffer9* pBuffer;
19 mof::PRIMITIVE_TYPE primitiveType;
22 Impl(mof::PRIMITIVE_TYPE primitiveType_);
28 mof::VertexBuffer<T>::Impl::Impl(mof::PRIMITIVE_TYPE primitiveType_ )
29 : pBuffer(NULL) , primitiveType(primitiveType_)
34 mof::VertexBuffer<T>::Impl::~Impl()
36 if(pBuffer != NULL)pBuffer->Release();
41 mof::VertexBuffer<T>::VertexBuffer(const T& front , const T& back , mof::PRIMITIVE_TYPE primitiveType )
42 : m_pImpl(new Impl( primitiveType ))
44 int length = &back - &front + 1;
45 if(length <= 0)throw std::invalid_argument("
\95s
\90³
\82È
\92¸
\93_
\94z
\97ñ
\8ew
\92è");
47 const LPDIRECT3DDEVICE9 pDevice = mof::GraphicsDevice::getRawDevice();
48 HRESULT hr = pDevice->CreateVertexBuffer( sizeof(T) * length , 0 , mof::getFVF<T>() , D3DPOOL_DEFAULT , &m_pImpl->pBuffer , NULL);
49 if(FAILED(hr))throw std::runtime_error("Failed -- CreateVertexBuffer");
52 hr = m_pImpl->pBuffer->Lock( 0, 0 ,(LPVOID*)&pBuffer , 0);
53 if(FAILED(hr))throw std::invalid_argument(std::string("
\92¸
\93_
\83o
\83b
\83t
\83@
\82Ì
\83\8d\83b
\83N
\82É
\8e¸
\94s
\82µ
\82Ü
\82µ
\82½"));
55 memcpy( pBuffer , &front , sizeof(T) * length );
56 m_pImpl->pBuffer->Unlock();
62 mof::VertexBuffer<T>::~VertexBuffer(){
67 int mof::VertexBuffer<T>::getLength() const{
68 D3DVERTEXBUFFER_DESC desc;
69 m_pImpl->pBuffer->GetDesc(&desc);
70 return desc.Size / sizeof(T);
75 mof::PRIMITIVE_TYPE mof::VertexBuffer<T>::getPrimitiveType() const{
76 return m_pImpl->primitiveType;
81 void mof::VertexBuffer<T>::draw() const{
84 const LPDIRECT3DDEVICE9 pDevice = mof::GraphicsDevice::getRawDevice();
86 pDevice->SetFVF(mof::getFVF<T>());
87 pDevice->SetStreamSource(0 , m_pImpl->pBuffer , 0 , sizeof(T));
90 int length = getLength();
92 if(m_pImpl->primitiveType == mof::PRIMITIVE_TYPE_TRIANGLESTRIP){
93 hr = pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP , 0 , length -2 );
95 else if(m_pImpl->primitiveType == mof::PRIMITIVE_TYPE_TRIANGLELIST){
96 hr = pDevice->DrawPrimitive( D3DPT_TRIANGLELIST , 0 , length /3 );
99 if(FAILED(hr))throw std::runtime_error("Failed -- DrawPrimitive");