Visual Servoing Platform  version 3.1.0
vpD3DRenderer.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * D3D renderer for windows 32 display
33  *
34  * Authors:
35  * Bruno Renier
36  *
37  *****************************************************************************/
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 
41 #include <visp3/core/vpConfig.h>
42 
43 #if (defined(VISP_HAVE_D3D9))
44 
45 #ifndef VPD3DRENDERER_HH
46 #define VPD3DRENDERER_HH
47 
48 // Include WinSock2.h before windows.h to ensure that winsock.h is not
49 // included by windows.h since winsock.h and winsock2.h are incompatible
50 #include <WinSock2.h>
51 #include <d3dx9.h>
52 #include <visp3/core/vpDisplayException.h>
53 #include <visp3/gui/vpWin32Renderer.h>
54 #include <windows.h>
55 
56 #include <iostream>
57 
65 class VISP_EXPORT vpD3DRenderer : public vpWin32Renderer
66 {
67 
68  IDirect3D9 *pD3D;
69 
70  // The d3d device we will be working with.
71  IDirect3DDevice9 *pd3dDevice;
72 
73  // Sprite used to render the texture.
74  ID3DXSprite *pSprite;
75 
76  // The system memory texture :
77  // The one we will be drawing on.
78  IDirect3DTexture9 *pd3dText;
79 
80  // The video memory texture :
81  // The one we will use for display.
82  IDirect3DTexture9 *pd3dVideoText;
83 
84  // The texture's width.
85  unsigned int textWidth;
86 
87  // The window's handle.
88  HWND hWnd;
89 
90  // Colors for overlay drawn with d3d directly.
91  unsigned long colors[vpColor::id_unknown];
92 
93  // Colors for overlay drawn with GDI.
94  COLORREF colorsGDI[vpColor::id_unknown];
95 
96  // Font used for text drawing.
97  HFONT hFont;
98 
99 public:
100  bool init(HWND hwnd, unsigned int width, unsigned int height);
101  bool render();
102 
103  vpD3DRenderer();
104  virtual ~vpD3DRenderer();
105 
106  void setImg(const vpImage<vpRGBa> &im);
107  void setImg(const vpImage<unsigned char> &im);
108  void setImgROI(const vpImage<vpRGBa> &im, const vpImagePoint &iP, const unsigned int width,
109  const unsigned int height);
110  void setImgROI(const vpImage<unsigned char> &im, const vpImagePoint &iP, const unsigned int width,
111  const unsigned int height);
112 
113  void setPixel(const vpImagePoint &iP, const vpColor &color);
114 
115  void drawLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness,
116  int style = PS_SOLID);
117 
118  void drawRect(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color,
119  bool fill = false, unsigned int thickness = 1);
120 
121  void clear(const vpColor &color);
122 
123  void drawCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill = false,
124  unsigned int thickness = 1);
125 
126  void drawText(const vpImagePoint &ip, const char *text, const vpColor &color);
127 
128  void drawCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness = 1);
129 
130  void drawArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int w, unsigned int h,
131  unsigned int thickness = 1);
132 
133  void getImage(vpImage<vpRGBa> &I);
134 
135 private:
136  void initView(float, float);
137 
142  void subDrawCircle(int i, int j, int x, int y, vpColor col, unsigned char *buf, unsigned int pitch, unsigned int maxX,
143  unsigned int maxY);
144 
145  void convert(const vpImage<vpRGBa> &I, unsigned char *imBuffer, unsigned int pitch);
146  void convert(const vpImage<unsigned char> &I, unsigned char *imBuffer, unsigned int pitch);
147  void convertROI(const vpImage<vpRGBa> &I, unsigned char *imBuffer, unsigned int pitch, int i_min, int j_min,
148  int i_max, int j_max);
149  void convertROI(const vpImage<unsigned char> &I, unsigned char *imBuffer, unsigned int pitch, int i_min, int j_min,
150  int i_max, int j_max);
151 
164  inline void setBufferPixel(unsigned char *buf, unsigned int pitch, int x, int y, const vpColor &color,
165  unsigned int maxX, unsigned int maxY)
166  {
167  unsigned long c;
168  if (color.id < vpColor::id_unknown)
169  c = colors[color.id];
170  else {
171  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
172  }
173 
174  if (x >= 0 && y >= 0 && x <= (int)maxX && y <= (int)maxY)
175  *(unsigned long *)(buf + (y * pitch) + (x << 2)) = c; // colors[color];
176  }
186  inline void setBufferPixel(unsigned char *buf, unsigned int pitch, int x, int y, const vpColor &color)
187  {
188  unsigned long c;
189  if (color.id < vpColor::id_unknown)
190  c = colors[color.id];
191  else {
192  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
193  }
194 
195  *(unsigned long *)(buf + (y * pitch) + (x << 2)) = c; // colors[color];
196  }
197 
198  unsigned int supPowerOf2(unsigned int n);
199 };
200 #endif
201 #endif
202 #endif
unsigned char B
Blue component.
Definition: vpRGBa.h:150
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
unsigned char G
Green component.
Definition: vpRGBa.h:149
vpColorIdentifier id
Definition: vpColor.h:169
unsigned char R
Red component.
Definition: vpRGBa.h:148
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88