Otclient  14/8/2020
painterogl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "painterogl.h"
26 
28 {
29  m_glTextureId = 0;
30  m_oldStateIndex = 0;
32  m_opacity = 1.0f;
35  m_shaderProgram = nullptr;
36  m_texture = nullptr;
37  m_alphaWriting = false;
39 }
40 
42 {
43  resetColor();
44  resetOpacity();
47  resetClipRect();
49  resetTexture();
52 }
53 
55 {
62 }
63 
65 {
66  assert(m_oldStateIndex<10);
80 }
81 
83 {
84  saveState();
85  resetState();
86 }
87 
89 {
103 }
104 
105 void PainterOGL::clear(const Color& color)
106 {
107  glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
108  glClear(GL_COLOR_BUFFER_BIT);
109 }
110 
111 void PainterOGL::clearRect(const Color& color, const Rect& rect)
112 {
113  Rect oldClipRect = m_clipRect;
114  setClipRect(rect);
115  glClearColor(color.rF(), color.gF(), color.bF(), color.aF());
116  glClear(GL_COLOR_BUFFER_BIT);
117  setClipRect(oldClipRect);
118 }
119 
121 {
122  if(m_compositionMode == compositionMode)
123  return;
124  m_compositionMode = compositionMode;
126 }
127 
129 {
130  if(m_blendEquation == blendEquation)
131  return;
132  m_blendEquation = blendEquation;
134 }
135 
136 void PainterOGL::setClipRect(const Rect& clipRect)
137 {
138  if(m_clipRect == clipRect)
139  return;
140  m_clipRect = clipRect;
142 }
143 
145 {
146  if(m_texture == texture)
147  return;
148 
149  m_texture = texture;
150 
151  uint glTextureId;
152  if(texture) {
154  glTextureId = texture->getId();
155  } else
156  glTextureId = 0;
157 
158  if(m_glTextureId != glTextureId) {
159  m_glTextureId = glTextureId;
160  updateGlTexture();
161  }
162 }
163 
165 {
166  if(m_alphaWriting == enable)
167  return;
168 
169  m_alphaWriting = enable;
171 }
172 
173 void PainterOGL::setResolution(const Size& resolution)
174 {
175  // The projection matrix converts from Painter's coordinate system to GL's coordinate system
176  // * GL's viewport is 2x2, Painter's is width x height
177  // * GL has +y -> -y going from bottom -> top, Painter is the other way round
178  // * GL has [0,0] in the center, Painter has it in the top-left
179  //
180  // This results in the Projection matrix below.
181  //
182  // Projection Matrix
183  // Painter Coord ------------------------------------------------ GL Coord
184  // ------------- | 2.0 / width | 0.0 | 0.0 | ---------------
185  // | x y 1 | * | 0.0 | -2.0 / height | 0.0 | = | x' y' 1 |
186  // ------------- | -1.0 | 1.0 | 1.0 | ---------------
187 
188  Matrix3 projectionMatrix = { 2.0f/resolution.width(), 0.0f, 0.0f,
189  0.0f, -2.0f/resolution.height(), 0.0f,
190  -1.0f, 1.0f, 1.0f };
191 
192  m_resolution = resolution;
193 
194  setProjectionMatrix(projectionMatrix);
195  if(g_painter == this)
197 }
198 
199 void PainterOGL::scale(float x, float y)
200 {
201  Matrix3 scaleMatrix = {
202  x, 0.0f, 0.0f,
203  0.0f, y, 0.0f,
204  0.0f, 0.0f, 1.0f
205  };
206 
208 }
209 
210 void PainterOGL::translate(float x, float y)
211 {
212  Matrix3 translateMatrix = {
213  1.0f, 0.0f, x,
214  0.0f, 1.0f, y,
215  0.0f, 0.0f, 1.0f
216  };
217 
218  setTransformMatrix(m_transformMatrix * translateMatrix.transposed());
219 }
220 
221 void PainterOGL::rotate(float angle)
222 {
223  Matrix3 rotationMatrix = {
224  std::cos(angle), -std::sin(angle), 0.0f,
225  std::sin(angle), std::cos(angle), 0.0f,
226  0.0f, 0.0f, 1.0f
227  };
228 
229  setTransformMatrix(m_transformMatrix * rotationMatrix.transposed());
230 }
231 
232 void PainterOGL::rotate(float x, float y, float angle)
233 {
234  translate(-x, -y);
235  rotate(angle);
236  translate(x, y);
237 }
238 
240 {
242  assert(m_transformMatrixStack.size() < 100);
243 }
244 
246 {
247  assert(m_transformMatrixStack.size() > 0);
249  m_transformMatrixStack.pop_back();
250 }
251 
253 {
254  if(m_glTextureId != 0)
255  glBindTexture(GL_TEXTURE_2D, m_glTextureId);
256 }
257 
259 {
260  switch(m_compositionMode) {
263  glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
264  else
265  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
266  break;
268  glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
269  break;
270  case CompositionMode_Add:
271  glBlendFunc(GL_ONE, GL_ONE);
272  break;
274  glBlendFunc(GL_ONE, GL_ZERO);
275  break;
277  glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
278  break;
280  glBlendFunc(GL_ZERO, GL_SRC_COLOR);
281  break;
282  }
283 }
284 
286 {
288  return;
290  glBlendEquation(0x8006); // GL_FUNC_ADD
292  glBlendEquation(0x8008); // GL_MAX
293 }
294 
296 {
297  if(m_clipRect.isValid()) {
298  glEnable(GL_SCISSOR_TEST);
300  } else {
301  glScissor(0, 0, m_resolution.width(), m_resolution.height());
302  glDisable(GL_SCISSOR_TEST);
303  }
304 }
305 
307 {
308  if(m_alphaWriting)
309  glColorMask(1,1,1,1);
310  else
311  glColorMask(1,1,1,0);
312 }
313 
315 {
316  glViewport(0, 0, m_resolution.width(), m_resolution.height());
317 }
Painter::setColor
virtual void setColor(const Color &color)
Definition: painter.h:77
PainterOGL::m_transformMatrixStack
std::vector< Matrix3 > m_transformMatrixStack
Definition: painterogl.h:104
graphics.h
PainterOGL::pushTransformMatrix
void pushTransformMatrix()
Definition: painterogl.cpp:239
painterogl.h
Painter::BlendEquation_Add
@ BlendEquation_Add
Definition: painter.h:35
Color
Definition: color.h:32
PainterOGL::PainterState::compositionMode
Painter::CompositionMode compositionMode
Definition: painterogl.h:38
Painter::CompositionMode_Normal
@ CompositionMode_Normal
Definition: painter.h:39
Painter::CompositionMode_Add
@ CompositionMode_Add
Definition: painter.h:41
Graphics::canUseBlendFuncSeparate
bool canUseBlendFuncSeparate()
Definition: graphics.cpp:375
platformwindow.h
TRect< int >
PainterOGL::m_textureMatrix
Matrix3 m_textureMatrix
Definition: painterogl.h:107
Painter::m_opacity
float m_opacity
Definition: painter.h:118
Color::rF
float rF() const
Definition: color.h:52
Texture
Definition: texture.h:28
PlatformWindow::getSize
Size getSize()
Definition: platformwindow.h:75
PainterOGL::PainterOGL
PainterOGL()
Definition: painterogl.cpp:27
PainterOGL::resetState
void resetState()
Definition: painterogl.cpp:41
Color::gF
float gF() const
Definition: color.h:51
PainterOGL::m_texture
Texture * m_texture
Definition: painterogl.h:110
PainterOGL::updateGlClipRect
void updateGlClipRect()
Definition: painterogl.cpp:295
Graphics::canUseBlendEquation
bool canUseBlendEquation()
Definition: graphics.cpp:388
PainterOGL::PainterState::blendEquation
Painter::BlendEquation blendEquation
Definition: painterogl.h:39
TRect::left
T left() const
Definition: rect.h:52
PainterOGL::PainterState::projectionMatrix
Matrix3 projectionMatrix
Definition: painterogl.h:34
Painter::m_resolution
Size m_resolution
Definition: painter.h:117
PainterOGL::PainterState::alphaWriting
bool alphaWriting
Definition: painterogl.h:43
PainterOGL::setResolution
void setResolution(const Size &resolution)
Definition: painterogl.cpp:173
TRect::bottom
T bottom() const
Definition: rect.h:55
PainterOGL::resetTexture
void resetTexture()
Definition: painterogl.h:90
Painter::CompositionMode_DestBlending
@ CompositionMode_DestBlending
Definition: painter.h:43
PainterOGL::PainterState::texture
Texture * texture
Definition: painterogl.h:41
PainterOGL::PainterState::clipRect
Rect clipRect
Definition: painterogl.h:40
TSize::width
int width() const
Definition: size.h:43
PainterOGL::m_olderStates
PainterState m_olderStates[10]
Definition: painterogl.h:113
PainterOGL::setClipRect
virtual void setClipRect(const Rect &clipRect)
Definition: painterogl.cpp:136
PainterOGL::PainterState::resolution
Size resolution
Definition: painterogl.h:32
Painter::CompositionMode_Light
@ CompositionMode_Light
Definition: painter.h:44
Painter::CompositionMode
CompositionMode
Definition: painter.h:38
PainterOGL::m_alphaWriting
bool m_alphaWriting
Definition: painterogl.h:111
PainterOGL::restoreSavedState
void restoreSavedState()
Definition: painterogl.cpp:88
Painter::m_color
Color m_color
Definition: painter.h:116
PainterOGL::resetAlphaWriting
void resetAlphaWriting()
Definition: painterogl.h:91
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
PainterOGL::PainterState::color
Color color
Definition: painterogl.h:36
PainterOGL::updateGlTexture
void updateGlTexture()
Definition: painterogl.cpp:252
uint
unsigned int uint
Definition: types.h:31
Painter::CompositionMode_Replace
@ CompositionMode_Replace
Definition: painter.h:42
PainterOGL::updateGlBlendEquation
void updateGlBlendEquation()
Definition: painterogl.cpp:285
PainterOGL::translate
void translate(float x, float y)
Definition: painterogl.cpp:210
Texture::getTransformMatrix
const Matrix3 & getTransformMatrix()
Definition: texture.h:52
PainterOGL::m_glTextureId
uint m_glTextureId
Definition: painterogl.h:116
Color::white
static const Color white
Definition: color.h:101
Painter::resetOpacity
void resetOpacity()
Definition: painter.h:106
Matrix::transposed
Matrix< M, N, T > transposed() const
Definition: matrix.h:101
PainterOGL::PainterState::shaderProgram
PainterShaderProgram * shaderProgram
Definition: painterogl.h:42
PainterOGL::clear
void clear(const Color &color)
Definition: painterogl.cpp:105
g_graphics
Graphics g_graphics
Definition: graphics.cpp:44
Painter::BlendEquation
BlendEquation
Definition: painter.h:34
PainterOGL::m_transformMatrix
Matrix3 m_transformMatrix
Definition: painterogl.h:105
Texture::getId
uint getId()
Definition: texture.h:46
Color::aF
float aF() const
Definition: color.h:49
Painter::resetShaderProgram
void resetShaderProgram()
Definition: painter.h:109
PainterOGL::PainterState::opacity
float opacity
Definition: painterogl.h:37
PainterOGL::setTextureMatrix
virtual void setTextureMatrix(const Matrix3 &textureMatrix)
Definition: painterogl.h:63
PainterOGL::setShaderProgram
virtual void setShaderProgram(PainterShaderProgram *shaderProgram)
Definition: painterogl.h:67
PainterOGL::updateGlAlphaWriting
void updateGlAlphaWriting()
Definition: painterogl.cpp:306
PainterOGL::clearRect
void clearRect(const Color &color, const Rect &rect)
Definition: painterogl.cpp:111
PainterOGL::setCompositionMode
virtual void setCompositionMode(CompositionMode compositionMode)
Definition: painterogl.cpp:120
TSize::height
int height() const
Definition: size.h:44
Color::bF
float bF() const
Definition: color.h:50
Painter::resetColor
void resetColor()
Definition: painter.h:108
PainterOGL::m_blendEquation
BlendEquation m_blendEquation
Definition: painterogl.h:109
TRect::isValid
bool isValid() const
Definition: rect.h:50
PainterOGL::setProjectionMatrix
virtual void setProjectionMatrix(const Matrix3 &projectionMatrix)
Definition: painterogl.h:62
g_painter
Painter * g_painter
Definition: painter.cpp:28
TRect::height
T height() const
Definition: rect.h:70
PainterOGL::PainterState::transformMatrix
Matrix3 transformMatrix
Definition: painterogl.h:33
PainterOGL::m_projectionMatrix
Matrix3 m_projectionMatrix
Definition: painterogl.h:106
PainterOGL::resetBlendEquation
void resetBlendEquation()
Definition: painterogl.h:89
PainterOGL::setTexture
virtual void setTexture(Texture *texture)
Definition: painterogl.cpp:144
Painter::CompositionMode_Multiply
@ CompositionMode_Multiply
Definition: painter.h:40
PainterOGL::popTransformMatrix
void popTransformMatrix()
Definition: painterogl.cpp:245
PainterOGL::resetTransformMatrix
void resetTransformMatrix()
Definition: painterogl.h:92
PainterOGL::saveState
void saveState()
Definition: painterogl.cpp:64
Painter::BlendEquation_Max
@ BlendEquation_Max
Definition: painter.h:36
Matrix
Definition: matrix.h:32
PainterOGL::setBlendEquation
virtual void setBlendEquation(BlendEquation blendEquation)
Definition: painterogl.cpp:128
PainterOGL::PainterState::textureMatrix
Matrix3 textureMatrix
Definition: painterogl.h:35
PainterOGL::saveAndResetState
void saveAndResetState()
Definition: painterogl.cpp:82
TRect::width
T width() const
Definition: rect.h:69
Painter::resetClipRect
void resetClipRect()
Definition: painter.h:105
PainterOGL::setTransformMatrix
virtual void setTransformMatrix(const Matrix3 &transformMatrix)
Definition: painterogl.h:61
TSize< int >
PainterOGL::setAlphaWriting
virtual void setAlphaWriting(bool enable)
Definition: painterogl.cpp:164
Painter::m_clipRect
Rect m_clipRect
Definition: painter.h:119
PainterOGL::updateGlViewport
void updateGlViewport()
Definition: painterogl.cpp:314
PainterOGL::rotate
void rotate(float angle)
Definition: painterogl.cpp:221
Painter::m_compositionMode
CompositionMode m_compositionMode
Definition: painter.h:115
Painter::m_shaderProgram
PainterShaderProgram * m_shaderProgram
Definition: painter.h:114
PainterOGL::scale
void scale(float x, float y)
Definition: painterogl.cpp:199
Painter::setOpacity
virtual void setOpacity(float opacity)
Definition: painter.h:91
PainterOGL::m_oldStateIndex
int m_oldStateIndex
Definition: painterogl.h:114
PainterOGL::updateGlCompositionMode
void updateGlCompositionMode()
Definition: painterogl.cpp:258
Painter::resetCompositionMode
void resetCompositionMode()
Definition: painter.h:107
PainterOGL::refreshState
virtual void refreshState()
Definition: painterogl.cpp:54