Otclient  14/8/2020
painterogl2.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 "painterogl2.h"
26 
28 
30 {
31  m_drawProgram = nullptr;
32  resetState();
33 
34  m_drawTexturedProgram = PainterShaderProgramPtr(new PainterShaderProgram);
35  assert(m_drawTexturedProgram);
36  m_drawTexturedProgram->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
37  m_drawTexturedProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslTextureSrcFragmentShader);
38  m_drawTexturedProgram->link();
39 
40  m_drawSolidColorProgram = PainterShaderProgramPtr(new PainterShaderProgram);
41  assert(m_drawSolidColorProgram);
42  m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Vertex, glslMainVertexShader + glslPositionOnlyVertexShader);
43  m_drawSolidColorProgram->addShaderFromSourceCode(Shader::Fragment, glslMainFragmentShader + glslSolidColorFragmentShader);
44  m_drawSolidColorProgram->link();
45 
47 }
48 
50 {
52 
53  // vertex and texture coord attributes are always enabled
54  // to avoid massive enable/disables, thus improving frame rate
57 }
58 
60 {
64 }
65 
66 void PainterOGL2::drawCoords(CoordsBuffer& coordsBuffer, DrawMode drawMode)
67 {
68  int vertexCount = coordsBuffer.getVertexCount();
69  if(vertexCount == 0)
70  return;
71 
72  bool textured = coordsBuffer.getTextureCoordCount() > 0 && m_texture;
73 
74  // skip drawing of empty textures
75  if(textured && m_texture->isEmpty())
76  return;
77 
78  // update shader with the current painter state
79  m_drawProgram->bind();
80  m_drawProgram->setTransformMatrix(m_transformMatrix);
82  if(textured) {
83  m_drawProgram->setTextureMatrix(m_textureMatrix);
84  m_drawProgram->bindMultiTextures();
85  }
86  m_drawProgram->setOpacity(m_opacity);
87  m_drawProgram->setColor(m_color);
88  m_drawProgram->setResolution(m_resolution);
89  m_drawProgram->updateTime();
90 
91  // update coords buffer hardware caches if enabled
92  coordsBuffer.updateCaches();
93  bool hardwareCached = coordsBuffer.isHardwareCached();
94 
95  // only set texture coords arrays when needed
96  if(textured) {
97  if(hardwareCached) {
98  coordsBuffer.getHardwareTextureCoordArray()->bind();
99  m_drawProgram->setAttributeArray(PainterShaderProgram::TEXCOORD_ATTR, nullptr, 2);
100  } else
102  } else
104 
105  // set vertex array
106  if(hardwareCached) {
107  coordsBuffer.getHardwareVertexArray()->bind();
108  m_drawProgram->setAttributeArray(PainterShaderProgram::VERTEX_ATTR, nullptr, 2);
110  } else
111  m_drawProgram->setAttributeArray(PainterShaderProgram::VERTEX_ATTR, coordsBuffer.getVertexArray(), 2);
112 
113  // draw the element in coords buffers
114  if(drawMode == Triangles)
115  glDrawArrays(GL_TRIANGLES, 0, vertexCount);
116  else if(drawMode == TriangleStrip)
117  glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexCount);
118 
119  if(!textured)
121 }
122 
124 {
125  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
126  setTexture(nullptr);
127  drawCoords(coordsBuffer);
128 }
129 
130 void PainterOGL2::drawTextureCoords(CoordsBuffer& coordsBuffer, const TexturePtr& texture)
131 {
132  if(texture && texture->isEmpty())
133  return;
134 
135  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
136  setTexture(texture);
137  drawCoords(coordsBuffer);
138 }
139 
140 void PainterOGL2::drawTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
141 {
142  if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
143  return;
144 
145  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
146  setTexture(texture);
147 
149  m_coordsBuffer.addQuad(dest, src);
151 }
152 
153 void PainterOGL2::drawUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
154 {
155  if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
156  return;
157 
158  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
159  setTexture(texture);
160 
164 }
165 
166 void PainterOGL2::drawRepeatedTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src)
167 {
168  if(dest.isEmpty() || src.isEmpty() || texture->isEmpty())
169  return;
170 
171  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawTexturedProgram.get());
172  setTexture(texture);
173 
175  m_coordsBuffer.addRepeatedRects(dest, src);
177 }
178 
180 {
181  if(dest.isEmpty())
182  return;
183 
184  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
185 
187  m_coordsBuffer.addRect(dest);
189 }
190 
191 void PainterOGL2::drawFilledTriangle(const Point& a, const Point& b, const Point& c)
192 {
193  if(a == b || a == c || b == c)
194  return;
195 
196  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
197 
199  m_coordsBuffer.addTriangle(a, b, c);
201 }
202 
203 void PainterOGL2::drawBoundingRect(const Rect& dest, int innerLineWidth)
204 {
205  if(dest.isEmpty() || innerLineWidth == 0)
206  return;
207 
208  setDrawProgram(m_shaderProgram ? m_shaderProgram : m_drawSolidColorProgram.get());
209 
211  m_coordsBuffer.addBoudingRect(dest, innerLineWidth);
213 }
CoordsBuffer::addRepeatedRects
void addRepeatedRects(const Rect &dest, const Rect &src)
Definition: coordsbuffer.cpp:57
PainterOGL2::unbind
void unbind()
Definition: painterogl2.cpp:59
PainterOGL2::drawFillCoords
void drawFillCoords(CoordsBuffer &coordsBuffer)
Definition: painterogl2.cpp:123
CoordsBuffer::getVertexArray
float * getVertexArray()
Definition: coordsbuffer.h:75
PainterShaderProgram::updateTime
void updateTime()
Definition: paintershaderprogram.cpp:140
HardwareBuffer::bind
void bind()
Definition: hardwarebuffer.h:46
PainterOGL2::drawFilledTriangle
void drawFilledTriangle(const Point &a, const Point &b, const Point &c)
Definition: painterogl2.cpp:191
PainterShaderProgram::setColor
void setColor(const Color &color)
Definition: paintershaderprogram.cpp:110
Painter::TriangleStrip
@ TriangleStrip
Definition: painter.h:48
platformwindow.h
TRect< int >
PainterOGL2::PainterOGL2
PainterOGL2()
Definition: painterogl2.cpp:29
PainterOGL::m_textureMatrix
Matrix3 m_textureMatrix
Definition: painterogl.h:107
Painter::m_opacity
float m_opacity
Definition: painter.h:118
PainterOGL::resetState
void resetState()
Definition: painterogl.cpp:41
PainterOGL::m_texture
Texture * m_texture
Definition: painterogl.h:110
CoordsBuffer::addUpsideDownQuad
void addUpsideDownQuad(const Rect &dest, const Rect &src)
Definition: coordsbuffer.h:62
TRect::isEmpty
bool isEmpty() const
Definition: rect.h:49
ShaderProgram::release
static void release()
Definition: shaderprogram.cpp:117
Painter::DrawMode
DrawMode
Definition: painter.h:46
Painter::m_resolution
Size m_resolution
Definition: painter.h:117
PainterOGL::m_coordsBuffer
CoordsBuffer m_coordsBuffer
Definition: painterogl.h:102
CoordsBuffer::getTextureCoordCount
int getTextureCoordCount()
Definition: coordsbuffer.h:78
PainterOGL::bind
virtual void bind()
Definition: painterogl.h:49
PainterShaderProgram::setProjectionMatrix
void setProjectionMatrix(const Matrix3 &projectionMatrix)
Definition: paintershaderprogram.cpp:90
HardwareBuffer::unbind
static void unbind(Type type)
Definition: hardwarebuffer.h:47
PainterShaderProgram::link
bool link()
Definition: paintershaderprogram.cpp:66
PainterShaderProgram::setResolution
void setResolution(const Size &resolution)
Definition: paintershaderprogram.cpp:130
PainterShaderProgram::setTransformMatrix
void setTransformMatrix(const Matrix3 &transformMatrix)
Definition: paintershaderprogram.cpp:80
PainterOGL2::drawTexturedRect
void drawTexturedRect(const Rect &dest, const TexturePtr &texture, const Rect &src)
Definition: painterogl2.cpp:140
ShaderProgram::disableAttributeArray
static void disableAttributeArray(int location)
Definition: shaderprogram.h:50
HardwareBuffer::VertexBuffer
@ VertexBuffer
Definition: hardwarebuffer.h:33
PainterOGL2::drawCoords
void drawCoords(CoordsBuffer &coordsBuffer, DrawMode drawMode=Triangles)
Definition: painterogl2.cpp:66
PainterShaderProgram::TEXCOORD_ATTR
@ TEXCOORD_ATTR
Definition: paintershaderprogram.h:35
PainterOGL2::drawTextureCoords
void drawTextureCoords(CoordsBuffer &coordsBuffer, const TexturePtr &texture)
Definition: painterogl2.cpp:130
CoordsBuffer::getVertexCount
int getVertexCount()
Definition: coordsbuffer.h:77
Painter::m_color
Color m_color
Definition: painter.h:116
PainterOGL2::drawUpsideDownTexturedRect
void drawUpsideDownTexturedRect(const Rect &dest, const TexturePtr &texture, const Rect &src)
Definition: painterogl2.cpp:153
CoordsBuffer::updateCaches
void updateCaches()
Definition: coordsbuffer.cpp:96
CoordsBuffer::isHardwareCached
bool isHardwareCached()
Definition: coordsbuffer.h:73
painterogl2_shadersources.h
PainterOGL2::drawFilledRect
void drawFilledRect(const Rect &dest)
Definition: painterogl2.cpp:179
Painter::Triangles
@ Triangles
Definition: painter.h:47
CoordsBuffer::clear
void clear()
Definition: coordsbuffer.h:38
g_painterOGL2
PainterOGL2 * g_painterOGL2
Definition: painterogl2.cpp:27
PainterShaderProgram::setOpacity
void setOpacity(float opacity)
Definition: paintershaderprogram.cpp:120
ShaderProgram::bind
bool bind()
Definition: shaderprogram.cpp:106
PainterOGL2::drawRepeatedTexturedRect
void drawRepeatedTexturedRect(const Rect &dest, const TexturePtr &texture, const Rect &src)
Definition: painterogl2.cpp:166
CoordsBuffer::addBoudingRect
void addBoudingRect(const Rect &dest, int innerLineWidth)
Definition: coordsbuffer.cpp:41
PainterOGL::m_transformMatrix
Matrix3 m_transformMatrix
Definition: painterogl.h:105
PainterOGL2
Definition: painterogl2.h:35
PainterOGL2::bind
void bind()
Definition: painterogl2.cpp:49
PainterShaderProgram
Definition: paintershaderprogram.h:30
CoordsBuffer::getHardwareTextureCoordArray
HardwareBuffer * getHardwareTextureCoordArray()
Definition: coordsbuffer.h:81
PainterShaderProgramPtr
stdext::shared_object_ptr< PainterShaderProgram > PainterShaderProgramPtr
Definition: declarations.h:56
PainterOGL2::drawBoundingRect
void drawBoundingRect(const Rect &dest, int innerLineWidth=1)
Definition: painterogl2.cpp:203
PainterShaderProgram::VERTEX_ATTR
@ VERTEX_ATTR
Definition: paintershaderprogram.h:34
Shader::Vertex
@ Vertex
Definition: shader.h:32
Shader::Fragment
@ Fragment
Definition: shader.h:33
Texture::isEmpty
bool isEmpty()
Definition: texture.h:53
stdext::shared_object_ptr< Texture >
CoordsBuffer
Definition: coordsbuffer.h:29
CoordsBuffer::addRect
void addRect(const Rect &dest)
Definition: coordsbuffer.h:48
ShaderProgram::enableAttributeArray
static void enableAttributeArray(int location)
Definition: shaderprogram.h:51
PainterOGL::m_projectionMatrix
Matrix3 m_projectionMatrix
Definition: painterogl.h:106
PainterOGL::setTexture
virtual void setTexture(Texture *texture)
Definition: painterogl.cpp:144
PainterShaderProgram::bindMultiTextures
void bindMultiTextures()
Definition: paintershaderprogram.cpp:166
CoordsBuffer::getHardwareVertexArray
HardwareBuffer * getHardwareVertexArray()
Definition: coordsbuffer.h:80
PainterShaderProgram::setTextureMatrix
void setTextureMatrix(const Matrix3 &textureMatrix)
Definition: paintershaderprogram.cpp:100
TPoint< int >
ShaderProgram::setAttributeArray
void setAttributeArray(int location, const float *values, int size, int stride=0)
Definition: shaderprogram.h:59
CoordsBuffer::addQuad
void addQuad(const Rect &dest, const Rect &src)
Definition: coordsbuffer.h:57
CoordsBuffer::getTextureCoordArray
float * getTextureCoordArray()
Definition: coordsbuffer.h:76
CoordsBuffer::addTriangle
void addTriangle(const Point &a, const Point &b, const Point &c)
Definition: coordsbuffer.h:44
ShaderProgram::addShaderFromSourceCode
bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string &sourceCode)
Definition: shaderprogram.cpp:55
PainterOGL2::setDrawProgram
void setDrawProgram(PainterShaderProgram *drawProgram)
Definition: painterogl2.h:53
Painter::m_shaderProgram
PainterShaderProgram * m_shaderProgram
Definition: painter.h:114
painterogl2.h
stdext::shared_object_ptr::get
T * get() const
Definition: shared_object.h:82