GteOverlayEffect.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
10 using namespace gte;
11 
12 
13 OverlayEffect::OverlayEffect(std::shared_ptr<ProgramFactory> const& factory,
14  int windowWidth, int windowHeight, int textureWidth, int textureHeight,
15  SamplerState::Filter filter, SamplerState::Mode mode0, SamplerState::Mode mode1,
16  bool useColorPShader)
17  :
18  mWindowWidth(static_cast<float>(windowWidth)),
19  mWindowHeight(static_cast<float>(windowHeight))
20 {
21  Initialize(windowWidth, windowHeight, textureWidth, textureHeight);
22 
23  mFactoryAPI = factory->GetAPI();
24  std::string psSource =
25  (useColorPShader ? *msPSColorSource[mFactoryAPI] : *msPSGraySource[mFactoryAPI]);
26 
27  mProgram = factory->CreateFromSources(*msVSSource[mFactoryAPI], psSource, "");
28  if (mProgram)
29  {
30  std::shared_ptr<SamplerState> sampler =
31  std::make_shared<SamplerState>();
32  sampler->filter = filter;
33  sampler->mode[0] = mode0;
34  sampler->mode[1] = mode1;
35  mProgram->GetPShader()->Set("imageSampler", sampler);
36  mEffect = std::make_shared<VisualEffect>(mProgram);
37  }
38 }
39 
40 OverlayEffect::OverlayEffect(std::shared_ptr<ProgramFactory> const& factory,
41  int windowWidth, int windowHeight, int textureWidth, int textureHeight,
42  std::string const& psSource)
43  :
44  mWindowWidth(static_cast<float>(windowWidth)),
45  mWindowHeight(static_cast<float>(windowHeight))
46 {
47  mFactoryAPI = factory->GetAPI();
48  Initialize(windowWidth, windowHeight, textureWidth, textureHeight);
49 
50  mProgram = factory->CreateFromSources(*msVSSource[mFactoryAPI], psSource, "");
51  if (mProgram)
52  {
53  mEffect = std::make_shared<VisualEffect>(mProgram);
54  }
55 }
56 
57 void OverlayEffect::SetTextureRectangle(std::array<int, 4> const& rectangle)
58 {
59  mTextureRectangle = rectangle;
61 }
62 
63 void OverlayEffect::SetRectangles(std::array<int, 4> const& overlayRectangle,
64  std::array<int, 4> const& textureRectangle)
65 {
66  mOverlayRectangle = overlayRectangle;
67  mTextureRectangle = textureRectangle;
69 }
70 
71 bool OverlayEffect::Contains(int x, int y) const
72 {
73  return mOverlayRectangle[0] <= x
75  && mOverlayRectangle[1] <= y
76  && y < mOverlayRectangle[1] + mOverlayRectangle[3];
77 }
78 
79 void OverlayEffect::SetTexture(std::shared_ptr<Texture2> const& texture)
80 {
81  if (texture)
82  {
83 #if defined(GTE_DEV_OPENGL)
84  mEffect->GetPixelShader()->Set("imageSampler", texture);
85 #else
86  mEffect->GetPixelShader()->Set("imageTexture", texture);
87 #endif
88  }
89 }
90 
91 void OverlayEffect::SetTexture(std::string const& textureName,
92  std::shared_ptr<Texture2> const& texture)
93 {
94  if (texture)
95  {
96  mEffect->GetPixelShader()->Set(textureName, texture);
97  }
98 }
99 
100 void OverlayEffect::Initialize(int windowWidth, int windowHeight,
101  int textureWidth, int textureHeight)
102 {
103  if (windowWidth <= 0 || windowHeight <= 0
104  || textureWidth <= 0 || textureHeight <= 0)
105  {
106  LogError("Invalid input rectangle.");
107 
108  // Use dummy sizes.
109  windowWidth = 1;
110  windowHeight = 1;
111  textureWidth = 1;
112  textureHeight = 1;
113  }
114 
115  mInvTextureWidth = 1.0f/static_cast<float>(textureWidth);
116  mInvTextureHeight = 1.0f/static_cast<float>(textureHeight);
117 
118  mOverlayRectangle[0] = 0;
119  mOverlayRectangle[1] = 0;
120  mOverlayRectangle[2] = windowWidth;
121  mOverlayRectangle[3] = windowHeight;
122 
123  mTextureRectangle[0] = 0;
124  mTextureRectangle[1] = 0;
125  mTextureRectangle[2] = textureWidth;
126  mTextureRectangle[3] = textureHeight;
127 
128  // Create the vertex buffer.
129  VertexFormat vformat;
130  vformat.Bind(VA_POSITION, DF_R32G32_FLOAT, 0);
131  vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0);
132  mVBuffer = std::make_shared<VertexBuffer>(vformat, 4);
135 
136  // Create the index buffer.
137  mIBuffer = std::make_shared<IndexBuffer>(IP_TRIMESH, 2,
138  sizeof(unsigned int));
139  unsigned int* indices = mIBuffer->Get<unsigned int>();
140  indices[0] = 0; indices[1] = 2; indices[2] = 3;
141  indices[3] = 0; indices[4] = 3; indices[5] = 1;
142 }
143 
145 {
146  // Convert to normalized coordinates.
147  float invWindowWidth = 1.0f/mWindowWidth;
148  float invWindowHeight = 1.0f/mWindowHeight;
149  float px = static_cast<float>(mOverlayRectangle[0])*invWindowWidth;
150  float py = static_cast<float>(mOverlayRectangle[1])*invWindowHeight;
151  float pw = static_cast<float>(mOverlayRectangle[2])*invWindowWidth;
152  float ph = static_cast<float>(mOverlayRectangle[3])*invWindowHeight;
153 
154  float tx = static_cast<float>(mTextureRectangle[0])*mInvTextureWidth;
155  float ty = static_cast<float>(mTextureRectangle[1])*mInvTextureHeight;
156  float tw = static_cast<float>(mTextureRectangle[2])*mInvTextureWidth;
157  float th = static_cast<float>(mTextureRectangle[3])*mInvTextureHeight;
158 
159  Vertex* vertex = mVBuffer->Get<Vertex>();
160  vertex[0].position = { px, py };
161  vertex[0].tcoord = { tx, ty };
162  vertex[1].position = { px + pw, py };
163  vertex[1].tcoord = { tx + tw, ty };
164  vertex[2].position = { px, py + ph };
165  vertex[2].tcoord = { tx, ty + th };
166  vertex[3].position = { px + pw, py + ph };
167  vertex[3].tcoord = { tx + tw, ty + th };
168 }
169 
170 
172 "layout(location = 0) in vec3 modelPosition;\n"
173 "layout(location = 1) in vec2 modelTCoord;\n"
174 "layout(location = 0) out vec2 vertexTCoord;\n"
175 "\n"
176 "void main()\n"
177 "{\n"
178 " vertexTCoord = modelTCoord;\n"
179 " gl_Position.x = 2.0f*modelPosition.x - 1.0f;\n"
180 " gl_Position.y = -2.0f*modelPosition.y + 1.0f;\n"
181 " gl_Position.z = -1.0f;\n"
182 " gl_Position.w = 1.0f;\n"
183 "}\n";
184 
186 "uniform sampler2D imageSampler;\n"
187 "\n"
188 "layout(location = 0) in vec2 vertexTCoord;\n"
189 "layout(location = 0) out vec4 pixelColor;\n"
190 "\n"
191 "void main()\n"
192 "{\n"
193 " pixelColor = texture(imageSampler, vertexTCoord);\n"
194 "}\n";
195 
197 "uniform sampler2D imageSampler;\n"
198 "\n"
199 "layout(location = 0) in vec2 vertexTCoord;\n"
200 "layout(location = 0) out vec4 pixelColor;\n"
201 "\n"
202 "void main()\n"
203 "{\n"
204 " float gray = texture(imageSampler, vertexTCoord).r;\n"
205 " pixelColor = vec4(gray, gray, gray, 1.0f);\n"
206 "}\n";
207 
209 "struct VS_INPUT\n"
210 "{\n"
211 " float2 modelPosition : POSITION;\n"
212 " float2 modelTCoord : TEXCOORD0;\n"
213 "};\n"
214 "\n"
215 "struct VS_OUTPUT\n"
216 "{\n"
217 " float2 vertexTCoord : TEXCOORD0;\n"
218 " float4 clipPosition : SV_POSITION;\n"
219 "};\n"
220 "\n"
221 "VS_OUTPUT VSMain (VS_INPUT input)\n"
222 "{\n"
223 " VS_OUTPUT output;\n"
224 " output.clipPosition.x = 2.0f*input.modelPosition.x - 1.0f;\n"
225 " output.clipPosition.y = -2.0f*input.modelPosition.y + 1.0f;\n"
226 " output.clipPosition.z = 0.0f;\n"
227 " output.clipPosition.w = 1.0f;\n"
228 " output.vertexTCoord = input.modelTCoord;\n"
229 " return output;\n"
230 "}\n";
231 
233 "Texture2D<float4> imageTexture;\n"
234 "SamplerState imageSampler;\n"
235 "\n"
236 "struct PS_INPUT\n"
237 "{\n"
238 " float2 vertexTCoord : TEXCOORD0;\n"
239 "};\n"
240 "\n"
241 "struct PS_OUTPUT\n"
242 "{\n"
243 " float4 pixelColor0 : SV_TARGET0;\n"
244 "};\n"
245 "\n"
246 "PS_OUTPUT PSMain(PS_INPUT input)\n"
247 "{\n"
248 " PS_OUTPUT output;\n"
249 " output.pixelColor0 = imageTexture.Sample(imageSampler, input.vertexTCoord);\n"
250 " return output;\n"
251 "}\n";
252 
254 "Texture2D<float> imageTexture;\n"
255 "SamplerState imageSampler;\n"
256 "\n"
257 "struct PS_INPUT\n"
258 "{\n"
259 " float2 vertexTCoord : TEXCOORD0;\n"
260 "};\n"
261 "\n"
262 "struct PS_OUTPUT\n"
263 "{\n"
264 " float4 pixelColor0 : SV_TARGET0;\n"
265 "};\n"
266 "\n"
267 "PS_OUTPUT PSMain(PS_INPUT input)\n"
268 "{\n"
269 " PS_OUTPUT output;\n"
270 " float gray = imageTexture.Sample(imageSampler, input.vertexTCoord);\n"
271 " output.pixelColor0 = float4(gray, gray, gray, 1.0f);\n"
272 " return output;\n"
273 "}\n";
274 
276 {
279 };
280 
282 {
285 };
286 
288 {
291 };
std::array< int, 4 > mTextureRectangle
DYNAMIC_UPDATE
Definition: GteResource.h:42
static std::string const msGLSLVSSource
void SetTextureRectangle(std::array< int, 4 > const &rectangle)
static std::string const * msPSGraySource[ProgramFactory::PF_NUM_API]
VA_TEXCOORD
std::shared_ptr< VertexBuffer > mVBuffer
static std::string const * msVSSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< VisualProgram > mProgram
IP_TRIMESH
bool Bind(VASemantic semantic, DFType type, unsigned int unit)
GLuint sampler
Definition: glcorearb.h:1651
GLint GLenum GLint x
Definition: glcorearb.h:404
static std::string const msHLSLVSSource
std::shared_ptr< VisualEffect > mEffect
void SetTexture(std::shared_ptr< Texture2 > const &texture)
GLbyte ty
Definition: glext.h:6341
VA_POSITION
static std::string const * msPSColorSource[ProgramFactory::PF_NUM_API]
void SetRectangles(std::array< int, 4 > const &overlayRectangle, std::array< int, 4 > const &textureRectangle)
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
GLsizei GLenum const void * indices
Definition: glcorearb.h:401
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
#define LogError(message)
Definition: GteLogger.h:92
GLuint texture
Definition: glcorearb.h:410
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1292
std::array< int, 4 > mOverlayRectangle
static std::string const msHLSLPSColorSource
OverlayEffect(std::shared_ptr< ProgramFactory > const &factory, int windowWidth, int windowHeight, int textureWidth, int textureHeight, SamplerState::Filter filter, SamplerState::Mode mode0, SamplerState::Mode mode1, bool useColorPShader)
DF_R32G32_FLOAT
Definition: GteDataFormat.h:20
static std::string const msHLSLPSGraySource
std::shared_ptr< IndexBuffer > mIBuffer
static std::string const msGLSLPSColorSource
bool Contains(int x, int y) const
static std::string const msGLSLPSGraySource
void Initialize(int windowWidth, int windowHeight, int textureWidth, int textureHeight)
GLint y
Definition: glcorearb.h:98


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01