LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CardRoundedCornerShaderStage.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 CARDROUNDEDCORNERSHADERSTAGE_H_
23 #define CARDROUNDEDCORNERSHADERSTAGE_H_
24 
25 #include "Common.h"
26 
27 #if defined(USE_ROUNDEDCORNER_SHADER)
28 
29 #include <QtOpenGL/qglcustomshaderstage_p.h>
30 #include <QDebug>
31 
32 class CardRoundedCornerShaderStage : public QGLCustomShaderStage
33 {
34 public:
35 
36  CardRoundedCornerShaderStage()
37  : m_srcWidth(0)
38  , m_srcHeight(0)
39  , m_dstWidth(0)
40  , m_dstHeight(0)
41  , m_radius(0)
42  , m_scale(1.0)
43  , m_active(1.0)
44  {
45 
46  static char const shaderSrc[] =
47  " \n\
48  uniform highp vec2 Start; \n\
49  uniform highp vec2 Center; \n\
50  uniform highp float Delta; \n\
51  uniform highp float Active; \n\
52  lowp vec4 customShader(lowp sampler2D imageTexture, highp vec2 textureCoords) \n\
53  { \n\
54  highp vec2 Coord = max((abs(textureCoords - Center) - Start) / (Center - Start), vec2(0.0)); \n\
55  lowp float Alpha = smoothstep(1.0, 1.0 - Delta, length(Coord)); \n\
56  return vec4(texture2D(imageTexture, textureCoords).rgb * Alpha * Active, Alpha); \n\
57  }\n";
58 
59  setSource(shaderSrc);
60  }
61 
62  virtual void setUniforms(QGLShaderProgram* program) {
63 
64  float horizRadiusFactor = 0.5f - (m_radius * 1.0f)/m_dstWidth;
65  float vertRadiusFactor = 0.5f - (m_radius * 1.0f)/m_dstHeight;
66 
67 
68  if(m_radius>60){//EmulatedCardWindow
69  if (m_dstWidth > m_dstHeight) {
70  horizRadiusFactor = 0.491f;
71  vertRadiusFactor = 0.49f;
72  }
73  else {
74  horizRadiusFactor = 0.491f;
75  vertRadiusFactor = 0.49f;
76  }
77  }
78  else if(m_radius>50){//CardHostWindow
79  if (m_dstWidth > m_dstHeight) {
80  horizRadiusFactor = 0.491f;
81  vertRadiusFactor = 0.473f;
82  }
83  else {
84  horizRadiusFactor = 0.483f;
85  vertRadiusFactor = 0.478f;
86  }
87  }
88  else if (m_radius>45){//CardHostWindow in Port
89  horizRadiusFactor = 0.481f;
90  vertRadiusFactor = 0.487f;
91  }
92  else {
93  if (m_dstWidth > m_dstHeight) {
94  horizRadiusFactor = 0.491f;
95  vertRadiusFactor = 0.473f;
96  }
97  else {
98  horizRadiusFactor = 0.491f;
99  vertRadiusFactor = 0.478f;
100  }
101  }
102 
103 
104  //to keep from demonimator from going nevitive/breaking the shader
105  float CenterX=(m_dstWidth * 0.5f) / m_srcWidth;
106  float CenterY=(m_dstHeight * 0.5f) / m_srcHeight;
107 
108  horizRadiusFactor = qMin(horizRadiusFactor, CenterX);
109  vertRadiusFactor = qMin(vertRadiusFactor, CenterY);
110 
111  /*not tuned for sharper rounded corners
112  if (m_scale > 0.5f) {
113  horizRadiusFactor += (0.5f - horizRadiusFactor) * (m_scale - 0.5f) / 0.5f;
114  vertRadiusFactor += (0.5f - vertRadiusFactor) * (m_scale - 0.5f) / 0.5f;
115  horizRadiusFactor = qMin(horizRadiusFactor, 0.48f);
116  vertRadiusFactor = qMin(vertRadiusFactor, 0.48f);
117  }*/
118 
119  program->setUniformValue("Start", horizRadiusFactor, vertRadiusFactor);
120  program->setUniformValue("Center", (m_dstWidth * 0.5f) / m_srcWidth, (m_dstHeight * 0.5f) / m_srcHeight);
121  program->setUniformValue("Delta", qFuzzyCompare(m_scale, 1.0f) ? 0.01f : 0.3f);
122  program->setUniformValue("Active", m_active);
123  }
124 
125  void setParameters(int srcWidth, int srcHeight, int dstWidth, int dstHeight, int radius, float active = 1.0f) {
126 
127  m_srcWidth = srcWidth;
128  m_srcHeight = srcHeight;
129  m_dstWidth = dstWidth;
130  m_dstHeight = dstHeight;
131  m_radius = radius;
132  m_active = active;
133 
134  setUniformsDirty();
135  }
136 
137  void setSourceParameters(int srcWidth, int srcHeight) {
138 
139  m_srcWidth = srcWidth;
140  m_srcHeight = srcHeight;
141 
142  setUniformsDirty();
143  }
144 
145  void dumpParameters() {
146  qDebug() << "srcW" << m_srcWidth
147  << "srcH" << m_srcHeight
148  << "dstW" << m_dstWidth
149  << "dstH" << m_dstHeight
150  << "radius" << m_radius
151  << "scale" << m_scale
152  << "active" << m_active;
153  }
154 
155  void clone(CardRoundedCornerShaderStage* other) const {
156 
157  other->m_srcWidth = m_srcWidth;
158  other->m_srcHeight = m_srcHeight;
159  other->m_dstWidth = m_dstWidth;
160  other->m_dstHeight = m_dstHeight;
161  other->m_radius = m_radius;
162  other->m_scale = m_scale;
163  other->m_active = m_active;
164  }
165 
166  void setScale(float scale) {
167  m_scale = scale;
168  if (m_scale < 0.5f)
169  m_scale = 0.5f;
170  else if (m_scale > 1.0f)
171  m_scale = 1.0f;
172  }
173 
174 private:
175 
176  int m_srcWidth;
177  int m_srcHeight;
178  int m_dstWidth;
179  int m_dstHeight;
180  int m_radius;
181  float m_scale;
182  float m_active;
183 };
184 
185 #endif
186 
187 #endif
188