comparison Framework/OpenGL/OpenGLProgram.cpp @ 579:fadacfbf5538

OpenGL programs and textures
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Apr 2019 16:06:45 +0200
parents
children 1091b2adeb5a
comparison
equal deleted inserted replaced
578:21fd70df3fc9 579:fadacfbf5538
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 "OpenGLProgram.h"
23
24 #include "OpenGLShader.h"
25
26 #include <Core/OrthancException.h>
27
28
29 namespace OrthancStone
30 {
31 namespace OpenGL
32 {
33 OpenGLProgram::OpenGLProgram()
34 {
35 program_ = glCreateProgram();
36 if (program_ == 0)
37 {
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
39 "Cannot create an OpenGL program");
40 }
41 }
42
43
44 OpenGLProgram::~OpenGLProgram()
45 {
46 assert(program_ != 0);
47 glDeleteProgram(program_);
48 }
49
50
51 void OpenGLProgram::Use()
52 {
53 glUseProgram(program_);
54 }
55
56
57 void OpenGLProgram::CompileShaders(const std::string& vertexCode,
58 const std::string& fragmentCode)
59 {
60 assert(program_ != 0);
61
62 OpenGLShader vertexShader(GL_VERTEX_SHADER, vertexCode);
63 OpenGLShader fragmentShader(GL_FRAGMENT_SHADER, fragmentCode);
64
65 glAttachShader(program_, vertexShader.Release());
66 glAttachShader(program_, fragmentShader.Release());
67 glLinkProgram(program_);
68 glValidateProgram(program_);
69 }
70
71
72 GLint OpenGLProgram::GetUniformLocation(const std::string& name)
73 {
74 GLint location = glGetUniformLocation(program_, name.c_str());
75
76 if (location == -1)
77 {
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
79 "Inexistent uniform variable in shader: " + name);
80 }
81 else
82 {
83 return location;
84 }
85 }
86
87
88 GLint OpenGLProgram::GetAttributeLocation(const std::string& name)
89 {
90 GLint location = glGetAttribLocation(program_, name.c_str());
91
92 if (location == -1)
93 {
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem,
95 "Inexistent attribute in shader: " + name);
96 }
97 else
98 {
99 return location;
100 }
101 }
102 }
103 }