Otclient  14/8/2020
paintershaderprogram.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 "paintershaderprogram.h"
24 #include "painter.h"
25 #include "texture.h"
26 #include "texturemanager.h"
27 #include "graphics.h"
28 #include <framework/core/clock.h>
30 
32 {
33  m_startTime = g_clock.seconds();
34  m_opacity = 1;
35  m_color = Color::white;
36  m_time = 0;
37 }
38 
40 {
41  bindUniformLocation(TRANSFORM_MATRIX_UNIFORM, "u_TransformMatrix");
42  bindUniformLocation(PROJECTION_MATRIX_UNIFORM, "u_ProjectionMatrix");
43  bindUniformLocation(TEXTURE_MATRIX_UNIFORM, "u_TextureMatrix");
51  bindUniformLocation(RESOLUTION_UNIFORM, "u_Resolution");
52 
53  setUniformValue(TRANSFORM_MATRIX_UNIFORM, m_transformMatrix);
54  setUniformValue(PROJECTION_MATRIX_UNIFORM, m_projectionMatrix);
55  setUniformValue(TEXTURE_MATRIX_UNIFORM, m_textureMatrix);
57  setUniformValue(OPACITY_UNIFORM, m_opacity);
63  setUniformValue(RESOLUTION_UNIFORM, (float)m_resolution.width(), (float)m_resolution.height());
64 }
65 
67 {
68  m_startTime = g_clock.seconds();
70  bindAttributeLocation(TEXCOORD_ATTR, "a_TexCoord");
71  if(ShaderProgram::link()) {
72  bind();
73  setupUniforms();
74  release();
75  return true;
76  }
77  return false;
78 }
79 
81 {
82  if(transformMatrix == m_transformMatrix)
83  return;
84 
85  bind();
86  setUniformValue(TRANSFORM_MATRIX_UNIFORM, transformMatrix);
87  m_transformMatrix = transformMatrix;
88 }
89 
91 {
92  if(projectionMatrix == m_projectionMatrix)
93  return;
94 
95  bind();
96  setUniformValue(PROJECTION_MATRIX_UNIFORM, projectionMatrix);
97  m_projectionMatrix = projectionMatrix;
98 }
99 
101 {
102  if(textureMatrix == m_textureMatrix)
103  return;
104 
105  bind();
106  setUniformValue(TEXTURE_MATRIX_UNIFORM, textureMatrix);
107  m_textureMatrix = textureMatrix;
108 }
109 
111 {
112  if(color == m_color)
113  return;
114 
115  bind();
117  m_color = color;
118 }
119 
121 {
122  if(m_opacity == opacity)
123  return;
124 
125  bind();
127  m_opacity = opacity;
128 }
129 
131 {
132  if(m_resolution == resolution)
133  return;
134 
135  bind();
136  setUniformValue(RESOLUTION_UNIFORM, (float)resolution.width(), (float)resolution.height());
137  m_resolution = resolution;
138 }
139 
141 {
142  float time = g_clock.seconds() - m_startTime;
143  if(m_time == time)
144  return;
145 
146  bind();
148  m_time = time;
149 }
150 
151 void PainterShaderProgram::addMultiTexture(const std::string& file)
152 {
153  if(m_multiTextures.size() > 3)
154  g_logger.error("cannot add more multi textures to shader, the max is 3");
155 
156  TexturePtr texture = g_textures.getTexture(file);
157  if(!texture)
158  return;
159 
160  texture->setSmooth(true);
161  texture->setRepeat(true);
162 
163  m_multiTextures.push_back(texture);
164 }
165 
167 {
168  if(m_multiTextures.empty())
169  return;
170 
171  int i=1;
172  for(const TexturePtr& tex : m_multiTextures) {
173  glActiveTexture(GL_TEXTURE0 + i++);
174  glBindTexture(GL_TEXTURE_2D, tex->getId());
175  }
176 
177  glActiveTexture(GL_TEXTURE0);
178 }
graphics.h
PainterShaderProgram::TEX0_UNIFORM
@ TEX0_UNIFORM
Definition: paintershaderprogram.h:41
Color
Definition: color.h:32
PainterShaderProgram::updateTime
void updateTime()
Definition: paintershaderprogram.cpp:140
PainterShaderProgram::TEX1_UNIFORM
@ TEX1_UNIFORM
Definition: paintershaderprogram.h:42
PainterShaderProgram::setColor
void setColor(const Color &color)
Definition: paintershaderprogram.cpp:110
PainterShaderProgram::TEXTURE_MATRIX_UNIFORM
@ TEXTURE_MATRIX_UNIFORM
Definition: paintershaderprogram.h:37
paintershaderprogram.h
platformwindow.h
PainterShaderProgram::addMultiTexture
void addMultiTexture(const std::string &file)
Definition: paintershaderprogram.cpp:151
PainterShaderProgram::TIME_UNIFORM
@ TIME_UNIFORM
Definition: paintershaderprogram.h:40
ShaderProgram::release
static void release()
Definition: shaderprogram.cpp:117
Logger::error
void error(const std::string &what)
Definition: logger.h:54
texturemanager.h
PainterShaderProgram::setupUniforms
virtual void setupUniforms()
Definition: paintershaderprogram.cpp:39
PainterShaderProgram::setProjectionMatrix
void setProjectionMatrix(const Matrix3 &projectionMatrix)
Definition: paintershaderprogram.cpp:90
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
stdext::time
ticks_t time()
Definition: time.cpp:33
ShaderProgram::setUniformValue
void setUniformValue(int location, const Color &color)
Definition: shaderprogram.h:68
TSize::width
int width() const
Definition: size.h:43
PainterShaderProgram::TEX2_UNIFORM
@ TEX2_UNIFORM
Definition: paintershaderprogram.h:43
clock.h
PainterShaderProgram::TEXCOORD_ATTR
@ TEXCOORD_ATTR
Definition: paintershaderprogram.h:35
ShaderProgram::link
virtual bool link()
Definition: shaderprogram.cpp:90
painter.h
g_logger
Logger g_logger
Definition: logger.cpp:35
PainterShaderProgram::TRANSFORM_MATRIX_UNIFORM
@ TRANSFORM_MATRIX_UNIFORM
Definition: paintershaderprogram.h:46
Color::white
static const Color white
Definition: color.h:101
PainterShaderProgram::setOpacity
void setOpacity(float opacity)
Definition: paintershaderprogram.cpp:120
ShaderProgram::bind
bool bind()
Definition: shaderprogram.cpp:106
PainterShaderProgram::PROJECTION_MATRIX_UNIFORM
@ PROJECTION_MATRIX_UNIFORM
Definition: paintershaderprogram.h:36
TextureManager::getTexture
TexturePtr getTexture(const std::string &fileName)
Definition: texturemanager.cpp:90
PainterShaderProgram::COLOR_UNIFORM
@ COLOR_UNIFORM
Definition: paintershaderprogram.h:38
ShaderProgram::bindAttributeLocation
void bindAttributeLocation(int location, const char *name)
Definition: shaderprogram.cpp:143
TSize::height
int height() const
Definition: size.h:44
PainterShaderProgram::VERTEX_ATTR
@ VERTEX_ATTR
Definition: paintershaderprogram.h:34
PainterShaderProgram::TEX3_UNIFORM
@ TEX3_UNIFORM
Definition: paintershaderprogram.h:44
PainterShaderProgram::PainterShaderProgram
PainterShaderProgram()
Definition: paintershaderprogram.cpp:31
texture.h
PainterShaderProgram::OPACITY_UNIFORM
@ OPACITY_UNIFORM
Definition: paintershaderprogram.h:39
stdext::shared_object_ptr< Texture >
Texture::setSmooth
virtual void setSmooth(bool smooth)
Definition: texture.cpp:127
PainterShaderProgram::bindMultiTextures
void bindMultiTextures()
Definition: paintershaderprogram.cpp:166
g_textures
TextureManager g_textures
Definition: texturemanager.cpp:33
ShaderProgram::bindUniformLocation
void bindUniformLocation(int location, const char *name)
Definition: shaderprogram.cpp:148
Matrix
Definition: matrix.h:32
PainterShaderProgram::setTextureMatrix
void setTextureMatrix(const Matrix3 &textureMatrix)
Definition: paintershaderprogram.cpp:100
TSize< int >
g_clock
Clock g_clock
Definition: clock.cpp:25
PainterShaderProgram::RESOLUTION_UNIFORM
@ RESOLUTION_UNIFORM
Definition: paintershaderprogram.h:45
Clock::seconds
float seconds()
Definition: clock.h:38
Texture::setRepeat
virtual void setRepeat(bool repeat)
Definition: texture.cpp:140