1/*
2 * Copyright (c) 2010-2017 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
26#include <framework/core/application.h>
27#include <framework/core/resourcemanager.h>
28
29Shader::Shader(Shader::ShaderType shaderType)
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
45Shader::~Shader()
46{
47 VALIDATE(!g_app.isTerminated());
48 if(g_graphics.ok())
49 glDeleteShader(m_shaderId);
50}
51
52bool Shader::compileSourceCode(const std::string& sourceCode)
53{
54#ifdef OPENGL_ES
55 static const char *qualifierDefines =
56 "precision highp float;\n";
57 std::string code = qualifierDefines;
58 code.append(sourceCode);
59 const char* c_source = code.c_str();
60#else
61 const char* c_source = sourceCode.c_str();
62#endif
63 glShaderSource(m_shaderId, 1, &c_source, NULL);
64 glCompileShader(m_shaderId);
65
66 int res = GL_FALSE;
67 glGetShaderiv(m_shaderId, GL_COMPILE_STATUS, &res);
68 return (res == GL_TRUE);
69}
70
71bool Shader::compileSourceFile(const std::string& sourceFile)
72{
73 try {
74 std::string sourceCode = g_resources.readFileContents(sourceFile);
75 return compileSourceCode(sourceCode);
76 } catch(stdext::exception& e) {
77 g_logger.error(stdext::format("unable to load shader source form file '%s': %s", sourceFile, e.what()));
78 }
79 return false;
80}
81
82std::string Shader::log()
83{
84 std::string infoLog;
85 int infoLogLength = 0;
86 glGetShaderiv(m_shaderId, GL_INFO_LOG_LENGTH, &infoLogLength);
87 if(infoLogLength > 1) {
88 std::vector<char> buf(infoLogLength);
89 glGetShaderInfoLog(m_shaderId, infoLogLength-1, NULL, &buf[0]);
90 infoLog = &buf[0];
91 }
92 return infoLog;
93}
94