Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Bruno Renier
35  *
36  *****************************************************************************/
37 
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if ( defined(VISP_HAVE_D3D9) )
43 
44 #ifndef VPD3DRENDERER_HH
45 #define VPD3DRENDERER_HH
46 
47 #include <windows.h>
48 #include <d3dx9.h>
49 #include <visp3/gui/vpWin32Renderer.h>
50 #include <visp3/core/vpDisplayException.h>
51 
52 
53 #include <iostream>
54 
62 class VISP_EXPORT vpD3DRenderer : public vpWin32Renderer
63 {
64 
65  IDirect3D9 * pD3D;
66 
67  //The d3d device we will be working with.
68  IDirect3DDevice9 * pd3dDevice;
69 
70  //Sprite used to render the texture.
71  ID3DXSprite * pSprite;
72 
73  //The system memory texture :
74  //The one we will be drawing on.
75  IDirect3DTexture9 * pd3dText;
76 
77  //The video memory texture :
78  //The one we will use for display.
79  IDirect3DTexture9 * pd3dVideoText;
80 
81  //The texture's width.
82  unsigned int textWidth;
83 
84  //The window's handle.
85  HWND hWnd;
86 
87  //Colors for overlay drawn with d3d directly.
88  unsigned long colors[vpColor::id_unknown];
89 
90  //Colors for overlay drawn with GDI.
91  COLORREF colorsGDI[vpColor::id_unknown];
92 
93  //Font used for text drawing.
94  HFONT hFont;
95 
96 public:
97 
98  bool init(HWND hwnd, unsigned int width, unsigned int height);
99  bool render();
100 
101  vpD3DRenderer();
102  virtual ~vpD3DRenderer();
103 
104  void setImg(const vpImage<vpRGBa>& im);
105  void setImg(const vpImage<unsigned char>& im);
106  void setImgROI(const vpImage<vpRGBa>& im, const vpImagePoint &iP, const unsigned int width, const unsigned int height );
107  void setImgROI(const vpImage<unsigned char>& im, const vpImagePoint &iP, const unsigned int width, const unsigned int height );
108 
109  void setPixel(const vpImagePoint &iP, const vpColor &color);
110 
111  void drawLine(const vpImagePoint &ip1,
112  const vpImagePoint &ip2,
113  const vpColor &color, unsigned int thickness, int style=PS_SOLID);
114 
115  void drawRect(const vpImagePoint &topLeft,
116  unsigned int width, unsigned int height,
117  const vpColor &color, bool fill=false,
118  unsigned int thickness=1);
119 
120  void clear(const vpColor &color);
121 
122  void drawCircle(const vpImagePoint &center, unsigned int radius,
123  const vpColor &color, bool fill=false, unsigned int thickness=1);
124 
125  void drawText(const vpImagePoint &ip, const char * text,
126  const vpColor &color);
127 
128  void drawCross(const vpImagePoint &ip, unsigned int size,
129  const vpColor &color, unsigned int thickness=1);
130 
131  void drawArrow(const vpImagePoint &ip1,
132  const vpImagePoint &ip2,
133  const vpColor &color, unsigned int w,unsigned int h, unsigned int thickness=1);
134 
135  void getImage(vpImage<vpRGBa> &I);
136 
137 
138 
139 private:
140 
141  void initView(float, float);
142 
147  void subDrawCircle(int i, int j,
148  int x, int y,
149  vpColor col, unsigned char* buf,
150  unsigned int pitch, unsigned int maxX, unsigned int maxY);
151 
152  void convert(const vpImage<vpRGBa>& I, unsigned char * imBuffer, unsigned int pitch);
153  void convert(const vpImage<unsigned char>& I, unsigned char * imBuffer, unsigned int pitch);
154  void convertROI(const vpImage<vpRGBa>& I, unsigned char * imBuffer, unsigned int pitch,
155  int i_min, int j_min, int i_max, int j_max);
156  void convertROI(const vpImage<unsigned char>& I, unsigned char * imBuffer, unsigned int pitch,
157  int i_min, int j_min, int i_max, int j_max);
158 
170  inline void setBufferPixel(unsigned char* buf, unsigned int pitch,
171  int x, int y,
172  const vpColor &color,
173  unsigned int maxX, unsigned int maxY)
174  {
175  unsigned long c;
176  if (color.id < vpColor::id_unknown)
177  c = colors[color.id];
178  else {
179  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
180  }
181 
182  if(x>=0 && y>=0 && x<= (int)maxX && y<= (int)maxY)
183  *(unsigned long*)(buf + (y*pitch) + (x<<2)) = c; //colors[color];
184  }
194  inline void setBufferPixel(unsigned char* buf, unsigned int pitch,
195  int x, int y,
196  const vpColor &color)
197  {
198  unsigned long c;
199  if (color.id < vpColor::id_unknown)
200  c = colors[color.id];
201  else {
202  c = D3DCOLOR_ARGB(0xFF, color.R, color.G, color.B);
203  }
204 
205  *(unsigned long*)(buf + (y*pitch) + (x<<2)) = c; //colors[color];
206  }
207 
208  unsigned int supPowerOf2(unsigned int n);
209 
210 };
211 #endif
212 #endif
213 #endif
unsigned char B
Blue component.
Definition: vpRGBa.h:155
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
unsigned char G
Green component.
Definition: vpRGBa.h:154
vpColorIdentifier id
Definition: vpColor.h:152
unsigned char R
Red component.
Definition: vpRGBa.h:153
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88