comparison Framework/OpenGL/OpenGLShader.cpp @ 578:21fd70df3fc9

starting work on OpenGL
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 15:57:46 +0200
parents
children fadacfbf5538
comparison
equal deleted inserted replaced
577:b098a3aaf694 578:21fd70df3fc9
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "OpenGLShader.h"
23
24 #include <Core/OrthancException.h>
25
26 namespace OrthancStone
27 {
28 namespace OpenGL
29 {
30 static GLuint CompileShader(GLenum type,
31 const std::string& source)
32 {
33 // Create shader object
34 const GLchar* sourceString[1];
35 GLint sourceStringLengths[1];
36
37 sourceString[0] = source.c_str();
38 sourceStringLengths[0] = source.length();
39 GLuint shader = glCreateShader(type);
40
41 if (shader == 0)
42 {
43 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
44 "Cannot create an OpenGL shader");
45 }
46 else
47 {
48 // Assign and compile the source to the shader object
49 glShaderSource(shader, 1, sourceString, sourceStringLengths);
50 glCompileShader(shader);
51
52 // Check if there were errors
53 int infoLen = 0;
54 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
55
56 if (infoLen > 1)
57 {
58 char infoLog[infoLen + 1];
59 glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
60 glDeleteShader(shader);
61
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
63 "Error while creating an OpenGL shader: " + std::string(infoLog));
64 }
65 else
66 {
67 return shader;
68 }
69 }
70 }
71
72
73 OpenGLShader::OpenGLShader(GLenum type,
74 const std::string& source)
75 {
76 shader_ = CompileShader(type, source);
77 isValid_ = true;
78 }
79
80
81 OpenGLShader::OpenGLShader(GLenum type,
82 Orthanc::EmbeddedResources::FileResourceId resource)
83 {
84 std::string content;
85 Orthanc::EmbeddedResources::GetFileResource(content, resource);
86
87 shader_ = CompileShader(type, content);
88 isValid_ = true;
89 }
90
91
92 OpenGLShader::~OpenGLShader()
93 {
94 if (isValid_)
95 {
96 glDeleteShader(shader_);
97 }
98 }
99
100
101 GLuint OpenGLShader::Release()
102 {
103 if (isValid_)
104 {
105 isValid_ = false;
106 return shader_;
107 }
108 else
109 {
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
111 }
112 }
113 }
114 }