annotate Framework/OpenGL/OpenGLShader.cpp @ 579:fadacfbf5538

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