Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/OpenGL/OpenGLProgram.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/OpenGL/OpenGLProgram.cpp@30deba7bc8e2 |
children | 85e117739eca |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
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-2020 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 "OpenGLProgram.h" | |
23 | |
24 #include "OpenGLShader.h" | |
25 #include "IOpenGLContext.h" | |
26 | |
27 #include <OrthancException.h> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 namespace OpenGL | |
32 { | |
33 OpenGLProgram::OpenGLProgram(OpenGL::IOpenGLContext& context) | |
34 : context_(context) | |
35 { | |
36 program_ = glCreateProgram(); | |
37 ORTHANC_OPENGL_CHECK("glCreateProgram"); | |
38 if (program_ == 0) | |
39 { | |
40 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, | |
41 "Cannot create an OpenGL program"); | |
42 } | |
43 } | |
44 | |
45 | |
46 OpenGLProgram::~OpenGLProgram() | |
47 { | |
48 try | |
49 { | |
50 if (!context_.IsContextLost()) | |
51 { | |
52 ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteProgram"); | |
53 assert(program_ != 0); | |
54 glDeleteProgram(program_); | |
55 ORTHANC_OPENGL_CHECK("glDeleteProgram"); | |
56 } | |
57 } | |
58 catch (const Orthanc::OrthancException& e) | |
59 { | |
60 if (e.HasDetails()) | |
61 { | |
62 LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What() << " Details: " << e.GetDetails(); | |
63 } | |
64 else | |
65 { | |
66 LOG(ERROR) << "OrthancException in ~OpenGLProgram: " << e.What(); | |
67 } | |
68 } | |
69 catch (const std::exception& e) | |
70 { | |
71 LOG(ERROR) << "std::exception in ~OpenGLProgram: " << e.what(); | |
72 } | |
73 catch (...) | |
74 { | |
75 LOG(ERROR) << "Unknown exception in ~OpenGLProgram"; | |
76 } | |
77 } | |
78 | |
79 void OpenGLProgram::Use() | |
80 { | |
81 //ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glUseProgram"); | |
82 glUseProgram(program_); | |
83 ORTHANC_OPENGL_CHECK("glUseProgram"); | |
84 } | |
85 | |
86 void OpenGLProgram::CompileShaders(const std::string& vertexCode, | |
87 const std::string& fragmentCode) | |
88 { | |
89 assert(program_ != 0); | |
90 | |
91 OpenGLShader vertexShader(GL_VERTEX_SHADER, vertexCode); | |
92 OpenGLShader fragmentShader(GL_FRAGMENT_SHADER, fragmentCode); | |
93 | |
94 glAttachShader(program_, vertexShader.Release()); | |
95 ORTHANC_OPENGL_CHECK("glAttachShader"); | |
96 glAttachShader(program_, fragmentShader.Release()); | |
97 ORTHANC_OPENGL_CHECK("glAttachShader"); | |
98 glLinkProgram(program_); | |
99 ORTHANC_OPENGL_CHECK("glLinkProgram"); | |
100 glValidateProgram(program_); | |
101 ORTHANC_OPENGL_CHECK("glValidateProgram"); | |
102 } | |
103 | |
104 GLint OpenGLProgram::GetUniformLocation(const std::string& name) | |
105 { | |
106 GLint location = glGetUniformLocation(program_, name.c_str()); | |
107 ORTHANC_OPENGL_CHECK("glGetUniformLocation"); | |
108 | |
109 if (location == -1) | |
110 { | |
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, | |
112 "Inexistent uniform variable in shader: " + name); | |
113 } | |
114 else | |
115 { | |
116 return location; | |
117 } | |
118 } | |
119 | |
120 | |
121 GLint OpenGLProgram::GetAttributeLocation(const std::string& name) | |
122 { | |
123 GLint location = glGetAttribLocation(program_, name.c_str()); | |
124 ORTHANC_OPENGL_CHECK("glGetAttribLocation"); | |
125 | |
126 if (location == -1) | |
127 { | |
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, | |
129 "Inexistent attribute in shader: " + name); | |
130 } | |
131 else | |
132 { | |
133 return location; | |
134 } | |
135 } | |
136 } | |
137 } |