GteTextEffect.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 #include <Graphics/GteTextEffect.h>
11 using namespace gte;
12 
13 TextEffect::TextEffect(std::shared_ptr<ProgramFactory> const& factory,
14  std::shared_ptr<Texture2> const& texture)
15 {
16  auto api = factory->GetAPI();
17  mProgram = factory->CreateFromSources(*msVSSource[api], *msPSSource[api], "");
18  if (mProgram)
19  {
20  mTranslate = std::make_shared<ConstantBuffer>(sizeof(Vector2<float>), true);
21  mColor = std::make_shared<ConstantBuffer>(sizeof(Vector4<float>), true);
22  mSamplerState = std::make_shared<SamplerState>();
23 
24  SetTranslate(0.0f, 0.0f);
25  mProgram->GetVShader()->Set("Translate", mTranslate);
26 
27  SetColor({ 0.0f, 0.0f, 0.0f, 0.0f });
28  mProgram->GetPShader()->Set("TextColor", mColor);
29 #if defined(GTE_DEV_OPENGL)
30  mProgram->GetPShader()->Set("baseSampler", texture);
31 #else
32  mProgram->GetPShader()->Set("baseTexture", texture);
33 #endif
34  mProgram->GetPShader()->Set("baseSampler", mSamplerState);
35  }
36 }
37 
38 std::shared_ptr<ConstantBuffer> const& TextEffect::GetTranslate() const
39 {
40  return mTranslate;
41 }
42 
43 std::shared_ptr<ConstantBuffer> const& TextEffect::GetColor() const
44 {
45  return mColor;
46 }
47 
48 void TextEffect::SetTranslate(float x, float y)
49 {
50  float* data = mTranslate->Get<float>();
51  data[0] = x;
52  data[1] = y;
53 }
54 
56 {
58  *data = color;
59 }
60 
61 
63 "uniform Translate\n"
64 "{\n"
65 " vec2 translate;\n"
66 "};\n"
67 "\n"
68 "layout(location = 0) in vec2 modelPosition;\n"
69 "layout(location = 1) in vec2 modelTCoord;\n"
70 "layout(location = 0) out vec2 vertexTCoord;\n"
71 "\n"
72 "void main()\n"
73 "{\n"
74 " vertexTCoord = modelTCoord;\n"
75 " gl_Position.x = 2.0f * modelPosition.x - 1.0f + 2.0f * translate.x;\n"
76 " gl_Position.y = 2.0f * modelPosition.y - 1.0f + 2.0f * translate.y;\n"
77 " gl_Position.z = -1.0f;\n"
78 " gl_Position.w = 1.0f;\n"
79 "}\n";
80 
82 "uniform TextColor\n"
83 "{\n"
84 " vec4 textColor;\n"
85 "};\n"
86 "\n"
87 "layout(location = 0) in vec2 vertexTCoord;\n"
88 "layout(location = 0) out vec4 pixelColor;\n"
89 "\n"
90 "uniform sampler2D baseSampler;\n"
91 "\n"
92 "void main()\n"
93 "{\n"
94 " float bitmapAlpha = texture(baseSampler, vertexTCoord).r;\n"
95 " if (bitmapAlpha > 0.5f)\n"
96 " {\n"
97 " discard;\n"
98 " }\n"
99 " pixelColor = textColor;\n"
100 "}\n";
101 
103 "cbuffer Translate\n"
104 "{\n"
105 " float2 translate;\n"
106 "};\n"
107 "struct VS_INPUT\n"
108 "{\n"
109 " float2 modelPosition : POSITION;\n"
110 " float2 modelTCoord : TEXCOORD0;\n"
111 "};\n"
112 "\n"
113 "struct VS_OUTPUT\n"
114 "{\n"
115 " float2 vertexTCoord : TEXCOORD0;\n"
116 " float4 clipPosition : SV_POSITION;\n"
117 "};\n"
118 "\n"
119 "VS_OUTPUT VSMain (VS_INPUT input)\n"
120 "{\n"
121 " VS_OUTPUT output;\n"
122 " output.vertexTCoord = input.modelTCoord;\n"
123 " output.clipPosition.x = 2.0f*input.modelPosition.x - 1.0f + 2.0f*translate.x;\n"
124 " output.clipPosition.y = 2.0f*input.modelPosition.y - 1.0f + 2.0f*translate.y;\n"
125 " output.clipPosition.z = 0.0f;\n"
126 " output.clipPosition.w = 1.0f;\n"
127 " return output;\n"
128 "}\n"
129 "\n"
130 "cbuffer TextColor\n"
131 "{\n"
132 " float4 textColor;\n"
133 "};\n"
134 "\n"
135 "Texture2D baseTexture;\n"
136 "SamplerState baseSampler;\n"
137 "\n"
138 "struct PS_INPUT\n"
139 "{\n"
140 " float2 vertexTCoord : TEXCOORD0;\n"
141 "};\n"
142 "\n"
143 "struct PS_OUTPUT\n"
144 "{\n"
145 " float4 pixelColor0 : SV_TARGET0;\n"
146 "};\n"
147 "\n"
148 "PS_OUTPUT PSMain(PS_INPUT input)\n"
149 "{\n"
150 " PS_OUTPUT output;\n"
151 " float bitmapAlpha = baseTexture.Sample(baseSampler, input.vertexTCoord).r;\n"
152 " if (bitmapAlpha > 0.5f)\n"
153 " {\n"
154 " discard;\n"
155 " }\n"
156 " output.pixelColor0 = textColor;\n"
157 " return output;\n"
158 "}\n";
159 
161 {
163  &msHLSLSource
164 };
165 
167 {
169  &msHLSLSource
170 };
std::shared_ptr< ConstantBuffer > const & GetTranslate() const
void SetTranslate(float x, float y)
GLuint color
Definition: glcorearb.h:1256
static std::string const msHLSLSource
Definition: GteTextEffect.h:41
static std::string const * msPSSource[ProgramFactory::PF_NUM_API]
Definition: GteTextEffect.h:43
void SetColor(Vector4< float > const &color)
TextEffect(std::shared_ptr< ProgramFactory > const &factory, std::shared_ptr< Texture2 > const &texture)
static std::string const msGLSLVSSource
Definition: GteTextEffect.h:39
std::shared_ptr< ConstantBuffer > mTranslate
Definition: GteTextEffect.h:34
GLint GLenum GLint x
Definition: glcorearb.h:404
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
GLboolean * data
Definition: glcorearb.h:126
GLuint texture
Definition: glcorearb.h:410
static std::string const * msVSSource[ProgramFactory::PF_NUM_API]
Definition: GteTextEffect.h:42
std::shared_ptr< ConstantBuffer > mColor
Definition: GteTextEffect.h:35
std::shared_ptr< SamplerState > mSamplerState
Definition: GteTextEffect.h:36
GLfloat f
Definition: glcorearb.h:1921
static std::string const msGLSLPSSource
Definition: GteTextEffect.h:40
std::shared_ptr< VisualProgram > mProgram
GLint y
Definition: glcorearb.h:98
std::shared_ptr< ConstantBuffer > const & GetColor() const


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