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