Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpD3DRenderer.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 VP_D3D_RENDERER_H
35 #define VP_D3D_RENDERER_H
36 
37 #include <visp3/core/vpConfig.h>
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 #if (defined(VISP_HAVE_D3D9))
41 
42 // Include WinSock2.h before windows.h to ensure that winsock.h is not
43 // included by windows.h since winsock.h and winsock2.h are incompatible
44 #include <WinSock2.h>
45 #include <d3dx9.h>
46 #include <visp3/core/vpDisplayException.h>
47 #include <visp3/gui/vpWin32Renderer.h>
48 #include <windows.h>
49 
50 #include <iostream>
51 
53 
60 class VISP_EXPORT vpD3DRenderer : public vpWin32Renderer
61 {
62  IDirect3D9 *pD3D;
63 
64  // The d3d device we will be working with.
65  IDirect3DDevice9 *pd3dDevice;
66 
67  // Sprite used to render the texture.
68  ID3DXSprite *pSprite;
69 
70  // The system memory texture :
71  // The one we will be drawing on.
72  IDirect3DTexture9 *pd3dText;
73 
74  // The video memory texture :
75  // The one we will use for display.
76  IDirect3DTexture9 *pd3dVideoText;
77 
78  // The texture's width.
79  unsigned int textWidth;
80 
81  // The window's handle.
82  HWND hWnd;
83 
84  // Colors for overlay drawn with d3d directly.
85  unsigned long colors[vpColor::id_unknown];
86 
87  // Colors for overlay drawn with GDI.
88  COLORREF colorsGDI[vpColor::id_unknown];
89 
90  // Font used for text drawing.
91  HFONT hFont;
92 
93 public:
94  bool init(HWND hwnd, unsigned int width, unsigned int height);
95  bool render();
96 
97  vpD3DRenderer();
98  virtual ~vpD3DRenderer() VP_OVERRIDE;
99 
100  void setImg(const vpImage<vpRGBa> &im);
101  void setImg(const vpImage<unsigned char> &im);
102  void setImgROI(const vpImage<vpRGBa> &im, const vpImagePoint &iP, unsigned int width, unsigned int height);
103  void setImgROI(const vpImage<unsigned char> &im, const vpImagePoint &iP, unsigned int width, unsigned int height);
104 
105  void setPixel(const vpImagePoint &iP, const vpColor &color);
106 
107  void drawLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness,
108  int style = PS_SOLID);
109 
110  void drawRect(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color,
111  bool fill = false, unsigned int thickness = 1);
112 
113  void clear(const vpColor &color);
114 
115  void drawCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill = false,
116  unsigned int thickness = 1);
117 
118  void drawText(const vpImagePoint &ip, const char *text, const vpColor &color);
119 
120  void drawCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness = 1);
121 
122  void drawArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int w, unsigned int h,
123  unsigned int thickness = 1);
124 
125  void getImage(vpImage<vpRGBa> &I) VP_OVERRIDE;
126 
127 private:
128  void initView(float, float);
129 
134  void subDrawCircle(int i, int j, int x, int y, vpColor col, unsigned char *buf, unsigned int pitch, unsigned int maxX,
135  unsigned int maxY);
136 
137  void convert(const vpImage<vpRGBa> &I, unsigned char *imBuffer, unsigned int pitch);
138  void convert(const vpImage<unsigned char> &I, unsigned char *imBuffer, unsigned int pitch);
139  void convertROI(const vpImage<vpRGBa> &I, unsigned char *imBuffer, unsigned int pitch, int i_min, int j_min,
140  int i_max, int j_max);
141  void convertROI(const vpImage<unsigned char> &I, unsigned char *imBuffer, unsigned int pitch, int i_min, int j_min,
142  int i_max, int j_max);
143 
156  inline void setBufferPixel(unsigned char *buf, unsigned int pitch, int x, int y, const vpColor &color,
157  unsigned int maxX, unsigned int maxY)
158  {
159  unsigned long c;
160  if (color.id < vpColor::id_unknown)
161  c = colors[color.id];
162  else {
163  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
164  }
165 
166  if (x >= 0 && y >= 0 && x <= (int)maxX && y <= (int)maxY)
167  *(unsigned long *)(buf + (y * pitch) + (x << 2)) = c; // colors[color];
168  }
178  inline void setBufferPixel(unsigned char *buf, unsigned int pitch, int x, int y, const vpColor &color)
179  {
180  unsigned long c;
181  if (color.id < vpColor::id_unknown)
182  c = colors[color.id];
183  else {
184  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
185  }
186 
187  *(unsigned long *)(buf + (y * pitch) + (x << 2)) = c; // colors[color];
188  }
189 
190  unsigned int supPowerOf2(unsigned int n);
191 };
192 #endif
193 
194 END_VISP_NAMESPACE
195 
196 #endif
197 #endif
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
vpColorIdentifier id
Definition: vpColor.h:206
@ id_unknown
Definition: vpColor.h:199
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:131
Definition: vpRGBa.h:65
unsigned char B
Blue component.
Definition: vpRGBa.h:169
unsigned char R
Red component.
Definition: vpRGBa.h:167
unsigned char G
Green component.
Definition: vpRGBa.h:168