LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CardSmoothEdgeShaderStage.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2011-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef CARDSMOOTHEDGESHADERSTAGE_H_
23 #define CARDSMOOTHEDGESHADERSTAGE_H_
24 
25 #include "Common.h"
26 
27 #if defined(HAVE_OPENGL) && defined(TARGET_DEVICE)
28 #define USE_SMOOTHEDGE_SHADER 1
29 #else
30 #undef USE_SMOOTHEDGE_SHADER
31 #endif
32 
33 #if defined(USE_SMOOTHEDGE_SHADER)
34 
35 #include <QtOpenGL/qglcustomshaderstage_p.h>
36 #include <QDebug>
37 
38 class CardSmoothEdgeShaderStage : public QGLCustomShaderStage
39 {
40 public:
41 
42  CardSmoothEdgeShaderStage()
43  : m_srcWidth(0)
44  , m_srcHeight(0)
45  , m_dstWidth(0)
46  , m_dstHeight(0)
47  , m_scale(1.0)
48  {
49  static char const shaderSrc[] =
50  " \n\
51  uniform highp vec2 Start; \n\
52  uniform highp vec2 Center; \n\
53  uniform highp float Delta; \n\
54  lowp vec4 customShader(lowp sampler2D imageTexture, highp vec2 textureCoords) \n\
55  { \n\
56  highp vec2 CoordH = max((abs(textureCoords - (/*vec2(1.0,1.0) -*/ Center)) / Center), vec2(0.0)); \n\
57  lowp float AlphaH = smoothstep(1.0, (1.0-Delta), CoordH.y); \n\
58  highp vec2 CoordV = max((abs(textureCoords - Center)) / (Center), vec2(0.0)); \n\
59  lowp float AlphaV = smoothstep(1.0, (1.0-Delta), CoordV.x); \n\
60  lowp float Alpha = min(AlphaH, AlphaV); \n\
61  return vec4(texture2D(imageTexture, textureCoords).rgb * Alpha, Alpha); \n\
62  }\n";
63 
64  setSource(shaderSrc);
65  }
66 
67  virtual void setUniforms(QGLShaderProgram* program) {
68  program->setUniformValue("Center", (m_dstWidth * 0.5f) / m_srcWidth, (m_dstHeight * 0.5f) / m_srcHeight);
69  program->setUniformValue("Delta", qFuzzyCompare(m_scale, 1.0f) ? 0.01f : 0.09f);
70  }
71 
72  void setParameters(int srcWidth, int srcHeight, int dstWidth, int dstHeight) {
73 
74  m_srcWidth = srcWidth;
75  m_srcHeight = srcHeight;
76  m_dstWidth = dstWidth;
77  m_dstHeight = dstHeight;
78 
79  setUniformsDirty();
80  }
81 
82  void setSourceParameters(int srcWidth, int srcHeight) {
83 
84  m_srcWidth = srcWidth;
85  m_srcHeight = srcHeight;
86 
87  setUniformsDirty();
88  }
89 
90  void dumpParameters() {
91  qDebug() << "srcW" << m_srcWidth
92  << "srcH" << m_srcHeight
93  << "dstW" << m_dstWidth
94  << "dstH" << m_dstHeight
95  << "scale" << m_scale;
96  }
97 
98  void clone(CardSmoothEdgeShaderStage* other) const {
99 
100  other->m_srcWidth = m_srcWidth;
101  other->m_srcHeight = m_srcHeight;
102  other->m_dstWidth = m_dstWidth;
103  other->m_dstHeight = m_dstHeight;
104  other->m_scale = m_scale;
105  }
106 
107  void setScale(float scale) {
108  m_scale = scale;
109  if (m_scale < 0.5f)
110  m_scale = 0.5f;
111  else if (m_scale > 1.0f)
112  m_scale = 1.0f;
113  }
114 
115 private:
116 
117  int m_srcWidth;
118  int m_srcHeight;
119  int m_dstWidth;
120  int m_dstHeight;
121  float m_scale;
122 };
123 
124 #endif
125 
126 #endif
127