37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 #include <visp3/core/vpConfig.h>
40 #if ( defined(_WIN32) & defined(VISP_HAVE_D3D9) )
42 #include <visp3/gui/vpD3DRenderer.h>
43 #include <visp3/core/vpColor.h>
44 #include <visp3/core/vpMath.h>
71 vpD3DRenderer::vpD3DRenderer()
156 hFont = CreateFont(18, 0, 0, 0, FW_NORMAL,
false,
false,
false,
157 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
158 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
159 DEFAULT_PITCH | FF_DONTCARE, NULL);
166 vpD3DRenderer::~vpD3DRenderer()
170 if(pd3dDevice != NULL)
171 pd3dDevice->Release();
176 if(pd3dVideoText != NULL)
177 pd3dVideoText->Release();
185 unsigned int vpD3DRenderer::supPowerOf2(
unsigned int n)
193 return static_cast<unsigned int>(1<<(i+1));
203 bool vpD3DRenderer::init(HWND hwnd,
unsigned int width,
unsigned int height)
211 if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
213 "Can't initialize D3D!");
215 D3DDISPLAYMODE d3ddm;
216 if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
218 "Can't get the adapter's display mode!");
221 D3DPRESENT_PARAMETERS d3dpp;
222 ZeroMemory(&d3dpp,
sizeof(d3dpp));
223 d3dpp.BackBufferCount=1;
224 d3dpp.Windowed = TRUE;
225 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
226 d3dpp.BackBufferFormat = d3ddm.Format;
229 if( FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT , D3DDEVTYPE_HAL, hWnd,
230 D3DCREATE_SOFTWARE_VERTEXPROCESSING |
231 D3DCREATE_MULTITHREADED,
232 &d3dpp, &pd3dDevice )))
234 "Can't create the Direct3D device!");
238 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
242 initView((
float)nbCols,(
float)nbRows);
246 textWidth = supPowerOf2( (nbCols>nbRows) ? nbCols : nbRows );
251 if( D3DXCreateTexture(pd3dDevice, textWidth, textWidth, D3DX_DEFAULT, 0,
252 D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM , &pd3dText)
256 "Can't create memory texture!");
260 if( D3DXCreateTexture(pd3dDevice, textWidth, textWidth, D3DX_DEFAULT,
262 D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pd3dVideoText)
266 "Can't create video texture!");
270 if(D3DXCreateSprite(pd3dDevice, &pSprite) != S_OK)
272 "Can't create the texture's sprite!");
287 void vpD3DRenderer::initView(
float WindowWidth,
float WindowHeight)
292 D3DXMatrixOrthoLH(&Ortho2D, WindowWidth, WindowHeight, 0.0f, 1.0f);
293 D3DXMatrixIdentity(&Identity);
295 if( pd3dDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D) != D3D_OK
296 || pd3dDevice->SetTransform(D3DTS_WORLD, &Identity) != D3D_OK
297 || pd3dDevice->SetTransform(D3DTS_VIEW, &Identity) != D3D_OK)
299 "Can't set the view!");
309 void vpRGBaToTexture(
const vpImage<vpRGBa>& I,
unsigned char * imBuffer,
338 unsigned char * imBuffer,
351 imBuffer[k+0] = imBuffer[k+1] = imBuffer[k+2] = I.
bitmap[i];
352 imBuffer[k+3] = 0xFF;
366 if(pd3dDevice != NULL)
368 D3DLOCKED_RECT d3dLRect;
372 r.bottom=
static_cast<signed long>(nbRows);
373 r.right=
static_cast<signed long>(nbCols);
376 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
378 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
383 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
384 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
387 vpRGBaToTexture(im, buf, pitch);
390 if( pd3dText->UnlockRect(0) != D3D_OK)
391 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
400 void vpD3DRenderer::setImgROI(
const vpImage<vpRGBa>& im,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height )
403 if(pd3dDevice != NULL)
405 D3DLOCKED_RECT d3dLRect;
408 r.top=(LONG)iP.
get_v(); r.left=(LONG)iP.
get_u();
409 r.bottom=(LONG)(iP.
get_v()+height-1); r.right=(LONG)(iP.
get_u()+
width);
412 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
414 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
419 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
420 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
423 vpRGBaToTexture(im, buf, pitch);
426 if( pd3dText->UnlockRect(0) != D3D_OK)
427 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
440 if(pd3dDevice != NULL)
442 D3DLOCKED_RECT d3dLRect;
447 r.bottom =
static_cast<LONG
>(nbRows);
448 r.right =
static_cast<LONG
>(nbCols);
451 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
453 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
458 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
459 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
462 vpGreyToTexture(im, buf, pitch);
465 if( pd3dText->UnlockRect(0) != D3D_OK)
466 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
479 if(pd3dDevice != NULL)
481 D3DLOCKED_RECT d3dLRect;
484 r.top=(LONG)iP.
get_v(); r.left=(LONG)iP.
get_u();
485 r.bottom=(LONG)(iP.
get_v()+height-1); r.right=(LONG)(iP.
get_u()+
width);
488 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
490 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
495 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
496 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
499 vpGreyToTexture(im, buf, pitch);
502 if( pd3dText->UnlockRect(0) != D3D_OK)
503 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
513 bool vpD3DRenderer::render()
519 pd3dDevice->BeginScene();
525 r.bottom =
static_cast<LONG
>(nbRows);
526 r.right =
static_cast<LONG
>(nbCols);
530 pd3dDevice->UpdateTexture(pd3dText,pd3dVideoText);
534 #if (D3DX_SDK_VERSION <= 9)
536 pSprite->Draw(pd3dVideoText, &r, NULL, NULL, NULL, NULL, 0xFFFFFFFF );
539 pSprite->Draw(pd3dVideoText, &r, NULL, NULL, 0xFFFFFFFF );
544 pd3dDevice->EndScene();
546 pd3dDevice->Present( NULL, NULL, NULL, NULL );
567 if(pd3dDevice != NULL)
569 D3DLOCKED_RECT d3dLRect;
573 r.top=(LONG)iP.
get_i();
574 r.left=(LONG)iP.
get_j();
575 r.bottom=(LONG)iP.
get_i()+1;
576 r.right=(LONG)iP.
get_j()+1;
579 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
581 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
586 unsigned int pitch =
static_cast<unsigned int>( d3dLRect.Pitch );
587 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
590 setBufferPixel(buf, pitch, 0, 0,color);
593 if( pd3dText->UnlockRect(0) != D3D_OK)
594 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
609 unsigned int thickness,
int style)
618 if(pd3dDevice != NULL)
625 IDirect3DSurface9 * pd3dSurf;
626 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
629 pd3dSurf->GetDC(&hDCMem);
634 hPen = CreatePen(style, static_cast<int>(thickness), colorsGDI[color.
id]);
636 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
637 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
641 SetBkMode(hDCMem, TRANSPARENT);
644 SelectObject(hDCMem, hPen);
653 pd3dSurf->ReleaseDC(hDCMem);
671 void vpD3DRenderer::drawRect(
const vpImagePoint &topLeft,
672 unsigned int width,
unsigned int height,
673 const vpColor &color,
bool fill ,
676 if(topLeft.
get_i()>(int)nbRows-1 || topLeft.
get_j()>(int)nbCols-1|| topLeft.
get_i()+height<0 ||topLeft.
get_j()+width<0)
683 if(pd3dDevice != NULL)
685 D3DLOCKED_RECT d3dLRect;
688 r.top= (LONG)((topLeft.
get_i()>0)? topLeft.
get_i() : 0 );
689 r.left=(LONG)((topLeft.
get_j()>0)? topLeft.
get_j() : 0 );
690 r.bottom=(LONG)((topLeft.
get_i()+height < (int)nbRows) ? topLeft.
get_i()+height : nbRows-1);
691 r.right=(LONG)((topLeft.
get_j()+width < (int)nbCols) ? topLeft.
get_j()+width : nbCols-1);
693 int rectW = r.right - r.left;
694 int rectH = r.bottom - r.top;
697 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
699 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
704 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
705 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
713 if(topLeft.
get_i()>=0)
715 setBufferPixel(buf, pitch, x, y, color);
718 if(topLeft.
get_j()+width < nbCols)
720 setBufferPixel(buf, pitch, x, y, color);
723 if(topLeft.
get_i()+height < nbRows)
725 setBufferPixel(buf, pitch, x, y, color);
728 if(topLeft.
get_j()>=0)
730 setBufferPixel(buf, pitch, x, y, color);
735 if(topLeft.
get_i()>=0 && topLeft.
get_j()+width < nbCols && topLeft.
get_i()+height < nbRows && topLeft.
get_j()>=0)
737 for (x = 0; x<rectW; x++)
739 for (y = 0; y<rectH; y++)
740 setBufferPixel(buf, pitch, x, y, color);
746 if( pd3dText->UnlockRect(0) != D3D_OK)
747 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
755 void vpD3DRenderer::clear(
const vpColor &color)
758 if(pd3dDevice != NULL)
760 D3DLOCKED_RECT d3dLRect;
765 r.bottom =
static_cast<LONG
>( nbRows );
766 r.right =
static_cast<LONG
>( nbCols );
769 if(pd3dText->LockRect(0, &d3dLRect, &r, 0)!= D3D_OK)
771 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
776 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
777 long * buf = (
long *) ( d3dLRect.pBits );
781 c = colors[color.
id];
783 c = D3DCOLOR_ARGB(0xFF, color.
R, color.
G, color.
B);
785 long * end = (
long*)((
long)buf + (pitch * nbRows));
789 *buf++ =
static_cast<long>( c );
792 if( pd3dText->UnlockRect(0) != D3D_OK)
793 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
800 void vpD3DRenderer::subDrawCircle(
int i,
int j,
803 unsigned char* buf,
unsigned int pitch,
804 unsigned int maxX,
unsigned int maxY)
807 setBufferPixel(buf, pitch, i , j+y, col, maxX, maxY);
808 setBufferPixel(buf, pitch, i , j-y, col, maxX, maxY);
809 setBufferPixel(buf, pitch, i+y, j, col, maxX, maxY);
810 setBufferPixel(buf, pitch, i-y, j, col, maxX, maxY);
813 setBufferPixel(buf, pitch, i+x,j+y,col, maxX, maxY);
814 setBufferPixel(buf, pitch, i-x,j+y,col, maxX, maxY);
815 setBufferPixel(buf, pitch, i+x,j-y,col, maxX, maxY);
816 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);
821 setBufferPixel(buf, pitch, i+x,j-y,col, maxX, maxY);
822 setBufferPixel(buf, pitch, i-x,j-y,col, maxX, maxY);
823 setBufferPixel(buf, pitch, i+y,j+x,col, maxX, maxY);
824 setBufferPixel(buf, pitch, i-y,j+x,col, maxX, maxY);
825 setBufferPixel(buf, pitch, i+y,j-x,col, maxX, maxY);
826 setBufferPixel(buf, pitch, i-y,j-x,col, maxX, maxY);
837 void vpD3DRenderer::drawCircle(
const vpImagePoint ¢er,
unsigned int radius,
838 const vpColor &color,
bool ,
unsigned int )
844 if(pd3dDevice != NULL)
846 D3DLOCKED_RECT d3dLRect;
849 int radius_ =
static_cast<int>( radius );
859 unsigned int rectW =
static_cast<unsigned int> ( rec.right - rleft );
860 unsigned int rectH =
static_cast<unsigned int> ( rec.bottom - rtop );
863 if(pd3dText->LockRect(0, &d3dLRect, &rec, 0)!= D3D_OK)
865 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
870 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
871 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
876 int y = static_cast<int>( radius );
877 int p = (3 - (y<<1));
893 p += (((x-y)<<1)+1)<<1;
901 if( pd3dText->UnlockRect(0) != D3D_OK)
902 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
914 void vpD3DRenderer::drawText(
const vpImagePoint &ip,
const char * text,
921 IDirect3DSurface9 * pd3dSurf;
922 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
925 pd3dSurf->GetDC(&hDCMem);
928 SelectObject(hDCMem, hFont);
932 SetTextColor(hDCMem, colorsGDI[color.
id]);
934 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
935 SetTextColor(hDCMem, gdicolor);
939 SetBkMode(hDCMem, TRANSPARENT);
942 int length = (int) strlen(text);
945 GetTextExtentPoint32(hDCMem, text, length, &size);
951 pd3dSurf->ReleaseDC(hDCMem);
968 const vpColor &color,
unsigned int thickness)
970 if(ip.
get_i()<0 || ip.
get_j()<0 || ip.
get_i()>(int)nbRows || ip.
get_j()>(int)nbCols || thickness<=0)
974 if(pd3dDevice != NULL)
976 D3DLOCKED_RECT d3dLRect;
979 thickness = (thickness<size)? thickness : size;
980 int half_size_ =
static_cast<int>( size/2 );
988 rec.bottom= (LONG)(ip.
get_i() + (size/2));
989 rec.right = (LONG)(ip.
get_j() + (size/2));
992 if( pd3dText->LockRect(0, &d3dLRect, &rec, 0) != D3D_OK)
994 vpCERROR<<
"D3D : Couldn't lock the texture!"<<std::endl;
999 unsigned int pitch =
static_cast<unsigned int>(d3dLRect.Pitch);
1000 unsigned char * buf = (
unsigned char *) d3dLRect.pBits;
1009 unsigned int re = thickness;
1016 for(x=0; x<(rec.right - rec.left); x++)
1017 setBufferPixel(buf, pitch, x, y, color);
1024 y += ( (re&1) != 0u) ? cpt : -cpt;
1037 for(y=0; y<rec.bottom - rec.top; y++)
1038 setBufferPixel(buf, pitch, x, y, color);
1044 x += ( (re&1) != 0) ? cpt : -cpt;
1048 if( pd3dText->UnlockRect(0) != D3D_OK)
1049 vpCERROR<<
"D3D : Couldn't unlock the texture!"<<std::endl;
1064 unsigned int w,
unsigned int h,
unsigned int thickness)
1069 int _h =
static_cast<int>( h );
1075 IDirect3DSurface9 * pd3dSurf;
1076 pd3dText->GetSurfaceLevel(0, &pd3dSurf);
1079 pd3dSurf->GetDC(&hDCMem);
1084 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colorsGDI[color.
id]);
1086 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
1087 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1091 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;
1212 #elif !defined(VISP_BUILD_SHARED_LIBS)
1214 void dummy_vpD3DRenderer() {};
unsigned int getWidth() const
unsigned char B
Blue component.
Type * bitmap
points toward the bitmap
static const vpColor black
static const vpColor darkRed
Class to define colors available for display functionnalities.
static const vpColor lightGray
static const vpColor darkBlue
unsigned char G
Green component.
static const vpColor green
static int round(const double x)
static const vpColor lightRed
static const vpColor orange
void set_i(const double ii)
static const vpColor cyan
static const vpColor lightGreen
static double sqr(double x)
unsigned char A
Additionnal component.
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
static const vpColor gray
static const vpColor darkGray
void set_j(const double jj)
Error that can be emited by the vpDisplay class and its derivates.
unsigned char R
Red component.
unsigned int getHeight() const
static const vpColor darkGreen
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static const vpColor yellow
static const vpColor lightBlue
static const vpColor purple
static const vpColor white
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static const vpColor blue