Otclient  14/8/2020
shadermanager.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 "shadermanager.h"
28 
30 
32 {
34  return;
35 
36  m_defaultItemShader = createFragmentShaderFromCode("Item", glslMainFragmentShader + glslTextureSrcFragmentShader);
37  setupItemShader(m_defaultItemShader);
38 
39  m_defaultMapShader = createFragmentShaderFromCode("Map", glslMainFragmentShader + glslTextureSrcFragmentShader);
40 
42 }
43 
45 {
46  m_defaultItemShader = nullptr;
47  m_defaultMapShader = nullptr;
48  m_shaders.clear();
49 }
50 
52 {
53  if(!g_graphics.canUseShaders()) {
54  g_logger.error(stdext::format("unable to create shader '%s', shaders are not supported", name));
55  return nullptr;
56  }
57 
59  m_shaders[name] = shader;
60  return shader;
61 }
62 
63 PainterShaderProgramPtr ShaderManager::createFragmentShader(const std::string& name, std::string file)
64 {
66  if(!shader)
67  return nullptr;
68 
69  file = g_resources.guessFilePath(file, "frag");
70 
71  shader->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
72  if(!shader->addShaderFromSourceFile(Shader::Fragment, file)) {
73  g_logger.error(stdext::format("unable to load fragment shader '%s' from source file '%s'", name, file));
74  return nullptr;
75  }
76 
77  if(!shader->link()) {
78  g_logger.error(stdext::format("unable to link shader '%s' from file '%s'", name, file));
79  return nullptr;
80  }
81 
82  m_shaders[name] = shader;
83  return shader;
84 }
85 
86 PainterShaderProgramPtr ShaderManager::createFragmentShaderFromCode(const std::string& name, const std::string& code)
87 {
89  if(!shader)
90  return nullptr;
91 
92  shader->addShaderFromSourceCode(Shader::Vertex, glslMainWithTexCoordsVertexShader + glslPositionOnlyVertexShader);
93  if(!shader->addShaderFromSourceCode(Shader::Fragment, code)) {
94  g_logger.error(stdext::format("unable to load fragment shader '%s'", name));
95  return nullptr;
96  }
97 
98  if(!shader->link()) {
99  g_logger.error(stdext::format("unable to link shader '%s'", name));
100  return nullptr;
101  }
102 
103  m_shaders[name] = shader;
104  return shader;
105 }
106 
107 PainterShaderProgramPtr ShaderManager::createItemShader(const std::string& name, const std::string& file)
108 {
109  PainterShaderProgramPtr shader = createFragmentShader(name, file);
110  if(shader)
111  setupItemShader(shader);
112  return shader;
113 }
114 
115 PainterShaderProgramPtr ShaderManager::createMapShader(const std::string& name, const std::string& file)
116 {
117  PainterShaderProgramPtr shader = createFragmentShader(name, file);
118  if(shader)
119  setupMapShader(shader);
120  return shader;
121 }
122 
123 void ShaderManager::setupItemShader(const PainterShaderProgramPtr& shader)
124 {
125  if(!shader)
126  return;
127  shader->bindUniformLocation(ITEM_ID_UNIFORM, "u_ItemId");
128 }
129 
130 void ShaderManager::setupMapShader(const PainterShaderProgramPtr& shader)
131 {
132  if(!shader)
133  return;
134  shader->bindUniformLocation(MAP_CENTER_COORD, "u_MapCenterCoord");
135  shader->bindUniformLocation(MAP_GLOBAL_COORD, "u_MapGlobalCoord");
136  shader->bindUniformLocation(MAP_ZOOM, "u_MapZoom");
137 }
138 
140 {
141  auto it = m_shaders.find(name);
142  if(it != m_shaders.end())
143  return it->second;
144  return nullptr;
145 }
146 
ShaderManager::createItemShader
PainterShaderProgramPtr createItemShader(const std::string &name, const std::string &file)
Definition: shadermanager.cpp:107
graphics.h
paintershaderprogram.h
ShaderManager::terminate
void terminate()
Definition: shadermanager.cpp:44
ShaderProgram::addShaderFromSourceFile
bool addShaderFromSourceFile(Shader::ShaderType shaderType, const std::string &sourceFile)
Definition: shaderprogram.cpp:64
ShaderProgram::release
static void release()
Definition: shaderprogram.cpp:117
resourcemanager.h
ShaderManager::createMapShader
PainterShaderProgramPtr createMapShader(const std::string &name, const std::string &file)
Definition: shadermanager.cpp:115
Logger::error
void error(const std::string &what)
Definition: logger.h:54
ShaderManager::createFragmentShaderFromCode
PainterShaderProgramPtr createFragmentShaderFromCode(const std::string &name, const std::string &code)
Definition: shadermanager.cpp:86
PainterShaderProgram::link
bool link()
Definition: paintershaderprogram.cpp:66
stdext::format
std::string format()
Definition: format.h:82
ShaderManager::MAP_ZOOM
@ MAP_ZOOM
Definition: shadermanager.h:37
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
ShaderManager::MAP_GLOBAL_COORD
@ MAP_GLOBAL_COORD
Definition: shadermanager.h:36
ShaderManager::getShader
PainterShaderProgramPtr getShader(const std::string &name)
Definition: shadermanager.cpp:139
g_logger
Logger g_logger
Definition: logger.cpp:35
painterogl2_shadersources.h
g_shaders
ShaderManager g_shaders
Definition: shadermanager.cpp:29
Graphics::canUseShaders
bool canUseShaders()
Definition: graphics.cpp:279
ShaderManager::createFragmentShader
PainterShaderProgramPtr createFragmentShader(const std::string &name, std::string file)
Definition: shadermanager.cpp:63
ShaderManager
Definition: shadermanager.h:30
g_graphics
Graphics g_graphics
Definition: graphics.cpp:44
ShaderManager::MAP_CENTER_COORD
@ MAP_CENTER_COORD
Definition: shadermanager.h:35
PainterShaderProgram
Definition: paintershaderprogram.h:30
ShaderManager::ITEM_ID_UNIFORM
@ ITEM_ID_UNIFORM
Definition: shadermanager.h:34
Shader::Vertex
@ Vertex
Definition: shader.h:32
Shader::Fragment
@ Fragment
Definition: shader.h:33
stdext::shared_object_ptr< PainterShaderProgram >
ShaderProgram::bindUniformLocation
void bindUniformLocation(int location, const char *name)
Definition: shaderprogram.cpp:148
ShaderManager::createShader
PainterShaderProgramPtr createShader(const std::string &name)
Definition: shadermanager.cpp:51
ShaderProgram::addShaderFromSourceCode
bool addShaderFromSourceCode(Shader::ShaderType shaderType, const std::string &sourceCode)
Definition: shaderprogram.cpp:55
ShaderManager::init
void init()
Definition: shadermanager.cpp:31
shadermanager.h
ResourceManager::guessFilePath
std::string guessFilePath(const std::string &filename, const std::string &type)
Definition: resourcemanager.cpp:352