Otclient  14/8/2020
shader.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 "shader.h"
24 #include "graphics.h"
25 
28 
30 {
31  m_shaderType = shaderType;
32  switch(shaderType) {
33  case Vertex:
34  m_shaderId = glCreateShader(GL_VERTEX_SHADER);
35  break;
36  case Fragment:
37  m_shaderId = glCreateShader(GL_FRAGMENT_SHADER);
38  break;
39  }
40 
41  if(!m_shaderId)
42  g_logger.fatal("Unable to create GL shader");
43 }
44 
46 {
47 #ifndef NDEBUG
48  assert(!g_app.isTerminated());
49 #endif
50  if(g_graphics.ok())
51  glDeleteShader(m_shaderId);
52 }
53 
54 bool Shader::compileSourceCode(const std::string& sourceCode)
55 {
56 #ifndef OPENGL_ES
57  static const char *qualifierDefines =
58  "#define lowp\n"
59  "#define mediump\n"
60  "#define highp\n";
61 #else
62  static const char *qualifierDefines =
63  "#ifndef GL_FRAGMENT_PRECISION_HIGH\n"
64  "#define highp mediump\n"
65  "#endif\n"
66  "precision highp float;\n";
67 #endif
68 
69  std::string code = qualifierDefines;
70  code.append(sourceCode);
71  const char *c_source = code.c_str();
72  glShaderSource(m_shaderId, 1, &c_source, nullptr);
73  glCompileShader(m_shaderId);
74 
75  int res = GL_FALSE;
76  glGetShaderiv(m_shaderId, GL_COMPILE_STATUS, &res);
77  return (res == GL_TRUE);
78 }
79 
80 bool Shader::compileSourceFile(const std::string& sourceFile)
81 {
82  try {
83  std::string sourceCode = g_resources.readFileContents(sourceFile);
84  return compileSourceCode(sourceCode);
85  } catch(stdext::exception& e) {
86  g_logger.error(stdext::format("unable to load shader source form file '%s': %s", sourceFile, e.what()));
87  }
88  return false;
89 }
90 
91 std::string Shader::log()
92 {
93  std::string infoLog;
94  int infoLogLength = 0;
95  glGetShaderiv(m_shaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
96  if(infoLogLength > 1) {
97  std::vector<char> buf(infoLogLength);
98  glGetShaderInfoLog(m_shaderId, infoLogLength-1, nullptr, &buf[0]);
99  infoLog = &buf[0];
100  }
101  return infoLog;
102 }
graphics.h
Graphics::ok
bool ok()
Definition: graphics.h:65
resourcemanager.h
Logger::error
void error(const std::string &what)
Definition: logger.h:54
Shader::ShaderType
ShaderType
Definition: shader.h:31
stdext::format
std::string format()
Definition: format.h:82
Logger::fatal
void fatal(const std::string &what)
Definition: logger.h:55
g_resources
ResourceManager g_resources
Definition: resourcemanager.cpp:32
Shader::Shader
Shader(ShaderType shaderType)
Definition: shader.cpp:29
Shader::~Shader
~Shader()
Definition: shader.cpp:45
g_logger
Logger g_logger
Definition: logger.cpp:35
Shader::compileSourceCode
bool compileSourceCode(const std::string &sourceCode)
Definition: shader.cpp:54
Shader::compileSourceFile
bool compileSourceFile(const std::string &sourceFile)
Definition: shader.cpp:80
g_graphics
Graphics g_graphics
Definition: graphics.cpp:44
g_app
ConsoleApplication g_app
Definition: consoleapplication.cpp:32
ResourceManager::readFileContents
std::string readFileContents(const std::string &fileName)
Definition: resourcemanager.cpp:185
stdext::exception::what
virtual const char * what() const
Definition: exception.h:37
Shader::Vertex
@ Vertex
Definition: shader.h:32
Shader::Fragment
@ Fragment
Definition: shader.h:33
Shader::log
std::string log()
Definition: shader.cpp:91
Application::isTerminated
bool isTerminated()
Definition: application.h:50
shader.h
application.h
stdext::exception
Definition: exception.h:31