41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 #include <visp/vpConfig.h>
44 #if ( defined(WIN32) & defined(VISP_HAVE_D3D9) )
46 #include <visp/vpD3DRenderer.h>
47 #include <visp/vpColor.h>
48 #include <visp/vpMath.h>
75 vpD3DRenderer::vpD3DRenderer()
160 hFont = CreateFont(18, 0, 0, 0, FW_NORMAL,
false,
false,
false,
161 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
162 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
163 DEFAULT_PITCH | FF_DONTCARE, NULL);
170 vpD3DRenderer::~vpD3DRenderer()
174 if(pd3dDevice != NULL)
175 pd3dDevice->Release();
180 if(pd3dVideoText != NULL)
181 pd3dVideoText->Release();
189 unsigned int vpD3DRenderer::supPowerOf2(
unsigned int n)
197 return static_cast<unsigned int>(1<<(i+1));
207 bool vpD3DRenderer::init(HWND hwnd,
unsigned int width,
unsigned int height)
215 if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
217 "Can't initialize D3D!");
219 D3DDISPLAYMODE d3ddm;
220 if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
222 "Can't get the adapter's display mode!");
225 D3DPRESENT_PARAMETERS d3dpp;
226 ZeroMemory(&d3dpp,
sizeof(d3dpp));
227 d3dpp.BackBufferCount=1;
228 d3dpp.Windowed = TRUE;
229 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
230 d3dpp.BackBufferFormat = d3ddm.Format;
233 if( FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL, hWnd,
234 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
235 D3DCREATE_MULTITHREADED,
236 &d3dpp, &pd3dDevice )))
238 "Can't create the Direct3D device!");
242 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
246 initView((
float)nbCols,(
float)nbRows);
250 textWidth = supPowerOf2( (nbCols>nbRows) ? nbCols : nbRows );
255 if( D3DXCreateTexture(pd3dDevice, textWidth, textWidth, D3DX_DEFAULT, 0,
256 D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM , &pd3dText)
260 "Can't create memory texture!");
264 if( D3DXCreateTexture(pd3dDevice, textWidth, textWidth, D3DX_DEFAULT,
266 D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pd3dVideoText)
270 "Can't create video texture!");
274 if(D3DXCreateSprite(pd3dDevice, &pSprite) != S_OK)
276 "Can't create the texture's sprite!");
291 void vpD3DRenderer::initView(
float WindowWidth,
float WindowHeight)
296 D3DXMatrixOrthoLH(&Ortho2D, WindowWidth, WindowHeight, 0.0f, 1.0f);
297 D3DXMatrixIdentity(&Identity);
299 if( pd3dDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D) != D3D_OK
300 || pd3dDevice->SetTransform(D3DTS_WORLD, &Identity) != D3D_OK
301 || pd3dDevice->SetTransform(D3DTS_VIEW, &Identity) != D3D_OK)
303 "Can't set the view!");
313 void vpRGBaToTexture(
const vpImage<vpRGBa>& I,
unsigned char * imBuffer,
342 unsigned char * imBuffer,
355 imBuffer[k+0] = imBuffer[k+1] = imBuffer[k+2] = I.
bitmap[i];
356 imBuffer[k+3] = 0xFF;
370 if(pd3dDevice != NULL)
372 D3DLOCKED_RECT d3dLRect;
376 r.bottom=
static_cast<signed long>(nbRows);
377 r.right=
static_cast<signed long>(nbCols);
380 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
382 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
387 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
388 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
391 vpRGBaToTexture(im, buf, pitch);
394 if( pd3dText->UnlockRect(0) != D3D_OK)
395 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
404 void vpD3DRenderer::setImgROI(
const vpImage<vpRGBa>& im,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height )
407 if(pd3dDevice != NULL)
409 D3DLOCKED_RECT d3dLRect;
412 r.top=(LONG)iP.
get_v(); r.left=(LONG)iP.
get_u();
413 r.bottom=(LONG)(iP.
get_v()+height-1); r.right=(LONG)(iP.
get_u()+width);
416 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
418 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
423 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
424 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
427 vpRGBaToTexture(im, buf, pitch);
430 if( pd3dText->UnlockRect(0) != D3D_OK)
431 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
444 if(pd3dDevice != NULL)
446 D3DLOCKED_RECT d3dLRect;
451 r.bottom =
static_cast<LONG
>(nbRows);
452 r.right =
static_cast<LONG
>(nbCols);
455 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
457 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
462 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
463 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
466 vpGreyToTexture(im, buf, pitch);
469 if( pd3dText->UnlockRect(0) != D3D_OK)
470 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
483 if(pd3dDevice != NULL)
485 D3DLOCKED_RECT d3dLRect;
488 r.top=(LONG)iP.
get_v(); r.left=(LONG)iP.
get_u();
489 r.bottom=(LONG)(iP.
get_v()+height-1); r.right=(LONG)(iP.
get_u()+width);
492 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
494 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
499 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
500 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
503 vpGreyToTexture(im, buf, pitch);
506 if( pd3dText->UnlockRect(0) != D3D_OK)
507 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
517 bool vpD3DRenderer::render()
523 pd3dDevice->BeginScene();
529 r.bottom =
static_cast<LONG
>(nbRows);
530 r.right =
static_cast<LONG
>(nbCols);
534 pd3dDevice->UpdateTexture(pd3dText,pd3dVideoText);
538 #if (D3DX_SDK_VERSION <= 9)
540 pSprite->Draw(pd3dVideoText, &r, NULL, NULL, NULL, NULL, 0xFFFFFFFF );
543 pSprite->Draw(pd3dVideoText, &r, NULL, NULL, 0xFFFFFFFF );
548 pd3dDevice->EndScene();
550 pd3dDevice->Present( NULL, NULL, NULL, NULL );
571 if(pd3dDevice != NULL)
573 D3DLOCKED_RECT d3dLRect;
577 r.top=(LONG)iP.
get_i();
578 r.left=(LONG)iP.
get_j();
579 r.bottom=(LONG)iP.
get_i()+1;
580 r.right=(LONG)iP.
get_j()+1;
583 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
585 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
590 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
591 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
594 setBufferPixel(buf, pitch, 0, 0,color);
597 if( pd3dText->UnlockRect(0) != D3D_OK)
598 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
613 unsigned int thickness,
int style)
622 if(pd3dDevice != NULL)
629 IDirect3DSurface9 * pd3dSurf;
630 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
633 pd3dSurf->GetDC(&hDCMem);
638 hPen = CreatePen(style, static_cast<int>(thickness), colorsGDI[color.
id]);
640 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
641 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
645 SetBkMode(hDCMem, TRANSPARENT);
648 SelectObject(hDCMem, hPen);
657 pd3dSurf->ReleaseDC(hDCMem);
675 void vpD3DRenderer::drawRect(
const vpImagePoint &topLeft,
676 unsigned int width,
unsigned int height,
677 const vpColor &color,
bool fill ,
680 if(topLeft.
get_i()>(int)nbRows-1 || topLeft.
get_j()>(int)nbCols-1|| topLeft.
get_i()+height<0 ||topLeft.
get_j()+width<0)
687 if(pd3dDevice != NULL)
689 D3DLOCKED_RECT d3dLRect;
692 r.top= (LONG)((topLeft.
get_i()>0)? topLeft.
get_i() : 0 );
693 r.left=(LONG)((topLeft.
get_j()>0)? topLeft.
get_j() : 0 );
694 r.bottom=(LONG)((topLeft.
get_i()+height < (int)nbRows) ? topLeft.
get_i()+height : nbRows-1);
695 r.right=(LONG)((topLeft.
get_j()+width < (int)nbCols) ? topLeft.
get_j()+width : nbCols-1);
697 int rectW = r.right - r.left;
698 int rectH = r.bottom - r.top;
701 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
703 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
708 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
709 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
717 if(topLeft.
get_i()>=0)
719 setBufferPixel(buf, pitch, x, y, color);
722 if(topLeft.
get_j()+width < nbCols)
724 setBufferPixel(buf, pitch, x, y, color);
727 if(topLeft.
get_i()+height < nbRows)
729 setBufferPixel(buf, pitch, x, y, color);
732 if(topLeft.
get_j()>=0)
734 setBufferPixel(buf, pitch, x, y, color);
739 if(topLeft.
get_i()>=0 && topLeft.
get_j()+width < nbCols && topLeft.
get_i()+height < nbRows && topLeft.
get_j()>=0)
741 for (x = 0; x<rectW; x++)
743 for (y = 0; y<rectH; y++)
744 setBufferPixel(buf, pitch, x, y, color);
750 if( pd3dText->UnlockRect(0) != D3D_OK)
751 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
759 void vpD3DRenderer::clear(
const vpColor &color)
762 if(pd3dDevice != NULL)
764 D3DLOCKED_RECT d3dLRect;
769 r.bottom =
static_cast<LONG
>( nbRows );
770 r.right =
static_cast<LONG
>( nbCols );
773 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
775 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
780 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
781 long * buf = (
long *) ( d3dLRect.pBits );
785 c = colors[color.
id];
787 c = D3DCOLOR_ARGB(0xFF, color.
R, color.
G, color.
B);
789 long * end = (
long*)((
long)buf + (pitch * nbRows));
793 *buf++ =
static_cast<long>( c );
796 if( pd3dText->UnlockRect(0) != D3D_OK)
797 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
804 void vpD3DRenderer::subDrawCircle(
int i,
int j,
807 unsigned char* buf,
unsigned int pitch,
808 unsigned int maxX,
unsigned int maxY)
811 setBufferPixel(buf, pitch, i , j+y, col, maxX, maxY);
812 setBufferPixel(buf, pitch, i , j-y, col, maxX, maxY);
813 setBufferPixel(buf, pitch, i+y, j, col, maxX, maxY);
814 setBufferPixel(buf, pitch, i-y, j, col, maxX, maxY);
817 setBufferPixel(buf, pitch, i+x,j+y,col, maxX, maxY);
818 setBufferPixel(buf, pitch, i-x,j+y,col, maxX, maxY);
819 setBufferPixel(buf, pitch, i+x,j-y,col, maxX, maxY);
820 setBufferPixel(buf, pitch, i-x,j-y,col, maxX, maxY);
823 setBufferPixel(buf, pitch, i+x,j+y,col, maxX, maxY);
824 setBufferPixel(buf, pitch, i-x,j+y,col, maxX, maxY);
825 setBufferPixel(buf, pitch, i+x,j-y,col, maxX, maxY);
826 setBufferPixel(buf, pitch, i-x,j-y,col, maxX, maxY);
827 setBufferPixel(buf, pitch, i+y,j+x,col, maxX, maxY);
828 setBufferPixel(buf, pitch, i-y,j+x,col, maxX, maxY);
829 setBufferPixel(buf, pitch, i+y,j-x,col, maxX, maxY);
830 setBufferPixel(buf, pitch, i-y,j-x,col, maxX, maxY);
841 void vpD3DRenderer::drawCircle(
const vpImagePoint ¢er,
unsigned int radius,
842 const vpColor &color,
bool ,
unsigned int )
848 if(pd3dDevice != NULL)
850 D3DLOCKED_RECT d3dLRect;
853 int radius_ =
static_cast<int>( radius );
863 unsigned int rectW =
static_cast<unsigned int> ( rec.right - rleft );
864 unsigned int rectH =
static_cast<unsigned int> ( rec.bottom - rtop );
867 if(pd3dText->LockRect(0, &d3dLRect, &rec, 0)!= D3D_OK)
869 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
874 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
875 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
880 int y = static_cast<int>( radius );
881 int p = (3 - (y<<1));
897 p += (((x-y)<<1)+1)<<1;
905 if( pd3dText->UnlockRect(0) != D3D_OK)
906 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
918 void vpD3DRenderer::drawText(
const vpImagePoint &ip,
const char * text,
925 IDirect3DSurface9 * pd3dSurf;
926 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
929 pd3dSurf->GetDC(&hDCMem);
932 SelectObject(hDCMem, hFont);
936 SetTextColor(hDCMem, colorsGDI[color.
id]);
938 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
939 SetTextColor(hDCMem, gdicolor);
943 SetBkMode(hDCMem, TRANSPARENT);
946 int length = (int) strlen(text);
949 GetTextExtentPoint32(hDCMem, text, length, &size);
955 pd3dSurf->ReleaseDC(hDCMem);
972 const vpColor &color,
unsigned int thickness)
974 if(ip.
get_i()<0 || ip.
get_j()<0 || ip.
get_i()>(int)nbRows || ip.
get_j()>(int)nbCols || thickness<=0)
978 if(pd3dDevice != NULL)
980 D3DLOCKED_RECT d3dLRect;
983 thickness = (thickness<size)? thickness : size;
984 int half_size_ =
static_cast<int>( size/2 );
992 rec.bottom= (LONG)(ip.
get_i() + (size/2));
993 rec.right = (LONG)(ip.
get_j() + (size/2));
996 if( pd3dText->LockRect(0, &d3dLRect, &rec, 0) != D3D_OK)
998 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
1003 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
1004 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
1013 unsigned int re = thickness;
1020 for(x=0; x<(rec.right - rec.left); x++)
1021 setBufferPixel(buf, pitch, x, y, color);
1028 y += ( (re&1) != 0u) ? cpt : -cpt;
1041 for(y=0; y<rec.bottom - rec.top; y++)
1042 setBufferPixel(buf, pitch, x, y, color);
1048 x += ( (re&1) != 0) ? cpt : -cpt;
1052 if( pd3dText->UnlockRect(0) != D3D_OK)
1053 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
1068 unsigned int w,
unsigned int h,
unsigned int thickness)
1073 int _h =
static_cast<int>( h );
1079 IDirect3DSurface9 * pd3dSurf;
1080 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
1083 pd3dSurf->GetDC(&hDCMem);
1088 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colorsGDI[color.
id]);
1090 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
1091 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1095 SelectObject(hDCMem, hPen);
1133 pd3dSurf->ReleaseDC(hDCMem);
1135 pd3dSurf->Release();
1161 I.
bitmap[i].
B = imBuffer[k+0];
1162 I.
bitmap[i].
G = imBuffer[k+1];
1163 I.
bitmap[i].
R = imBuffer[k+2];
1164 I.
bitmap[i].
A = imBuffer[k+3];
1178 if(pd3dDevice != NULL)
1182 I.
resize(nbRows, nbCols);
1184 D3DLOCKED_RECT d3dLRect;
1189 r.bottom =
static_cast<LONG
>( nbRows );
1190 r.right =
static_cast<LONG
>( nbCols );
1193 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
1195 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
1200 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
1201 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
1204 TextureToRGBa(I,buf, pitch);
1207 if( pd3dText->UnlockRect(0) != D3D_OK)
1208 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;